杭电多校第四场-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)
以前我们学习了线段树可以知道,线段树的每一个节点都储存的是一段区间,所以线段树可以做简单的区间查询,更改等简单的操作. 而后面再做有些题目,就可能会碰到一种回退的操作.这里的回退是指回到未做各种操作之 ...
随机推荐
- Vue中子组件数据跟着父组件改变和父组件数据跟着子组件改变的方法
一,子组件数据跟着父组件改变 父组件的代码 <template> <div class="home"> <img alt="Vue logo ...
- webacp4.0
'use strict';const path = require('path'); var APP_PATH = path.resolve(__dirname, 'src');const webpa ...
- 【记录】MongoDB
什么情况建议使用MongoDB? 1:满足对数据库的高并发读写 2:对海量数据的高效存储和访问 3:对数据库高扩展性和高可用性 4:灵活的数据结构,满足数据结构不固定的场景 5:应用需要2000-30 ...
- Python 第三天学习整理①
今天学习的内容包括以下几个方面:1.字符编码 2.文件操作 3.函数 1.字符编码 关于字符编码,关注以下几个点 1.1 什么是字符编码 字符编码:字符转化为数字的过程所遵循的标准 字符编码包括:un ...
- Python实现简单抓取功能
一直以来都想好好学习Python,但是每次学习了从基础感觉学了一会就感觉没意思.今天学习一下爬虫,也算是自己学python的目的吧,但是在学习过程中遇到很多困难,但幸好遇到了一篇好博文,分享给大家:h ...
- CentOS 7 virtualenv创建python3与python2的环境&&运行项目
(一)安装virtualenv 可以 yum -y install python-virtualenv 或者pip install python-virtualenv (二)在希望的路径下,创建e ...
- java web项目获取项目路径
注意:有时获取到的项目路径后再+“自定义路径后” 路径不可用,这时要看下项目里自定义路径是不是空文件夹,如果是空文件夹则调试和运行时文件夹不会编译到部署文件里. 1.方法一 调试时只能获取eclips ...
- 关于python接口测试connect error
接口测试里如果报错出现 socket.gaierror: [Errno 8] nodename nor servname provided, or not known 或者 urllib3.excep ...
- java.sql.SQLException: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_0900_ai_ci,IMPLICIT) for operation '='
查询视图时报错:java.sql.SQLException: Illegal mix of collations (utf8mb4_general_ci,IMPLICIT) and (utf8mb4_ ...
- 【原理】Reids字典
I.字典的实现 Redis的字典使用哈希表作为底层实现. 1.1 哈希表 Redis字典所使用的哈希表结构定义如下: typedef struct dictht { // 哈希表数组 dictEntr ...