题目链接:http://codeforces.com/contest/522/problem/D

题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查询,输出在这个区间内的两个相等的数的最短距离,如果没有相等的则输出-1.

线段树+扫描线,线段树维护的值是区间的最小值,从后往前扫,然后每次要做的事有两个:

1.判断当前这个位置 i 的数刚刚是不是出现过,假设刚刚出现的位置是 l ,如果出现过,则在线段树中把l这个位置的值更新为 l - i,同时更新包含这个点的区间的最小值.

2.判断有没有以当前这个位置为左端点的查询区间,如果有,就在线段树中查找这个区间里面的最小值.

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<list>
using namespace std;
const int maxn = ,INF = 0x7fffff;
map<int,int> mp; int n,m,A[maxn];
struct node
{
int d,l,r;
}tree[*maxn];
struct Node
{
int flag,ans;
int l,r;
}Q[maxn];
bool cmp(Node a,Node b)
{
return a.l >= b.l;
}
void maketree(node *tree,int p)
{
if(tree[p].l == tree[p].r)
{
tree[p].d = INF;
return ;
}
int mid = (tree[p].l + tree[p].r) / ;
tree[*p].l = tree[p].l;
tree[*p].r = mid;
tree[*p+].l = mid + ;
tree[*p+].r = tree[p].r;
tree[*p].d = tree[*p+].d = INF;
maketree(tree,*p);
maketree(tree,*p+);
}
void update(node *tree,int p,int loc,int d)
{
if(tree[p].l == tree[p].r)
{
tree[p].d = min(tree[p].d,d);
return ;
}
int mid = (tree[p].l + tree[p].r) / ;
if(loc <= mid)
update(tree,*p,loc,d);
else update(tree,*p+,loc,d);
tree[p].d = min(tree[p].d,d);
}
int data_sear;
void search(node *tree,int p,int l,int r)
{
if(tree[p].l == l && tree[p].r == r)
{
data_sear = min(data_sear,tree[p].d);
return ;
}
int mid = (tree[p].l + tree[p].r) / ;
if(r <= mid) search(tree,*p,l,r);
else if(l <= mid && r > mid)
{
search(tree,*p,l,mid);
search(tree,*p+,mid+,r);
}
else if(l > mid) search(tree,*p+,l,r);
} bool cmp2(Node a,Node b)
{
return a.flag < b.flag;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i = ;i <= n;++i)
scanf("%d",&A[i]);
for(int i = ;i < m;++i)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].flag = i;
Q[i].ans = INF;
}
sort(Q,Q+m,cmp); //按照左端点排好序,方便下面二分查找
tree[].l = ;
tree[].r = n;
tree[].d = INF;
maketree(tree,); //构建好线段树
mp.clear();
int f = ; //指向当前的查询
for(int i = n;i >= ;--i)
{
if(mp[A[i]] != )
update(tree,,mp[A[i]],mp[A[i]]-i); //更新线段树
mp[A[i]] = i;
while(f < m && Q[f].l == i)
{
data_sear = INF;
search(tree,,Q[f].l,Q[f].r); //查询这个区间内的最小值
Q[f].ans = (data_sear <= n? data_sear:-);
f++;
}
}
sort(Q,Q+m,cmp2);
for(int i = ;i < m;++i)
printf("%d\n",Q[i].ans);
}
return ;
}

Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)的更多相关文章

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

  2. codeforces 522D. Closest Equals 线段树+离线

    题目链接 n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离. 先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段 ...

  3. $Codeforces\ 522D\ Closest\ Equals$ 线段树

    正解:线段树 解题报告: 传送门$QwQ$ 题目大意是说给定一个数列,然后有若干次询问,每次询问一个区间内相同数字之间距离最近是多少$QwQ$.如果不存在相同数字输出-1就成$QwQ$ 考虑先预处理出 ...

  4. D. Closest Equals(线段树)

    题目链接: D. Closest Equals time limit per test 3 seconds memory limit per test 256 megabytes input stan ...

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

  6. Codeforces VK Cup 2015 A.And Yet Another Bracket Sequence(后缀数组+平衡树+字符串)

    这题做得比较复杂..应该有更好的做法 题目大意: 有一个括号序列,可以对其进行两种操作: ·        向里面加一个括号,可以在开头,在结尾,在两个括号之间加. ·        对当前括号序列进 ...

  7. Codeforces Round VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM 暴力出奇迹!

    VK Cup 2015 - Round 1 (unofficial online mirror, Div. 1 only)E. The Art of Dealing with ATM Time Lim ...

  8. 51nod 1494 选举拉票 (线段树+扫描线)

    1494 选举拉票  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  关注 现在你要竞选一个县的县长.你去对每一个选民进 ...

  9. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

随机推荐

  1. HTML标记语言篇--学习笔记01

    HTML标记语言篇 第1章  HTML基础 1.1 基本概念 WWW 是"World Wide Web"(全球广域网)的缩写,简称为Web,中文又称为"万维网" ...

  2. JS组件系列——Bootstrap Table 表格行拖拽(二:多行拖拽)

    前言:前天刚写了篇JS组件系列——Bootstrap Table 表格行拖拽,今天接到新的需要,需要在之前表格行拖拽的基础上能够同时拖拽选中的多行.博主用了半天时间研究了下,效果是出来了,但是感觉不尽 ...

  3. go 数组(array)、切片(slice)、map、结构体(struct)

    一 数组(array) go语言中的数组是固定长度的.使用前必须指定数组长度. go语言中数组是值类型.如果将数组赋值给另一个数组或者方法中参数使用都是复制一份,方法中使用可以使用指针传递地址. 声明 ...

  4. 用机器名访问和用Localhost访问时在IE中的区别(备忘)

    meta中未指定文档模式的时候, localhost访问文档模式默认是Edge 机器名访问时文档模式默认是IE7 <head>中添加 <meta http-equiv="X ...

  5. Ubuntu 安裝 嘸蝦米 輸入法

    O S : 14.04.1-Ubuntu 加入fcitx開發團隊的repository: sudo add-apt-repository ppa:fcitx-team/nightly sudo apt ...

  6. maven环境配置+eclipse环境配置

    一 . (1),下载maven 有点类似于tomcat 解压后就可以用 ,不用安装 (2), 配置环境变量 在系统变量的path 中添加  E:\01Server\maven\bin    注意是bi ...

  7. OpenLayers的定制

    最近因为工作的需要,把主流的的一些GIS的javascript库看了一遍,主要是ArcGIS Server API for Javascript,Openlayers和Leaflet. 先说说ArcG ...

  8. 教你一招:Excel中使用vlookup函数查询序列所对应的值

    以一个简单的例子做示范,列数相对较少,看起来也比较清楚:在奥运会或其他比赛上我们可以看到各个国家的奖牌数的变化:那么我们如何查询国家对应的总奖牌数. 我们用到的函数是vlookup,它是一个纵向查询函 ...

  9. bzoj4458: GTY的OJ

    题目大意:给定一棵带点权的有根树,同时给定L,R,要求找M条链,每条链满足以下条件的情况下,要求所有链权和最大: 1.两两不相同(可以包含/相交等) 2.节点数在[L,R]间 3.其中一个端点的深度必 ...

  10. PyChram中同目录下import引包报错的解决办法?

    相信很多同学和我一样在PyChram工具中新建python项目的同目录下import引包会报错提示找不到,这是因为该项目找不到python的环境导致的: 如果文件开始的时候包引包的错误可以,都可以用用 ...