把左括号看做$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. bzoj1007[HNOI2008]水平可见直线

    cycleke神说要用半平面交(其实他也用的凸包),把我吓了一跳,后来发现(看题解)其实可以先按斜率排序,再将最小的两条线入栈,如果其与栈顶元素的交点在上一个点的左边,则将栈顶元素出栈.这是一个开口向 ...

  2. Ural-1146Maximum Sum-最大子矩阵

    Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative inte ...

  3. 关于AngularJs,数据绑定与自定义验证

    最近开始着手学起了Angular,抱着好奇的心情开始研究了起来.忽然发现angular可以巧妙而方便的进行数据的绑定验证啊什么的.(当然,我只是刚开始学,所有可能有更强大的功能,只是我还没有看到) 那 ...

  4. c++多线程のunique和lazy initation

    unique更方便使用,但是会消耗更多的计算机性能 onceflag保证一个线程被调用一次,防止不能的加锁开锁

  5. usr类库的使用(一般用在第三方类库使用系统库报错头文件找不到时)

    第三方Html解析类库Hpple,在导入框架libxml2.2.dylib后,XCode仍然找不到<libxml/tree.h>. 1 .项目 -Targets 中的 Build P ha ...

  6. 原创一看便知、Maven创建web项目

    创建maven-项目    如果 pom.xml 文件报错 右击项目-->Maven-->update Project 详细步骤   上图中Next 2.继续Next 3.选maven-a ...

  7. SQL Join的一些总结

    1.1.1 摘要 Join是关系型数据库系统的重要操作之一,SQL Server中包含的常用Join:内联接.外联接和交叉联接等.如果我们想在两个或以上的表获取其中从一个表中的行与另一个表中的行匹配的 ...

  8. python走起之第十二话

    1. ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型 ...

  9. Maven教程

    找了一个很详细的maven教程 收藏下 学习网址:http://www.yiibai.com/maven/

  10. HTML <a> download 属性,点击链接来下载图片

    Html5里面的 标签的 Download 属性可以设置一个值来规定下载文件的名称.所允许的值没有限制,浏览器将自动检测正确的文件扩展名并添加到文件 (.img, .pdf, .txt, .html, ...