题目链接

n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离。

先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段树。

如果当前的i等于某个询问的r, 那么就查询, 具体看代码。

 #include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 5e5+;
int minn[maxn<<], a[maxn], ans1[maxn], ans;
struct node
{
int l, r, id;
bool operator < (node a)const
{
if(r == a.r)
id<a.id;
return r<a.r;
}
}q[maxn];
map <int, int> mp;
void pushUp(int rt) {
minn[rt] = min(minn[rt<<], minn[rt<<|]);
}
void update(int p, int val, int l, int r, int rt) {
if(l == r) {
minn[rt] = val;
return ;
}
int m = l+r>>;
if(p<=m)
update(p, val, lson);
else
update(p, val, rson);
pushUp(rt);
}
void query(int L, int R, int l, int r, int rt) {
if(L<=l&&R>=r) {
ans = min(ans, minn[rt]);
return ;
}
int m = l+r>>;
if(L<=m)
query(L, R, lson);
if(R>m)
query(L, R, rson);
}
int main()
{
int n, m;
cin>>n>>m;
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].id = i;
}
sort(q, q+m);
int pos = ;
mem2(minn);
for(int i = ; i<=n; i++) {
if(mp[a[i]]) {
update(mp[a[i]], i-mp[a[i]], , n, );
}
mp[a[i]] = i;
while(i == q[pos].r) {
ans = inf;
query(q[pos].l, i, , n, );
if(ans == inf)
ans = -;
ans1[q[pos].id] = ans;
pos++;
}
}
for(int i = ; i<m; i++)
cout<<ans1[i]<<" ";
return ;
}

codeforces 522D. Closest Equals 线段树+离线的更多相关文章

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

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

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

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

  3. D. Closest Equals(线段树)

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

  4. CodeForces 522D Closest Equals 树状数组

    题意: 给出一个序列\(A\),有若干询问. 每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1. 分析: 令\(pre_i = max\{j| A_j = A_i, j < ...

  5. Codeforces 522D Closest Equals

    题解: 傻逼题 直接从左向右扫描每个点作为右端点 然后单点修改区间查询就行了 另外一种更直观的做法就是$(i,j)$之间产生了$(j-i)$ 于是变成矩形查最大值,kd-tree维护 代码: #inc ...

  6. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  7. 线段树+离线 hdu5654 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...

  8. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  9. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

随机推荐

  1. asp.net 使用my97 datepicker实现前后两个日期的范围界定

    说明:日期选择后,前面的日期小于等后面的日期,后面的日期大于等于前面的日期.点点看就知道了:) - 这里将周末日期不可选.代码如下: <html xmlns="http://www.w ...

  2. 关于Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY

    google 给的android的例子里,有用到google APIs,新版本的ADT默认只有Android的SDK,如果运行平台与目标平台不一致会报:Installation error: INST ...

  3. eclipse中删除多余的工作空间记录

    所以对于不再使用的工作空间,每次出现在eclipse的“文件”>>“切换工作空间”里面的时候就觉得特别不爽. 所以认真研究了eclipse目录之后让我找到了,删除不需要工作空间记录的方法. ...

  4. wcf xml消息客户端cookie

    public override bool Login(string userName, string password) { using (HttpClient client = new HttpCl ...

  5. SqlServer数据类型、C#SqlDbType对应关系及转换

    { { } } { SqlDbType dbType = SqlDbType.Variant; { dbType = SqlDbType.Int; dbType = SqlDbType.VarChar ...

  6. windows8 安装IIS 和 添加网站(转)

    Internet Information Services(IIS,互联网信息服务),是由微软公司提供的基于运行Microsoft Windows的互联网基本服务.最初是Windows NT版本的可选 ...

  7. JQuery初识

    一.什么是JQuery       JQuery官方网站上是这样解释的:JQuery是一个快速简洁的JavaScript库,它可以简化HTML文档的元素遍历.事件处理.动画及Ajax交互,快速开发We ...

  8. 使用jquery控制display属性

    //隐藏 $("#id").css('display','none'); //显示 $("#id").css('display','block'); 或 $(& ...

  9. halcon与C#混合编程进阶版

    这篇主要是C#和Halcon的混合编程,在此基础上对按键不同功能的划分,以及图片适应窗口和从本地打开图片. 新手来这里:http://www.cnblogs.com/badguy518/p/55150 ...

  10. Java处理java.util.ConcurrentModificationException异常

    代码: public static void reduce(HashMap<String, Integer> hashMap, final Integer count) { Iterato ...