题目链接

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. Android getReadableDatabase() 和 getWritableDatabase()

    Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例.(getReadableDa ...

  2. 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)

    上一篇我们简单的介绍了一下RoboGuice的使用([八]注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受 ...

  3. CentOS配置ftp服务器

    1.首先更新yum 源 yum update 2. 安装vsftpd yum install vsftpd -y   // 安装    移除是 yum remove vsftpd 3. 配置Vsftp ...

  4. JMS详细的工作原理【转】

    如果手机只能进行实时通话,没有留言和短信功能会怎么样?一个电话打过来,正好没有来得及接上,那么这个电话要传递的信息肯定就收不到了.为什么不能先将信息存下来,当用户需要查看信息的时候再去获得信息呢?伴随 ...

  5. 全局通知Notification

    Notification 全局通知 关于全局通知的个人理解: 即有一个发射消息的,在整个应用中任何对象都可以接受这个消息 但是无论是哪个对象接受消息,都要在这个对象结束时移除消息 简单的说 就是给对象 ...

  6. sql语句中特殊函数的用法

    1.concat CONCAT(字串1, 字串2, 字串3, ...): 将字串1.字串2.字串3,等字串连在一起. 例如: Geography 表格 region_name     store_na ...

  7. memcached look status

    $ STAT pid STAT STAT STAT version STAT pointer_size STAT rusage_user 7.054927 STAT rusage_system 14. ...

  8. Scala学习之延迟绑定

    package com.swust.example object TraitDemo2 extends App{ //抽象类 abstract class Writer { def writeMess ...

  9. 获取select赋值

    <select class="sel-ul-add" id="xuanzhe"> <option>A</option> &l ...

  10. MarkDown使用 (三)表格

    MarkDown表格的用法 MarkDown表格的用法 例如: $$ \begin{array}{c|lcr} n & \text{Left} & \text{Center} & ...