CodeForces 522D Closest Equals 树状数组
题意:
给出一个序列\(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 树状数组的更多相关文章
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- Codeforces 830B - Cards Sorting 树状数组
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CodeForces–830B--模拟,树状数组||线段树
B. Cards Sorting time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- Codeforces 650D - Zip-line(树状数组)
Codeforces 题目传送门 & 洛谷题目传送门 我怕不是个 nt--一开始忽略了"询问独立"这个条件--然后就一直在想有什么办法维护全局 LIS--心态爆炸 首先离散 ...
- Codeforces 1139F Dish Shopping 树状数组套平衡树 || 平衡树
Dish Shopping 将每个物品拆成p 和 s 再加上人排序. 然后问题就变成了, 对于一个线段(L - R), 问有多少个(li, ri)满足 L >= li && R ...
- codeforces 589G G. Hiring(树状数组+二分)
题目链接: G. Hiring time limit per test 4 seconds memory limit per test 512 megabytes input standard inp ...
- Codeforces 960F Pathwalks ( LIS && 树状数组 )
题意 : 给出若干个边,每条边按照给出的顺序编号,问你找到一条最长的边权以及边的编号同时严格升序的一条路径,要使得这条路径包含的边尽可能多,最后输出边的条数 分析 : 这题和 LIS 很相似,不同的 ...
- $Codeforces\ 522D\ Closest\ Equals$ 线段树
正解:线段树 解题报告: 传送门$QwQ$ 题目大意是说给定一个数列,然后有若干次询问,每次询问一个区间内相同数字之间距离最近是多少$QwQ$.如果不存在相同数字输出-1就成$QwQ$ 考虑先预处理出 ...
- codeforces 522D. Closest Equals 线段树+离线
题目链接 n个数m个询问, 每次询问输出给定区间中任意两个相同的数的最近距离. 先将询问读进来, 然后按r从小到大排序, 将n个数按顺序插入, 并用map统计之前是否出现过, 如果出现过, 就更新线段 ...
随机推荐
- C#保存图片到数据库并读取显示图片的方法
private void button2_Click_1(object sender, System.EventArgs e) { string pathName; if (this.openFile ...
- 51nod 1366 贫富差距
题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个国家有N个公民,标记为0,1,2,...,N-1,每个公民有一个存款额.已知每个公 ...
- ubuntu linux断点续传下载工具 uGet 的安装
网址 http://ugetdm.com/downloads-ubuntu 使用命令行安装 sudo add-apt-repository ppa:plushuang-tw/uget-stable s ...
- Windows Python 版本切换工具 --- Switch Python Version Tool For Windows
2018年1月13日 更新: 如果要用到不同版本的Python 请使用 virtualenv, pip install virtualenv , 我做的这个工具可以拿来维护下环境变量~~ 填好路径自 ...
- Git学习环境搭建和git对用户的增删改查命令
git安装 windows下安装git git下载进入网址: https://git-scm.com/downloads 点击下载,进行安装,安装成功,你会看到图标,点击进入到git bash 查看g ...
- 【BZOJ1067】[SCOI2007] 降雨量(RMQ+分类讨论)
点此看题面 大致题意:请你判断"\(x\)年是自\(y\)年以来降雨量最多的"这句话的真假. 离散化/\(lower\_bound\) 首先,考虑到年份的范围非常大,便可以离散化. ...
- 自己编写shave函数
import numpy def shave(I,border=None): I = I[border[0]:I.shape[0]-border[0],border[1]:I.shape[1]-bor ...
- Oracle 将 A 用户下所有表的增删改查 赋予 B 用户
第一步:创建用户 create user username identified by password; 第二步:给用户赋值接触锁定(仅仅赋予会话权限) grant create session t ...
- jquery 表单事件
.blur() 当元素失去焦点的时候触发事件. .blur(handler(eventObject)) handler(eventObject) 每当事件触发时候执行的函数. .blur([event ...
- lintcode_115_不同的路径 II
不同的路径 II 描述 笔记 数据 评测 "不同的路径" 的跟进问题: 现在考虑网格中有障碍物,那样将会有多少条不同的路径? 网格中的障碍和空位置分别用 1 和 0 来表示. ...