UVALive 6838 (线段树)

来源:互联网 发布:java future get 阻塞 编辑:程序博客网 时间:2024/06/10 13:30

题意是给出长度为n的合法括号序列,有q个询问,每次把某个位置的一个括号反转,求需要反转的最左端的一个括号的位置,使得括号序列仍然合法.


考虑一个前缀和的数组a,如果某个位置是(那么前缀+1,否则-1,一个括号序列合法当且仅当所有的a[i]>=0&&a[n]==0,所以在改变某个位置的括号后,我们要找的就是最左端的一个括号使得改变它以后整个括号序列仍然满足这个性质.


假设改变的位置是x

情况1:( -> )

[x,n]区间的a数组值都-2,显然只要在x之前的地方找到一个右括号就是答案,因为这之后的所有的a数组的值都+2以后必然满足括号序列合法的条件.而x之前的右括号就是整个区间的第一个右括号.

找到第一个右括号很简单,因为第一个右括号左边必然都是左括号,a[i]-i都是0,也就是找到第一个a[i]-i<0的位置.

情况2:) -> (

[x,n]区间的a数组值都+2,假设我们需要找到的那个位置为pos,那么需要在[pos,n]区间的a数组值都-2,要使得序列合法,必须是[pos,n]之间的所有的a数组的值都>=2,也就是找到最右边的a数组值小于2的位置,这个位置+1就是所求的pos.


#include <bits/stdc++.h>using namespace std;#define maxn 311111#define pl c<<1#define pr (c<<1)|1#define lson tree[c].l,tree[c].mid,c<<1#define rson tree[c].mid+1,tree[c].r,(c<<1)|1#define INF 111111111struct node {    int l, r, mid;    int num1, num2, add;}tree[maxn<<4];char s[maxn];int a[maxn];int n, q;void push_up (int c) {    if (tree[c].l == tree[c].r)        return ;    tree[c].num1 = min (tree[pl].num1, tree[pr].num1);    tree[c].num2 = min (tree[pl].num2, tree[pr].num2);}void push_down (int c) {    if (tree[c].add == 0 || tree[c].l == tree[c].r)        return;    tree[pl].add += tree[c].add, tree[pr].add += tree[c].add;    tree[pl].num1 += tree[c].add, tree[pr].num1 += tree[c].add;    tree[pl].num2 += tree[c].add, tree[pr].num2 += tree[c].add;    tree[c].add = 0;    return ;}void build_tree (int l, int r, int c) {    tree[c].l = l, tree[c].r = r, tree[c].mid = (l+r)>>1;    tree[c].add = 0;    if (l == r) {        tree[c].num1 = a[l];        tree[c].num2 = a[l]-l;        return ;    }    build_tree (lson);    build_tree (rson);    push_up (c);}void update (int l, int r, int c, int x, int y, int val) { //[x,y]+val    push_down (c);    if (l == x && y == r) {        tree[c].add += val;        tree[c].num1 += val, tree[c].num2 += val;        return ;    }    if (tree[c].mid >= y)        update (lson, x, y, val);    else if (tree[c].mid < x)        update (rson, x, y, val);    else {        update (lson, x, tree[c].mid, val);        update (rson, tree[c].mid+1, y, val);    }    push_up (c);}int find1 (int l, int r, int c) { //(->)    push_down (c);    if (l == r)        return l;    if (tree[pl].num2 < 0)        return find1 (lson);    else        return find1 (rson);}int find2 (int l, int r, int c) { //)->( 知道到右边第一个num2小于2的    push_down (c);    if (l == r)        return l+1;    if (tree[pr].num1 < 2)        return find2 (rson);    else        return find2 (lson);}void debug (int c) {    cout << tree[c].l << " " << tree[c].r << " " << tree[c].num1 << " " << tree[c].num2 << endl;    if (tree[c].l == tree[c].r)        return ;    debug (pl);    debug (pr);}int main () {    //freopen ("in", "r", stdin);    while (scanf ("%d%d", &n, &q) == 2) {        scanf ("%s", s+1);        a[0] = 0;        for (int i = 1; i <= n; i++)            a[i] = a[i-1]+(s[i] == '(' ? 1 : -1);        build_tree (1, n, 1);        while (q--) {            int x;            scanf ("%d", &x);            if (s[x] == '(') {                update (1, n, 1, x, n, -2);                s[x] = ')';                int pos = find1 (1, n, 1); printf ("%d\n", pos);                s[pos] = '(';                update (1, n, 1, pos, n, 2);            }            else if (s[x] == ')') {                update (1, n, 1, x, n, 2);                s[x] = '(';                int pos = find2 (1, n, 1); printf ("%d\n", pos);                s[pos] = ')';                update (1, n, 1, pos, n, -2);            }        }    }    return 0;}


0 0