题目链接:

D. Closest Equals

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nm (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

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.

Examples
input
5 3
1 1 2 3 2
1 5
2 4
3 5
output
1
-1
2 题意:给一个区间,问这个区间里面相同的两个数的最小距离是多少;
思路:由于询问特别多,可以对右端点进行排序,然后按右端点从小到大更新与这个点相同的前一个位置的距离,然后线段树求[l,r]的最小值;
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
const int inf=1e9;
int n,m,ans[maxn],p[maxn];
map<int,int>mp;
struct node
{
int l,r,id;
}po[maxn];
int cmp(node x,node y){return x.r<y.r;}
struct Tree
{
int l,r,mn;
}tr[4*maxn];
void build(int o,int L,int R)
{
tr[o].l=L;tr[o].r=R;tr[o].mn=inf;
if(L>=R)return ;
int mid=(tr[o].l+tr[o].r)>>1;
build(2*o,L,mid);build(2*o+1,mid+1,R);
}
void update(int o,int pos,int num)
{
if(tr[o].l==tr[o].r&&tr[o].l==pos){tr[o].mn=min(tr[o].mn,num);return ;}
int mid=(tr[o].l+tr[o].r)>>1;
if(pos<=mid)update(2*o,pos,num);
else update(2*o+1,pos,num);
tr[o].mn=min(tr[2*o].mn,tr[2*o+1].mn);
}
int query(int o,int L,int R)
{
if(L<=tr[o].l&&R>=tr[o].r)return tr[o].mn;
int mid=(tr[o].l+tr[o].r)>>1;
int tep=inf;
if(L<=mid)tep=min(tep,query(2*o,L,R));
if(R>mid)tep=min(tep,query(2*o+1,L,R));
return tep;
}
int main()
{
scanf("%d%d",&n,&m);
build(1,1,n);
int x;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(mp[x])p[i]=mp[x],mp[x]=i;
else mp[x]=i,p[i]=0;
}
for(int i=1;i<=m;i++)scanf("%d%d",&po[i].l,&po[i].r),po[i].id=i;
sort(po+1,po+m+1,cmp);
int r=1;
for(int i=1;i<=m;i++)
{
while(r<=po[i].r)
{
if(p[r])update(1,p[r],r-p[r]);
r++;
}
int tep=query(1,po[i].l,r-1);
if(tep>=inf)ans[po[i].id]=-1;
else ans[po[i].id]=tep;
}
for(int i=1;i<=m;i++)printf("%d\n",ans[i]);
return 0;
}

  

 

D. Closest Equals(线段树)的更多相关文章

  1. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

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

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

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

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

  4. VK Cup 2015 - Qualification Round 1 D. Closest Equals 离线+线段树

    题目链接: http://codeforces.com/problemset/problem/522/D D. Closest Equals time limit per test3 secondsm ...

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

  6. [hdu4347]The Closest M Points(线段树形式kd-tree)

    解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...

  7. HDU6621 K-th Closest Distance 第 k 小绝对值(主席树(统计范围的数有多少个)+ 二分 || 权值线段树+二分)

    题意:给一个数组,每次给 l ,r, p, k,问区间 [l, r] 的数与 p 作差的绝对值的第 k 小,这个绝对值是多少 分析:首先我们先分析单次查询怎么做: 题目给出的数据与多次查询已经在提示着 ...

  8. 【HDU6621】K-th Closest Distance【线段树】

    题目大意:给你一堆数,每次询问区间[l,r]中离p第k小的|ai-p| 题解:考虑二分答案,对于每个可能的答案,我们只需要看在区间[l,r]里是否有≥k个比二分的答案还要接近于p的 考虑下标线段树,并 ...

  9. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

随机推荐

  1. Jackson 框架JSON、XML、List、Map直接相互转换

    博客分类: json   参考:http://www.cnblogs.com/hoojo/archive/2011/04/22/2024628.html 在其基础上做了稍微调整 详情见附件 jacks ...

  2. Windows7使用无线网卡建立WiFi热点

    在Windows7下设置热点需要用到命令netsh wlan,具体的设置步骤如下: 1.配置热点 以管理员身份打开命令行模式,输入命令 netsh wlan set hostednetwork mod ...

  3. Quick Launcher FAQ

    Q: Which category can be previewed and selected in Finder? A: All the categories can be selected in ...

  4. iOS开发进阶 - 自定义UICollectionViewLayout实现瀑布流布局

    移动端访问不佳,请访问我的个人博客 最近项目中需要用到瀑布流的效果,但是用UICollectionViewFlowLayout又达不到效果,自己动手写了一个瀑布流的layout,下面是我的心路路程 先 ...

  5. HDU 5763 Another Meaning(DP+KMP)

    http://acm.hdu.edu.cn/showproblem.php?pid=5763 题意: 给出一个字符串和一个模式串,模式串有两种意思,问这句话有几种意思. 思路:因为肯定要去字符串去找模 ...

  6. 如何查看一个进程打开哪些fd及对应的文件或套接字操作

  7. spring boot 国际化MessageSource

    转自:https://blog.csdn.net/flowingflying/article/details/76358970 spring中ResourceBundleMessageSource的配 ...

  8. 数据可视化——matplotlib(2)

    导入相关模块 import matplotlib.pyplot as plt import numpy as np import pandas as pd 图表设置 添加X.Y轴标签以及图标标题 a ...

  9. 分布式系统中的幂等性-zookeeper与dubbo

    现如今我们的系统大多拆分为分布式SOA,或者微服务,一套系统中包含了多个子系统服务,而一个子系统服务往往会去调用另一个服务,而服务调用服务无非就是使用RPC通信或者restful,既然是通信,那么就有 ...

  10. ACM比赛辅导--授课内容

    Lesson1(3月19日) 1.讲解Dev-C++.VC++6.0的单步调试方法 2.学习比赛的基本输入输出,练习C语言网的1085—1092 Lesson2(3月21日) 1.学习挑战程序设计,第 ...