题目链接:

http://codeforces.com/problemset/problem/522/D

D. Closest Equals

time limit per test3 seconds
memory limit per test256 megabytes
#### 问题描述
> You are given sequence a1, a2, ..., an and m queries lj, rj (1 ≤ lj ≤ rj ≤ n). For each query you need to print the minimum distance between such pair of elements ax and ay (x ≠ y), that:
>
> both indexes of the elements lie within range [lj, rj], that is, lj ≤ x, y ≤ rj;
> the values of the elements are equal, that is ax = ay.
> The text above understands distance as |x - y|.

输入

The first line of the input contains a pair of integers n, m (1 ≤ n, m ≤ 5·105) — the length of the sequence and the number of queries, correspondingly.

The second line contains the sequence of integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109).

Next m lines contain the queries, one per line. Each query is given by a pair of numbers lj, rj (1 ≤ lj ≤ rj ≤ n) — the indexes of the query range limits.

输出

Print m integers — the answers to each query. If there is no valid match for some query, please print -1 as an answer to this query.

样例输入

5 3

1 1 2 3 2

1 5

2 4

3 5

样例输出

1

-1

2

题意

求区间内相邻最近的两个相同的数的距离。

题解

线段树,先处理出所有的相同的数的相邻的间隔区间,把这些区间(之后称为事件)按右端点排序,对于所有的查询区间也同样排序,然后一边扫查询,一边插入事件,事件按左端点插入,值为区间大小。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=5e5+10; int minv[maxn<<2]; int ql,qr,qmin;
void query(int o,int l,int r){
if(ql<=l&&r<=qr){
qmin=min(qmin,minv[o]);
}else{
if(ql<=mid) query(lson,l,mid);
if(qr>mid) query(rson,mid+1,r);
}
} int _p,uv;
void update(int o,int l,int r){
if(l==r){
minv[o]=uv;
}else{
if(_p<=mid) update(lson,l,mid);
else update(rson,mid+1,r);
minv[o]=min(minv[lson],minv[rson]);
}
} struct Node{
int l,r,v;
Node(int l,int r,int v):l(l),r(r),v(v){}
}; bool cmp(const Node& n1,const Node& n2){
return n1.r<n2.r;
} map<int,int> mp;
int ans[maxn],arr[maxn],n,m; void init(){
rep(i,0,maxn<<2) minv[i]=INF;
} int main() {
scf("%d%d",&n,&m);
init();
vector<Node> lis;
for(int i=1;i<=n;i++){
scf("%d",&arr[i]);
if(mp[arr[i]]){
lis.pb(Node(mp[arr[i]],i,i-mp[arr[i]]));
}
mp[arr[i]]=i;
} // rep(i,0,lis.sz()) prf("(%d,%d)\n",lis[i].l,lis[i].r); vector<Node> que;
for(int i=0;i<m;i++){
int l,r;
scf("%d%d",&l,&r);
que.pb(Node(l,r,i));
} sort(all(lis),cmp);
sort(all(que),cmp); int p=0;
for(int i=0;i<que.sz();i++){
while(p<lis.sz()&&lis[p].r<=que[i].r){ _p=lis[p].l; uv=lis[p].v;
update(1,1,n); p++;
} ql=que[i].l,qr=que[i].r,qmin=INF;
query(1,1,n); ans[que[i].v]=qmin>=INF?-1:qmin;
} for(int i=0;i<m;i++) prf("%d\n",ans[i]); return 0;
} //end-----------------------------------------------------------------------

最新文章

  1. MySQL数据库1067 问题
  2. PHP Strict Standards:问题解决
  3. jQuery属性操作
  4. 实例存储支持的AMI创建步骤
  5. JDK历史版本下载
  6. Web前端性能优化教程09:图像和Cookie优化
  7. sublime 使用技巧
  8. 对于unallocated space的翻译 我想说几句话
  9. Intent Receiver
  10. windows7安装IE11点击图标没反应
  11. Strider 持续集成(gitlab)
  12. If-Modified-Since页面是否更新
  13. R分词
  14. 【翻译】Ext JS 6 Beta发布
  15. selenium-确定找到的element唯一(三)
  16. promise在angular中的基本使用
  17. Scrapy快速上手
  18. ARCore中四元数的插值算法实现
  19. MQTT压力测试工具之JMeter插件教程
  20. Android-Java-封装

热门文章

  1. 课时9.HTML发展史(了解)
  2. [笔记] 升級到 Delphi 10.2 Tokyo 笔记
  3. VM上Hadoop3.1伪分布式模式搭建
  4. TCP/IP协议的数据传输过程详解——IP与以太网的包收发操作
  5. CALL TRANSACTION
  6. js点击后将文字复制到剪贴板,将图片复制到画图
  7. 20155229--Java实验四《Android开发基础》
  8. 初步安装配置虚拟机、Ubuntu、git、vim、码云项目
  9. 为什么说private方法是有罪的
  10. jdk各种老版本的下载链接