UVALive 6838 Flipping Parentheses // 线段树 区间修改 最值查询

题目描述

UVALive 6838 Flipping Parentheses

解题思路

题目大意:
给一个已经匹配好的 ‘(’ , ‘)’ 序列,然后每次在 i 处将这个位置的括号反转,问如果要将这个序列重新变成匹配序列,应该反转最左侧的哪个括号?


先用一个a[i]数组, 表示的是 在前i个括号,左括号’(‘与右括号’)’的数量之差

index123456
si (()())
ai 121210

不难发现: 对于一个匹配好的括号序列, a[i]满足:
1) a[i] >= 0
2) a[len] = 0


(1) 若我们将’(’ 反转成 ‘)’ , 比如将第4个括号反转, 得到新的序列:

index123456
si (())))
ai 1210-1-2

可以发现 反转完之后: 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值