Test instructions: There is a string all composed of ' (' and ') ', then there are three operations, query a B output interval [A,] the string of parentheses is valid, reverse a B to the interval [A, a] string all ' (' replace ') ', and All ') ' to ' (', set A b c, replacing all the characters of the interval [A, a] with C.
The main thing: the line tree is obviously, in order to let the line tree maintenance, to determine whether a string is a valid parenthesis, you can put all the ' (' replaced by-1, ') ' to replace with 1, then if the string is valid, the entire string should be 0 and the prefix maximum and cannot exceed 0. Therefore, you can add Maxx and sum to the line tree nodes to maintain the maximum and sum of prefixes within the interval. Then the reverse operation, in order to facilitate the calculation of Maxx, the node should also maintain the Minn prefix minimum and so Maxx =-minn can be calculated directly. There is also the maximal and the larger one of the maximum and the left sub-interval and the sum of the left dial hand interval combined with the right sub-interval, and the minimum and the same method.
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;Const intN =100005; struct Tree {intSum, MAXX, Minn;intSetv, rev; void F (intValint Left,int Right) {if(!val) {sum =-sum;inttemp = Maxx; Maxx =-minn; Minn =-temp; Rev ^=1; }Else{minn = min (val, Val * ( Right- Left+1)); Maxx = Max (Val, Val * ( Right- Left+1)); sum = val * ( Right- Left+1); Setv = val; Rev =0; }}}tree[n <<2];intN, Q, A[n];char S[n], op[Ten], c[5];void Pushdown (intKint Left,int Right) {int Mid= ( Left+ Right) /2;if(TREE[K].SETV) {Tree[k *2].F (Tree[k].setv, Left,Mid); Tree[k *2+1].F (Tree[k].setv,Mid+1, Right); Tree[k].setv =0; }if(Tree[k].rev) {Tree[k *2].F (0, Left,Mid); Tree[k *2+1].F (0,Mid+1, Right); Tree[k].rev =0; }}void Pushup (intK) {tree[k].sum = Tree[k *2].sum + tree[k *2+1].sum; Tree[k].maxx = Max (Tree[k *2].maxx, Tree[k *2].sum + tree[k *2+1].maxx); Tree[k].minn = min (tree[k *2].minn, Tree[k *2].sum + tree[k *2+1].minn);} void Build (intKint Left,int Right) {Tree[k].setv = Tree[k].rev =0;if( Left== Right) {tree[k].sum = Tree[k].maxx = Tree[k].minn = a[ Left]; Return }int Mid= ( Left+ Right) /2; Build (k *2, Left,Mid); Build (k *2+1,Mid+1, Right); Pushup (k);} void Modify (intKint Left,int Right,intLintRintV) {if(L <= Left&& Right<= r) {tree[k].f (V, Left, Right); Return } pushdown (k, Left, Right);int Mid= ( Left+ Right) /2;if(L <=Mid) Modify (k *2, Left,Mid, L, R, v);if(R >Mid) Modify (k *2+1,Mid+1, Right, L, R, v); Pushup (k);} void Query (intKint Left,int Right,intLintRint&sum,int&maxx) {if(L <= Left&& Right<= r) {maxx = Tree[k].maxx; sum = tree[k].sum; Return } pushdown (k, Left, Right);int Mid= ( Left+ Right) /2;if(R <=Mid) Query (k *2, Left,Mid, L, R, Sum, Maxx);Else if(L >Mid) Query (k *2+1,Mid+1, Right, L, R, Sum, Maxx);Else{intSum1, Maxx1, sum2, maxx2; Query (k *2, Left,MidLMid, SUM1, maxx1); Query (k *2+1,Mid+1, Right,Mid+1, R, Sum2, MAXX2); sum = sum1 + sum2; Maxx = Max (maxx1, sum1 + maxx2); } pushup (k);}intMain () {intT, CAS =1; scanf"%d", &t); while(t--) {scanf ("%d%s", &n, S +1); for(inti =1; I <= N; i++)if(S[i] = =' (')A[i] =-1;ElseA[i] =1; Build1,1, n); printf"Case%d:\n", cas++); scanf"%d", &q);intL, R, Sum, Maxx; while(q--) {scanf ('%s ', op);if(op[0] ==' Q ') {scanf"%d%d", &l, &r); Query1,1, N, L +1, R +1, Sum, Maxx);if(!sum && Maxx <=0) printf ("yes\n");Elseprintf"no\n"); }Else if(op[0] ==' s ') {scanf"%d%d%s", &l, &r, C);if(c[0] ==' (')Modify1,1, N, L +1, R +1, -1);ElseModify1,1, N, L +1, R +1,1); }Else{scanf ("%d%d", &l, &r); Modify1,1, N, L +1, R +1,0); }} printf ("\ n"); } return0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UESTC 94 (interval update)