题目链接: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. 【codeforces 764B】Timofey and cubes

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. HDU 3974 Assign the task

    Assign the task Problem Description There is a company that has N employees(numbered from 1 to N),ev ...

  3. CCPC 2018 吉林 C "JUSTICE" (数学)

    传送门 参考资料: [1]:https://blog.csdn.net/mmk27_word/article/details/89789770 题目描述 Put simply, the Justice ...

  4. 用Xshell连接谷歌云

    谷歌云服务器,默认用浏览器进行SSH链接,而且也不告知密码.以Centos为例,先使用浏览器连接 1,给root修改密码 1 sudo passwd root 2,编辑ssh配置文件 sudo nan ...

  5. Linux 内核设备属性

    sysfs 中的设备入口可有属性. 相关的结构是: struct device_attribute { struct attribute attr; ssize_t (*show)(struct de ...

  6. P3810 陌上花开 CDQ分治

    陌上花开 CDQ分治 传送门:https://www.luogu.org/problemnew/show/P3810 题意: \[ 有n 个元素,第 i 个元素有 a_i. b_i. c_i 三个属性 ...

  7. ES基本语法

    7.2.0版本 1 创建库 http://{ip}:{port}/{库名称} put 2 查询库 http://{ip}:{port}/_cat/indices?v get health status ...

  8. 第二阶段:4.商业需求文档MRD:4.PRD-用例和规则

    类似之前的泳道图 可以在下面添加一些描述 有时候用图还是会有一些限制 不能够很好的表达

  9. RecursiveTask和RecursiveAction的使用总结

    一:什么是Fork/Join框架    Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.我们再通 ...

  10. java中使用javaMail工具类发送邮件

    1.引入依赖 <!--javaMail--> <dependency> <groupId>javax.mail</groupId> <artifa ...