codeforces 522D. Closest Equals 线段树+离线
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 线段树+离线的更多相关文章
- $Codeforces\ 522D\ Closest\ Equals$ 线段树
正解:线段树 解题报告: 传送门$QwQ$ 题目大意是说给定一个数列,然后有若干次询问,每次询问一个区间内相同数字之间距离最近是多少$QwQ$.如果不存在相同数字输出-1就成$QwQ$ 考虑先预处理出 ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- D. Closest Equals(线段树)
题目链接: D. Closest Equals time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- CodeForces 522D Closest Equals 树状数组
题意: 给出一个序列\(A\),有若干询问. 每次询问某个区间中值相等且距离最短的两个数,输出该距离,没有则输出-1. 分析: 令\(pre_i = max\{j| A_j = A_i, j < ...
- Codeforces 522D Closest Equals
题解: 傻逼题 直接从左向右扫描每个点作为右端点 然后单点修改区间查询就行了 另外一种更直观的做法就是$(i,j)$之间产生了$(j-i)$ 于是变成矩形查最大值,kd-tree维护 代码: #inc ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- 线段树+离线 hdu5654 xiaoxin and his watermelon candy
传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
随机推荐
- POJ 3692 Kindergarten (二分图 最大团)
Kindergarten Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5660 Accepted: 2756 Desc ...
- Hdu Binary Tree Traversals
Problem Description A binary tree is a finite set of vertices that is either empty or consis ...
- powerdesigener 12.5注册机
下载链接 下载链接 密码:awg9
- 全局通知Notification
Notification 全局通知 关于全局通知的个人理解: 即有一个发射消息的,在整个应用中任何对象都可以接受这个消息 但是无论是哪个对象接受消息,都要在这个对象结束时移除消息 简单的说 就是给对象 ...
- Winform中上传、下载文件选择打开文件的位置
打开将要上传的文件 var fileName="";OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Mul ...
- Unity5UGUI 官方教程学习笔记(二)Rect Transform
Rect Transform Posx Posy Posz : ui相对于父级的位置 Anchors :锚点 定义了与父体之间的位置关系 一个锚点由四个锚组成 四个锚分别代表了 ...
- ORA-01045: user XXZY lacks CREATE SESSION privilege; logon denied
在创建用户时,一般我们都分配connect.dba.resource 角色,但是,为什么登陆时还报错呢 原因:用户角色没有激动 解决:ALTER USER XXXX DEFAULT ROLE &quo ...
- 星际SC 地图 Big Game Fort 要塞之战 修正了 BIG GAME 地图的平衡性
星际SC 地图 Big Game Fort 要塞之战 修正了 BIG GAME 地图的平衡性 也适合BIG 1V1 对战 此版本目前不开放1打1造功能
- 有意思,搞了这么多年WEB,还是第一次知道这个东西 关键字 前端模板
有意思,搞了这么多年WEB,还是第一次知道这个东西 关键字 前端模板 jquery-tmpl handlebars 项目中用的是handlebars jqtmpl配置不成功 不做记载 百度吧 小媳妇 ...
- Unix/Linux笔记全集
1:Unix/Linux操作系统概述 要求:理解应用软件和操作系统的区别,掌握系统的Kernel(核心)和shell(外壳)之间的关系以及各自的作用 Solaris:Solaris 是Sun Micr ...