题意:

给出一个序列\(A\),有若干询问。

每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1.

分析:

令\(pre_i = max\{j| A_j = A_i, j < i\}\),也就是前一个与\(A_i\)相等的数字的下标。

这个可以通过对序列排序,数值相等按照下标排序来计算。

将询问离线,按照询问区间的右端点从左到右排序。

从左到右扫描,向树状数组中在位置\(pre_i\)插入\(i - pre_i\),并维护一个后缀最小值。

查询的时候直接查即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define PER(i, a, b) for(int i = b - 1; i >= a; i--)
#define SZ(a) ((int)a.size())
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
typedef long long LL;
typedef pair<int, int> PII; const int maxn = 500000 + 10;
const int INF = 0x3f3f3f3f; int n, m;
inline int lowbit(int x) { return x&(-x); }
int C[maxn]; void upd(int& a, int b) { if(a > b) a = b; } void update(int x, int v) {
while(x) {
upd(C[x], v);
x -= lowbit(x);
}
} int query(int x) {
int ans = INF;
while(x <= n) {
upd(ans, C[x]);
x += lowbit(x);
}
if(INF == ans) ans = -1;
return ans;
} struct Query
{
int l, r, id;
bool operator < (const Query& t) const {
return r < t.r;
}
}; PII a[maxn];
int ans[maxn], pre[maxn];
Query q[maxn]; int main() {
scanf("%d%d", &n, &m);
memset(C, 0x3f, sizeof(C));
REP(i, 1, n + 1) {
scanf("%d", &a[i].first);
a[i].second = i;
}
sort(a + 1, a + 1 + n);
REP(i, 2, n + 1) {
if(a[i].first == a[i-1].first)
pre[a[i].second] = a[i-1].second;
}
REP(i, 0, m) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q, q + m); int p = 1;
REP(i, 0, m) {
for( ;p <= q[i].r; p++) {
if(pre[p]) update(pre[p], p - pre[p]);
}
ans[q[i].id] = query(q[i].l);
} REP(i, 0, m) printf("%d\n", ans[i]); return 0;
}

CodeForces 522D Closest Equals 树状数组的更多相关文章

  1. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

  2. Codeforces 830B - Cards Sorting 树状数组

    B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  3. CodeForces–830B--模拟,树状数组||线段树

    B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. Codeforces 650D - Zip-line(树状数组)

    Codeforces 题目传送门 & 洛谷题目传送门 我怕不是个 nt--一开始忽略了"询问独立"这个条件--然后就一直在想有什么办法维护全局 LIS--心态爆炸 首先离散 ...

  5. Codeforces 1139F Dish Shopping 树状数组套平衡树 || 平衡树

    Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足  L >= li && R ...

  6. codeforces 589G G. Hiring(树状数组+二分)

    题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...

  7. Codeforces 960F Pathwalks ( LIS && 树状数组 )

    题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 :  这题和 LIS 很相似,不同的 ...

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

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

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

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

随机推荐

  1. 学习lucene5.5.4的笔记

    说说几个常用的类. OpenMode是一个枚举类,有三个元素,分别表示IndexWriter的打开模式. CREATE:每次打开IndexWriter时清空当前索引目录下的索引,再新建索引. APPE ...

  2. Windows下安装ElasticSearch及工具

    转载自个人主页 前言 什么是ElasticSearch 官网如是介绍:Elasticsearch 是一个分布式.可扩展.实时的搜索与数据分析引擎. 它能从项目一开始就赋予你的数据以搜索.分析和探索的能 ...

  3. April 13 2017 Week 15 Thursday

    Happiness takes no account of time. 幸福不觉光阴过. Do you know the theory of relativity? If you know about ...

  4. 如何处理用代码创建SD Sales order时遇到的错误消息KI 180

    错误消息KI 180:You must enter a company code for transaction Create sales document 代码: REPORT zcreate_so ...

  5. 广搜最短路径变形,(POJ3414)

    题目链接:http://poj.org/problem?id=3414 解题报告: 1.每个节点都是一个独立的状态 2.这里的状态转移就是有几种出路,4种:1.倒掉a中的水,2.把a中的水倒到b中去, ...

  6. Linux 初学者:移动文件

    你学习了有关目录和访问目录的权限是如何工作的.你在这些文章中学习的大多数内容都可应用于文件 -- Paul Brown 在之前的该系列的部分中, 你学习了有关目录 和 访问目录 的权限 是如何工作的. ...

  7. 的NodeJS异步数据库函数需要同步的答案 +

    我是新来的NodeJS和我写,需要从我去过的所有的函数应该是在这种情况下,读QUERY我的MySQL数据库,并返回代码,我非常希望服务器能够对其他事件作出回应而这个地方是轨迹查询请求.然而,它并不特别 ...

  8. java基础必备单词讲解 day two

    variable 变量 count 统计 sum 总数 salary 薪水 Scanner 接收 import 导入 eclipse 日食 control 控制 shift 改变 alt 替换键 ha ...

  9. iOS第三方支付(支付宝)

    使用支付宝进行一个完整的支付功能,大致有以下步骤: 与支付宝签约,获得商户ID(partner)和账号ID(seller) 下载相应的公钥私钥文件(加密签名用) 下载支付宝SDK 生成订单信息 调用支 ...

  10. CSS3一些特殊属性

    (一)-webkit-tap-highlight-color         这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就 ...