K-th Closest Distance
Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 3851 Accepted Submission(s): 1368
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
Source
2019 Multi-University Training Contest 4
OJ题号
HDU - 6621 K-th Closest Distance
简单题意
给出一段长度为n≤1e5的序列a1,a2,…,an(a≤1e6),有m≤1e5次询问:L R p k问ax~an中与p的第K小个差值的绝对值是多少?
正解思路
对答案进行二分,对于可能的答案x,对al~ar构成的权值线段树查找[p-x,p+x]是否有K个(主席树实现),最后一次满足≥K的x既是答案。
//https://cloud.tencent.com/developer/article/1486582
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+7;
const int mod=1e9+7;
int t,n,m,cnt,root[maxn];
LL a[maxn],b[maxn];
//cnt和root:主席树的总点数和每一个根
struct node
{
int l,r,sum;
} T[maxn*40];
vector<int> v;
int getid(int x) //离散化
{
return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
/*
v.clear();
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
*/
//清空,默认给root[0]
int build(int l,int r)
{
int cur=++cnt;
T[cur].sum=0;
if(l==r)
return cur;
int mid=l+r>>1;
build(l,mid);
build(mid+1,r);
return cur;
}
///单点修改
void update(int l,int r,int &now,int pre,int pos,int add)
{
T[++cnt]=T[pre];
T[cnt].sum+=add;
now=cnt;
if(l==r)
return;
int mid=(l+r)>>1;
if(pos<=mid)
update(l,mid,T[cnt].l,T[pre].l,pos,add);
else
update(mid+1,r,T[cnt].r,T[pre].r,pos,add);
}
///查询区间[x,y]第k小,使用v[query(1,n,root[x-1],root[y],mid)-1]
LL query(int l,int r,int x,int y,int k)
{
if(l==r)
return l;
int mid=(l+r)/2;
int sum=T[T[y].l].sum-T[T[x].l].sum;
if(sum>=k)
return query(l,mid,T[x].l,T[y].l,k);
else
return query(mid+1,r,T[x].r,T[y].r,k-sum);
}
///查询区间[x,y]里面数在[ql,qr]里面的个数 query_num1(1,b_n,lp,rp,root[x-1],root[y]);
LL ans_num=0;
void query_num1(int l,int r,int ql,int qr,int x,int y)
{
//cout<<l<<" "<<r<<endl;
if(ql<=l&&qr>=r)
{
ans_num+=T[y].sum-T[x].sum;
return ;
}
if(l>qr||r<ql)
return ;
int mid=(l+r)>>1;
if(ql<=mid)
query_num1(l,mid,ql,qr,T[x].l,T[y].l);
if(mid<qr)
query_num1(mid+1,r,ql,qr,T[x].r,T[y].r);
}
///查询区间[x,y]里面数<=k里面的个数 query_num2(1,b_n,root[x-1],root[y],rp)
int query_num2(int l,int r,int x,int y,int k)
{
//cout<<l<<" "<<r<<endl;
if(k==0)
return 0;
if(l==r)
return T[y].sum - T[x].sum;
int tmp = T[T[y].l].sum - T[T[x].l].sum;
int mid = (l+r)>>1;
if(k<=mid)
return query_num2(l,mid,T[x].l,T[y].l,k);
else
return tmp+query_num2(mid+1,r, T[x].r,T[y].r,k);
}
/*
int two_find(int x,int y,int k)///二分查找第i小的元素小于等于k的最大元素
//即求k是第几小的元素
{
int l=0,r=y-x+1;
while(l<r)
{
int mid=(l+r+1)>>1;
if(v[query(1,n,root[x-1],root[y],mid)-1]<=k)
l=mid;
else
r=mid-1;
}
return l;
}
///查询区间[x,y]<=k的元素个数 query_min(root[x-1],root[y],1,n,k)
///查询区间[x,y]>=k的最小值 query_min(root[x-1],root[y],1,n,k)
int query_min(int lrot,int rrot,int l,int r,int k)
{
if(l==r)
{
if(T[rrot].sum-T[lrot].sum>0)
return l;
else
return 1e9;
}
int mid=(l+r)>>1;
if(k<=mid)
{
int ans=1e9;
if(k<=l)//这里相当于一个剪枝,在小于l的时候,如果左子树有符合条件的就进入左子树,否则再进入右子树。
{
if(T[T[rrot].l].sum-T[T[lrot].l].sum>0)
ans=min(ans,query_min(T[lrot].l,T[rrot].l,l,mid,k));
else if(T[T[rrot].r].sum-T[T[lrot].r].sum>0)
ans=min(ans,query_min(T[lrot].r,T[rrot].r,mid+1,r,k));
return ans;
}
if(T[T[rrot].l].sum-T[T[lrot].l].sum>0) //k在l到mid之间的时候,左右子树都有可能涉及,就左右都看一遍寻找最优解
ans=min(ans,query_min(T[lrot].l,T[rrot].l,l,mid,k));
if(T[T[rrot].r].sum-T[T[lrot].r].sum>0)
ans=min(ans,query_min(T[lrot].r,T[rrot].r,mid+1,r,k));
return ans;
}
else
{
int ans=1e9;//k大于mid的时候,直接进入右子树,左子树不用找了
if(T[T[rrot].r].sum-T[T[lrot].r].sum>0)
ans=min(ans,query_min(T[lrot].r,T[rrot].r,mid+1,r,k));
return ans;
}
}
///查询区间[x,y]的种类个数query_num(1,n,root[y],x)
int query_num(int l,int r,int root,int left)
{
if(l>=left)
return T[root].sum;
int mid=(l+r)>>1;
if(mid>=left)
return query_num(l,mid,T[root].l,left)+T[T[root].r].sum;
else
return query_num(mid+1,r,T[root].r,left);
}*/
int main()
{
int Q;
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
scanf("%lld",&a[i]);
b[i]=a[i];
}
sort(b+1,b+n+1);
int b_n=unique(b+1,b+n+1)-(b+1);
for(int i=1; i<=n; i++)
{
a[i]=lower_bound(b+1,b+b_n+1,a[i])-b;
}
cnt=0;
root[0]=build(1,b_n);
for(int i=1; i<=n; i++)
{
update(1,b_n,root[i],root[i-1],a[i],1);
}
int ans=0;
while(m--)
{
int x,y,p,k;
scanf("%d%d%d%d",&x,&y,&p,&k);
x^=ans;
y^=ans;
p^=ans;
k^=ans;
int l=-1,r=1e9;
while(l<r)
{
int mid=(l+r)>>1;
int lp=max(lower_bound(b+1,b+b_n+1,p-mid)-b,1);
int rp=min(upper_bound(b+1,b+b_n+1,p+mid)-b-1,b_n);
ans_num=0;
query_num1(1,b_n,lp,rp,root[x-1],root[y]);
//ans_num=query_num2(1,b_n,root[x-1],root[y],rp)-query_num2(1,b_n,root[x-1],root[y],lp-1);
cout<<mid<<" "<<lp<<" "<<rp<<" "<<ans_num<<endl;
if(ans_num>=k)
r=mid;
else
l=mid+1;
}
ans=l;
printf("%d\n", ans);
}
}
return 0 ;
}
Recommend
chendu | We have carefully selected several similar problems for you: 6730 6729 6728 6727 6726