题目链接

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. (12)ASP.NET Core 中的配置二(Configuration)

    1.内存配置 MemoryConfigurationProvider使用内存中集合作为配置键值对.若要激活内存中集合配置,请在ConfigurationBuilder的实例上调用AddInMemory ...

  2. Ubuntu下python安装mysqldb(驱动)

    最近在学习Django框架,需要使用到数据库,我使用的是mysql,跟java一样,需要安装驱动,这是驱动的下载网址http://sourceforge.net/projects/mysql-pyth ...

  3. 去掉matlab图像显示刻度

    图像显示后面加 set( gca, 'XTick', [], 'YTick', [] );

  4. 算法与数据结构基础 - 哈希表(Hash Table)

    Hash Table基础 哈希表(Hash Table)是常用的数据结构,其运用哈希函数(hash function)实现映射,内部使用开放定址.拉链法等方式解决哈希冲突,使得读写时间复杂度平均为O( ...

  5. Java匹马行天下之J2EE框架开发——Spring—>Spring框架知多少

    ————也许我注定成不了一个伟大的人,但是至少我可以做一个很棒的自己.我想我现在应该做的不是瞻前顾后,而是活在当下,正确认知自己,做好自己现在的工作,努力提升自己的能力,踏踏实实地做一个程序员 一.思 ...

  6. 【Java例题】5.5 两个字符串中最长公共子串

    5. 查找两个字符串中含有的最长字符数的公共子串. package chapter5; import java.util.Scanner; public class demo5 { public st ...

  7. Spark 系列(四)—— RDD常用算子详解

    一.Transformation spark 常用的 Transformation 算子如下表: Transformation 算子 Meaning(含义) map(func) 对原 RDD 中每个元 ...

  8. 使用webstorm调试node.js

    折腾半天,还是webstorm顺手,但也遇到一些小问题. 1.代码补全问题 nodeJS自身的补全 File->Project Setting->JavaScript->Librar ...

  9. Django2.2中间件详解

    中间件是 Django 用来处理请求和响应的钩子框架.它是一个轻量级的.底层级的"插件"系统,用于全局性地控制Django 的输入或输出,可以理解为内置的app或者小框架. 在dj ...

  10. Amazon S3 分布式存储的 python 接口实现

    Amazon s3 是一种分布式的对象存储.用键值对的方式,来存储数据.其中,存入的所有数据都是一个对象(object),每一个对象都有一个键(key)存在. 具有非常方便的 web 访问接口,以及权 ...