杭电多校第四场1008K-th Closest Distance[二分+主席树]

K-th Closest Distance
Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2400 Accepted Submission(s): 860

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

题意:给一个序列,有q次询问,每次询问一个区间[L,R]内元素和x相减后得到的差的绝对值的第k小

题解:因为每次的x都不一样,所以不可能直接使用主席树求出区间第k小,可以把求与x差绝对值的第k小转化为找一个y使在区间[L,R]内元素值在[x-y,x+y]区间内元素个数==k,可以使用二分y的方式通过主席树进行check

#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout<<#x<<" is "<<x<<endl;
typedef long long ll;
const int maxn=1e6+5;
struct node{
    int l;
    int r;
    int val;
}nod[maxn<<2];
int tot,root[maxn],a[maxn],b[maxn],siz;
void build(int &rt,int l,int r){
    rt=++tot;
    nod[rt].val=0;
    if(l==r)return;
    int mid=(l+r)>>1;
    build(nod[rt].l,l,mid);
    build(nod[rt].r,mid+1,r);
}
void update(int &rt,int l,int r,int last,int x){
    rt=++tot;
    nod[rt].val=nod[last].val+1;
    nod[rt].l=nod[last].l;
    nod[rt].r=nod[last].r;
    if(l==r){
        return;
    }
    int mid=(l+r)>>1;
    if(mid>=x)update(nod[rt].l,l,mid,nod[last].l,x);
    else update(nod[rt].r,mid+1,r,nod[last].r,x);
}
int query(int rt,int l,int r,int l0,int r0){
    if(l>=l0&&r<=r0)return nod[rt].val;
    int mid=(l+r)>>1;
    int xx=0;
    if(mid>=l0)xx+=query(nod[rt].l,l,mid,l0,r0);
    if(mid<r0)xx+=query(nod[rt].r,mid+1,r,l0,r0);
    return xx;
}
bool check(int x,int p,int lw,int rw,int k){
    int l=max(b[1],p-x);
    int r=min(b[siz],p+x);
    int L=lower_bound(b+1,b+1+siz,l)-b;
    int R=lower_bound(b+1,b+1+siz,r)-b;
    if(R>siz||b[R]>r)R--;
    if(L>R)return false;
    int ac=query(root[rw],1,siz,L,R)-query(root[lw-1],1,siz,L,R);
    return ac>=k;
}
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        tot=0;
        int n,q;
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            b[i]=a[i];
        }
        sort(b+1,b+1+n);
        siz=(b+1,b+1+n)-(b+1);
		build(root[0],1,siz);
        for(int i=1;i<=n;i++){
            int c=lower_bound(b+1,b+1+siz,a[i])-b;
            update(root[i],1,n,root[i-1],c);
        }
        int x=0;
        while(q--){
            int l,r,p,s;
            scanf("%d%d%d%d",&l,&r,&p,&s);
            l^=x;
            r^=x;
            p^=x;
            s^=x;
            int L=0;
            int R=1e6;
            int ans;
            while(L<=R){
                int mid=(L+R)>>1;
                if(check(mid,p,l,r,s)){
                    ans=mid;
                    R=mid-1;
                }
                else{
                    L=mid+1;
                }
            }
            printf("%d\n",ans);
            x=ans;
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值