把左括号看做$1$,右括号看做$-1$,于是查询操作等于查询一个区间左边右边最大(最小)子段和

支持区间翻转,反转,覆盖操作。。。注意如果有覆盖操作,之前的操作全部作废了。。。于是在下传标记的时候要最后做。。。

 /**************************************************************
Problem: 2329
User: rausen
Language: C++
Result: Accepted
Time:4252 ms
Memory:7352 kb
****************************************************************/ #include <cstdio>
#include <algorithm> #define _max(x, y) (x > y ? x : y)
#define _min(x, y) (x < y ? x : y)
using namespace std;
const int N = 1e5 + ; int read();
int get_c();
int get_op(); namespace treap {
struct node;
node *root, *null; struct node {
node *ls, *rs;
int val, rev, inv, fill, maxl, maxr, minl, minr, sum, sz; #define Len (1 << 16)
inline void* operator new(size_t, int _v = ) {
static node *mempool, *c;
if (mempool == c)
mempool = (c = new node[Len]) + Len;
c -> ls = c -> rs = null;
c -> val = c -> sum = _v, c -> rev = c -> inv = c -> fill = ;
c -> maxl = c -> maxr = c -> minl = c -> minr = ;
c -> sz = ;
return c++;
}
#undef Len inline void reverse() {
rev ^= ;
swap(ls, rs);
swap(minl, minr), swap(maxl, maxr);
}
inline void inverse() {
inv ^= ;
fill = -fill, val = -val, sum = -sum;
swap(maxl, minl), maxl = -maxl, minl = -minl;
swap(maxr, minr), maxr = -maxr, minr = -minr;
}
inline void replace(int t) {
fill = val = t, sum = sz * t;
maxl = maxr = sz * (t == );
minl = minr = -sz * (t == -);
} inline node* update() {
sz = ls -> sz + rs -> sz + ;
sum = ls -> sum + rs -> sum + val;
maxl = _max(ls -> maxl, ls -> sum + val + _max(, rs -> maxl));
minl = _min(ls -> minl, ls -> sum + val + _min(, rs -> minl));
maxr = _max(rs -> maxr, rs -> sum + val + _max(, ls -> maxr));
minr = _min(rs -> minr, rs -> sum + val + _min(, ls -> minr));
return this;
}
inline node* push() {
if (rev) {
ls -> reverse(), rs -> reverse();
rev = ;
}
if (inv) {
ls -> inverse(), rs -> inverse();
inv = ;
}
if (fill) {
ls -> replace(fill), rs -> replace(fill);
fill = ;
}
return this;
}
}; inline void init() {
null = new()node;
null -> ls = null -> rs = null;
null -> sz = null -> val = ;
} inline unsigned int Rand() {
static unsigned int res = ;
return res += res << | ;
}
inline int random(int x, int y) {
return Rand() % (x + y) < x;
} void build(node *&p, int l, int r, int *a) {
if (l > r) {
p = null;
return;
}
p = new(a[l + r >> ])node;
if (l == r) {
p -> update();
return;
}
build(p -> ls, l, (l + r >> ) - , a);
build(p -> rs, (l + r >> ) + , r, a);
p -> update();
} void merge(node *&p, node *x, node *y) {
if (x == null || y == null)
p = x == null ? y -> push() : x -> push();
else if (random(x -> sz, y -> sz)) {
p = x -> push();
merge(p -> rs, x -> rs, y);
} else {
p = y -> push();
merge(p -> ls, x, y -> ls);
}
p -> update();
} void split(node *p, node *&x, node *&y, int k) {
if (!k) {
x = null, y = p -> push();
return;
}
if (k == p -> sz) {
x = p -> push(), y = null;
return;
}
if (p -> ls -> sz >= k) {
y = p -> push();
split(p -> ls, x, y -> ls, k);
y -> update();
} else {
x = p -> push();
split(p -> rs, x -> rs, y, k - p -> ls -> sz - );
x -> update();
}
}
}
using namespace treap; int n;
int a[N]; int main() {
int Q, i, oper, l, r;
node *x, *y, *z;
init();
n = read(), Q = read();
for (i = ; i <= n; ++i) a[i] = get_c();
build(root, , n, a);
while (Q--) {
oper = get_op(), l = read(), r = read();
split(root, x, y, l - ), split(y, y, z, r - l + );
if (oper == ) y -> replace(get_c());
else if (oper == ) printf("%d\n", (y -> maxr + ) / - (y -> minl - ) / );
else if (oper == ) y -> reverse();
else if (oper == ) y -> inverse();
merge(root, x, y -> push()), merge(root, root, z);
}
return ;
} inline int read() {
register int x = ;
register char ch = getchar();
while (ch < '' || '' < ch) ch = getchar();
while ('' <= ch && ch <= '')
x = x * + ch - '', ch = getchar();
return x;
} inline int get_c() {
register char ch = getchar();
while (ch != '(' && ch != ')') ch = getchar();
return ch == '(' ? : -;
} inline int get_op() {
register char ch = getchar();
while (ch != 'R' && ch != 'Q' && ch != 'S' && ch != 'I') ch = getchar();
if (ch == 'R') return ;
if (ch == 'Q') return ;
if (ch == 'S') return ;
if (ch == 'I') return ;
}

BZOJ2329 [HNOI2011]括号修复的更多相关文章

  1. bzoj千题计划222:bzoj2329: [HNOI2011]括号修复(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2329 需要改变的括号序列一定长这样 :)))((( 最少改变次数= 多余的‘)’/2 [上取整] + ...

  2. BZOJ2329 HNOI2011 括号修复 splay+贪心

    找平衡树练习题的时候发现了这道神题,可以说这道题是近几年单考splay的巅峰之作了. 题目大意:给出括号序列,实现区间翻转,区间反转和区间更改.查询区间最少要用几次才能改成合法序列. 分析: 首先我们 ...

  3. 2019.03.25 bzoj2329: [HNOI2011]括号修复(fhq_treap)

    传送门 题意简述: 给一个括号序列,要求支持: 区间覆盖 区间取负 区间翻转 查询把一个区间改成合法括号序列最少改几位 思路: 先考虑静态的时候如何维护答案. 显然把所有合法的都删掉之后序列长这样: ...

  4. BZOJ2329: [HNOI2011]括号修复(Splay)

    解题思路: Replace.Swap.Invert都可以使用Splay完美解决(只需要解决一下标记冲突就好了). 最后只需要统计左右括号冲突就好了. 相当于动态统计最大前缀合和最小后缀和. 因为支持翻 ...

  5. 【BZOJ2329/2209】[HNOI2011]括号修复/[Jsoi2011]括号序列 Splay

    [BZOJ2329/2209][HNOI2011]括号修复/[Jsoi2011]括号序列 题解:我们的Splay每个节点维护如下东西:左边有多少多余的右括号,右边有多少多余的左括号,同时为了反转操作, ...

  6. BZOJ 2329: [HNOI2011]括号修复( splay )

    把括号序列后一定是))))((((这种形式的..所以维护一个最大前缀和l, 最大后缀和r就可以了..答案就是(l+1)/2+(r+1)/2...用splay维护,O(NlogN). 其实还是挺好写的, ...

  7. ●BZOJ 2329 [HNOI2011]括号修复.cpp

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=2329 题解: Splay 类似 BZOJ 2329 [HNOI2011]括号修复 只是多了一 ...

  8. 【BZOJ2329】括号修复(Splay)

    [BZOJ2329]括号修复(Splay) 题面 BZOJ 洛谷 题解 本来想着用线段树来写 但是有一个区间翻转 所以不能用线段树了,就只能用平衡树 然后直接\(Splay\)就好了 注意一下几个标记 ...

  9. 【bzoj2329】[HNOI2011]括号修复 Splay

    题目描述 题解 Splay 由于有区间反转操作,因此考虑Splay. 考虑答案:缩完括号序列后剩下的一定是 $a$ 个')'+ $b$ 个'(',容易发现答案等于 $\lceil\frac a2\rc ...

随机推荐

  1. centos中基于随机数,再加入班级学生姓名

    这只需要在上一篇的随机数中加入数值就可以了 代码如下 #!/bin/bash num=$(date +%N); c=(wanghao xieyunsheng) a=`expr $num % 39 ` ...

  2. LoadRunner使用之变量参数化

    LR性能测试之参数化设置 Q:何为参数化? LR在录制程序运行的过程中,VuGen(脚本生成器) 自动生成了包含录制过程中实际用到的数值的脚本,如果你企图在录制的脚本中使用不同的数值执行脚本的活动(如 ...

  3. test homework ~ coverage about method printPrimes

    /******************************************************* * Finds and prints n prime integers * Jeff ...

  4. 如何自定义ReportDesigner的向导界面(WIN)

    https://www.devexpress.com/Support/Center/Example/Details/T140683

  5. Javascript 知识点整理

    1.十进制和十六进制相互转换 /* 十六进制转十进制 */ var test = FF; var x = parseInt(test ,16); //方法一 x = parseInt('0x'+tes ...

  6. Strus2第一次课:dom4j操作xml

    先从底层的xml操作技术记录: 当我们新建一个项目,什么架包都没加入的时候,java提供了 org.w3c.dom给我们操作xml里面的元素 import org.w3c.dom.Document; ...

  7. venus

    The Venus system was a small timesharing system serving five or six users at a time:分时系统 The design ...

  8. JS获取元素尺寸大小、鼠标位置

    //e.clientX|Y:表示鼠标相对浏览器可视窗口的当前坐标 //e.offsetX|Y:表示鼠标相对于事件源对象的坐标 //e.pageX|Y:表示鼠标相对于网页的坐标 /* element.o ...

  9. 尝试用md编辑器写博客, 内容为 jupyter笔记

    Matplotlib tutorial http://www.labri.fr/perso/nrougier/teaching/matplotlib/#introduction %matplotlib ...

  10. /etc/xinetd.conf 和 /etc/xinetd.d/*【新网络服务配置】

    http://blog.csdn.net/kelven2004/article/details/1701930 xinetd 是 inetd 的安全加强版,它内置了自己的 TCP wrapper, 可 ...