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

Problem Description
You have an array: a1, a2, , an and you must answer for some queries.
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.
 
Input
The first line of the input contains an integer T (1 <= T <= 3) denoting the number of test cases.
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).
 
Output
For each query print a single line containing the Kth closest distance between p and aL, aL+1, ..., aR.
 
Sample Input
1
5 2
31 2 5 45 4
1 5 5 1
2 5 3 2
 
Sample Output
0
1
 
 
杭电多校第四场...
结构:主席树 + 二分
 
题解:首先按照模板走,创建一颗主席树。然后更具题目意思,我先设ans就是我二分的时候要找到的答案,那么就有 | p - an | = ans, 则有两种情况 p - an = ans , an - p = ans, 那么可以确定 an 的值域 [ p - ans, p + ans ]。
 
注意:主席树的数组一定要开的够大,不然就会TLE...我开的40倍就TLE了,看大佬的开的55倍,改了一下就过了。
 
#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(主席树 + 二分)的更多相关文章

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

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

  5. 计蒜客 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 ...

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

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

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

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

  8. HDU - 4866 主席树 二分

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

  9. HDU6621 K-th Closest Distance 第 k 小绝对值(主席树(统计范围的数有多少个)+ 二分 || 权值线段树+二分)

    题意:给一个数组,每次给 l ,r, p, k,问区间 [l, r] 的数与 p 作差的绝对值的第 k 小,这个绝对值是多少 分析:首先我们先分析单次查询怎么做: 题目给出的数据与多次查询已经在提示着 ...

随机推荐

  1. 2019-2020Nowcoder Girl初赛题解

    写了一天计算几何,心态崩了,水一篇题解休息休息. emmmm,如果您是一名现役OIer/CSPer,那看这篇文章也许并不能在你的生命中留下些什么(潮子语录),因为相比NOIP/CSP这个比赛其实比较简 ...

  2. Java web验证码——kaptcha的使用

    一.配置kaptcha的jar包 pom.xml配置: <-- 目前只有2.3.2版本--> <!-- https://mvnrepository.com/artifact/com. ...

  3. golang 多线程查找文件内容

    package main import ( "fmt" "io/ioutil" "os" "path/filepath" ...

  4. MySQL性能优化(一):优化方式

    原文:MySQL性能优化(一):优化方式 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/v ...

  5. 第四章 子查询 T-SQL语言基础

    子查询 SQL支持在查询语句中编写查询,或者嵌套其他查询. 最外层查询的结果集会返回给调用者,称为外部查询. 内部查询的结果是供外部查询使用的,也称为子查询. 子查询可以分为独立子查询和相关子查询.独 ...

  6. c# 转换Image为Icon

    /// <summary> /// 转换Image为Icon /// </summary> /// <param name="image">要转 ...

  7. 分布式的几件小事(六)dubbo如何做服务治理、服务降级以及重试

    1.服务治理 服务治理主要作用是改变运行时服务的行为和选址逻辑,达到限流,权重配置等目的. ①调用链路自动生成 一个大型的分布式系统,会由大量的服务组成,那么这些服务之间的依赖关系和调用链路会很复杂, ...

  8. bilibili小程序项目总结

    1.关于mock的使用 第一步:先到Mock官网(http://mockjs.com/)上面熟悉一下基本用法 第一步:具体使用实例: 下载wxMock.js和mock.js文件 下载地址:https: ...

  9. react 基础语法使用

    刚开始不久react,在菜鸟上及其他前辈网站上学习,下面开始我的自学笔记. 包括: 渲染元素 组件(函数方法定义.es6 class定义) 事件处理 条件渲染 列表 下面代码部分将不会再写html部分 ...

  10. Oracle创建表和创建序列和修改,增加sql字段

    一.创建表 create table y_parts_classify( pt_id number(10) not null, pt_name varchar2(1000), update_time ...