杭电多校第四场-H- K-th Closest Distance
题目描述
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.
输入
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).
输出
样例输入
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的更多相关文章
- 杭电多校第四场 Problem K. Expression in Memories 思维模拟
Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262 ...
- 2019杭电多校第四场hdu6621 K-th Closest Distance(二分答案+主席树)
K-th Closest Distance 题目传送门 解题思路 二分答案+主席树 先建主席树,然后二分答案mid,在l和r的区间内查询[p-mid, p+mid]的范围内的数的个数,如果大于k则说明 ...
- 2019杭电多校第四场hdu6623 Minimal Power of Prime
Minimal Power of Prime 题目传送门 解题思路 先打\(N^\frac{1}{5}\)内的素数表,对于每一个n,先分解\(N^\frac{1}{5}\)范围内的素数,分解完后n变为 ...
- 杭电多校第四场 E Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- [2019杭电多校第四场][hdu6623]Minimal Power of Prime
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6623 题目大意为求一个数的唯一分解的最小幂次.即120=23*31*51则答案为1. 因为数字太大不能 ...
- [2019杭电多校第四场][hdu6621]K-th Closest Distance(主席树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621 题意为求区间[l,r]内第k小|a[i]-p|的值. 可以二分答案,如果二分的值为x,则判断区间 ...
- [2019杭电多校第四场][hdu6616]Divide the Stones
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6616 题意是说n个数分别为1-n,将n个数分成k堆,能否满足每堆个数相等,数值之和相等.保证n%k=0 ...
- [2019杭电多校第四场][hdu6614]AND Minimum Spanning Tree(贪心)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6614 题目大意是有一张n个点的完全图,n个点点权为1-n,边权为两点点权按位与(&).求最小生 ...
- 可持久化线段树的学习(区间第k大和查询历史版本的数据)(杭电多校赛第二场1011)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
随机推荐
- elasticsearch 中的Multi Match Query
在Elasticsearch全文检索中,我们用的比较多的就是Multi Match Query,其支持对多个字段进行匹配.Elasticsearch支持5种类型的Multi Match,我们一起来深入 ...
- Java高级应用(一)
下面来介绍一下Java的高级应用有哪些. Java高级应用 第一讲 类加载 (一).类加载 类加载器是一个特殊的类,负责在运行时寻找和加载类文件.Java允许使用不同的类加载器,甚至是自定义类加载器. ...
- Servlet中如何获取用户提交的查询参数或表单数据?
①HttpServletRequest的getParameter()方法. ②HttpServletRequest的getParameterValues()方法. ③HttpServletReques ...
- 第二则java读取excel文件代码
// 得到上传文件的保存目录,将上传的文件存放于WEB-INF目录下,不允许外界直接访问,保证上传文件的安全 String savePath = this.getServletContext().ge ...
- array_shift — 将数组开头的单元移出数组
<?php $stack = array("orange", "banana", "apple", "raspberry&q ...
- USB转串口CH340接线方法
https://blog.csdn.net/wangjiaweiwei/article/details/49612207 USB转串口模块可以使用5V电压供电,需要将跳帽按下图安装. USB转串口模块 ...
- Anaconda/Conda创建环境时报错的解决方案
按照Conda网站上的提示安装完Conda之后,想要用conda create创建环境,一直报错: ERROR conda.core.link:_execute_actions(337): An er ...
- Spring data jpa 依赖配置
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- 部署.net Core 到 Windows server 2008 r2 IIs
1. 将项目发布 2.iis 新建网站,设置 3 安装windows server hosting 4 关于报错 1.下载sdk https://www.microsoft.com/net/lea ...
- linux IPC socket(3)server简单写法
写server的一些流程总结 一.向内核申请一个socket TCP形式 sock_fd = socket(AF_INET, SOCK_STREAM, ); UDP形式 sfd = socket(AF ...