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. 初步了解autoencoder

    初步了解autoencoder 学习自莫烦python 什么是autoencoder? 自编码(autoencoder)是一种神经网络的形式. 例子:一张图片->对其进行打码->最后再将其 ...

  2. VMware中centos7访问外网配置

    1.配置虚拟机网络适配器,选择NAT模式 2.在编辑->虚拟机网络编辑器->更改设置 选择目前使用的网卡 3.通过ifconfig查看网卡配置 4.编辑网络配置对应上面网卡名称ens33 ...

  3. node工具之nodemon

    nodemon nodemon是一种工具,可以自动检测到目录中的文件更改时通过重新启动应用程序来调试基于node.js的应用程序. 安装 npm install -g nodemon //或 npm ...

  4. maven项目转换为gradle项目

    进入到项目更目录,运行 gradle init --type pom 上面的命令会根据pom文件自动生成gradle项目所需的文件和配置,然后以gradle项目重新导入即可.

  5. selenium自动化测试工具模拟登陆爬取当当网top500畅销书单

    selenium自动化测试工具可谓是爬虫的利器,基本动态加载的网页都能抓取,当然随着大型网站的更新,也出现针对selenium的反爬,有些网站可以识别你是否用的是selenium访问,然后对你加以限制 ...

  6. 数据绘图工具之Matplotlib

    一.安装:绘图和可视化 pip install matplotlib 我们已经下好了anaconda 包含了绘图工具包 直接导入即可 import matplotlib.pyplotlib as pl ...

  7. jq无限极树结构

    //群组树结构$(function () { var params= { "companyId":cmpId }; var loadUrl="/apiv2/classif ...

  8. ASP.NET c# 实验日记(1)

    第一次写有一些紧张,以前学过html,c语言,vb,c#等语言.也自己翻过有关javascript的书,现在的目的是怎么把学习经验写的更具结构化和条理化,大佬勿喷. 在一个集成开发平台里第一步就是新建 ...

  9. 自学Python-基于tcp协议的socket

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  10. Java语言基础(1)

    1 计算机语言发展的分类 1)机器语言:由0,1组成(二进制),可以在计算机底层直接识别并执行(唯一). 2)汇编语言:由助记符组成,比机器语言简单.当执行的时候,把汇编语言转换为机器语言(0101) ...