UVALive 6838 Flipping Parentheses // 线段树 区间修改 最值查询
2015-08-17 23:57
543 查看
题目描述
UVALive 6838 Flipping Parentheses解题思路
题目大意:给一个已经匹配好的 ‘(’ , ‘)’ 序列,然后每次在 i 处将这个位置的括号反转,问如果要将这个序列重新变成匹配序列,应该反转最左侧的哪个括号?
先用一个a[i]数组, 表示的是 在前i个括号,左括号’(‘与右括号’)’的数量之差
index | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
si{s}_{i} | ( | ( | ) | ( | ) | ) |
ai{a}_{i} | 1 | 2 | 1 | 2 | 1 | 0 |
1) a[i] >= 0
2) a[len] = 0
(1) 若我们将’(’ 反转成 ‘)’ , 比如将第4个括号反转, 得到新的序列:
index | 1 | 2 | 3 | 4 | 5 | 6 |
---|---|---|---|---|---|---|
si{s}_{i} | ( | ( | ) | ) | ) | ) |
ai{a}_{i} | 1 | 2 | 1 | 0 | -1 | -2 |
(2) 同理,若将 ’ ) ’ 反转成 ’ ( ’ , 则所有的 a[i ~ len] += 2
对于情况(1), 若要重新匹配, 只要将序列里面的从左往右 第一个右括号 ‘)’ 反转即可.
对于情况(2), 若要重新匹配,则要从右往左找到最靠左的一个左括号’ ( ’ 的位置 i, 且满足 a[i ~ len] >= 2 全部成立
同时, 再维护一个值 f[i] = a[i] - i ;
若该值小于0, 说明 在区间[1, i] 里 至少存在 一个 右括号, 方便情况(1)的查找.
而对于情况(2),只需要判断区间的最小值是否>=2即可.
至此, 线段树需要支持的操作是 : 区间修改+最值查询
线段树每个节点维护的信息是当前区间 a 的最小值 和 f 的最小值
参考代码
#include <stdio.h> #include <string.h> #define lson rt<<1 #define rson rt<<1|1 #define mid ((l+r)>>1) #define clr(a, b) memset(a, b, sizeof(a)) inline int min(int a, int b){return a<b?a:b;} const int MAX_N = 300010; struct SegTree{ int f, a, lazy; }node[MAX_N << 2]; char s[MAX_N]; int N, Q, k; void pushup(int rt){ node[rt].a = min(node[lson].a, node[rson].a); node[rt].f = min(node[lson].f, node[rson].f); } void pushdown(int rt){ if (node[rt].lazy){ node[lson].lazy += node[rt].lazy; node[rson].lazy += node[rt].lazy; node[lson].a += node[rt].lazy; node[rson].a += node[rt].lazy; node[lson].f += node[rt].lazy; node[rson].f += node[rt].lazy; node[rt].lazy = 0; } } void build(int l, int r, int rt, int a, int i){ node[rt].lazy = 0; if(l == r){ node[rt].f = a - l; node[rt].a = a; return ; } if (i <= mid) build(l, mid, lson, a, i); else build(mid+1, r, rson, a, i); pushup(rt); } void init(){ clr(node, 0); int sum = 0; for (int i = 1;i <= N;++i){ sum += (s[i] == '(' ? 1 : -1); build(1, N, 1, sum, i); } } void update(int L, int R, int val, int l, int r, int rt){ if (L <= l && r <= R){ node[rt].lazy += val; node[rt].f += val; node[rt].a += val; return ; } pushdown(rt); if (L <= mid) update(L, R, val, l, mid, lson); if (R > mid) update(L, R, val, mid+1, r, rson); pushup(rt); } int query1(int l, int r, int rt){ // 对应情况1) if (l == r) return l; pushdown(rt); if (node[lson].f < 0) return query1(l ,mid, lson); return query1(mid+1, r, rson); } int query2(int l, int r, int rt){ // 对应情况2) if (l == r) return l+1; pushdown(rt); if (node[rson].a < 2) return query2(mid+1, r, rson); return query2(l, mid, lson); } int main(){ while (~scanf("%d %d", &N, &Q)){ scanf("%s", s+1); init(); while (Q--){ scanf("%d", &k); bool flag = (s[k] == '('); s[k] = flag ? ')' : '('; update(k, N, flag ? -2 : 2, 1, N, 1); k = (flag ? query1(1, N, 1) : query2(1, N, 1)); flag = (s[k] == '('); s[k] = flag ? ')' : '('; update(k, N, flag ? -2 : 2, 1, N, 1); printf("%d\n", k); } } return 0; }
相关文章推荐
- C/C++获取本地时间常见方法
- 让大家学会复用
- Codeforces Round #249 (Div. 2) (模拟)
- C/C++获取本地时间常见方法
- Centos 6.4 KVM安装和配置
- Android 双进程Service常驻后台,无惧“一键清理
- apache和IIS同时存在,apache建立多个网站
- Codeforces Gym 100431B Binary Search 搜索+组合数学+高精度
- JQuery学习笔记 选择器 序
- XML报错
- javascript
- 《Java解惑》读书笔记
- CSU 1542 Flipping Parentheses(线段树)
- Java设计思想(1)
- Java基础---面向对象
- 手把手实现andriod应用增量升级
- hdu 1166 敌兵布阵
- JavaScript权威指南_156_第16章_脚本化CSS_16.6-脚本化样式表
- nslookup工具
- Mac OS安装使用docker