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 ...
随机推荐
- CMS Collector and G1 Collector
Understanding the CMS Collector CMS has three basic operations: CMS collects the young generation (s ...
- SpringBoot之普通类获取Spring容器中的bean
package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...
- python doc格式转文本格式
首先python是不能直接读写doc格式的文件的,这是python先天的缺陷.但是可以利用python-docx (0.8.6)库可以读取.docx文件或.txt文件,且一路畅通无阻. 这样的话,可以 ...
- WC2019滚粗记
什么?你问WC2019滚粗记在哪里? 抱歉,这篇文章鸽了. 原因? 引用神仙\(yyb\)的话. 恩,想了想还是更一点吧. Day 0 签到海星,我写了个大大的\(Cgod\)有没有人看见啊,然后被广 ...
- EOJ 306 树上问题
题解: 因为w大于1,所以,题意就是,有多少(x,z),存在x到z的路径上,有一个x<y<z的y w没用的其实. 树上路径问题,有什么方法吗? 1.树链剖分.这个主要方便处理修改操作. 2 ...
- LOJ#2553 暴力写挂
题意:给定两棵树T1,T2,求d1[x] + d1[y] - d1[lca1(x, y)] - d2[lca2(x, y)]的最大值. 解:考虑把上面这个毒瘤东西化一下.发现它就是T1中x,y到根的路 ...
- django系列5:视图
在Django中,网页和其他内容由视图提供.每个视图都由一个简单的Python函数(或基于类的视图的方法)表示.Django将通过检查所请求的URL(确切地说,是域名后面的URL部分)来选择视图. 在 ...
- MapReduce 概述
定义 Hadoop MapReduce 是一个分布式运算程序的编程框架,用于轻松编写分布式应用程序,以可靠,容错的方式在大型集群(数千个节点)上并行处理大量数据(TB级别),是用户开发 “基于 Had ...
- Maven 学习总结 (一)
一.何为Maven 1.Maven是优秀的构建工具 maven的用途之一是用于构建,他是一个强大的构建工具,能够帮助我们自动化构建过程,从清理.编译.测试到生成报告,再到打包和部署. 他抽象了一个完整 ...
- 新年 flag
在浮躁的年代本不该如此贪多,奈何鸭梨山大...温故知新吧 GO中文社区 深入学习一两门新的编程语言: -Go编程基础 -Go Web基础 -Go名库讲解 rustlang 中文文档 知乎板块 GO 知 ...