【主席树+二分答案】 HDU-6621 杭电2019多校4 K-th Closest Distance

http://acm.hdu.edu.cn/showproblem.php?pid=6621

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

题意:给你n个数(大小在1-1e6),100000次询问,问你【l,r】区间的数与p的差的绝对值的第k小是多少
题解:一眼主席树,因为数的权值最大到1e6,直接按照权值建树,维护区间权值个数,询问时,二分答案,答案一定是在0-1000000之间,二分如果查出来的个数>=k,就缩左区间,否则,放右区间,更新答案

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int num=1e6;
int T[maxn*10],L[maxn*200],R[maxn*200],sum[maxn*200],a[maxn];
int tot;

void build(int &rt,int l,int r)
{
    rt=++tot;
    sum[rt]=0;
    if(l==r)
        return;
    int mid=(l+r)/2;
    build(L[rt],l,mid);
    build(R[rt],mid+1,r);
}

void update(int &rt,int pre,int l,int r,int x)
{
    rt=++tot;
    L[rt]=L[pre],R[rt]=R[pre],sum[rt]=sum[pre]+1;
    if(l==r)
        return;
    int mid=(l+r)/2;
    if(x<=mid) update(L[rt],L[pre],l,mid,x);
    else update(R[rt],R[pre],mid+1,r,x);
}

int query(int rt,int pre,int l,int r,int ql,int qr)
{
    if(ql<=l&&qr>=r)
    {
        return sum[pre]-sum[rt];
    }
    int mid=(l+r)/2;
    int ans=0;
    if(ql<=mid) ans+=query(L[rt],L[pre],l,mid,ql,qr);
    if(qr>mid) ans+=query(R[rt],R[pre],mid+1,r,ql,qr);
    return ans;
}

int main()
{
    int cas;
    scanf("%d",&cas);
    while(cas--)
    {
        int ans=0;
        T[0]=L[0]=R[0]=sum[0]=tot=0;
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        build(T[0],1,num);
        for(int i=1;i<=n;i++)
        {
            update(T[i],T[i-1],1,num,a[i]);
        }
        while(m--)
        {
            int ql,qr,p,k;
            scanf("%d%d%d%d",&ql,&qr,&p,&k);
            ql^=ans,qr^=ans,p^=ans,k^=ans;
            if(ql>qr) swap(ql,qr);
            int l=0,r=num;
            while(l<=r)
            {
                int mid=(l+r)/2;
                if(query(T[ql-1],T[qr],1,num,p-mid,p+mid)>=k)
                {
                    r=mid-1;
                    ans=mid;
                }
                else l=mid+1;
            }
            printf("%d\n",ans);
        }
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值