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 ...
随机推荐
- 多线程中的超时, 如Socket超时
; ,,, ->$port { print "-->$port\r"; #say "\r"; await Promise.anyof( Promis ...
- 对RSA的认识
#没有经过专业老师的指导,所以您在阅读本文时建议参考即可. 学习视屏:https://www.youtube.com/watch?v=TqX0AHHwRYQ https://www.youtub ...
- linux用户修改用户shell
要拒绝系统用户登录,可以将其shell设置为/usr/sbin/nologin或者/bin/false # usermod -s /usr/sbin/nologin username 或者 # use ...
- 大数据系列之Flume+kafka 整合
相关文章: 大数据系列之Kafka安装 大数据系列之Flume--几种不同的Sources 大数据系列之Flume+HDFS 关于Flume 的 一些核心概念: 组件名称 功能介绍 Agent ...
- 深入解析Mysql 主从同步延迟原理及解决方案
MySQL的主从同步是一个很成熟的架构,优点为:①在从服务器可以执行查询工作(即我们常说的读功能),降低主服务器压力;②在从主服务器进行备份,避免备份期间影响主服务器服务;③当主服务器出现问题时,可以 ...
- C# Winform频繁刷新导致界面闪烁解决方法
C#Winform频繁刷新导致界面闪烁解决方法 一.通过对窗体和控件使用双缓冲来减少图形闪烁(当绘制图片时出现闪烁时,使用双缓冲) 对于大多数应用程序,.NET Framework 提供的默认双缓冲将 ...
- gtk+学习笔记(二)
如何创建一个按钮呢,直接贴代码把,有详细的注释. #include<gtk/gtk.h> gint data_count=; void on_button_clicked (GtkWidg ...
- SICP第三章题解
目录 SICP第三章题解 ex3-17 ex3-18 ex3-19 队列 ex3-21 ex3-22 ex3-24 ex3-25 3.4 并发:时间是一个本质问题 ex3-38 3.4.2 控制并发的 ...
- python脚本传入参数--精讲(getopt模块)
1.最常用的sys.argv[],这个不多谈 2.形如 dahu@dahu-OptiPlex-:~/json_folder$ python sub1.py -abb -oaaa --output=ou ...
- QT编译发布程序后报错如缺少dll、“应用程序无法正常启动(0xc000007b)”的可能解决方法
QT编译发布程序后报错如缺少dll.“应用程序无法正常启动(0xc000007b)”的可能解决方法 最近项目要用qt,因为初学没有经验,遇到些小问题常常没什么头绪,也查不到解决方法,刚刚还因为低端错误 ...