题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621

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
 
求区间内| a[i]-p | 第k小的数,二分答案,查询区间[ p-ans,p+ans ]内数的数量是否大于等于k
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define Max 1000000
#define maxn 100005
int a[maxn],b[maxn],T[maxn<<],L[maxn<<],R[maxn<<],sum[maxn<<],tot;
inline int update(int pre,int l,int r,int x)
{
int rt=++tot;
L[rt]=L[pre];
R[rt]=R[pre];
sum[rt]=sum[pre]+;
if(l<r)
{
int mid=l+r>>;
if(x<=mid)L[rt]=update(L[pre],l,mid,x);
else R[rt]=update(R[pre],mid+,r,x);
}
return rt;
}
inline int query(int u,int v,int ql,int qr,int l,int r)
{
if(ql<=l&&qr>=r)return sum[v]-sum[u];
int mid=l+r>>,ans=;
if(ql<=mid)ans+=query(L[u],L[v],ql,qr,l,mid);
if(qr>mid)ans+=query(R[u],R[v],ql,qr,mid+,r);
return ans;
}
int main()
{
int t;
cin>>t;
for(int W=;W<=t;W++)
{
int n,m;
cin>>n>>m;
for(int i=;i<=n;i++)
{
cin>>a[i];
}
T[]=sum[]=L[]=R[]=tot=;
for(int i=;i<=n;i++)
{
T[i]=update(T[i-],,Max,a[i]);
}
int l,r,p,k,x=;
for(int i=;i<=m;i++)
{
cin>>l>>r>>p>>k;
l^=x;r^=x;p^=x;k^=x;
int pl=,pr=Max;
while(pl<pr)
{
int mid=pl+pr>>;
if(query(T[l-],T[r],max(,p-mid),min(Max,p+mid),,Max)>=k)
{
x=mid;
pr=mid;
}
else pl=mid+;
}
cout<<x<<endl;
}
}
return ;
}

hdu6621 二分加主席树的更多相关文章

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

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

  2. BZOJ5343: [Ctsc2018]混合果汁 二分答案+主席树

    分析: 整体二分或二分答案+主席树,反正没有要求强制在线,两个都可以做... 贪心还是比较显然的,那么就是找前K大的和...和CQOI的任务查询系统很像 附上代码: #include <cstd ...

  3. BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树

    BZOJ_5343_[Ctsc2018]混合果汁_二分答案+主席树 题意:给出每个果汁的价格p,美味度d,最多能放的体积l.定义果汁混合后的美味度为果汁的美味度的最小值. m次询问,要求花费不大于g, ...

  4. BZOJ3277 串(后缀数组+二分答案+主席树)

    因为不会SAM,考虑SA.将所有串连起来并加分隔符,每次考虑计算以某个位置开始的子串有多少个合法. 对此首先二分答案,找到名次数组上的一个区间,那么只需要统计有多少个所给串在该区间内出现就可以了.这是 ...

  5. BZOJ4556 [Tjoi2016&Heoi2016]字符串 SA ST表 二分答案 主席树

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ4556.html 题目传送门 - BZOJ4556 题意 给定一个长度为 $n$ 的字符串 $s$ . ...

  6. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

  7. BZOJ 4556: [Tjoi2016&Heoi2016]字符串(后缀数组 + 二分答案 + 主席树 + ST表 or 后缀数组 + 暴力)

    题意 一个长为 \(n\) 的字符串 \(s\),和 \(m\) 个询问.每次询问有 \(4\) 个参数分别为 \(a,b,c,d\). 要你告诉它 \(s[a...b]\) 中的所有子串 和 \(s ...

  8. BZOJ2653 middle(二分答案+主席树)

    与中位数有关的题二分答案是很常用的trick.二分答案之后,将所有大于它的看成1小于它的看成-1,那么只需要判断是否存在满足要求的一段和不小于0. 由于每个位置是1还是-1并不固定,似乎不是很好算.考 ...

  9. bzoj4556: [Tjoi2016&Heoi2016]字符串 (后缀数组加主席树)

    题目是给出一个字符串,每次询问一个区间[a,b]中所有的子串和另一个区间[c,d]的lcp最大值,首先求出后缀数组,对于lcp的最大值肯定是rank[c]的前驱和后继,但是对于这个题会出现问题,就是题 ...

随机推荐

  1. java 泛型的嵌套(map例子)

    package july7; //泛型加Map的输出! import java.util.Iterator; import java.util.Map; import java.util.Map.En ...

  2. Js 时间戳显示和计算时间间隔

    显示时间戳 很多地方会让页面显示当前时间并实时计时功能,例:2019年5月23号 10:28::34 代码实现如下: getTime(){ var mydate = new Date(); var y ...

  3. mysql导出csv/sql/newTable/txt的方法,mysql的导入txt/sql方法...mysql备份恢复mysqlhotcopy、二进制日志binlog、直接备份文件、备份策略、灾难恢复.....................................................

    mysql备份表结构和数据 方法一. Create table new_table_nam备份到新表:MYSQL不支持: Select * Into new_table_name from old_t ...

  4. The call() and apply() Mtheods

    Example 6-4function classof(o) {     if (o === null) return "Null";     if (o ===undefined ...

  5. 2019-9-3-win10-uwp-收集-DUMP-文件

    title author date CreateTime categories win10 uwp 收集 DUMP 文件 lindexi 2019-09-03 17:48:44 +0800 2018- ...

  6. H3C 端口绑定典型配置举例

  7. linux 基于 jiffy 的超时

    到目前为止所展示的次优化的延时循环通过查看 jiffy 计数器而不告诉任何人来工作. 但是最好的实现一个延时的方法, 如你可能猜想的, 常常是请求内核为你做. 有 2 种方 法来建立一个基于 jiff ...

  8. jquery file upload + asp.net 异步多文件上传

    百度了很久,国内一直 找不到 使用jquery file upload 插件 +asp.net 的相关代码 一开始使用 jquery uploadify ,一款基于 flash的插件,但是不支持 Sa ...

  9. kubernetes实战(三十):CentOS 8 二进制 高可用 安装 k8s 1.17.x

    1. 基本说明 本文章将演示CentOS 8二进制方式安装高可用k8s 1.17.x,相对于其他版本,二进制安装方式并无太大区别. 2. 基本环境配置 主机信息 192.168.1.19 k8s-ma ...

  10. java中把某个字符串中的单引号替换成双引号

    String regexp = "\'";String str = "'good'";System.out.println("替换前:" + ...