HDU 6521 K-th Closest Distance (主席树+二分)
题意:
给你一个数组,q次询问,每次问你[l,r]范围内与p距离第k大的元素的与p的距离,强制在线
思路:
主席树提取出[l,r]内的权值线段树,然后二分与p的距离mid
ask该权值线段树里[p-mid,p+mid]的数的个数,使其刚好大于等于k
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1 using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e7+2e6+;
const int maxm = 4e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0); int ls[maxn],rs[maxn],dat[maxn];
int tot;
int n,q;
int a[maxm];
int root[maxm];
int t;
int insert(int now, int l, int r, int x, int val){
int p = ++tot;
ls[p]=ls[now];rs[p]=rs[now];dat[p]=dat[now];
if(l==r){
dat[p]+=val;
return p;
}
int mid = (l+r)>>;
if(x<=mid)ls[p]=insert(ls[now],l,mid,x,val);
else rs[p]=insert(rs[now],mid+,r,x,val);
dat[p]=dat[ls[p]]+dat[rs[p]];
return p;
}
int ask(int lst, int now, int l, int r, int L, int R){
//printf("%d %d %d %d %d %d\n",lst,now,l,r,L,R);
int mid = (l+r)>>;
int ans = ;
if(L<=l&&r<=R)return dat[now]-dat[lst];
if(L<=mid)ans+=ask(ls[lst],ls[now],l,mid,L,R);
if(mid<R)ans+=ask(rs[lst],rs[now],mid+,r,L,R);
return ans;
}
int K,p;
bool cmp(int a, int b){
return abs(a-p)<abs(b-p);
} int main(){
scanf("%d", &t); while(t--){
int lstans = ;
mem(dat,);
tot=;
scanf("%d %d", &n, &q);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
root[i]=insert(root[i-],,,a[i],);
} while(q--){
int l, r;
scanf("%d %d %d %d", &l, &r, &p, &K);
l^=lstans;
r^=lstans;
p^=lstans;
K^=lstans;
int ans;
int L = ;
int R = ;
while(L<=R){
int mid = (L+R)>>;
int res = ask(root[l-],root[r],,,max(,p-mid),min(,p+mid));
if(res>=K){
ans=mid;
R=mid-;
}
else L=mid+;
}
lstans=ans;
printf("%d\n",ans); }
}
return ;
}
/*
1
5 2
31 2 5 45 4
1 5 5 1
2 5 3 2 */
HDU 6521 K-th Closest Distance (主席树+二分)的更多相关文章
- HDU - 6621 K-th Closest Distance 主席树+二分答案
K-th Closest Distance 主席树第二波~ 题意 给你\(n\)个数\(m\)个询问,问\(i\in [l,r]\)计算每一个\(|a_{i}-p|\)求出第\(k\)小 题目要求强制 ...
- 2019HDU多校第四场 K-th Closest Distance ——主席树&&二分
题意 给定 $n$ 个数,接下来有 $q$ 次询问,每个询问的 $l, r, p, k$ 要异或上一次的答案,才是真正的值(也就是强制在线).每次询问,输出 $[l, r]$ 内第 $k$ 小的 $| ...
- HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)
HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- POJ 6621: K-th Closest Distance(主席树 + 二分)
K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
- 计蒜客 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 ...
- HDU - 4866 主席树 二分
题意:在x轴\([1,X]\)内的上空分布有n个占据空间\([L_i,R_i]\),高度\(D_i\)的线段,射中线段的得分为其高度,每次询问从x轴的\(x\)往上空射的最近k个线段的总得分,具体得分 ...
- 2018湘潭邀请赛C题(主席树+二分)
题目地址:https://www.icpc.camp/contests/6CP5W4knRaIRgU 比赛的时候知道这题是用主席树+二分,可是当时没有学主席树,就连有模板都不敢套,因为代码实在是太长了 ...
- BZOJ.1926.[SDOI2010]粟粟的书架(前缀和 主席树 二分)
题目链接 题意: 在给定矩形区域内找出最少的数,满足和>=k.输出数的个数.两种数据范围. 0~50 注意到(真没注意到...)P[i,j]<=1000,我们可以利用前缀和预处理. num ...
- HDU6621 K-th Closest Distance 第 k 小绝对值(主席树(统计范围的数有多少个)+ 二分 || 权值线段树+二分)
题意:给一个数组,每次给 l ,r, p, k,问区间 [l, r] 的数与 p 作差的绝对值的第 k 小,这个绝对值是多少 分析:首先我们先分析单次查询怎么做: 题目给出的数据与多次查询已经在提示着 ...
随机推荐
- json查询结果绑定
M_Hisorder.doQuery = function (){ $("#dataList").empty(); var data = ""; var url ...
- 学习集合Collection_通用方法
Collection 常用接口 集合List和Set通用的方法 public boolean add(E e) 添加对象到集合 public boolean remove(E e) 删除指定元素 pu ...
- Go Web 编程之 静态文件
概述 在 Web 开发中,需要处理很多静态资源文件,如 css/js 和图片文件等.本文将介绍在 Go 语言中如何处理文件请求. 接下来,我们将介绍两种处理文件请求的方式:原始方式和http.File ...
- mysql 多主一从
一.主服务器准备 1.1.环境准备 两台主机器ip分别为 100.100.100.105 (主1) 100.100.100.106(主2) 安装 mysql [root@centos ~]# yum ...
- txLive模块(直播类)试用分享
本文出自APICloud官方论坛, 感谢论坛版主uoaccw的分享. txLive 模块封装了腾讯云直播服务 https://docs.apicloud.com/Client-API/Open-SDK ...
- 关于爬虫的日常复习(11)—— 实战:flask+redis维护代理池(to be continue)
- 关于爬虫的日常复习(7)—— DOM操作及selenium库
- DirectX11 Windows Windows SDK--28 计算着色器:波浪(水波)
前言 有关计算着色器的基础其实并不是很多.接下来继续讲解如何使用计算着色器实现水波效果,即龙书中所实现的水波.但是光看代码可是完全看不出来是在做什么的.个人根据书中所给的参考书籍找到了对应的实现原理, ...
- Java入门 - 语言基础 - 08.运算符
原文地址:http://www.work100.net/training/java-operator.html 更多教程:光束云 - 免费课程 运算符 序号 文内章节 视频 1 概述 2 算术运算符 ...
- spring boot配置spring-data-jpa的时候报错CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager f ...