2019 Multi-University Training Contest 4 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): 1723    Accepted Submission(s): 640


 

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

题目大意:求区间绝对值第k小。

解题思路:比赛的时候稍微暴力了一点,在主席树上二分找到p的位置,然后维护一个大小为k的大根堆,就完了。但是复杂度好像不太对劲。多了一个k*2*log(k).....。T飞了。。。

题解上说直接二分绝对值,然后求【mid-p,mid+p】值域之间有多少个数字,>=k就是合法的。

#include<bits/stdc++.h>
#define LL long long
#define pb(x) push_back(x)
#define inf 0x3f3f3f3f
#define sca(x) scanf("%d",&x)
using namespace std;

const int N = 1e5+5;
int a[N],b[N];
int c[N];
int L[N*20],R[N*20];
int sum[N*20],T[N];
int cnt=0;

#define FI(n) FastIO::read(n)

namespace FastIO {
    const int SIZE = 1 << 16;
    char buf[SIZE], obuf[SIZE], str[60];
    int bi = SIZE, bn = SIZE, opt;
    int read(char *s) {
        while (bn) {
            for (; bi < bn && buf[bi] <= ' '; bi++);
            if (bi < bn) break;
            bn = fread(buf, 1, SIZE, stdin);
            bi = 0;
        }
        int sn = 0;
        while (bn) {
            for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
            if (bi < bn) break;
            bn = fread(buf, 1, SIZE, stdin);
            bi = 0;
        }
        s[sn] = 0;
        return sn;
    }
    bool read(int& x) {
        int n = read(str), bf;

        if (!n) return 0;
        int i = 0; if (str[i] == '-') bf = -1, i++; else bf = 1;
        for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
        if (bf < 0) x = -x;
        return 1;
    }
};

inline void build(int &rt,int l,int r)
{
    rt=++cnt;
    if(l==r)return ;
    int m=(l+r)>>1;
    build(L[rt],l,m);
    build(R[rt],m+1,r);
}

inline void update(int &rt,int pre,int l,int r,int x)
{
    rt=++cnt;
    L[rt]=L[pre],R[rt]=R[pre];
    sum[rt]=sum[pre]+1;
    if(l==r)return ;
    int m=(l+r)>>1;
    if(x<=m) update(L[rt],L[pre],l,m,x);
    else update(R[rt],R[pre],m+1,r,x);
}


int ask(int now,int pre,int l,int r,int pos)
{
    if(b[r]<=pos)
    {
        return sum[now]-sum[pre];
    }
    if(l==r&&b[l]>pos)
    {
        return 0;
    }
    int m=(l+r)>>1;
    int del=sum[L[now]]-sum[L[pre]];

    if(pos<=b[m]) return ask(L[now],L[pre],l,m,pos);
    else return del+ask(R[now],R[pre],m+1,r,pos);
}

int top;

int main()
{
    int t;
    FI(t);
    while(t--)
    {
        cnt=0;top=0;
        int n,m;
        FI(n),FI(m);
        for(register int i=1;i<=n;i++)
        {
            FI(a[i]);
            b[i]=a[i];
        }

        sort(b+1,b+1+n);
        build(T[0],1,n);

        for(register int i=1;i<=n;i++)
        {
            int p=lower_bound(b+1,b+1+n,a[i])-b;
            update(T[i],T[i-1],1,n,p);
        }

        int pre=0;
        for(register int i=1;i<=m;i++)
        {
            int l,r,p,k;
            FI(l),FI(r),FI(p),FI(k);
            l=(l^pre);
            r=(r^pre);
            p=(p^pre);
            k=(k^pre);
            k=min(k,169);
            if(l>r)swap(l,r);
            int L=0,R=3e6;
            int ans=1;
            while(L<=R)
            {
                int mid=(L+R)>>1;
                if(ask(T[r],T[l-1],1,n,p+mid)-ask(T[r],T[l-1],1,n,p-mid-1)>=k)R=mid-1,ans=mid;
                else L=mid+1;
            }
            printf("%d\n",ans);
            pre=ans;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值