题目链接: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. H3C 以跳数评估的路由并非最优路径

  2. 无限调用函数add(1)(2)(3)......

    无限调用函数,并且累计结果 其实这也算一道面试题吧,笔者曾经被提问过,可惜当时没能答上来...

  3. web.xml和@WebServlet

    web.xml <servlet> <servlet-name>DZDYServlet</servlet-name> <servlet-class>包名 ...

  4. new Date(2019-08-24 12:30:00)和new Date(2019-08-29T02:15:08.000+0000)在ios的兼容NAN问题

    new Date()在安卓和pc端上正常显示,但是却在ios上显示 NAN的问题 正常写法: var time = new Date("2019-08-24 12:30:00"); ...

  5. vue-cli3 使用 svg-sprite-loader 的坑

    chainWebpack: config => { config.module.rules.delete("svg"); //重点:删除默认配置中处理svg, //const ...

  6. vue-learning:12 - 2 - 区分:outerHTML - innerTHML - outerText - innerText - textContent

    区分:outerHTML - innerTHML - outerText - innerText - textContent 获取值 <div id="outer"> ...

  7. Java 学习笔记(11)——多线程

    Java内部提供了针对多线程的支持,线程是CPU执行的最小单位,在多核CPU中使用多线程,能够做到多个任务并行执行,提高效率. 使用多线程的方法 创建Thread类的子类,并重写run方法,在需要启动 ...

  8. Flutter 添加阴影效果

    Container( width: double.infinity, height: ScreenUtil.getInstance().setHeight(500), decoration: BoxD ...

  9. Windows 服务安装与卸载 (通过 Sc.exe)

    1. 安装 新建文本文件,重命名为 ServiceInstall.bat,将 ServiceInstall.bat 的内容替换为: sc create "Verity Platform De ...

  10. Nodejs模拟并发,尝试的两种解决方案

    一.准备数据库表 创建商品库存表 db_stock ,插入一条数据 DROP TABLE IF EXISTS `db_stock`; CREATE TABLE `db_stock` ( `id` ) ...