K-th Closest Distance HDU - 6621 主席樹+二分

K-th Closest Distance

Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 3305    Accepted Submission(s): 1186


 

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

 

題意:給你一個長度爲n的序列,有m個查詢,每次查詢給你l,r,p,k,讓你求[l,r]區間中|p-a[i]|第k小的值。

思路:這種求區間第k小一想就會想到主席樹。直接求不太好求,我們考慮二分枚舉答案ans。如果答案ans符合要求,那麼一定有[p-ans,p+ans]這個區間內的數的個數>=k.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const int maxm=1e6+10;
const int M=1e6;
int a[maxn],root[maxm],n,m,ans,cnt;
struct node{
	int l;
	int r;
	int num;
}tree[maxm*40];
void pushup(int cur)
{
	tree[cur].num=tree[tree[cur].l].num+tree[tree[cur].r].num;
}
void build(int &cur,int l,int r)
{
	cur=++cnt;
	tree[cur].num=0;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(tree[cur].l,l,m);
	build(tree[cur].r,m+1,r);
}
void update(int &now,int last,int l,int r,int tar)
{
	now=++cnt;
	tree[now]=tree[last];
	tree[now].num++;
	if(l==r) return;
	int m=(l+r)>>1;
	if(tar<=m) update(tree[now].l,tree[last].l,l,m,tar);
	else update(tree[now].r,tree[last].r,m+1,r,tar);
}
int query(int x,int y,int L,int R,int l,int r)
{
	if(L<=l&&r<=R) return tree[y].num-tree[x].num;
	int m=(l+r)>>1,res=0;
	if(L<=m) res+=query(tree[x].l,tree[y].l,L,R,l,m);
 	if(R>m) res+=query(tree[x].r,tree[y].r,L,R,m+1,r);
 	return res;
}
int main()
{
	int t,l,r,p,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		cnt=ans=0;
		build(root[0],1,M);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			update(root[i],root[i-1],1,M,a[i]);
		}
		while(m--)
		{
			scanf("%d%d%d%d",&l,&r,&p,&k);
			l^=ans;r^=ans;p^=ans;k^=ans;
			int L=0,R=M;
			while(L<=R)
			{
				int mid=(L+R)>>1;
				int tmp=query(root[l-1],root[r],max(1,p-mid),min(M,p+mid),1,M);
				if(tmp>=k)
				{
					ans=mid;
					R=mid-1;
				}
				else L=mid+1;
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章