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/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
Sample Input
1 1 2 3 2
1 5
2 4
3 5
Sample Output
-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 离线线段树 求区间相同数的最小距离的更多相关文章
- VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...
- 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/ ...
- 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 ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- 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 ...
- 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 ...
- VK Cup 2016 - Qualification Round 2 B. Making Genome in Berland
今天在codeforces上面做到一道题:http://codeforces.com/contest/638/problem/B 题目大意是:给定n个字符串,找到最短的字符串S使得n个字符串都是这个字 ...
- 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 ...
随机推荐
- Tomcat 调优及 JVM 参数优化
Tomcat 本身与 JVM 优化 Tomcat:调整Server.xml JVM:bat启动服务方式的话修改catalina.bat 服务式启动的话参考:http://www.cnblogs.com ...
- USB描述符【整理】
USB描述符 USB描述符信息存储在USB设备中,在枚举过程中,USB主机会向USB设备发送GetDescriptor请求,USB设备在收到这个请求之后,会将USB描述符信息返回给USB主机,USB主 ...
- shell脚本执行方式
# BY THE WAY, 其实这块内容算是比较简单的,但是都比较常记得它最基本的两种方式,另外两种却忘记了 1. 利用sh或bash命令执行 sh test.sh bash test.sh 2. 在 ...
- MySQL启动很慢的原因
我们在启动MySQL的时候,常常会遇到的是, 当执行启动命令后,它会"Start MySQL ....." 一直不停的执行,也不中断,也不成功 这里会出现此现象的原因有以下三条: ...
- Django项目上传到AWS服务器上
EC2是亚马逊(Amazon.com)提供的弹性云计算服务:Apache是一个跨平台的Web服务器端软件,可以使Python.PHP.Perl等语言编写的程序运行在服务器上:Django是一个Web程 ...
- sshpass-免交互SSH登录工具
sshpass用于自动向命令行提供密码,适用于ssh,scp,rsync,pssh,pscp等ssh系列的命令和工具 #安装sshpass yum install sshpass -y #注:当第一次 ...
- 自定义事件的触发dispatchEvent
1. 对于标准浏览器,其提供了可供元素触发的方法:element.dispatchEvent(). 不过,在使用该方法之前,我们还需要做其他两件事,及创建和初始化.因此,总结说来就是: documen ...
- Retrofit:类型安全的REST客户端for 安卓&Java
Retrofit:类型安全的REST客户端for 安卓&Java 2014年5月5日 星期一 21:11 官网: http://square.github.io/retrofit/ GitH ...
- csu 1798(树上最远点对,线段树+lca)
1798: 小Z的城市 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 60 Solved: 16[Submit][Status][Web Board] ...
- base64的作用
本函数将字符串以 MIME BASE64 编码.此编码方式可以让中文字或者图片也能在网络上顺利传输.在 BASE64 编码后的字符串只包含英文字母大小写.阿拉伯数字.加号与反斜线,共 64 个基本字符 ...