2019HDU多校第四场——HDU6621 K-th Closest Distance【主席树+二分】

题目链接 HDU6621 K-th Closest Distance

Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

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个数,每个询问给你 L R p k,要求你在[L,R]这个区间内找到|ai-p|的第k小

分析:

按照权值建树,二分以p为中点的区间半径mid,query查询[p-mid,p+mid]这个区间内数的个数,比较与k的大小关系从而更改二分区间,得到答案。

一开始想到是找到区间最小的点,然后这个点左右扩展找到第k小,但其实T的妈都不认识。

我自己的代码太丑了 悄咪咪贴队友的上来qwq

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1e5+5;
struct tree{int l,r,sum;}t[N*300];
int root[N],sz,a[N];
void update(int l,int r,int &x,int y,int n)
{
    t[++sz]=t[y],t[sz].sum++,x=sz;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(n<=mid) update(l,mid,t[x].l,t[y].l,n);
    else update(mid+1,r,t[x].r,t[y].r,n);
}
int query(int cl,int cr,int l,int r,int x,int y){
    if(cl<=l&&r<=cr) return t[y].sum-t[x].sum;
    int mid=(l+r)>>1,ans=0;
    if(cl<=mid) ans+=query(cl,cr,l,mid,t[x].l,t[y].l);
    if(cr>mid) ans+=query(cl,cr,mid+1,r,t[x].r,t[y].r);
    return ans;
}
int main()
{
    int T,n,m,X,Max,l,r,p,k,low,high,mid;scanf("%d",&T);
    while(T--)
    {
        sz=X=Max=0;scanf("%d%d",&n,&m);
        for(int i=1;i<=n;++i) scanf("%d",&a[i]),Max=max(Max,a[i]);
        for(int i=1;i<=n;++i) update(1,Max,root[i],root[i-1],a[i]);
        while(m--)
        {
            scanf("%d%d%d%d",&l,&r,&p,&k);l^=X,r^=X,p^=X,k^=X;
            low=0,high=1e6;
            while(low<high){
                mid=(low+high)>>1;//[p-mid,p+mid]
                if(query(max(1,p-mid),min(p+mid,Max),1,Max,root[l-1],root[r])>=k) high=mid;
                else low=mid+1;
            }
            printf("%d\n",X=high);
        }
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值