杭电多校第四场-H- K-th Closest Distance
题目描述
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.
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.
输入
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).
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).
输出
For each query print a single line containing the Kth closest distance between p and aL, aL+1, ..., aR.
样例输入
1
5 2
31 2 5 45 4
1 5 5 1
2 5 3 2
样例输出
0
1
题意 给一个序列A,每次询问L,R,p,k,输出[L,R]区间中第k大的|p-a[i]| 思路 二分答案,查询[L,R]中[p-mid,p+mid]的数的个数 可以用主席树/归并树维护区间x,y之间数的个数(题解说还可以用线段树) 主席树O(mlog1e6logn) 归并树O(mlog1e6logn^2)
归并树:https://www.cnblogs.com/bennettz/p/8342242.html
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> #define ll long long using namespace std; const int N=1e5+10; int T,n,m; int a[N],t[20][N]; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void build(int s,int l,int r) { if (l==r) { t[s][l]=a[l]; return; } int mid=(l+r)>>1; build(s+1,l,mid); build(s+1,mid+1,r); for (int i=l,j=mid+1,k=l;i<=mid||j<=r;) { if (j>r) t[s][k++]=t[s+1][i++]; else if (i>mid || t[s+1][i]>t[s+1][j]) t[s][k++]=t[s+1][j++]; else t[s][k++]=t[s+1][i++]; } } int query(int s,int l,int r,int L,int R,int x,int y) { if (x>y ) return 0; if (L<=l&r<=R) { //printf("x=%d,y=%d,l=%d,r=%d,>y=%d,>=x=%d\n",x,y,l,r,upper_bound(t[s]+l,t[s]+r+1,y),upper_bound(t[s]+l,t[s]+r+1,x)); return upper_bound(t[s]+l,t[s]+r+1,y)-lower_bound(t[s]+l,t[s]+r+1,x); } int mid=(l+r)>>1,ans=0; if (L<=mid) ans+=query(s+1,l,mid,L,R,x,y); if (R>mid) ans+=query(s+1,mid+1,r,L,R,x,y); return ans; } int main() { // freopen("14162.in","r",stdin); // freopen("1.out","w",stdout); T=read(); while(T--) { n=read(); m=read(); //memset(t,0,sizeof(t)); for (int i=1;i<=n;i++) a[i]=read(); build(0,1,n); int ans=0; int L,R,p,k; while (m--) { L=read(); R=read(); p=read(); k=read(); L^=ans; R^=ans; p^=ans; k^=ans; int l=0,r=1000005; while (l<=r) { int mid=(l+r)>>1; //cout<<mid<<endl; if (query(0,1,n,L,R,p-mid,p+mid)>=k) ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",ans); } } // fclose(stdin); // fclose(stdout); return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <bits/stdc++.h> using namespace std; const int N=1e5+10; int ls[N*21],rs[N*21],s[N*21],root[N]; int a[N],b[N]; int T,n,m,sz; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } void Insert(int l,int r,int x,int &y,int v) { y=++sz; s[y]=s[x]+1; if (l==r) return; ls[y]=ls[x]; rs[y]=rs[x]; int mid=(l+r)>>1; if (v<=mid) Insert(l,mid,ls[x],ls[y],v); else Insert(mid+1,r,rs[x],rs[y],v); } int query(int l,int r,int L,int R,int x,int y) { if (L<=l&&r<=R) return s[y]-s[x]; int ret=0; int mid=(l+r)>>1; if (L<=mid) ret+=query(l,mid,L,R,ls[x],ls[y]); if (R>mid) ret+=query(mid+1,r,L,R,rs[x],rs[y]); return ret; } int main() { T=read(); while(T--) { sz=0; n=read(); m=read(); for (int i=1;i<=n;i++) a[i]=read(),b[i]=a[i]; sort(b+1,b+1+n); int cnt=unique(b+1,b+1+n)-b-1; for (int i=1;i<=n;i++) { a[i]=lower_bound(b+1,b+1+cnt,a[i])-b+1; Insert(1,N,root[i-1],root[i],a[i]); } int ans=0; int L,R,p,k; while (m--) { L=read(); R=read(); p=read(); k=read(); L^=ans,R^=ans,p^=ans,k^=ans; int l=0,r=1000006; while (l<=r) { int mid=(l+r)>>1; int ll=lower_bound(b+1,b+1+cnt,p-mid)-b+1; int rr=upper_bound(b+1,b+1+cnt,p+mid)-b; if (query(1,N,ll,rr,root[L-1],root[R])>=k) ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",ans); } } return 0; }