D. Closest Equals(线段树)
题目链接:
3 seconds
256 megabytes
standard input
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|.
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 题意:给一个区间,问这个区间里面相同的两个数的最小距离是多少;
思路:由于询问特别多,可以对右端点进行排序,然后按右端点从小到大更新与这个点相同的前一个位置的距离,然后线段树求[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(线段树)的更多相关文章
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- codeforces 522D. Closest Equals 线段树+离线
题目链接 n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离. 先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段 ...
- $Codeforces\ 522D\ Closest\ Equals$ 线段树
正解:线段树 解题报告: 传送门$QwQ$ 题目大意是说给定一个数列,然后有若干次询问,每次询问一个区间内相同数字之间距离最近是多少$QwQ$.如果不存在相同数字输出-1就成$QwQ$ 考虑先预处理出 ...
- 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 D. Closest Equals 离线线段树 求区间相同数的最小距离
D. Closest Equals Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/prob ...
- [hdu4347]The Closest M Points(线段树形式kd-tree)
解题关键:kdtree模板题,距离某点最近的m个点. #include<cstdio> #include<cstring> #include<algorithm> ...
- HDU6621 K-th Closest Distance 第 k 小绝对值(主席树(统计范围的数有多少个)+ 二分 || 权值线段树+二分)
题意:给一个数组,每次给 l ,r, p, k,问区间 [l, r] 的数与 p 作差的绝对值的第 k 小,这个绝对值是多少 分析:首先我们先分析单次查询怎么做: 题目给出的数据与多次查询已经在提示着 ...
- 【HDU6621】K-th Closest Distance【线段树】
题目大意:给你一堆数,每次询问区间[l,r]中离p第k小的|ai-p| 题解:考虑二分答案,对于每个可能的答案,我们只需要看在区间[l,r]里是否有≥k个比二分的答案还要接近于p的 考虑下标线段树,并 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
随机推荐
- 20145328 《Java程序设计》第10周学习总结
20145328 <Java程序设计>第10周学习总结 资料学习内容总结 网络编程 13.1 网络概述 网络编程技术是当前一种主流的编程技术,随着联网趋势的逐步增强以及网络应用程序的大量出 ...
- 20145230熊佳炜《网络对抗》实验八:WEB基础
20145230熊佳炜<网络对抗>实验八:WEB基础 实验目标 Web前端HTML:能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTM ...
- Javascript 十移跳转页面
<srcipty> var t = 10; function openwin(){ t -= 1; if( t==0){ location.href='prototype.html'; } ...
- 用cookies判断用户首次登录
要求:判断24小时内用户是否是首次登录,如果是则显示弹窗,如果不是则不再显示弹窗 (function() { //是否是新访客 function isNewVisitor() { //从cookie读 ...
- Angular for TypeScript 语法快速指南 (基于2.0.0版本)
引导 import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; platformBrowserDynami ...
- npm install遇到的问题
phantomjs-prebuilt@2.1.16 install: 'node install.js' 在虚拟机上初始化vue-cli项目,npm install时遇到的问题 npm install ...
- mbstring.so下载安装
linux下安装: $:cd /php7.0/ext/mbstring 切换到源码包目录下 $:/usr/local/php/bin/phpize 执行这句 $:./configure –with-p ...
- taskset -pc PID 查看线程占用cpu核
taskset -pc PID 可以用于 查看 当前线程 对应绑定的 在 哪个核上面. 这个 可以用于 程序优化, 查看 哪个线程占用的 cpu 比重比较高 首先 可以通过 top -H - ...
- 转:HDFS运行原理
简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Go ...
- Oracle recovery manager failed to restore
解决办法: 1:清理过期失效的备份, 2:增加recovery_file_dest_size参数值即可: SQL> show parameter db_recover NAME ...