K-th Closest Distance
Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 444 Accepted Submission(s): 157
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) 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
建立一个主席树然后设一个t,【p-t,p+t】求最接近p的t值就是所求点
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
struct node
{
int l, r, sum;
} tree[N * 30];
int root[N], s[N];
int b[N];
vector<int>vec;
int cnt;
void build(int &now, int l, int r)//建树
{
now = ++cnt;
tree[now].sum = 0;
if(l == r)
{
return;
}
int mid = (l + r) >> 1;
build(tree[now].l, l, mid);
build(tree[now].r, mid + 1, r);
}
int getid(int x)
{
return lower_bound(vec.begin(), vec.end(), x) - vec.begin() + 1;
}
void update(int l, int r, int &x, int y, int pos)
{
x = ++cnt;
tree[x] = tree[y];
tree[x].sum++;
if(l == r)
return;
int mid = (l + r) >> 1;
if(pos <= mid)
update(l, mid, tree[x].l, tree[y].l, pos);
else
update(mid + 1, r, tree[x].r, tree[y].r, pos);
}
int query(int l, int r, int x, int y, int L,int R)
{
if(L<=l && R>=r)
return tree[y].sum-tree[x].sum;
int mid = (l + r) >> 1;
int sum =0;
if(mid>=L)
sum+=query(l, mid, tree[x].l, tree[y].l,L,R);
if(R>mid)
sum+=query(mid + 1, r, tree[x].r, tree[y].r,L,R);
return sum;
}
int main()
{
int T;
cin>>T;
while(T--)
{
int n, m;
scanf("%d %d", &n, &m);
vec.clear();
cnt = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d", &s[i]);
b[i]=s[i];
vec.push_back(s[i]);
}
sort(b+1,b+n+1);
int nn = unique( b+1, b+n+1 )-b-1;//去重
build(root[0], 1, nn);
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
for(int i = 1; i <= n; i++)
update(1, nn, root[i], root[i - 1], getid(s[i]));
int lans=0;
for(int i = 1; i <= m; i++)
{
int l, r, p, k;
scanf("%d %d %d %d", &l, &r,&p,&k);
// b[i]=vec[query(1, n, root[l - 1], root[r], k) - 1];
l = l^lans;
r = r^lans;
p = p^lans;
k = k^lans;
if(l>r)
swap(l,r);
int xl=0,xr=1000000;
while(xl<=xr)
{
int mid=(xl+xr)>>1;
int ll=p-mid;
int rr=p+mid;
int lt=lower_bound(b+1,b+nn+1,ll)-b;//求大于等于ll的下标
int rt=upper_bound(b+1,b+nn+1,rr)-b-1;//求大于rr的下标
int num=query(1,nn,root[l-1],root[r],lt,rt);
if(num>=k)
xr=mid-1;
else
xl=mid+1;
}
lans = xl;
printf("%d\n",lans);
}
}
return 0;
}