POJ 6621: 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): 1697 Accepted Submission(s): 633
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.
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).
5 2
31 2 5 45 4
1 5 5 1
2 5 3 2
1
#include <iostream>
#include <cstdio> using namespace std; const int maxn = 1e5+;
const int INF = 1e6+; struct node {
int l, r, s;
}tree[maxn * ]; int root[maxn];
int arr[maxn];
int n, m;
int cnt; void update(int l, int r, int pre, int &cur, int pos) {
cur = ++cnt;
tree[cur] = tree[pre];
tree[cur].s++;
if(l == r) {
return;
}
int mid = (l + r) >> ;
if(pos <= mid) {
update(l, mid, tree[pre].l, tree[cur].l, pos);
} else {
update(mid + , r, tree[pre].r, tree[cur].r, pos);
}
} int query(int x, int y, int l ,int r, int pre, int cur) {
if(x <= l && r <= y) {
return tree[cur].s - tree[pre].s;
}
int mid = (l + r) >> ;
int res = ;
if(x <= mid) {
res += query(x, y, l ,mid, tree[pre].l, tree[cur].l);
}
if(y > mid) {
res += query(x, y, mid + , r, tree[pre].r, tree[cur].r);
}
return res;
} int main() {
int T;
scanf("%d", &T);
while(T--) {
cnt = ;
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &arr[i]);
update(, INF, root[i - ], root[i], arr[i]);
}
int ans = ;
while(m--) {
int l, r, p, k;
scanf("%d %d %d %d", &l, &r, &p, &k);
l = l ^ ans;
r = r ^ ans;
p = p ^ ans;
k = k ^ ans;
int pl = , pr = INF;
while(pl <= pr) {
int mid = (pl + pr) >> ;
if(query(max(, p - mid), min(INF, p + mid), , INF, root[l - ], root[r]) >= k) { //此处的max和min是为了防止超出[0, 1e6]的范围
ans = mid;
pr = mid - ;
} else {
pl = mid + ;
}
}
printf("%d\n", ans);
}
}
return ;
}
POJ 6621: 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$ 小的 $| ...
- HDU 6521 K-th Closest Distance (主席树+二分)
题意: 给你一个数组,q次询问,每次问你[l,r]范围内与p距离第k大的元素的与p的距离,强制在线 思路: 主席树提取出[l,r]内的权值线段树,然后二分与p的距离mid ask该权值线段树里[p-m ...
- HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)
HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分) 传送门:http://acm.hdu.edu.cn/showproblem.php? ...
- 计蒜客 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 ...
- 2018湘潭邀请赛C题(主席树+二分)
题目地址:https://www.icpc.camp/contests/6CP5W4knRaIRgU 比赛的时候知道这题是用主席树+二分,可是当时没有学主席树,就连有模板都不敢套,因为代码实在是太长了 ...
- BZOJ.1926.[SDOI2010]粟粟的书架(前缀和 主席树 二分)
题目链接 题意: 在给定矩形区域内找出最少的数,满足和>=k.输出数的个数.两种数据范围. 0~50 注意到(真没注意到...)P[i,j]<=1000,我们可以利用前缀和预处理. num ...
- HDU - 4866 主席树 二分
题意:在x轴\([1,X]\)内的上空分布有n个占据空间\([L_i,R_i]\),高度\(D_i\)的线段,射中线段的得分为其高度,每次询问从x轴的\(x\)往上空射的最近k个线段的总得分,具体得分 ...
- HDU6621 K-th Closest Distance 第 k 小绝对值(主席树(统计范围的数有多少个)+ 二分 || 权值线段树+二分)
题意:给一个数组,每次给 l ,r, p, k,问区间 [l, r] 的数与 p 作差的绝对值的第 k 小,这个绝对值是多少 分析:首先我们先分析单次查询怎么做: 题目给出的数据与多次查询已经在提示着 ...
随机推荐
- HDU 2018 Cow Story DP
Basic DP Problem URL:https://vjudge.net/problem/HDU-2018 Describe: There is a cow that gives birth t ...
- 了解MyISAM与InnoDB的索引差异(转)
出处原文: 1分钟了解MyISAM与InnoDB的索引差异 数据库的索引分为主键索引(Primary Inkex)与普通索引(Secondary Index).InnoDB和MyISAM是怎么利用B+ ...
- JavaScript冒泡排序法实现排序操作
var arr = [10,8,6,9,1,7,1,13,5,1,9]; //冒泡排序 function bubbleSort(tmpArr){ for(var i = tmpArr.length-1 ...
- npm操作命令
查看所有高级的npm moudles npm list --depth= 查看所有全局安装的模块 npm list --depth= -global 查找npm全局安装模块路径 npm config ...
- SpringMVC基础02——HelloWorld
1.搭建环境 博主使用的环境是IDEA2017.3,首先我们需要创建一个maven项目父项目,创建一个project,选择maven,之后点击next 添写当前项目的坐标,之后点击next 填写项目名 ...
- Rinetd 通过ECS端口转发到内网RDS
前置条件 实现目的:开发本地电脑需要连接没有外网地址的RDS,通过ECS进行转发连接到RDS数据库 客户 PC 终端可以 ssh 登录有公网的 ECS 服务器. 有公网的 ECS 服务器可以通过内网访 ...
- cgicc使用
CgiCc 使用 一.下载 下载地址: http://ftp.gnu.org/gnu/cgicc/ ftp://ftp.gnu.org/gnu/cgicc/ 二.配置.编译.安装 下载完成后解压:sh ...
- iptables 设置特定IP访问指定端口
一.添加规则:设置禁止所有IP访问指定端口8075 [root@zabbix_server ~]# iptables -I INPUT -p tcp --dport -j DROP 二.测试telne ...
- c# HttpClient和HttpWebRequest添加Basic类型的Authentication认证
c#项目中用到调用客户接口,basic身份认证,base64格式加密(用户名:密码)贴上代码以备后用 1.使用HttpClient实现basic身份认证 using (HttpClient clien ...
- monkey命令大全
一.认识monkey Monkey 就是SDK中附带的一个工具.Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中: 它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入 ...