题目链接

K-th Closest Distance

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

题意

给出一个数组,m个询问l, r, p,k区间\([l, r]\)中\(|p-a_i|\)第k大的值是多少,ai相同多次计算

题解

二分答案,那么\(|p-a_i| \leq ans \Rightarrow p-ans \leq ai \leq p+ans\),利用主席树判断区间\([l,r]\)内的\(a_i\)满足上面限制的数是否大于等于k个,少于k个则增大ans,多于则减小ans,复杂度\(O(nlog^2n)\)。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int mx = 1e5+5;
typedef long long ll; int a[mx], root[mx], cnt;
vector <int> v;
struct node {
int l, r, sum;
}T[mx*40]; int getid(int x) {
return lower_bound(v.begin(), v.end(), x) - v.begin() + 1;
} void update(int l, int r, int &x, int y, int pos) {
T[++cnt] = T[y]; T[cnt].sum++; x = cnt;
if (l == r) return;
int mid = (l+r) / 2;
if (mid >= pos) update(l, mid, T[x].l, T[y].l, pos);
else update(mid+1, r, T[x].r, T[y].r, pos);
} int query(int l, int r, int x, int y, int k) {
if (k == 0) return 0;
if (1 <= l && r <= k) {
return T[y].sum - T[x].sum;
}
int mid = (l + r) / 2;
int ans = 0;
if (1 <= mid) ans += query(l, mid, T[x].l, T[y].l, k);
if (mid < k && mid < r) ans += query(mid+1, r, T[x].r, T[y].r, k);
return ans;
} int main() {
int T;
scanf("%d", &T); while (T--) {
v.clear(); cnt = 0;
int n, m;
scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 1; i <= n; i++) update(1, n, root[i], root[i-1], getid(a[i])); int l, r, p, k, ans = 0;
while (m--) {
scanf("%d%d%d%d", &l, &r, &p, &k);
l = l^ans;
r = r^ans;
p = p^ans;
k = k^ans;
int pk = query(1, n, root[l-1], root[r], p);
int L = 0, R = 0x3f3f3f3f;
while (L < R) {
int mid = (L + R) / 2;
int Lid = getid(p-mid) - 1;
int Rid = getid(p+mid);
if (v[Rid-1] != p+mid) Rid--;
int sum = query(1, n, root[l-1], root[r], Lid) + (r-l+1- query(1, n, root[l-1], root[r], Rid));
sum = (r-l+1 - sum);
if (sum >= k) R = mid;
else L = mid + 1;
}
ans = L;
printf("%d\n", ans);
}
}
return 0;
}

hdu-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. POJ 6621: K-th Closest Distance(主席树 + 二分)

    K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Jav ...

  3. HDU6621 K-th Closest Distance HDU2019多校训练第四场 1008(主席树+二分)

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

  4. 杭电多校第四场-H- K-th Closest Distance

    题目描述 You have an array: a1, a2, , an and you must answer for some queries.For each query, you are g ...

  5. 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)

    K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...

  6. HDU 6621"K-th Closest Distance"(二分+主席树)

    传送门 •题意 有 $m$ 次询问,每次询问求 $n$ 个数中, $[L,R]$ 区间距 $p$ 第 $k$ 近的数与 $p$ 差值的绝对值: •题解 二分答案,假设当前二分的答案为 $x$,那么如何 ...

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

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

  8. HDU 6521 K-th Closest Distance (主席树+二分)

    题意: 给你一个数组,q次询问,每次问你[l,r]范围内与p距离第k大的元素的与p的距离,强制在线 思路: 主席树提取出[l,r]内的权值线段树,然后二分与p的距离mid ask该权值线段树里[p-m ...

  9. 【35.43%】【hdu 4347】The Closest M Points

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) Total Submissio ...

随机推荐

  1. 洛谷 P5150 题解

    题面 因为 n=lcm(a,b)n = lcm(a, b)n=lcm(a,b) ,可以得出: a  和 b  的质因数都是 n 的质因数 对于 n  的每个质因数 x ,在 n 中的次数为 y ,那么 ...

  2. Mybatis整合Spring 使用

    1.继承通用的Mapper<T>,必须指定泛型<T> 例如下面的例子: public interface UserInfoMapper extends Mapper<Us ...

  3. 运行sh文件

    记下在Ubuntu下安装*.sh和*.bin的简单方法. *.sh文件安装方法: 运行终端到文件目录下 1.在终端输入:sudo sh *.sh直接运行 2.在终端输入:sudo chmod +x * ...

  4. 解释一下一门语言该有的东东(Javascript)

    注释 Js中有两种注释 // 单行注释 /**/ 多行注释 变量 变量就像学校学习的 未知数 如 3 + x = 8 x: 类似变量,在改造一下 x + y = z 当 x=3, y=5, z=8, ...

  5. F#周报2019年第31期

    新闻 现在开始接受FSSF的第七次师友计划申请 Xamarin播客:XAML热重载 TorchSharp:将PyTorch引擎带入.NET 视频及幻灯片 F#中的异步编程2/3--实现异步工作流 ML ...

  6. SonarQube系列三、Jenkins集成SonarQube(dotnetcore篇)

    [前言] 本系列主要讲述sonarqube的安装部署以及如何集成jenkins自动化分析.netcore项目.目录如下: SonarQube系列一.Linux安装与部署 SonarQube系列二.分析 ...

  7. 《机器学习技法》---核型SVM

    (本文内容和图片来自林轩田老师<机器学习技法>) 1. 核技巧引入 如果要用SVM来做非线性的分类,我们采用的方法是将原来的特征空间映射到另一个更高维的空间,在这个更高维的空间做线性的SV ...

  8. 100天搞定机器学习|Day23-25 决策树及Python实现

    算法部分不再细讲,之前发过很多: [算法系列]决策树 决策树(Decision Tree)ID3算法 决策树(Decision Tree)C4.5算法 决策树(Decision Tree)CART算法 ...

  9. 前端面试题集锦(二)之CSS部分

    1.CSS中的选择器都有哪些?权限情况如何? 解答: (1)类选择器 .className  (2) ID选择器 #id  (3) 元素选择器 div 可以多个,以逗号隔开 (4)父子选择器 以空格隔 ...

  10. Kubernetes-保障集群内节点和网络安全

    13.1.在pod中使用宿主节点的Linux命名空间 13.1.1.在pod中使用宿主节点的网络命名空间 在pod的yaml文件中就设置spec.hostNetwork: true 这个时候pod使用 ...