2019杭电多校第四场 K-th Closest Distance (主席树 二分)

K-th Closest Distance

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

 

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

 

//http://acm.hdu.edu.cn/showproblem.php?pid=6621
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5 + 5;
const int M = 2e6 + 5;
struct node{ int l, r, sum; }T[maxn * 40];
int cnt, a[maxn],root[maxn];
#define mid (l+r>>1)

void update(int l, int r, int &x, int y, int pos){
	T[++cnt] = T[y]; T[cnt].sum++; x = cnt;
	if (l == r) return;
	if (mid >= pos) update(l, mid, T[x].l, T[y].l, pos);
	else update(mid + 1, r, T[x].r, T[y].r, pos);
}

int query(int x, int y,int l, int r, int pl,int pr){
	//在[x,y]区间内查询数值在[pl,pr]中的出现次数
	if (pl <=l&&pr>=r) return T[y].sum-T[x].sum;
	int res = 0;
	if (pl <= mid&&T[T[y].l].sum - T[T[x].l].sum) res += query(T[x].l, T[y].l, l, mid, pl, pr);
	if (pr > mid&&T[T[y].r].sum - T[T[x].r].sum) res += query(T[x].r, T[y].r, mid + 1, r, pl, pr);
	return res;
}
int main(){
	//freopen("C:/Users/八百标兵奔北坡/Desktop/input.txt", "r", stdin);
	int T;
	scanf("%d", &T);
	while (T--){
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++)
			scanf("%d", &a[i]), update(1, M, root[i], root[i - 1], a[i]);
		int ans = 0;
		for (int i = 1; i <= m; i++){
			int L, R, p, k;
			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 m = l + r >> 1;
				int num = query(root[L - 1], root[R], 1, M, max(1, p - m), min(M, p + m));
				if (num >= k) ans = m, r = m - 1;
				else l = m + 1;
			}
			printf("%d\n", ans);
		}
	}
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值