Codeforces 522D Closest Equals
题解:
傻逼题
直接从左向右扫描每个点作为右端点
然后单点修改区间查询就行了
另外一种更直观的做法就是$(i,j)$之间产生了$(j-i)$
于是变成矩形查最大值,kd-tree维护
代码:
#include <bits/stdc++.h>
#define rint register int
#define IL inline
#define rep(i,h,t) for (int i=h;i<=t;i++)
#define dep(i,t,h) for (int i=t;i>=h;i--)
#define me(x) memset(x,0,sizeof(x))
#define ll long long
namespace IO{
char ss[<<],*A=ss,*B=ss;
IL char gc()
{
return A==B&&(B=(A=ss)+fread(ss,,<<,stdin),A==B)?EOF:*A++;
}
template<class T>void read(T &x)
{
rint f=,c; while (c=gc(),c<||c>) if (c=='-') f=-; x=(c^);
while (c=gc(),c>&&c<) x=(x<<)+(x<<)+(c^); x*=f;
}
char sr[<<],z[]; int Z,C=-;
template<class T>void wer(T x)
{
if (x<) sr[++C]='-',x=-x;
while (z[++Z]=x%+,x/=);
while (sr[++C]=z[Z],--Z);
}
IL void wer1() {sr[++C]=' ';}
IL void wer2() {sr[++C]='\n';}
template<class T>IL void maxa(T &x,T y) {if (x<y) x=y;}
template<class T>IL void mina(T &x,T y) {if (x>y) x=y;}
template<class T>IL T MAX(T x,T y) { return x>y?x:y;}
template<class T>IL T MIN(T x,T y) { return x<y?x:y;}
};
using namespace std;
using namespace IO;
const int N=6e5;
map<int,int> M;
int cnt,a[N],cmp_d,rt;
struct re{
int x,y,z;
}p[N];
bool cmp(re x,re y)
{
if (!cmp_d) return x.x<y.x;
else return x.y<y.y;
}
const int INF=1e9;
struct kd_tree{
int v[N],pv[N],ls[N],rs[N],Mx[N],Nx[N],My[N],Ny[N];
kd_tree()
{
v[]=pv[]=INF;
Nx[]=INF; Mx[]=;
Ny[]=INF; My[]=;
}
IL void updata(int x)
{
pv[x]=MIN(MIN(pv[ls[x]],pv[rs[x]]),v[x]);
Mx[x]=MAX(p[x].x,MAX(Mx[ls[x]],Mx[rs[x]]));
Nx[x]=MIN(p[x].x,MIN(Nx[ls[x]],Nx[rs[x]]));
My[x]=MAX(p[x].y,MAX(My[ls[x]],My[rs[x]]));
Ny[x]=MIN(p[x].y,MIN(Ny[ls[x]],Ny[rs[x]]));
}
int build(int h,int t,int o)
{
int x,mid; x=mid=(h+t)/;
cmp_d=o; nth_element(p+h,p+mid,p+t+,cmp);
v[x]=p[x].z;
if (h<mid) ls[x]=build(h,mid-,o^);
if (mid<t) rs[x]=build(mid+,t,o^);
updata(x);
return x;
}
int query(int x,int x1,int x2,int y1,int y2)
{
if (x1>Mx[x]||x2<Nx[x]||y1>My[x]||y2<Ny[x]) return(INF);
if (x1<=Nx[x]&&Mx[x]<=x2&&y1<=Ny[x]&&My[x]<=y2) return(pv[x]);
int ans=INF;
if (p[x].x>=x1&&p[x].x<=x2&&p[x].y>=y1&&p[x].y<=y2) ans=v[x];
mina(ans,query(ls[x],x1,x2,y1,y2));
mina(ans,query(rs[x],x1,x2,y1,y2));
return ans;
}
}K;
int main()
{
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
int n,m;
read(n); read(m);
rep(i,,n)
{
read(a[i]);
int k=M[a[i]];
if (k) p[++cnt]=(re){k,i,i-k};
M[a[i]]=i;
}
rt=K.build(,cnt,);
rep(i,,m)
{
int x,y;
read(x); read(y);
int ans=K.query(rt,x,y,x,y);
if (ans<INF) wer(ans); else wer(-);
wer2();
}
fwrite(sr,,C+,stdout);
return ;
}
Codeforces 522D Closest Equals的更多相关文章
- $Codeforces\ 522D\ Closest\ Equals$ 线段树
正解:线段树 解题报告: 传送门$QwQ$ 题目大意是说给定一个数列,然后有若干次询问,每次询问一个区间内相同数字之间距离最近是多少$QwQ$.如果不存在相同数字输出-1就成$QwQ$ 考虑先预处理出 ...
- codeforces 522D. Closest Equals 线段树+离线
题目链接 n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离. 先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段 ...
- CodeForces 522D Closest Equals 树状数组
题意: 给出一个序列\(A\),有若干询问. 每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1. 分析: 令\(pre_i = max\{j| A_j = A_i, j < ...
- Codeforces VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线线段树 求区间相同数的最小距离
D. Closest Equals Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/prob ...
- VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...
- D. Closest Equals(线段树)
题目链接: D. Closest Equals time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- Codeforces 962D - Merge Equals
链接: http://codeforces.com/problemset/problem/962/D 题意: 给出一个整数序列.选择其中最小且出现两次(或以上)的数,把最左边的两个从序列中移除,然后把 ...
- [CF522D]Closest Equals
题目大意:给一个区间,多次询问,每次问区间$[l,r]$里最近的两个相同的数的距离是多少. 题解:用一个数组$pre_i$表示第$i$个数前面最近的一个相同的数在哪,询问变成了询问$[l,r]$中$i ...
随机推荐
- Linux查看用户登录信息-last
last命令: last命令用于显示用户最近登录信息.单独执行last命令,将读取 /var/log/wtmp 文件,并将给该文件的内容记录的登入系统的用户名单全部显示出来. 语法: last (选项 ...
- 解决类似umount target is busy挂载盘卸载不掉问题
问题描述: Linux下挂载后的分区或者磁盘某些时候需要umount的时候出现类似“umount: /mnt: target is busy.”等字样,或者“umount: /xxx: device ...
- html页面中引入自签名证书的js web资源出现net::ERR_CERT_AUTHORITY_INVALID
其实是浏览器客户端对自签名的内容认为不安全引起的,临时方法可以再浏览器中先直接访问下那个自签名的https地址,然后再访问有引用的那个页面就可以了. 以下内容引用自https://www.morong ...
- css 函数
css还有一些强大的函数: 1. calc 可以混合多种单位来计算 div { font-size: calc(100vw/5 + 1rem - 100px) } 2. max.min.clamp m ...
- Python常用模块-时间模块
在写代码的过程中,我们常常需要与时间打交道,在python中,与时间处理有关的模块有time,datetime和calendar.,这里主要介绍time和datetime模块 在python中,表示时 ...
- Ceph集群搭建及Kubernetes上实现动态存储(StorageClass)
集群准备 ceph集群配置说明 节点名称 IP地址 配置 作用 ceph-moni-0 10.10.3.150 centos7.5 4C,16G,200Disk 管理节点,监视器 monitor ...
- VMware 设置网络
在VMware上安装 系统完成后,设置虚拟网络 这里的VMware 版本为 14. 本文以window server 2016 为例. 在虚拟机上菜单栏中, 编辑 >> 虚拟网络编辑器 ...
- Maven 学习总结(三) 之 依赖管理
聚合 为了要一次构建多个项目,而不是到每个模块目录下分别执行mvn命令.maven聚合这一特性就是为该需求服务的.为此我们需要创建一个额外的模块aggregator, 然后通过该模块构建整个项目的所有 ...
- HTML(五)HTML表格
标准表格 <table border="1"> <caption>Monthly savings</caption> <tr> &l ...
- requests中自定义adapter
# encoding:utf-8 import sslfrom requests import sessionsfrom requests import Requestfrom requests.ad ...