UVALive 6838 Flipping Parentheses // 线段树 区间修改 最值查询
来源:互联网 发布:数学矩阵的用途 编辑:程序博客网 时间:2024/05/22 03:19
题目描述
UVALive 6838 Flipping Parentheses
解题思路
题目大意:
给一个已经匹配好的 ‘(’ , ‘)’ 序列,然后每次在 i 处将这个位置的括号反转,问如果要将这个序列重新变成匹配序列,应该反转最左侧的哪个括号?
先用一个a[i]数组, 表示的是 在前i个括号,左括号’(‘与右括号’)’的数量之差
不难发现: 对于一个匹配好的括号序列, a[i]满足:
1) a[i] >= 0
2) a[len] = 0
(1) 若我们将’(’ 反转成 ‘)’ , 比如将第4个括号反转, 得到新的序列:
可以发现 反转完之后: a[i ~ len] -= 2 , ( 即从修改的位置开始到最后,所有的a值都减少了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;}
0 0
- UVALive 6838 Flipping Parentheses // 线段树 区间修改 最值查询
- UVALive 6838 Flipping Parentheses(线段树、单点更新、区间查询)
- UVALive 6838Flipping Parentheses (线段树)
- Flipping Parentheses (线段树 单点更新 区间查询)
- 【模板】线段树区间修改、区间求和、查询最值
- 线段树 csu1542 Flipping Parentheses
- CSU - 1542 Flipping Parentheses (线段树)
- CSU 1542 Flipping Parentheses(线段树)
- [线段树][区间修改&&查询]
- 【模板】线段树_区间最值、区间求和、修改
- 线段树区间修改+区间查询
- 线段树(区间修改,区间查询)
- [模板]-线段树-区间修改 + 区间查询
- hdu 1754 I Hate It【线段树】 区间修改,最值查询
- zkw线段树,区间修改,最值查询(差分)
- UVALive - 4108 SKYLINE (线段树的区间修改)
- UVALive - 4108 SKYLINE 线段树(区间修改)
- UVALive 4108 SKYLINE(线段树区间修改)
- 杭电 1004 (JAVA)
- HDOJ-1869-六度分离
- NYOJ 64 鸡兔同笼
- Dynamics AX 报告分页
- leetcode 137: Single Number II
- UVALive 6838 Flipping Parentheses // 线段树 区间修改 最值查询
- POJ 2991 Crane (线段树 维护旋转的向量和)
- [leetcode] 227.Basic Calculator II
- java基本输入输出编程:文本界面的输入与输出
- google C++编程风格指南之头文件的包含顺序
- 闲话js前端框架(1)——从avalonjs的模板说起
- 服务器后台TCP连接存活问题
- gpio 相关中文文档
- 深入浅出之正则表达式(转载)