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 ...
随机推荐
- 47、求1+2+3+...+n
一.题目 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 二.解法 public class Solut ...
- Android 反编译神器jadx的使用
一.前言 今天介绍一个非常好用的反编译的工具 jadx .jadx 的功能非常的强大,对我而言,基本上满足日常反编译需求. jadx 优点: 图形化的界面. 拖拽式的操作. 反编译输出 Java 代码 ...
- PHP解决并发问题的几种实现
对于商品抢购等并发场景下,可能会出现超卖的现象,这时就需要解决并发所带来的这些问题了 在PHP语言中并没有原生的提供并发的解决方案,因此就需要借助其他方式来实现并发控制. 方案一:使用文件锁排它锁 f ...
- Linux下的各类文件
.a文件是静态链接库文件.所谓静态链接是指把要调用的函数或者过程链接到可执行文件中,成为可执行文件的一部分.当多个程序都调用相同函数时,内存中就会存在这个函数的多个拷贝,这样就浪费了宝贵的内存资源.. ...
- LightOJ 1414 February 29(闰年统计+容斥原理)
题目链接:https://vjudge.net/contest/28079#problem/R 题目大意:给你分别给你两个日期(第二个日期大于等于第一个日期),闰年的2月29日称为闰日,让你求两个日期 ...
- Python股票信息抓取(三)
最近在看mongodb,然后会用了一些最简单的mongodb的操作,然后想着结合股票信息的数据的抓取,然后将数据存储在mongodb中,对于mongo和数据库的最大的区别是,mongo不需要建表,直接 ...
- Go语言入门之指针的使用
指针的使用: package main import "fmt" func zhi(){ a:= var b *int=&a //声明指针并赋值 *b=3 //改变内存地址 ...
- springboot1.5.4 idea 自动保存编译更新
maven dependencies增加 <dependency> <groupId>org.springframework.boot</groupId> < ...
- thinkphp5.1使用phpstudy隐藏index.php
apache的重写规则如下: <IfModule mod_rewrite.c> Options +FollowSymlinks -Multiviews RewriteEngine on R ...
- umask相关内容
umask权限过滤符新创建的文件的权限为666,即- rw- rw- rw-新创建的目录的权限为777,即d rwxrwxrwx如umask值为0022,后三位022,即 --- -w- -w-上述默 ...