题意:

给出一个序列\(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. Struts2_使用token拦截器控制重复提交(很少用)

    控制重复提交的方式:1.表单提交后页面重定向:2.Struts2.x token拦截器 大致流程: 例子: index.jsp <%@ page language="java" ...

  2. 探索Skip List (跳跃表)

    附William Pugh的论文 Skip Lists: A Probabilistic Alternative to Balanced Trees 写在前面 以下内容针对的是Skip List的插入 ...

  3. 搭建TFTP服务器配置

    实验内容: TFTP是TCP/IP协议族中的一个用来在客户机与服务器之间进行简单文件传输的协议,提供不复杂,开销不大的文件传输服务.TFTP承载在UDP上,提供不可靠的数据传输服务,不提供存取授权与认 ...

  4. IOS 4个容易混淆的属性(textAligment contentVerticalAlignment contentHorizontalAlignment contentMode)

    四个容易混淆的属性:1. textAligment : 文字的水平方向的对齐方式1> 取值NSTextAlignmentLeft      = 0,    // 左对齐NSTextAlignme ...

  5. Git学习环境搭建和git对用户的增删改查命令

    git安装 windows下安装git git下载进入网址: https://git-scm.com/downloads 点击下载,进行安装,安装成功,你会看到图标,点击进入到git bash 查看g ...

  6. maven parent version not found

    需要把parent工程,也就是package是pom的那个工程先install一下 要是不行的话可以试下mvn -X clean install,-X表示强制从远程库更新dependency:再不行可 ...

  7. AI-Info-Micron-Insight:5G、人工智能和即将到来的移动革命

    ylbtech-AI-Info-Micron-Insight:5G.人工智能和即将到来的移动革命 1.返回顶部 1. 5G.人工智能和即将到来的移动革命 人们都说自己的手机“智能”,但究竟有多智能?凡 ...

  8. 3.初识Cron表达式

    Cron: 计划任务,是任务在约定的时间执行已经计划好的工作,这是表面的意思.在Linux中,我们经常用到 cron 服务器来完成这项工作.cron服务器可以根据配置文件约定的时间来执行特定的作务. ...

  9. 解决nsis error!cant initialize plug-ins directory.please try again later

    情况1: 调用SectionEnd会释放掉dll初始化标记,所有Section都必须放在函数的最下面. 情况2: 有可能是栈里的数据错乱,特别注意的是,使用BgWorker.dll获取多线程能力的时候 ...

  10. POJ1286 Necklace of Beads(Polya定理)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9359   Accepted: 3862 Description Beads ...