VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树
题目链接:
http://codeforces.com/problemset/problem/522/D
D. Closest Equals
time limit per test3 secondsmemory 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-----------------------------------------------------------------------VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树的更多相关文章
- 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/prob ... 
- 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后面找回文子串相互配对,问有几对 ... 
- 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 2012 Qualification Round 1---C. Cd and pwd commands
		Cd and pwd commands time limit per test 3 seconds memory limit per test 256 megabytes input standard ... 
- 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 ... 
随机推荐
- JQuery中事件冒泡
			JQuery中事件冒泡 定义 在一个对象上触发某类事件,就会执行此事件程序,如果没有处理事件就会向这个对象的父级对象传播 直至它被处理,最顶层老大为document对象. 作用 事件冒泡允许多个操作被 ... 
- ubuntu软件安装
			介绍常用的ubuntu软件及其安装 首先声明,本人在以下的操作全部基于腾讯云16.04版本ubuntu,若版本不一,有些出入,遇到问题可以在楼下留言. ubuntu中文官网 汉化终端 下载中文包 su ... 
- LintCode 896. Prime Product 简明题解
			Given a non-repeating prime array arr, and each prime number is used at most once, find all the prod ... 
- 20155212 mybash的实现
			mybash的实现 题目 使用fork,exec,wait实现mybash 写出伪代码,产品代码和测试代码 发表知识理解,实现过程和问题解决的博客(包含代码托管链接) 准备 通过man命令了解fork ... 
- 20155213 2016-2017-2《Java程序设计》第四周学习总结
			20155213 2016-2017-2<Java程序设计>第四周学习总结 教材学习内容总结 继承与多态 继承 继承避免多个类间重复定义共同行为,使用关键字extends.继承表明了子类与 ... 
- 20155338 2016-2017-2 《JAVA程序设计》课程总结
			---恢复内容开始--- 20155338 2016-2017-2 <JAVA程序设计>课程总结 每周作业汇总: 预备作业一 预备作业二 预备作业三 <JAVA程序设计>第一周 ... 
- SublimeText 改变 tab的距离
			view -> Indentation -> Tab width …… 
- libuv源码分析
			项目开发过程中经常使用了基于libuv库封装的库接口来实现异步处理,一直没仔细研究过这些接口的内部如何实现,因此也就没有掌握它的设计思想.今天花了点时间研究了其事件循环内部的一些过程,总算有了一些理解 ... 
- [agc006D]Median Pyramid Hard-[二分+乱搞]
			Description 题目大意:给你一个长度为n*2-1的排列,将除了该序列头尾的两个数外的其他数(设为i)变为原序列i-1,i,i+1下标数的中位数.求最后的数是什么.例子如下: Solution ... 
- 后缀数组(SA)总结
			后缀数组(SA)总结 这个东西鸽了好久了,今天补一下 概念 后缀数组\(SA\)是什么东西? 它是记录一个字符串每个后缀的字典序的数组 \(sa[i]\):表示排名为\(i\)的后缀是哪一个. \(r ... 
