K-th Closest Distance
传送门
Problem Description
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
)
(1 <= T <= 3)
(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
<
=
1
0
5
)
(1 <= n, m <= 10^5)
(1<=n,m<=105)denoting the size of array and number of queries.
The second line contains n space-separated integers a1, a2, …, an
(
1
<
=
a
i
<
=
1
0
6
)
(1 <= ai <= 10^6)
(1<=ai<=106). 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
<
=
1
0
6
,
1
<
=
K
<
=
169
,
R
−
L
+
1
>
=
K
)
.
(1 <= L < R <= n, 1 <= p <= 10^6, 1 <= K <= 169, R - L + 1 >= K).
(1<=L<R<=n,1<=p<=106,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
题目大意:
给你一个数组,q个询问,让你找L到R距离p最近的第k个数。
戳一戳→最容易懂的主席树介绍博客
主席树:建立n课树,对于树tree[i]插入的是[1,i]。这样在求区间[l,r]时,tree[r] - tree[l-1]就是我们要找的树,遍历这颗树就行了。
思路:
对答案进行二分,对于可能的答案x,对
a
L
a_L
aL ~
a
R
a_R
aR 构成的权值线段树查[p−x,p+x]是否有K个,若
≥
K
≥K
≥K,令up=mid−1;若
<
K
<K
<K,则令low=mid+1,最后一次满足
≥
K
≥K
≥K的x既是答案.
得到
a
L
a_L
aL~
a
R
a_R
aR 构成的权值线段树用主席树就行,这题
a
≤
1
0
6
a≤10^6
a≤106
ac代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int M=1e6;
struct node{
int l,r;
int val;
}tree[N*55]; //主席树是多个线段树的结合
int root[N],tot;
int n,q,a[N];
int update(int pre,int pl,int pr,int val){ //插入树中
int cur=++tot;
tree[cur]=tree[pre]; //每次建一棵树都可以用到前面一棵树,然后再修改
tree[cur].val++; //更新节点
if(pl==pr) return cur;
int mid=(pl+pr)>>1;
if(val<=mid) tree[cur].l=update(tree[pre].l,pl,mid,val); //增加过程的子树
else tree[cur].r=update(tree[pre].r,mid+1,pr,val);
return cur;
}
int k,cnt;
int query(int pl,int pr,int l,int r,int rt,int lt){ //查询
if(pl<=l&&r<=pr){
return tree[rt].val-tree[lt].val;
}
int mid=(l+r)>>1;
int res=0;
if(pl<=mid) res+=query(pl,pr,l,mid,tree[rt].l,tree[lt].l);
if(pr>mid) res+=query(pl,pr,mid+1,r,tree[rt].r,tree[lt].r);
return res;
}
int main(){
int T,prex;
int l,r,p;
int pl,pr,mid;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
root[i]=update(root[i-1],1,M,a[i]);
}
prex=0;
while(q--){ //这题不用离散化
scanf("%d %d %d %d",&l,&r,&p,&k);
l=l^prex,r=r^prex,p=p^prex,k=k^prex;
pl=0,pr=M;
while(pl<=pr){
mid=(pl+pr)>>1;
if(query(max(1,p-mid),min(M,p+mid),1,M,root[r],root[l-1])>=k){ //二分值(离它的最大值最小值),判断有几个数大于它
prex=mid,pr=mid-1;
}
else pl=mid+1;
}
printf("%d\n",prex);
}
}
}