[BZOJ3339] Rmq Problem(线段树)
这个题的方法好像很多啊
1.莫队暴力
2.线段树 + 离线处理
先预处理出sg[i]表示前i个数的sg值,next[i]表示i的下一位置在哪里,如果后面再没有i,那么next[i] = n + 1
然后把线段树的每个叶子节点放上sg[i]。
把询问按照左端点由小到大排序,我们考虑如何从 l ~ r 转移到 l + 1 ~ r,
会发现,当把a[l]这个数去掉之后,如果后面没有a[l]那么答案就可能会更新,
那么我们可以更新 l + 1 ~ next[l] - 1这个区间,也就是用线段树操作
#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 200001
#define INF ~(1 << 31)
#define root 1, 1, n
#define ls now << 1, l, mid
#define rs now << 1 | 1, mid + 1, r
#define min(x, y) ((x) < (y) ? (x) : (y)) int n, q;
int a[N], next[N], vis[N], ans[N], mx[N << 2], sg[N]; struct node
{
int x, y, id;
}p[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline bool cmp(node x, node y)
{
return x.x < y.x;
} inline void build(int now, int l, int r)
{
if(l == r)
{
mx[now] = sg[l];
return;
}
mx[now] = INF;
int mid = (l + r) >> 1;
build(ls);
build(rs);
} inline void push_down(int now)
{
if(mx[now] != INF)
{
mx[now << 1] = min(mx[now << 1], mx[now]);
mx[now << 1 | 1] = min(mx[now << 1 | 1], mx[now]);
mx[now] = INF;
}
} inline void update(int now, int l, int r, int x, int y, int d)
{
if(x <= l && r <= y)
{
mx[now] = min(mx[now], d);
return;
}
push_down(now);
int mid = (l + r) >> 1;
if(x <= mid) update(ls, x, y, d);
if(mid < y) update(rs, x, y, d);
} inline int query(int now, int l, int r, int x)
{
if(l == r) return mx[now];
push_down(now);
int mid = (l + r) >> 1;
if(x <= mid) return query(ls, x);
else return query(rs, x);
} int main()
{
int i, j = 0, now = 1, nxt;
n = read();
q = read();
for(i = 1; i <= n; i++) a[i] = read();
for(i = 1; i <= n; i++)
{
vis[a[i]] = 1;
while(vis[j]) j++;
sg[i] = j;
}
build(root);
for(i = 0; i <= n; i++) vis[i] = n + 1;
for(i = n; i >= 1; i--) next[i] = vis[a[i]], vis[a[i]] = i;
for(i = 1; i <= q; i++)
{
p[i].id = i;
p[i].x = read();
p[i].y = read();
}
std::sort(p + 1, p + q + 1, cmp);
for(i = 1; i <= q; i++)
{
while(now < p[i].x)
{
if(now + 1 < next[now])
update(root, now + 1, next[now] - 1, a[now]);
now++;
}
ans[p[i].id] = query(root, p[i].y);
}
for(i = 1; i <= q; i++) printf("%d\n", ans[i]);
return 0;
}
3.主席树
。。不会
[BZOJ3339] Rmq Problem(线段树)的更多相关文章
- Codeforces 803G Periodic RMQ Problem 线段树
Periodic RMQ Problem 动态开点线段树直接搞, 我把它分成两部分, 一部分是原来树上的, 一部分是后来染上去的,两个部分取最小值. 感觉有点难写.. #include<bits ...
- bzoj 3489 A simple rmq problem - 线段树
Description 因为是OJ上的题,就简单点好了.给出一个长度为n的序列,给出M个询问:在[l,r]之间找到一个在这个区间里只出现过一次的数,并且要求找的这个数尽可能大.如果找不到这样的数,则直 ...
- [bzoj3339]Rmq Problem||[bzoj3585]mex_线段树
Rmq Problem bzoj-3339||mex bzoj-3585 题目大意:给定一个长度为n的数列a,多次讯问区间l,r中最小的不属于集合{$A_l,A_{l+1}...A_r$}的非负整数. ...
- bzoj 3489 A simple rmq problem——主席树套线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3489 题解:http://www.itdaan.com/blog/2017/11/24/9b ...
- BZOJ3339 Rmq Problem
[bzoj3339]Rmq Problem Description Input Output Sample Input 7 5 0 2 1 0 1 3 2 1 3 2 3 1 4 3 6 2 7 Sa ...
- bzoj 3489: A simple rmq problem k-d树思想大暴力
3489: A simple rmq problem Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 551 Solved: 170[Submit][ ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- 【题解】BZOJ3489 A Hard RMQ problem(主席树套主席树)
[题解]A simple RMQ problem 占坑,免得咕咕咕了,争取在2h内写出代码 upd:由于博主太菜而且硬是要用指针写两个主席树,所以延后2hQAQ upd:由于博主太菜而且太懒所以他决定 ...
- Uva 12299 带循环移动的RMQ(线段树)
题目链接:https://vjudge.net/contest/147973#problem/C 题意:传统的RMQ是一个不变的数组a求区间最值.现在要循环移动(往前移动). 分析:求区间问题,很容易 ...
随机推荐
- 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载二(生命周期)
4.1 什么是生命周期 想要真正地理解PhoneGap应用开发的内涵,首先需要理解什么是生命周期.这在字面上其实非常容易理解,一个应用从开始运行被手机加载到应用被退出之间的过程就称之为一个生命周期.为 ...
- NF!=1
NF表示列数,不等于1表示列数不为1列
- LINUX 安装JDK (rpm格式和tar.gz格式)
谷歌博客地址:http://tsaiquinn.blogspot.com/2014/10/linux-jdk-rpmtargz.html JDK rpm方式: 我使用的是SecureCRT,先下载了然 ...
- bat 批处理测试局域网速度 两端电脑
C:\Users\Administrator>iperf3 iperf3: parameter error - must either be a client (-c) or server (- ...
- 关于父类中的this指针的问题
在处理一个消息推送的问题的时候遇到个小问题,比如A是B的子类,当A生成实例时,会执行父类的构造函数,那么在父类中,this会是什么类型呢? 于是做了个小测试 子类ChildClass: public ...
- 贴一发STL源码
int my_lower_bound(int size, long long key){ int first = 0, middle; int half, len; len = si ...
- App Store中的开源游戏汇总
这是国外达人收集的曾经在app store上出现过,或者还在app store上卖的iOS开源游戏的列表,其中代码大部分人你托管在google code或者github上,其中有很多使用Cocos2D ...
- html备忘录
上传文件 <form action="/ajax/" method="post" enctype="multipart/form-data&qu ...
- OpenCV2:第八章 界面事件
一.简介 OpenCV中提供了程序界面中的鼠标和键盘事件 二.鼠标事件 // 设置鼠标回调函数 void setMouseCallback ( const string& winname, ...
- Codeforces 727C Guess the Array
题目传送门 长度为\(n\)的数组,询问\(n\)次来求出数组中每一个数的值 我们可以先询问三次 \(a[1]+a[2]\) \(a[1]+a[3]\) \(a[2]+a[3]\) 然后根据这三次询问 ...