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 小,这个绝对值是多少 分析:首先我们先分析单次查询怎么做: 题目给出的数据与多次查询已经在提示着 ...
随机推荐
- 数据绑定-绑定Servlet内置对象
数据绑定:获取用户提交的参数,绑定到入参的参数中,就叫数据绑定. 绑定Servlet内置对象: 测试:
- Shell随机生成字符串
随机生成18位的字符串,数字 大小写字符 斜线 password=`openssl rand -base64 |-`
- ccs之经典布局(一)(水平垂直居中)
经典的css布局有以下几种,下面分别用不同的方法进行实现且进行对比. 一.水平居中 水平居中布局指的是当前元素在父级元素的容器中,水平方向上显示的是居中的,有以下几种方式来完成布局: 1.margin ...
- scrapy-redis 实现分布式爬虫
分布式爬虫 一 介绍 原来scrapy的Scheduler维护的是本机的任务队列(存放Request对象及其回调函数等信息)+本机的去重队列(存放访问过的url地址) 所以实现分布式爬取的关键就是,找 ...
- 通用mapper将另外一个同名的表生成在同一个实体及mapper中
今天遇见了一个在网上都搜索不到的错误,使用通过mapper生成实体及mapper文件时会将另外一个数据库的同名文件生成在一个实体及mapper中,这样就会造成一个实体和mapper中有两个表的字段,经 ...
- Delphi 参数的传递
- 《python解释器源码剖析》第0章--python的架构与编译python
本系列是以陈儒先生的<python源码剖析>为学习素材,所记录的学习内容.不同的是陈儒先生的<python源码剖析>所剖析的是python2.5,本系列对应的是python3. ...
- Codeforces Round #581 (Div. 2)A BowWow and the Timetable (思维)
A. BowWow and the Timetable time limit per test1 second memory limit per test256 megabytes inputstan ...
- Atcoder Regular 099 暴力区间扩张 n/dig(n)极值打表 团分割背包
C 直接把第一次加在哪里for一遍即可 /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) u ...
- eclipse error pages 打红X的解决方法
建一个Maven项目转为web项目的时候,这里总有这么多文件,而且还会有一个Error Pages的错红的红叉,看着很不爽. 虽然项目中没有出现过错误,感觉是个错误,解决方法如下.原来这是eclips ...