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 ...
随机推荐
- 洛谷P1188PASTE题解
题目 这个题主要是一个考分类讨论的模拟题,做这个提的时候首先要脑子清醒,才可以清楚地写出怎么模拟来. \(Code\) #include <iostream> #include <a ...
- js侧边菜单
目标 实现一个侧边栏菜单,最多二级,可以收起展开.用于系统左侧的主菜单. 大多数系统都会有这样的菜单,用于导航功能,切换到不同的操作页面.在单页应用系统中,菜单一般是固定在左侧,分组节点上配图标,高亮 ...
- mysql日志分析工具之mysqlsla
背景介绍: 很多情况下,都需要对MySQL日志进行各种分析,来了解系统运行的方方面面.MySQL官方自带了一些工具对日志进行分析,比如mysqlbinlog可以用来分析二进制日志,mysqlslow可 ...
- 【洛谷P1516】青蛙的约会
题目大意:给定 \(a,b,c\),求线性同余方程 \(ax+by=c\) 的最小正整数解. 题解:首先判断方程是否有解,若 c 不能整出 a 与 b 的最大公约数,则无解.若有解,则利用扩展欧几里得 ...
- Day048--jQuery自定义动画和DOM操作
内容回顾 BOM location.reload() 全局刷新页面 location.href location.hash location.pathname location.hostname lo ...
- expansion pattern ‘Frame&’ contains no argument packs
camera/CameraImpl.h::: error: expansion pattern ‘Frame&’ contains no argument packs void read_fr ...
- 一文学会matplotlib
matplotlib基础 “““ 假设一天中每隔两个小时(range(2,26,2))的气温(℃)分别是[15,13,14.5,17,20,25,26,26,27,22,18,15] 用matplot ...
- 数据库学习之MySQL进阶
数据库进阶 一.视图 数据库视图是虚拟表或逻辑 ...
- nginx反向代理-解决前端跨域问题
1.定义 跨域是指a页面想获取b页面资源,如果a.b页面的协议.域名.端口.子域名不同,所进行的访问行动都是跨域的,而浏览器为了安全问题一般都限制了跨域访问,也就是不允许跨域请求资源.注意:跨域限制访 ...
- oracle数据库驱动(ojdbc)
第1部分 Q:为什么oralce的jdbc驱动,在maven上搜索到把pom配置复制到pom.xml里进行引用的时候会报错? ANS:虽然能在maven仓库里搜索到,但貌似不能用,原因是oracle是 ...