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. 初入JavaWeb(半成品)

    2019-10-21 20:51:03 初次进行Javaweb的开发. 要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示 ...

  2. 从入门到自闭之Python名称空间

    名称空间: 内置空间:python解释器自带的一块空间 全局空间:py文件中顶格写的就是全局空间 局部空间:函数体中就是局部空间 加载顺序: 内置空间 全局空间 局部空间 # def func(): ...

  3. mac 下安装mysql8.0

    有两种安装方式,一种是安装包安装,官网下载安装包,mysql8.0下载.mysql5.7安装: 这里记录brew安装: 1.brew uninstall mysql 卸载原有的: 2.brew ins ...

  4. 客户端相关知识学习(三)之Android原生与H5交互的实现

    Android原生与H5交互的实现 H5调用原生的方式 方式可能有多种,根据开发经验,接触过两种方式. 方法一:Android向H5注入全局js对象,也就是H5调Android 1.首先对WebVie ...

  5. Java中字符串排序

    package com.fs.test; import java.util.ArrayList; import java.util.Collections; import java.util.List ...

  6. 帝国cms 获取一条数据,但是从第二条开始获取

    /*这里的1指的是获取一条数据,2指的是从第二条开始获取*/ [e:loop={"select * from phome_ecms_news where classid='2' limit ...

  7. ios 动画:底部标签栏的概念设计

    本文汇集了大量优秀的ios底部标签栏动效设计,并附上技术实现的指南,对于设计师和开发者都提供了很好的灵感来源和学习资源. 我们每天都见到tab bars,它们指引用户,使得他们能够快速在不同的tab中 ...

  8. pl/sql 笔记之基础(上)

    由于公司中使用 oracle,而本人对存储过程一直也懵懵懂懂,故一周时间学习了一遍 pl/sql,在此记下笔记!!! 一.前提,pl/sql 是啥? 1.PL/SQL是一种高级数据库程序设计语言,该语 ...

  9. js 继承的简单理解

    什么是继承 js中的继承就是获取存在对象已有属性和方法的一种方式. 继承一 属性拷贝 就是将对象的成员复制一份给需要继承的对象. // 创建父对象 var superObj = { name:'liy ...

  10. 如何使用sqlalchemy获取表的主键、以及每一个字段名和对应类型

    使用sqlalchemy获取到的结果只包含数据,不包含字段,那么我们如何获取到对应字段和其属性呢?以及如何获取某张表的主键呢? # -*- coding:utf-8 -*- # @Author: Wa ...