题目链接:Click here

大致题意:q次询问,每次询问你区间[L,R]中|p-ai|的值第k小的是多少

Solution:

直接找是很困难的,我们考虑二分答案,那么本题就十分简单了

我们对权值维护一颗主席树,每次只要查询区间[L,R]中权值在[p-mid,p+mid]之的数的个数就行了

Code:

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+1;
int n,m,maxn,lst,L,R,k,p,a[N];
int rt[N],sz[N*40];
int tot,ls[N*40],rs[N*40];
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
void build(int &q,int l,int r){
q=++tot;sz[q]=0;
if(l==r) return ;
int mid=l+r>>1;
build(ls[q],l,mid);
build(rs[q],mid+1,r);
}
void ins(int &q,int lst,int l,int r,int x){
q=++tot;sz[q]=sz[lst]+1;
ls[q]=ls[lst],rs[q]=rs[lst];
if(l==r) return ;
int mid=l+r>>1;
if(mid>=x) ins(ls[q],ls[lst],l,mid,x);
else ins(rs[q],rs[lst],mid+1,r,x);
}
int query(int q,int lst,int l,int r,int x,int y){
if(l>=x&&r<=y) return sz[q]-sz[lst];
int mid=l+r>>1,re=0;
if(mid>=x) re+=query(ls[q],ls[lst],l,mid,x,y);
if(mid<y) re+=query(rs[q],rs[lst],mid+1,r,x,y);
return re;
}
int check(int x){
int l=max(0,p-x),r=min(maxn,p+x);
int v=query(rt[R],rt[L-1],1,maxn,l,r);
return v>=k;
}
void solve(){
maxn=lst=tot=0;
n=read(),m=read();
for(int i=1;i<=n;i++){
a[i]=read();
maxn=max(maxn,a[i]);
}build(rt[0],1,maxn);
for(int i=1;i<=n;i++)
ins(rt[i],rt[i-1],1,maxn,a[i]);
for(int i=1;i<=m;i++){
L=read(),R=read(),p=read(),k=read();
L^=lst,R^=lst,p^=lst,k^=lst;
int l=0,r=maxn,re=maxn;
while(l<=r){
int mid=l+r>>1;
if(check(mid)) re=mid,r=mid-1;
else l=mid+1;
}printf("%d\n",re);lst=re;
}
}
int main(){
int t=read();
while(t--) solve();
return 0;
}

2019hdu多校 K-th Closest Distance的更多相关文章

  1. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

    HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...

  2. 杭电多校第四场-H- K-th Closest Distance

    题目描述 You have an array: a1, a2, , an and you must answer for some queries.For each query, you are g ...

  3. 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)

    K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...

  4. POJ 6621: K-th Closest Distance(主席树 + 二分)

    K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Jav ...

  5. hdu-6621 K-th Closest Distance

    题目链接 K-th Closest Distance Problem Description You have an array: a1, a2, , an and you must answer ...

  6. HDU - 6621 K-th Closest Distance 主席树+二分答案

    K-th Closest Distance 主席树第二波~ 题意 给你\(n\)个数\(m\)个询问,问\(i\in [l,r]\)计算每一个\(|a_{i}-p|\)求出第\(k\)小 题目要求强制 ...

  7. HDU6578 2019HDU多校训练赛第一场 1001 (dp)

    HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...

  8. HDU6579 2019HDU多校训练赛第一场1002 (线性基)

    HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...

  9. 2019HDU多校第四场题解

    1001.AND Minimum Spanning Tree 传送门:HDU6614 题意:给你一个又n个点的完全图,点编号从1~n,每条边的权值为被连接的两点编号按位与后的值.现在要你找到最小生成树 ...

  10. 2019HDU多校第一场1001 BLANK (DP)(HDU6578)

    2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...

随机推荐

  1. 使用render函数渲染组件

    使用render函数渲染组件:https://www.jianshu.com/p/27ec4467a66b

  2. noip2013day1-货车运输

    题目描述 \(A\)国有\(n\)座城市,编号从 \(1\)到\(n\),城市之间有 \(m\) 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 \(q\) 辆货车在运输货物, 司机们想知 ...

  3. postgresql 用 like 可以 复制结构包括主键约束

    create tabletablename ( like tablename INCLUDING INDEXES INCLUDING COMMENTS); PostgreSQL 动态表复制(CREAT ...

  4. SCUT - 492 - 鬼符「搦手的鬼畜生」 - 简单数学

    https://scut.online/p/492 求[1,a]范围内的a模m的逆元的数量. 一开始用扩展欧几里得算法草了一发,WA了,当时不太清楚模非质数的周期,看来扩展欧几里得算法的笔记才知道要加 ...

  5. python网络爬虫(2)回顾Python编程

    文件写入 def storFile(data,fileName,method='a'): with open(fileName,method,newline ='') as f: f.write(da ...

  6. 2.学习Application

    2学习Application Application对象事件 名称 说明 Activated 当应用程序成为前台应用程序时触发 Deactivated 当应用程序不再是前台应用程序时触发 Dispat ...

  7. 什么是RESTful API、WSGI、pecan

    RESTful API REST的全称是Representational State Transfer(表征状态转移), 是Roy Fielding在他的博士论文Architectural Style ...

  8. 使用git配置ssh的文章推荐

    https://blog.51cto.com/sgk2011/1925922 https://www.cnblogs.com/superGG1990/p/6844952.html https://bl ...

  9. phpmyadmin导入大容量.sql文件

    phpmyadmin导入大容量.sql文件 在phpmyadmin目录文件夹下建立一个文件夹,如importSqlFile 将想要导入的sql文件放入importSqlFile文件夹中 打开confi ...

  10. 使用layer弹窗和layui表单做新增功能

    注释:代码参考http://blog.51cto.com/825272560/1891158,在其修改之上而来,在此感谢! 1.需求:使用layer在弹窗内完成新增,成功后提示并刷新页面(父页面,li ...