[CF997E] Good SubSegment
Description
给你一个长度为n的排列P,定义一段子区间是好的,当且仅当这个子区间内的值构成了连续的一段。例如对于排列\(\{1,3,2\}\),\([1,1],[2,2],[3,3],[2,3],[1,3]\)是好的区间。
共q次询问,每次询问L,R 求有多少\(L \leq l \leq r\leq R\),满足\([l,r]\)是好的区间。\(1≤n,q≤1.2×10^5\).
Solution
可以发现,区间\([l,r]\)是好的,当且仅当$ (Max - Min)=(r−l) $,考虑维护一段区间上式的最小值。
考虑固定右端点时,子区间的个数。每一个最小值为0的位置都可以和当前的r组成一个好的区间。
不难发现,一个右端点能产生的贡献为它左边的点的最小值为0的个数,于是可以在线段树上维护最小值和最小值个数,以及每个最小值为0的位置产生的贡献。
由于右边新加的点会对已有的点产生影响,考虑离线询问,按右端点排序,用两个单调栈分别维护当前Min和Max。右端点右移时,整个区间的值都会减一,又因为处理子区间,所以当右端点右移时我们必须统计当前的答案,也就是历史最小值个数。时间复杂度\(O(n \times log_2^n)\)
在下传标记时,因为之前的统计标记还未下传到当前点,所以我们不能判断当\(Min_{root} == 0\)时才计算贡献,而是当$ Min_{fa[root]} == Min_{root} $时才统计。 因为当前节点一旦被打了标记就代表出现过了最小值为0的情况。
这里我们可以总结出一个套路,就是当我们要统计一个节点的所有子区间的答案时,可以先行离线从小到大统计固定右端点的答案,然后移动右端点时再统计历史值。
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i, j, k) for(int i = (j), i##_end_ = (k); i <= i##_end_; ++i)
#define drep(i, j, k) for(int i = (j), i##_end_ = (k); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(s, ...) fprintf(stderr, s, __VA_ARGS__)
typedef long long LL;
typedef long double LD;
int read() {
char ch = getchar();
int x = 0, flag = 1;
for(;!isdigit(ch); ch = getchar()) if(ch == '-') flag *= -1;
for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(LL x) {
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
#define Maxn 120009
struct Quer {
int l, id;
};
vector <Quer> s[Maxn];
int n, a[Maxn], q, stk1[Maxn], stk2[Maxn], top1, top2;
LL ans[Maxn];
namespace SGMT_tree {
int tree[Maxn << 2], cnt[Maxn << 2], add[Maxn << 2], Time[Maxn << 2];
LL Sum[Maxn << 2];
#define lc(x) ((x) << 1)
#define rc(x) ((x) << 1 | 1)
#define ls rt << 1, l, mid
#define rs rt << 1 | 1, mid + 1, r
void setAdd(int rt, int val) {
tree[rt] += val;
add[rt] += val;
}
void setTime(int rt, int val) {
Sum[rt] += 1ll * cnt[rt] * val;
Time[rt] += val;
}
void (*setTag)(int, int);
void set(int dir) {
if(dir == 1) setTag = setAdd;
if(dir == 2) setTag = setTime;
}
void pushdown(int rt, int l, int r) {
if(add[rt]) {
setAdd(lc(rt), add[rt]), setAdd(rc(rt), add[rt]);
add[rt] = 0;
}
if(Time[rt]) {
if(tree[rt] == tree[lc(rt)]) setTime(lc(rt), Time[rt]);
if(tree[rt] == tree[rc(rt)]) setTime(rc(rt), Time[rt]);
Time[rt] = 0;
}
}
void pushup(int rt) {
cnt[rt] = 0;
tree[rt] = min(tree[lc(rt)], tree[rc(rt)]);
if(tree[rt] == tree[lc(rt)]) cnt[rt] += cnt[lc(rt)];
if(tree[rt] == tree[rc(rt)]) cnt[rt] += cnt[rc(rt)];
Sum[rt] = Sum[lc(rt)] + Sum[rc(rt)];
}
void build(int rt, int l, int r) {
tree[rt] = l; cnt[rt] = 1;
if(l == r) return ;
int mid = (l + r) >> 1;
build(ls), build(rs);
pushup(rt);
}
void modify(int rt, int l, int r, int x, int y, int val) {
if(x <= l && r <= y) {
setTag(rt, val);
return ;
}
int mid = (l + r) >> 1;
pushdown(rt, l, r);
if(y <= mid) modify(ls, x, y, val);
else if(x >= mid + 1) modify(rs, x, y, val);
else modify(ls, x, y, val), modify(rs, x, y, val);
pushup(rt);
}
LL query(int rt, int l, int r, int x, int y) {
if(x <= l && r <= y) return Sum[rt];
pushdown(rt, l, r);
int mid = (l + r) >> 1; LL res = 0;
if(x <= mid) res += query(ls, x, y);
if(y >= mid + 1) res += query(rs, x, y);
return res;
}
#undef lc
#undef rc
#undef ls
#undef rs
}
namespace INIT {
void Main() {
n = read();
rep(i, 1, n) a[i] = read();
q = read();
rep(i, 1, q) {
int l = read(), r = read();
s[r].push_back((Quer){l, i});
}
}
}
namespace SOLVE {
void Main() {
SGMT_tree :: build(1, 1, n);
rep(i, 1, n) {
SGMT_tree :: set(1);
SGMT_tree :: modify(1, 1, n, 1, n, -1);
for(SGMT_tree :: set(1); top1 && a[stk1[top1]] < a[i]; --top1)
SGMT_tree :: modify(1, 1, n, stk1[top1 - 1] + 1, stk1[top1], a[i] - a[stk1[top1]]);
stk1[++top1] = i;
for(SGMT_tree :: set(1); top2 && a[stk2[top2]] > a[i]; --top2)
SGMT_tree :: modify(1, 1, n, stk2[top2 - 1] + 1, stk2[top2],a[stk2[top2]] - a[i]);
stk2[++top2] = i;
SGMT_tree :: set(2);
SGMT_tree :: modify(1, 1, n, 1, n, 1);
rep(j, 0, s[i].size() - 1)
ans[s[i][j].id] = SGMT_tree :: query(1, 1, n, s[i][j].l, i);
}
rep(i, 1, q) write(ans[i]), putchar('\n');
}
}
int main() {
#ifdef Qrsikno
freopen("CF997E.in", "r", stdin);
freopen("CF997E.out", "w", stdout);
#endif
INIT :: Main();
SOLVE :: Main();
return 0;
}
[CF997E] Good SubSegment的更多相关文章
- 【CF997E】Good Subsegments (线段树+单调栈)
Description 原题链接 给你一个长度为\(n\)的排列\(~P\),定义一段子区间是好的,当且仅当这个子区间内的值构成了连续的一段.例如对于排列\(\{1,3,2 \}\),\([1, 1] ...
- [CC-SEINC]Sereja and Subsegment Increasings
[CC-SEINC]Sereja and Subsegment Increasings 题目大意: 有长度为\(n(n\le10^5)\)的序列\(A\)和\(B\). 在一次操作中,可以选择一个区间 ...
- CF1117A Best Subsegment
CF1117A Best Subsegment 乍一看好像很难,仔细想一下发现就是弱智题... 任意一段平均数显然不会超过最大的数,若只取最大数即可达到平均数为最大数. 于是只用取最长的一段连续的最大 ...
- 「CF997E」 Good Subsegments
CF997E Good Subsegments 传送门 和 CF526F 差不多,只不过这道题是对多个子区间进行询问. 据说有一个叫析合树的东西可以在线做,不过有时间再说吧. 考虑离线询问,将每个询问 ...
- 题解 CF997E 【Good Subsegments】
先将问题进行转化,发现满足\((max-min)-(r-l)=0\)的区间即为好区间. 对于本题这样的统计子区间的问题,先将询问离线,按右端点排序一个一个解决,固定右端点,然后通过数据结构来处理出区间 ...
- [cf997E]Good Subsegments
一个区间为好区间当且仅当$\max_{l\le i\le r}a_{i}-\min_{l\le i\le r}a_{i}=r-l$,考虑固定右端点$r$,维护所有左端点$l$的上述式子左-右的值,那么 ...
- CF724D. Dense Subsequence[贪心 字典序!]
D. Dense Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #369 (Div. 2)---C - Coloring Trees (很妙的DP题)
题目链接 http://codeforces.com/contest/711/problem/C Description ZS the Coder and Chris the Baboon has a ...
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
E. George and Cards George is a cat, so he loves playing very much. Vitaly put n cards in a row in ...
随机推荐
- sklearn特征工程总结
转自: http://www.cnblogs.com/jasonfreak/p/5448385.html https://www.zhihu.com/question/28641663/answer/ ...
- Scala入门到精通——第二十四节 高级类型 (三)
作者:摆摆少年梦 视频地址:http://blog.csdn.net/wsscy2004/article/details/38440247 本节主要内容 Type Specialization Man ...
- App中显示html网页
在现在的移动开发中,越来越多的web元素增加到了app里面,hybrid app可以综合native app 和 web app的长处,可以通过webView实现 htmllayout.xml: &l ...
- Office Adobe Acrobat把PDF转换为Word时候提示不支持的Type2字体怎么办
如下图所示,在使用Adobe Acrobat Pro9将PDF转换为Word的时候出现下面的错误 很简单,不要用Adobe Acrobat Pro9了,用Adobe Acrobat Pro X,还 ...
- Visual Studio VS如何卸载Visual assistant
1 点击工具-扩展管理器 2 选中Visual Assist X,点击卸载即可.
- yii2利用自带UploadedFile上传图片
创建一个 models/UploadForm.php: <?php namespace app\models; use yii\base\Model; use yii\web\UploadedF ...
- 机器学习笔记之PCA-SIFT总结
不多说,直接上干货! PCA-SIFT算法在描述子构建上作了创新,主要是 将统计学中的主成分分析(PCA)应用于对描述子向量的降维,以提高匹配效率 . PCA 的原理是:一般有效信号的方差大,噪声的方 ...
- EJB学习笔记六(EJB中的拦截器)
1.前言 听到拦截器,预计都不陌生,尤其是在Servlet规范中,充分应用了拦截器的概念.EJB3也提供了拦截器的支持,本质上是轻量级的AOP实现.拦截器能够将多个业务方法中的通用逻辑从业务方法中抽 ...
- Koa2学习(二)async/await
Koa2学习(二)async/await koa2中用到了大量的async/await语法,要学习koa2框架,首先要好好理解async/await语法. async/await顾名思义是一个异步等待 ...
- mongodb06---索引
索引(就像书的目录,先找大致的位置再细找,不是从头开始找): mongodb每行的列可以完全不同,没有列的概念. 索引作用类型: :单列索引 :多列索引 :子文档索引 索引性质: :普通索引 .唯一索 ...