UESTC 1546 bracket Sequence

Source: Internet
Author: User
Tags strcmp

Given a sequence of parentheses, you can set it and change the parentheses within a certain range into a certain bracket. Reverse swaps the Left and Right brackets, query whether the sequence of parentheses within a certain range is valid.

Practice: In the first thought, the node records the unmatched left and right parentheses and then operates, because it looks down on the reverse operation (I thought it would be nice to simply change the Left and Right parenthesis values, but ...), wa.

Then Baidu's great god discovered a good algorithm.

The cause of my error is that the right brackets that cannot be matched cannot be found accurately. The reason that the right parenthesis cannot be paired is that the right parenthesis does not have enough square brackets. Therefore, the great God sets the left parenthesis to 1 and the right parenthesis to-1, which is calculated from left to right, the minimum value of the value in the node. If the minimum value is 0, it may be completely matched, but it is not necessarily (only solves the right brace problem), and it is no longer afraid of reverse.

 
# Include <cstdio> # include <cstring> # define left L, M, x <1 # define right m + 1, R, x <1 | 1 const int LMT = 100003; int cov [LMT <2], Ox [LMT <2], sum [LMT <2], mxsum [LMT <2], misum [LMT <2]; char SEC [LMT]; inline int max (int A, int B) {return A> B? A: B;} inline int min (int A, int B) {return a <B? A: B;} void ope (INT op, int L, int R, int X) {cov [x] = op; mxsum [x] = op = 1? R-l + 1:0; misum [x] = op =-1? -R + L-1: 0; sum [x] = (R-l + 1) * op; ox [x] = 0;} void Fox (int x) {If (COV [x]) {cov [x] * =-1; sum [x] * =-1; ox [x] = 0 ;} else {ox [x] ^ = 1; sum [x] * =-1;} int T = mxsum [X]; mxsum [x] =-misum [x]; misum [x] =-T;} void cut (int l, int R, int X) {int M = (L + r)> 1; if (COV [x]) {ope (COV [X], left); ope (COV [X], right); cov [x] = 0 ;} if (OX [x]) {Fox (x <1); Fox (x <1 | 1); ox [x] = 0 ;}} void pushup (int x) {sum [x] = sum [x <1] + sum [x <1 | 1]; misum [X] = min (misum [x <1], sum [x <1] + misum [x <1 | 1]); mxsum [x] = max (mxsum [x <1], mxsum [x <1 | 1] + sum [x <1]); /******* maxsum is only used to flip the data, and the parentheses that are inconsistent with each other will match, maxsum [x <1 | 1] + sum [x <1] has done this step in advance. * ***/} Void Update (INT op, int L, int R, int L, int R, int X) {If (L <= L & R <= r) {If (OP =-1) {ope (-1, L, R, X ); ox [x] = 0;} If (OP = 1) {ope (1, L, R, x); ox [x] = 0 ;} if (OP = 2) Fox (x); return;} Cut (L, R, x); int M = (L + r)> 1; if (L <= m) Update (OP, L, R, left); If (r> m) Update (OP, L, R, right ); pushup (x);} void query (Int & Mi, Int & all, int L, int R, int L, int R, int X) {If (L <= L & R <= r) {mi = misum [X]; All = sum [X]; return;} Cut (L, R, X); int M = (L + r)> 1, Mi1 = 0, mi2 = 0, al1 = 0, Al = 0; If (L <= m) query (Mi1, al1, L, R, left); If (r> m) query (mi2, Al, L, R, right); All = al1 + Al; mi = min (Mi1, al1 + mi2);} void Init () {memset (COV, 0, sizeof (COV); memset (OX, 0, sizeof (OX); memset (sum, 0, sizeof (SUM); memset (misum, 0, sizeof (misum); memset (mxsum, 0, sizeof (mxsum);} int main (void) {int I, n, T, M, L, R, all, mi, I = 1; char ord [10], OP; scanf ("% d", & T); While (t --) {Init (); Scanf ("% d % s", & N, Sec); for (I = 0; SEC [I]; I ++) {If (SEC [I] = '(') Update (1, I, I, 0, n-1, 1); else Update (-1, I, I, 0, n-1, 1);} scanf ("% d", & M); printf ("case % d: \ n", I ++); While (M --) {scanf ("% s", ORD); If (! Strcmp (ORD, "query") {scanf ("% d", & L, & R); MI = All = 0; query (MI, all, l, r, 0, n-1, 1); If (MI = 0 & all = 0) puts ("yes"); else puts ("no ");} if (! Strcmp (ORD, "set") {scanf ("% d", & L, & R); OP = getchar (); While (op! = '(' & Op! = ') OP = getchar (); If (OP =' (') Update (1, L, R, 0, n-1, 1 ); else Update (-1, L, R, 0, n-1, 1);} If (! Strcmp (ORD, "reverse") {scanf ("% d", & L, & R); update (2, L, R, 0, n-1, 1) ;}} printf ("\ n") ;}return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.