日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

K-th Closest Distance HDU - 6621(第k小绝对值+主席树+二分)

發布時間:2023/12/15 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 K-th Closest Distance HDU - 6621(第k小绝对值+主席树+二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

You have an array: a1, a2, , an and you must answer for some queries.
For each query, you are given an interval [L, R] and two numbers p and K. Your goal is to find the Kth closest distance between p and aL, aL+1, …, aR.
The distance between p and ai is equal to |p - ai|.
For example:
A = {31, 2, 5, 45, 4 } and L = 2, R = 5, p = 3, K = 2.
|p - a2| = 1, |p - a3| = 2, |p - a4| = 42, |p - a5| = 1.
Sorted distance is {1, 1, 2, 42}. Thus, the 2nd closest distance is 1.
Input
The first line of the input contains an integer T (1 <= T <= 3) denoting the number of test cases.
For each test case:
冘The first line contains two integers n and m (1 <= n, m <= 10^5) denoting the size of array and number of queries.
The second line contains n space-separated integers a1, a2, …, an (1 <= ai <= 10^6). Each value of array is unique.
Each of the next m lines contains four integers L’, R’, p’ and K’.
From these 4 numbers, you must get a real query L, R, p, K like this:
L = L’ xor X, R = R’ xor X, p = p’ xor X, K = K’ xor X, where X is just previous answer and at the beginning, X = 0.
(1 <= L < R <= n, 1 <= p <= 10^6, 1 <= K <= 169, R - L + 1 >= K).
Output
For each query print a single line containing the Kth closest distance between p and aL, aL+1, …, aR.
Sample Input
1
5 2
31 2 5 45 4
1 5 5 1
2 5 3 2
Sample Output
0
1
現在才寫這篇博客有點晚了。當時做這道題的時候還不太會,現在想想大體上可以想明白了。
這種第k大或者第k小的問題就是主席樹。但是這次是絕對值第k小,這就很懵逼。。我們二分去找答案,二分到一個值之后,在[0,1000000]這個區間里去看[p-mid,p+mid]這個區間內有多少個值,要是個數小于k說明需要擴大范圍,要是大于k的話,就需要減少范圍。但是要是等于k,也要去減少范圍,因為我們需要去更精確這個值。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e5+100; struct node{int l;int r;int num; }p[maxx<<5]; int a[maxx],root[maxx]; int n,m,tot;inline int read() {char ch = getchar(); int x = 0, f = 1;while(ch < '0' || ch > '9') {if(ch == '-') f = -1;ch = getchar();} while('0' <= ch && ch <= '9') {x = x * 10 + ch - '0';ch = getchar();} return x * f; } inline int build(int l,int r) {int cur=++tot;p[cur].num=0;if(l==r) return cur;int mid=l+r>>1;p[cur].l=build(l,mid);p[cur].r=build(mid+1,r);return cur; } inline int update(int rot,int l,int r,int pos) {int cur=++tot;p[cur]=p[rot];p[cur].num++;if(l==r) return cur;int mid=l+r>>1;if(pos<=mid) p[cur].l=update(p[rot].l,l,mid,pos);else p[cur].r=update(p[rot].r,mid+1,r,pos);return cur; } inline int query(int l,int r,int L,int R,int lrot,int rrot) {if(l<=L&&R<=r) return p[rrot].num-p[lrot].num;int mid=L+R>>1;int ret=0;if(l<=mid) ret+=query(l,r,L,mid,p[lrot].l,p[rrot].l);if(r>mid) ret+=query(l,r,mid+1,R,p[lrot].r,p[rrot].r);return ret; } int main() {int t,l,r,p,k;int ans;t=read();while(t--){tot=0;n=read(),m=read();for(int i=1;i<=n;i++) a[i]=read();//root[0]=build(1,1000000);for(int i=1;i<=n;i++) root[i]=update(root[i-1],1,1000000,a[i]);ans=0;while(m--){l=read(),r=read(),p=read(),k=read();l^=ans;r^=ans;p^=ans;k^=ans;int li=0,ri=1000000;while(li<=ri){int mid=li+ri>>1;if(query(max(1,p-mid),min(1000000,p+mid),1,1000000,root[l-1],root[r])>=k) {ri=mid-1;ans=mid;}else li=mid+1;}printf("%d\n",ans);}}return 0; }

快速讀入優化后只需要3000+ms,有時候讀入優化真的很管用。。
努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的K-th Closest Distance HDU - 6621(第k小绝对值+主席树+二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲国产97 | 99国产精品无码 | 欧美成人精品欧美一级乱黄 | 色婷久久 | 欧美日韩1 | 日本伦理一区二区 | 免费看女人裸体 | 每日更新av | 国产高清在线不卡 | 色偷偷影院 | 日韩精品系列 | 国产亚洲综合在线 | 国产乱码精品一品二品 | аⅴ天堂中文在线网 | 澳门av在线| 777黄色| 亚洲乱码视频在线观看 | 黄色片aaa| 高清一区二区 | 黄瓜视频色 | 男人的天堂网在线 | 黄色日批网站 | 韩国美女一区 | 污污视频免费网站 | 亚洲欧美日韩不卡 | 色丁香综合 | 国产激情一区 | 久久婷色 | 男男做性免费视频网 | 精品乱子伦一区二区 | 伦理片中文字幕 | 国产最新精品视频 | 在线观看1区| 日韩精品一区二区三区国语自制 | 亚洲熟伦熟女新五十路熟妇 | 日本大胆人体视频 | 成人午夜久久 | 国产精品69av | 欧美手机在线观看 | 国产精品久久久久久久毛片 | 久久在现 | 荔枝视频污 | 国产国语老龄妇女a片 | 色xxxxxx| 男人看的网站 | 亚洲天天 | 嫩草视频在线观看视频 | 日本变态折磨凌虐bdsm在线 | 在线中文字幕一区 | 男女午夜爽爽爽 | av老司机在线观看 | 免费看国产精品 | 最近中文字幕一区二区 | 精品国产乱码久久久久久闺蜜 | 国产视频一区二 | 成人a网站| 制服丝袜快播 | 国产色无码精品视频国产 | 嫩草嫩草嫩草嫩草 | 国产福利一区视频 | 一区二区三区四区五区六区 | 都市激情麻豆 | 日韩精品一区二区三区色欲av | www.色人阁.com| 国产欧美中文字幕 | 国产夜夜操 | 国产精品我不卡 | 亚洲午夜av | 污污污污污污www网站免费 | av自拍 | 一区二区在线视频免费观看 | 亚洲第一免费播放区 | 爱爱爱网| 亚洲av永久纯肉无码精品动漫 | 国产亚洲精品美女久久久久 | 免费成人av | 麻豆系列在线观看 | 欧美图片第一页 | 亚洲字幕在线观看 | 久久精品在线 | 国产农村妇女精品一二区 | aa毛片视频 | 美攻壮受大胸奶汁(高h) | 国产乱了高清露脸对白 | www.亚洲综合 | 久久久久久久久久久综合 | 极品久久| 99久久精品国产色欲 | 久热综合| 国产精品一区二区三区在线播放 | 亚洲专区视频 | 亚洲一级中文字幕 | 日韩尤物 | 四虎成人网| 国产精品久久久久久久久免费桃花 | 精品国产一区二区三区久久狼黑人 | 亚洲偷偷自拍 | 欧美在线观看视频一区 | 日韩精品在线视频免费观看 |