D. Closest Equals

Time Limit: 1 Sec  Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=3224

Description

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|.

Input

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.

Output

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.

Sample Input

5 3
1 1 2 3 2
1 5
2 4
3 5

Sample Output

1
-1
2

HINT

题意

查询区间相同数的最小距离

题解:

用一个map记录前面的位置,然后离线搞一搞
用心去体会,我也不好说……

单点更新,区间查询最小值

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 500001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
//**************************************************************************************
inline ll 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;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
} struct node{
int l,r,v;
}a[maxn*];
struct ques
{
int l,r,an,v;
}qu[maxn];
void build(int x,int l,int r)
{
a[x].l=l,a[x].r=r;
a[x].v=maxn;
if(l==r)
return;
int mid=(l+r)>>;
build(x<<,l,mid);
build(x<<|,mid+,r);
}
void pushup(int x)
{
a[x].v=min(a[x<<].v,a[x<<|].v);
}
void update(int x,int pos,int val)
{
if(a[x].l==a[x].r)
{
a[x].v=val;
return;
}
int mid=(a[x].l+a[x].r)>>;
if(pos<=mid)
update(x<<,pos,val);
else
update(x<<|,pos,val);
pushup(x);
}
int mi;
void query(int x,int l,int r)
{
if(l<=a[x].r&&r>=a[x].r)
{
mi=min(mi,a[x].v);
return;
}
int mid=(a[x].l+a[x].r)>>;
if(l<=mid)
query(x<<,l,r);
if(r>mid)
query(x<<|,l,r);
}
map<int,int>mp;
int d[maxn];
bool cmp(ques a,ques b)
{
return a.l>b.l;
}
int ans[maxn];
int main()
{
int n=read(),m=read();
build(,,n);
for(int i=;i<=n;i++)
d[i]=read();
for(int i=;i<=m;i++)
{
qu[i].l=read(),qu[i].r=read();
qu[i].v=i;
}
sort(qu+,qu++m,cmp);
int t=;
for(int i=n;i;i--)
{
if(mp[d[i]])
{
update(,mp[d[i]],mp[d[i]]-i);
}
mp[d[i]]=i;
while(qu[t].l==i)
{
mi=maxn;
query(,qu[t].l,qu[t].r);
if(mi==maxn)
mi=-;
ans[qu[t].v]=mi;
t++;
}
}
for(int i=;i<=m;i++)
{
if(ans[i]<)
printf("-1\n");
else
P(ans[i]);
}
}

Codeforces VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线线段树 求区间相同数的最小距离的更多相关文章

  1. VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树

    题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...

  2. codeforces VK Cup 2015 - Qualification Round 1 B. Photo to Remember 水题

    B. Photo to Remember Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/522/ ...

  3. VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]

    传送门 A. Reposts time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. DP VK Cup 2012 Qualification Round D. Palindrome pairs

    题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...

  5. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  6. VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟

    C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...

  7. VK Cup 2012 Qualification Round 2 C. String Manipulation 1.0 字符串模拟

    C. String Manipulation 1.0 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/pr ...

  8. VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland

    今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...

  9. VK Cup 2016 - Qualification Round 2 D. Three-dimensional Turtle Super Computer 暴力

    D. Three-dimensional Turtle Super Computer 题目连接: http://www.codeforces.com/contest/638/problem/D Des ...

随机推荐

  1. go标识符、变量、常量

    标识符 标识符是用来表示Go中的变量名或者函数名,以字母或_开头.后可跟着字母.数字. _ 关键字 关键字是Go语言预先定义好的,有特殊含义的标识符. 变量 1. 语法:var identifier ...

  2. 64_r1

    R-3.4.0-2.fc26.x86_64.rpm 15-May-2017 14:49 31030 R-ALL-1.6.0-4.fc26.noarch.rpm 17-Feb-2017 22:05 11 ...

  3. 64_n3

    nodejs-yamlish-0.0.5-9.fc26.noarch.rpm 11-Feb-2017 16:48 11966 nodejs-yargs-3.2.1-6.fc26.noarch.rpm ...

  4. 设计模式之笔记--代理模式(Proxy)

    代理模式(Proxy) 定义 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. 类图 描述 Subject,定义了ConcreteSubject和Proxy的共用接口,这样就可以 ...

  5. VirtualBox与Genymotion命令行启动

    一.VirtualBox命令行启动 1.添加环境变量: %programfiles%\Oracle\VirtualBox 2.用VBoxManage查看已存在vmname|uuid命令: VBoxMa ...

  6. Getting Started with Django Rest Framework and AngularJS

    转载自:http://blog.kevinastone.com/getting-started-with-django-rest-framework-and-angularjs.html A ReST ...

  7. untiy3d学习笔记

    Unity3d 记录 1.63讲 主要讲了menicam 从3D软件里面导出过后,注意如果是人物模型命名一定要非常清晰并且对称.选择到模型后等到到humanoid后可以使用menicam.然后使用me ...

  8. Fiddler Web Session 列表(1)

    Web Session 列表 位置: Web Session 列表 位于Fiddler界面的左侧 ,是Fiddler所抓取到的所有Session会话的列表集合. Web Session 列表 栏名词解 ...

  9. 洛谷P1094纪念品分组 题解

    题目传送门 首先的思路就是贪心.先将所有的纪念品按照价格从低到高进行排序.在分别从左到右.从右到左合并纪念品.如果两端纪念品价格超过了上上限,那么就将较大的那一个纪念品独自放入.否则将两个纪念品一起放 ...

  10. C++输入与输出

    1 概述 C和C++都没有将输入和输出建立在语言中,C++将输入输出的解决方案放在类库中(由头文件iostream和fstream中定义的类) C++程序把输入和输出看作字节流.流充当了程序和流源流目 ...