题目描述

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.

输入

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).

输出

For each query print a single line containing the Kth closest distance between p and aL, aL+1, ..., aR.

样例输入

1
5 2
31 2 5 45 4
1 5 5 1
2 5 3 2

样例输出

0
1
题意
给一个序列A,每次询问L,R,p,k,输出[L,R]区间中第k大的|p-a[i]| 思路
二分答案,查询[L,R]中[p-mid,p+mid]的数的个数
可以用主席树/归并树维护区间x,y之间数的个数(题解说还可以用线段树)
主席树O(mlog1e6logn) 归并树O(mlog1e6logn^)
归并树:https://www.cnblogs.com/bennettz/p/8342242.html
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+;
int T,n,m;
int a[N],t[][N];
int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void build(int s,int l,int r)
{
if (l==r)
{
t[s][l]=a[l];
return;
}
int mid=(l+r)>>;
build(s+,l,mid); build(s+,mid+,r);
for (int i=l,j=mid+,k=l;i<=mid||j<=r;)
{
if (j>r) t[s][k++]=t[s+][i++];
else if (i>mid || t[s+][i]>t[s+][j]) t[s][k++]=t[s+][j++];
else t[s][k++]=t[s+][i++];
}
}
int query(int s,int l,int r,int L,int R,int x,int y)
{
if (x>y ) return ;
if (L<=l&r<=R)
{
//printf("x=%d,y=%d,l=%d,r=%d,>y=%d,>=x=%d\n",x,y,l,r,upper_bound(t[s]+l,t[s]+r+1,y),upper_bound(t[s]+l,t[s]+r+1,x));
return upper_bound(t[s]+l,t[s]+r+,y)-lower_bound(t[s]+l,t[s]+r+,x);
}
int mid=(l+r)>>,ans=;
if (L<=mid) ans+=query(s+,l,mid,L,R,x,y);
if (R>mid) ans+=query(s+,mid+,r,L,R,x,y);
return ans;
}
int main()
{
// freopen("14162.in","r",stdin);
// freopen("1.out","w",stdout);
T=read();
while(T--)
{
n=read(); m=read();
//memset(t,0,sizeof(t)); for (int i=;i<=n;i++) a[i]=read();
build(,,n); int ans=;
int L,R,p,k;
while (m--)
{
L=read(); R=read(); p=read(); k=read();
L^=ans; R^=ans; p^=ans; k^=ans;
int l=,r=;
while (l<=r)
{
int mid=(l+r)>>;
//cout<<mid<<endl;
if (query(,,n,L,R,p-mid,p+mid)>=k) ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
}
// fclose(stdin);
// fclose(stdout);
return ;
}

归并树

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+;
int ls[N*],rs[N*],s[N*],root[N];
int a[N],b[N];
int T,n,m,sz;
int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
void Insert(int l,int r,int x,int &y,int v)
{
y=++sz;
s[y]=s[x]+;
if (l==r) return;
ls[y]=ls[x]; rs[y]=rs[x];
int mid=(l+r)>>;
if (v<=mid) Insert(l,mid,ls[x],ls[y],v);
else Insert(mid+,r,rs[x],rs[y],v);
}
int query(int l,int r,int L,int R,int x,int y)
{
if (L<=l&&r<=R) return s[y]-s[x];
int ret=;
int mid=(l+r)>>;
if (L<=mid) ret+=query(l,mid,L,R,ls[x],ls[y]);
if (R>mid) ret+=query(mid+,r,L,R,rs[x],rs[y]);
return ret;
} int main()
{
T=read();
while(T--)
{
sz=;
n=read(); m=read();
for (int i=;i<=n;i++) a[i]=read(),b[i]=a[i];
sort(b+,b++n);
int cnt=unique(b+,b++n)-b-;
for (int i=;i<=n;i++)
{
a[i]=lower_bound(b+,b++cnt,a[i])-b+;
Insert(,N,root[i-],root[i],a[i]);
}
int ans=;
int L,R,p,k;
while (m--)
{
L=read(); R=read(); p=read(); k=read();
L^=ans,R^=ans,p^=ans,k^=ans;
int l=,r=;
while (l<=r)
{
int mid=(l+r)>>;
int ll=lower_bound(b+,b++cnt,p-mid)-b+;
int rr=upper_bound(b+,b++cnt,p+mid)-b;
if (query(,N,ll,rr,root[L-],root[R])>=k) ans=mid,r=mid-;
else l=mid+;
}
printf("%d\n",ans);
}
}
return ;
}

主席树

 
 

杭电多校第四场-H- K-th Closest Distance的更多相关文章

  1. 杭电多校第四场 Problem K. Expression in Memories 思维模拟

    Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262 ...

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

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

  3. 2019杭电多校第四场hdu6623 Minimal Power of Prime

    Minimal Power of Prime 题目传送门 解题思路 先打\(N^\frac{1}{5}\)内的素数表,对于每一个n,先分解\(N^\frac{1}{5}\)范围内的素数,分解完后n变为 ...

  4. 杭电多校第四场 E Matrix from Arrays

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  5. [2019杭电多校第四场][hdu6623]Minimal Power of Prime

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6623 题目大意为求一个数的唯一分解的最小幂次.即120=23*31*51则答案为1. 因为数字太大不能 ...

  6. [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 题意为求区间[l,r]内第k小|a[i]-p|的值. 可以二分答案,如果二分的值为x,则判断区间 ...

  7. [2019杭电多校第四场][hdu6616]Divide the Stones

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6616 题意是说n个数分别为1-n,将n个数分成k堆,能否满足每堆个数相等,数值之和相等.保证n%k=0 ...

  8. [2019杭电多校第四场][hdu6614]AND Minimum Spanning Tree(贪心)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6614 题目大意是有一张n个点的完全图,n个点点权为1-n,边权为两点点权按位与(&).求最小生 ...

  9. 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)

    以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...

随机推荐

  1. css 设置背景图片透明

    最终效果: 背景图片css不变,再背景图片的里层元素设置css样式即可 opacity:0.4; filter:alpha(opacity=40); /* 针对 IE8 以及更早的版本 */

  2. 0001.第一个多线程demo--分批处理数据

    public class UserEntity { private String userId; private String userName; public String getUserId() ...

  3. shell脚本相关关系、浮点、循环

    将一堆命令放在脚本里变成可执行脚本执行: 脚本编写,文件名为.sh,获取hostname的主机名 系统变量: Env:查看系统变量 Set :打印所有变量可以和grep和用 Export path:定 ...

  4. 【Beta阶段】第十一次Scrum Meeting!

    第十一周会议记录 [Beta阶段]第十一次Scrum Meeting! 一.每日任务内容 本次会议为第十一次Scrum Meeting会议~ 本次会议主要探讨了各自进展以及后续要继续开展的工作. 小组 ...

  5. 75 OpenCV编译、图像处理等

    0 引言 记录图像处理的一些经验和使用OpenCV 等库的注意事项. 1 opencv中的坐标系 一图以蔽之~ 2 opencv 3.4.0 + opencv_contrib + qt编译 主要参考了 ...

  6. jquery实现回车键登录/搜索等确认功能

    button按钮提交方式: $('#search').click(function() { get_table(); }); //keyCode=13是回车键,设置回车键提交 $("body ...

  7. nodejs搭建服务器 和 操作数据库

    1.express框架:是一个简洁而灵活的 node.js Web应用框架.一般的项目都是基于这个框架开发的.http://www.runoob.com/nodejs/nodejs-express-f ...

  8. 【从0到1,搭建Spring Boot+RESTful API+Shiro+Mybatis+SQLServer权限系统】06、Mybatis+SQLServer集成

    1.增加POM依赖 注意pagehelper插件,我重写过,可以到我的这篇文章了解https://www.cnblogs.com/LiveYourLife/p/9176934.html <dep ...

  9. java并发编程笔记(四)——安全发布对象

    java并发编程笔记(四)--安全发布对象 发布对象 使一个对象能够被当前范围之外的代码所使用 对象逸出 一种错误的发布.当一个对象还没构造完成时,就使它被其他线程所见 不安全的发布对象 某一个类的构 ...

  10. JSP 取list的长度

    引入:<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> L ...