题意:给定一个长为n的序列,有m次强制在线的询问,每次询问位置【L,R】中abs(a[i]-p)第k小的值

n,m<=1e5,a[i]<=1e6,p<=1e6,k<=169

思路:主席树外面套个二分

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> Pll;
typedef vector<int> VI;
#define N 1100000
#define M 4100000
#define fi first
#define se second
#define MP make_pair
#define pi acos(-1)
#define mem(a,b) memset(a,b,sizeof(a))
#define rep(i,a,b) for(int i=(int)a;i<=(int)b;i++)
#define per(i,a,b) for(int i=(int)a;i>=(int)b;i--)
#define lowbit(x) x&(-x)
#define Rand (rand()*(1<<16)+rand())
#define id(x) ((x)<=B?(x):m-n/(x)+1)
#define ls p<<1
#define rs p<<1|1 const ll MOD=,inv2=(MOD+)/;
double eps=1e-;
int INF=1e9; struct node
{
int l,r,s;
}t[N*]; int root[N],cnt; int read()
{
int v=,f=;
char c=getchar();
while(c<||<c) {if(c=='-') f=-; c=getchar();}
while(<=c&&c<=) v=(v<<)+v+v+c-,c=getchar();
return v*f;
} void update(int l,int r,int x,int &p)
{
t[++cnt]=t[p];
p=cnt;
t[p].s++;
if(l==r) return;
int mid=(l+r)>>;
if(x<=mid) update(l,mid,x,t[p].l);
else update(mid+,r,x,t[p].r);
} int query(int l,int r,int x,int y,int p1,int p2)
{
if(x<=l&&r<=y) return t[p2].s-t[p1].s;
int mid=(l+r)>>;
int tmp=;
if(x<=mid) tmp+=query(l,mid,x,y,t[p1].l,t[p2].l);
if(y>mid) tmp+=query(mid+,r,x,y,t[p1].r,t[p2].r);
return tmp;
} int main()
{
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
int cas=read();
while(cas--)
{
int n=read(),q=read();
rep(i,,cnt) t[i].l=t[i].r=t[i].s=;
cnt=; root[]=;
rep(i,,n)
{
int x=read();
root[i]=root[i-];
update(,1e6,x,root[i]);
}
int ans=;
rep(i,,q)
{
int l=read(),r=read(),p=read(),k=read();
l^=ans; r^=ans; p^=ans; k^=ans;
int left=,right=1e6;
ans=1e6;
while(left<=right)
{
int mid=(left+right)>>;
int x=max(,p-mid),y=min(,p+mid);
int t=query(,1e6,x,y,root[l-],root[r]);
if(t>=k){ans=mid; right=mid-;}
else left=mid+;
}
printf("%d\n",ans);
}
}
return ;
}

【HDOJ6621】K-th Closest Distance(主席树,二分)的更多相关文章

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

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

  2. 2019HDU多校第四场 K-th Closest Distance ——主席树&&二分

    题意 给定 $n$ 个数,接下来有 $q$ 次询问,每个询问的 $l, r, p, k$ 要异或上一次的答案,才是真正的值(也就是强制在线).每次询问,输出 $[l, r]$ 内第 $k$ 小的 $| ...

  3. HDU 6521 K-th Closest Distance (主席树+二分)

    题意: 给你一个数组,q次询问,每次问你[l,r]范围内与p距离第k大的元素的与p的距离,强制在线 思路: 主席树提取出[l,r]内的权值线段树,然后二分与p的距离mid ask该权值线段树里[p-m ...

  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. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

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

  6. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  7. 2018湘潭邀请赛C题(主席树+二分)

    题目地址:https://www.icpc.camp/contests/6CP5W4knRaIRgU 比赛的时候知道这题是用主席树+二分,可是当时没有学主席树,就连有模板都不敢套,因为代码实在是太长了 ...

  8. BZOJ.1926.[SDOI2010]粟粟的书架(前缀和 主席树 二分)

    题目链接 题意: 在给定矩形区域内找出最少的数,满足和>=k.输出数的个数.两种数据范围. 0~50 注意到(真没注意到...)P[i,j]<=1000,我们可以利用前缀和预处理. num ...

  9. HDU - 4866 主席树 二分

    题意:在x轴\([1,X]\)内的上空分布有n个占据空间\([L_i,R_i]\),高度\(D_i\)的线段,射中线段的得分为其高度,每次询问从x轴的\(x\)往上空射的最近k个线段的总得分,具体得分 ...

随机推荐

  1. 测开之路八十九:HTML之图片处理

    <!--width.height设置图片尺寸 alt:当图片不能展示时,显示的内容 title:鼠标放上去时展示的内容--> <img src="../imges/img0 ...

  2. 4 cdh 5.12 centos 6.10三节点安装

    4 cdh 5.12  centos 6.10 三节点安装 [root@hadoop1 opt]# cat /etc/redhat-release CentOS release 6.10 (Final ...

  3. laravel使用artisan报错SQLSTATE[42S02]: Base table or view not found: 1146

    说明你在应用初始化阶段使用到了数据库层面的东西,然而当时数据库不存在这个表/字段,所以会报错 需要在初始化比如 config 目录配置中,使用了数据库,在使用前需要添加一层判断,如果不存在 你需要用到 ...

  4. 【openstf】自己的云测平台——mac安装openstf

    openstf的github地址:https://github.com/openstf/stf 上图可以清晰看出openstf的使用场景和效果   openstf是一个web应用程序,用于远程调试智能 ...

  5. linux下安装php扩展的redis

    bu要在网上顺便找个redis扩展,一是不安全,而是,别人的redis可能只能new,能连接,但是不保证是否能进行其他操作, 1.百度一下php redis扩展. 搜索到这个网址:http://pec ...

  6. [10期]浅谈SSRF安全漏洞

    引子:SSRF 服务端请求伪造攻击 很多web应用都提供从其他服务器上获取数据的功能.使用用户指定的URL,web应用可以从其他服务器获取图片,下载文件,读取文件内容等. 这个功能被恶意使用的话,可以 ...

  7. pandas入门(1)

    import pandas as pd import numpy as np # 自动创建索引 obj = pd.Series([4, 7, -5, 2]) print(obj, type(obj)) ...

  8. [Web 前端] 014 css 盒子模型

    1. 简介 元素在页面中显示成一个方块,类似一个盒子 把元素叫做盒子,设置对应的样式分别为 盒子的边框(border) 盒子内的内容和边框之间的间距(padding) 盒子与盒子之间的间距(margi ...

  9. Sentinel之熔断降级

    除了流量控制以外,对调用链路中不稳定的资源进行熔断降级也是保障高可用的重要措施之一.由于调用关系的复杂性,如果调用链路中的某个资源不稳定,最终会导致请求发生堆积.Sentinel 熔断降级会在调用链路 ...

  10. Java8 特性详解(一) Lambda

    为什么要使用lambda表达式 从函数式接口说起 理解Functional Interface(函数式接口)是学习Java8 lambda表达式的关键所在. 函数式接口的定义其实很简单:任何接口,如果 ...