2019多校第四场 HDU6621 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): 3851    Accepted Submission(s): 1368


 

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

 

 

OJ题号

 HDU - 6621 K-th Closest Distance

 

简单题意

给出一段长度为n≤1e5的序列a1,a2,…,an(a≤1e6),有m≤1e5次询问:L R p k问ax~an中与p的第K小个差值的绝对值是多少?

正解思路

对答案进行二分,对于可能的答案x,对al~ar构成的权值线段树查找[p-x,p+x]是否有K个(主席树实现),最后一次满足≥K的x既是答案。

 


//https://cloud.tencent.com/developer/article/1486582
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn=1e5+7;
const int mod=1e9+7;
int t,n,m,cnt,root[maxn];
LL a[maxn],b[maxn];
//cnt和root:主席树的总点数和每一个根
struct node
{
    int l,r,sum;
} T[maxn*40];
vector<int> v;
int getid(int x)  //离散化
{
    return lower_bound(v.begin(),v.end(),x)-v.begin()+1;
}
/*
 v.clear();
 sort(v.begin(),v.end());
 v.erase(unique(v.begin(),v.end()),v.end());
*/
//清空,默认给root[0]
int build(int l,int r)
{
    int cur=++cnt;
    T[cur].sum=0;
    if(l==r)
        return cur;
    int mid=l+r>>1;
    build(l,mid);
    build(mid+1,r);
    return cur;
}
///单点修改
void update(int l,int r,int &now,int pre,int pos,int add)
{
    T[++cnt]=T[pre];
    T[cnt].sum+=add;
    now=cnt;
    if(l==r)
        return;
    int mid=(l+r)>>1;
    if(pos<=mid)
        update(l,mid,T[cnt].l,T[pre].l,pos,add);
    else
        update(mid+1,r,T[cnt].r,T[pre].r,pos,add);
}

///查询区间[x,y]第k小,使用v[query(1,n,root[x-1],root[y],mid)-1]
LL query(int l,int r,int x,int y,int k)
{
    if(l==r)
        return l;
    int mid=(l+r)/2;
    int sum=T[T[y].l].sum-T[T[x].l].sum;
    if(sum>=k)
        return query(l,mid,T[x].l,T[y].l,k);
    else
        return query(mid+1,r,T[x].r,T[y].r,k-sum);

}
///查询区间[x,y]里面数在[ql,qr]里面的个数  query_num1(1,b_n,lp,rp,root[x-1],root[y]);
LL ans_num=0;
void query_num1(int l,int r,int ql,int qr,int x,int y)
{
    //cout<<l<<" "<<r<<endl;
    if(ql<=l&&qr>=r)
    {
        ans_num+=T[y].sum-T[x].sum;
        return ;
    }
    if(l>qr||r<ql)
        return ;
    int mid=(l+r)>>1;
    if(ql<=mid)
        query_num1(l,mid,ql,qr,T[x].l,T[y].l);
    if(mid<qr)
        query_num1(mid+1,r,ql,qr,T[x].r,T[y].r);
}
///查询区间[x,y]里面数<=k里面的个数 query_num2(1,b_n,root[x-1],root[y],rp)
int query_num2(int l,int r,int x,int y,int k)  
{
    //cout<<l<<" "<<r<<endl;
    if(k==0)
        return 0;

    if(l==r)
        return T[y].sum - T[x].sum;
    int tmp = T[T[y].l].sum - T[T[x].l].sum;
    int mid = (l+r)>>1;
    if(k<=mid)
        return query_num2(l,mid,T[x].l,T[y].l,k);
    else
        return tmp+query_num2(mid+1,r, T[x].r,T[y].r,k);
}
/*

int two_find(int x,int y,int k)///二分查找第i小的元素小于等于k的最大元素
//即求k是第几小的元素
{
    int l=0,r=y-x+1;
    while(l<r)
    {
        int mid=(l+r+1)>>1;
        if(v[query(1,n,root[x-1],root[y],mid)-1]<=k)
            l=mid;
        else
            r=mid-1;
    }
    return l;
}
///查询区间[x,y]<=k的元素个数 query_min(root[x-1],root[y],1,n,k)
///查询区间[x,y]>=k的最小值 query_min(root[x-1],root[y],1,n,k)
int query_min(int lrot,int rrot,int l,int r,int k)
{
    if(l==r)
    {
        if(T[rrot].sum-T[lrot].sum>0)
            return l;
        else
            return 1e9;
    }
    int mid=(l+r)>>1;
    if(k<=mid)
    {
        int ans=1e9;
        if(k<=l)//这里相当于一个剪枝,在小于l的时候,如果左子树有符合条件的就进入左子树,否则再进入右子树。
        {
            if(T[T[rrot].l].sum-T[T[lrot].l].sum>0)
                ans=min(ans,query_min(T[lrot].l,T[rrot].l,l,mid,k));
            else if(T[T[rrot].r].sum-T[T[lrot].r].sum>0)
                ans=min(ans,query_min(T[lrot].r,T[rrot].r,mid+1,r,k));
            return ans;
        }
        if(T[T[rrot].l].sum-T[T[lrot].l].sum>0) //k在l到mid之间的时候,左右子树都有可能涉及,就左右都看一遍寻找最优解
            ans=min(ans,query_min(T[lrot].l,T[rrot].l,l,mid,k));
        if(T[T[rrot].r].sum-T[T[lrot].r].sum>0)
            ans=min(ans,query_min(T[lrot].r,T[rrot].r,mid+1,r,k));
        return ans;
    }
    else
    {
        int ans=1e9;//k大于mid的时候,直接进入右子树,左子树不用找了
        if(T[T[rrot].r].sum-T[T[lrot].r].sum>0)
            ans=min(ans,query_min(T[lrot].r,T[rrot].r,mid+1,r,k));
        return ans;
    }
}
///查询区间[x,y]的种类个数query_num(1,n,root[y],x)
int query_num(int l,int r,int root,int left)
{
    if(l>=left)
        return T[root].sum;
    int mid=(l+r)>>1;
    if(mid>=left)
        return query_num(l,mid,T[root].l,left)+T[T[root].r].sum;
    else
        return query_num(mid+1,r,T[root].r,left);
}*/
int main()
{
    int Q;
    scanf("%d",&Q);
    while(Q--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            b[i]=a[i];
        }

        sort(b+1,b+n+1);
        int b_n=unique(b+1,b+n+1)-(b+1);
        for(int i=1; i<=n; i++)
        {
            a[i]=lower_bound(b+1,b+b_n+1,a[i])-b;
        }
        cnt=0;
        root[0]=build(1,b_n);
        for(int i=1; i<=n; i++)
        {
            update(1,b_n,root[i],root[i-1],a[i],1);
        }
        int ans=0;
        while(m--)
        {
            int x,y,p,k;
            scanf("%d%d%d%d",&x,&y,&p,&k);
            x^=ans;
            y^=ans;
            p^=ans;
            k^=ans;

            int l=-1,r=1e9;
            while(l<r)
            {
                int mid=(l+r)>>1;

                int lp=max(lower_bound(b+1,b+b_n+1,p-mid)-b,1);
                int rp=min(upper_bound(b+1,b+b_n+1,p+mid)-b-1,b_n);

                ans_num=0;
                query_num1(1,b_n,lp,rp,root[x-1],root[y]);
                //ans_num=query_num2(1,b_n,root[x-1],root[y],rp)-query_num2(1,b_n,root[x-1],root[y],lp-1);
                cout<<mid<<" "<<lp<<" "<<rp<<" "<<ans_num<<endl;
                if(ans_num>=k)
                    r=mid;
                else
                    l=mid+1;

            }
            ans=l;
            printf("%d\n", ans);

        }
    }


    return 0 ;
}

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值