2019hdu多校 K-th Closest Distance
题目链接: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的更多相关文章
- HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)
HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- 杭电多校第四场-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 ...
- 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)
K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...
- POJ 6621: K-th Closest Distance(主席树 + 二分)
K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
- hdu-6621 K-th Closest Distance
题目链接 K-th Closest Distance Problem Description You have an array: a1, a2, , an and you must answer ...
- HDU - 6621 K-th Closest Distance 主席树+二分答案
K-th Closest Distance 主席树第二波~ 题意 给你\(n\)个数\(m\)个询问,问\(i\in [l,r]\)计算每一个\(|a_{i}-p|\)求出第\(k\)小 题目要求强制 ...
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- HDU6579 2019HDU多校训练赛第一场1002 (线性基)
HDU6579 2019HDU多校训练赛第一场1002 (线性基) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6579 题意: 两种操作 1.在序列末 ...
- 2019HDU多校第四场题解
1001.AND Minimum Spanning Tree 传送门:HDU6614 题意:给你一个又n个点的完全图,点编号从1~n,每条边的权值为被连接的两点编号按位与后的值.现在要你找到最小生成树 ...
- 2019HDU多校第一场1001 BLANK (DP)(HDU6578)
2019HDU多校第一场1001 BLANK (DP) 题意:构造一个长度为n(n<=10)的序列,其中的值域为{0,1,2,3}存在m个限制条件,表示为 l r x意义为[L,R]区间里最多能 ...
随机推荐
- CentOS 上面 恢复 Oracle 数据库实例的简单操作流程
1. 当获取了数据库的备份可以进行 oracle数据库的备份恢复操作 linux上面要复杂一些. 这里面简单描述一下. 2. 远程连接 linux 主要工具可以选择 xshell 如图示: 3. 建议 ...
- python nonlocal 的具体原理
很多文章都大概列了下nonlocal的具体用法,比如看到几篇文章写的 “nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量” 看完以后我感觉自己是懂了,但光从这句话来说还没完全理解它 ...
- CodeForces-431D Random Task
题目描述 求一个\(n\),使得\(n+1\)到\(2n\)这些数的二进制中恰好有\(k\)个\(1\)的数有\(m\)个. Input 输入包含两个正整数\(m,k\).$(0<=m<= ...
- redis5.0集群搭建
至少需要3个master节点,这里搭建三个master节点,并且给每个master节点在搭建一个slave节点,共6个redis节点,这里采用一台机器上创建6个redis实例,并将6个redis实例配 ...
- node(koa2)跨域与获取cookie
欲做一个node 的网关服务,通过 cookie 做信息传递,选择框架 koa2,这里简单记录跨域处理以及 cookie 获取. 首先:解决跨域问题,使用 koa2-cros 来处理,跨域问题后端处理 ...
- [转载]Linux运行模式及紧急、救援模式
运行模式 在Linux中,存在一个叫init(initialize)的进程,其进程号是1,该进程存在一个对应的配置文件inittab,叫做系统的运行级别配置文件,位置在/etc/inittab.(但是 ...
- java 正则表达式 复习
正则表达式在日常开发中会经常的接触到,学会了正则可以更有效的对字符进行验证.拆分.替换.判断字符串是否合法等操作... 常用语法: 字符的取值范围 1.[abc] : 表示可能是a,可能是b,也可能是 ...
- node端口被占用
Error: listen EADDRINUSE :::8000 at Object._errnoException (util.js:992:11) at _exceptionWithHostPor ...
- 在Linux上安装tomcat和JDK
1.tomcat的安装 a.#cd download(进入download文件夹) b.#wget http://111.23.5.142:82/mirrors.hust.edu.cn/apache/ ...
- websocket之拨云见雾
websocket是基于http相应的特性弥补其不足(就是个socket,不再是一次请求一次相应) 但缺点就是只有在版本较高的浏览器才支持websocket. 浏览器: <script type ...