Sequence operation
Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 4603 accepted submission (s): 1338
Problem descriptionlxhgww got a sequence contains n characters which are all '0's or '1' S.
We have five operations here:
Change operations:
0 a B change all characters into '0's in [a, B]
1 a B change all characters into '1' s in [a, B]
2 a B change all '0' s into '1' S and change all '1' s into '0' s in [a, B]
Output operations:
3 a B output the number of '1' s in [a, B]
4 a B output the length of the longest continuous '1' string in [a, B]
Inputt (T <= 10) in the first line is the case number.
Each case has two integers in the first line: N and M (1 <= n, m <= 100000 ).
The next line contains n characters, '0' or '1' separated by spaces.
Then M lines are the operations:
Op a B: 0 <= op <= 4, 0 <= A <= B <n.
Outputfor each output operation, output the result.
Sample Input
110 100 0 0 1 1 0 1 0 1 11 0 23 0 52 2 24 0 40 3 62 3 74 2 81 0 50 5 63 3 9
Sample output
5265
Authorlxhgww & SH
Sourcehdoj monthly contest-2010.05.01
Recommendlcy is a good question. Do you understand the nature of laziness ~~ PS: this is the longest code I have written ~~~
# Include <iostream> # include <cstring> # include <cstdio> # define lson POS <1 # define rson POS <1 | 1 using namespace STD; const int maxn = 100005; int num [maxn]; struct node {int L, R; int msum1, msum0; // consecutive longest 1 and 0 int lsum1, lsum0; // up to int rsum1 and rsum0 from the left; // up to int lazy from the right; // mark int sum; // up to 1 int mid () {return (L + r)> 1 ;}}; node tree [maxn * 4]; inline void pushup (int pos) {tree [POS]. msum0 = max (tree [lson]. msum0, tree [RSO N]. msum0); tree [POS]. msum1 = max (tree [lson]. msum1, tree [rson]. msum1); tree [POS]. lsum0 = tree [lson]. lsum0; tree [POS]. lsum1 = tree [lson]. lsum1; tree [POS]. rsum0 = tree [rson]. rsum0; tree [POS]. rsum1 = tree [rson]. rsum1; tree [POS]. msum0 = max (tree [POS]. msum0, tree [lson]. rsum0 + tree [rson]. lsum0); tree [POS]. msum1 = max (tree [POS]. msum1, tree [lson]. rsum1 + tree [rson]. lsum1); int L1 = tree [lson]. r-tree [lson]. L + 1; int L2 = tree [rson]. R-tree [rson]. L + 1; if (L1 = tree [lson]. lsum0) tree [POS]. lsum0 + = tree [rson]. lsum0; If (L1 = tree [lson]. lsum1) tree [POS]. lsum1 + = tree [rson]. lsum1; If (L2 = tree [rson]. rsum0) tree [POS]. rsum0 + = tree [lson]. rsum0; If (L2 = tree [rson]. rsum1) tree [POS]. rsum1 + = tree [lson]. rsum1; tree [POS]. sum = tree [lson]. sum + tree [rson]. SUM;} inline void Pushdown (int pos) {int x = tree [POS]. lazy; If (x = 0) {tree [lson]. lazy = 0; tree [lson]. su M = 0; tree [lson]. lsum1 = tree [lson]. rsum1 = tree [lson]. msum1 = 0; tree [lson]. lsum0 = tree [lson]. rsum0 = tree [lson]. msum0 = tree [lson]. r-tree [lson]. L + 1; tree [rson]. lazy = 0; tree [rson]. sum = 0; tree [rson]. lsum1 = tree [rson]. rsum1 = tree [rson]. msum1 = 0; tree [rson]. lsum0 = tree [rson]. rsum0 = tree [rson]. msum0 = tree [rson]. r-tree [rson]. L + 1;} else if (x = 1) {tree [lson]. lazy = 1; tree [lson]. sum = tree [lson]. r-tree [lson]. L + 1; tree [lson ]. Lsum1 = tree [lson]. rsum1 = tree [lson]. msum1 = tree [lson]. r-tree [lson]. L + 1; tree [lson]. lsum0 = tree [lson]. rsum0 = tree [lson]. msum0 = 0; tree [rson]. lazy = 1; tree [rson]. sum = tree [rson]. r-tree [rson]. L + 1; tree [rson]. lsum1 = tree [rson]. rsum1 = tree [rson]. msum1 = tree [rson]. r-tree [rson]. L + 1; tree [rson]. lsum0 = tree [rson]. rsum0 = tree [rson]. msum0 = 0;} else if (x = 2) {If (tree [lson]. l! = Tree [lson]. r) Pushdown (lson); tree [lson]. lazy = 2; tree [lson]. sum = tree [lson]. r-tree [lson]. L + 1-tree [lson]. SUM; int T = tree [lson]. lsum1; tree [lson]. lsum1 = tree [lson]. lsum0; tree [lson]. lsum0 = T; t = tree [lson]. rsum1; tree [lson]. rsum1 = tree [lson]. rsum0; tree [lson]. rsum0 = T; t = tree [lson]. msum1; tree [lson]. msum1 = tree [lson]. msum0; tree [lson]. msum0 = T; If (tree [rson]. l! = Tree [rson]. r) Pushdown (rson); tree [rson]. lazy = 2; tree [rson]. sum = tree [rson]. r-tree [rson]. L + 1-tree [rson]. SUM; t = tree [rson]. lsum1; tree [rson]. lsum1 = tree [rson]. lsum0; tree [rson]. lsum0 = T; t = tree [rson]. rsum1; tree [rson]. rsum1 = tree [rson]. rsum0; tree [rson]. rsum0 = T; t = tree [rson]. msum1; tree [rson]. msum1 = tree [rson]. msum0; tree [rson]. msum0 = T;} tree [POS]. lazy =-1;} void build (int l, int R, int POS) {tree [POS]. L = L; Tree [POS]. R = r; If (L = r) {int X; scanf ("% d", & X); tree [POS]. lazy =-1; tree [POS]. msum0 =! X; tree [POS]. msum1 = x; tree [POS]. lsum0 =! X; tree [POS]. lsum1 = x; tree [POS]. rsum0 =! X; tree [POS]. rsum1 = x; tree [POS]. sum = x; return;} int mid = tree [POS]. mid (); Build (L, mid, lson); Build (Mid + 1, R, rson); tree [POS]. lazy =-1; pushup (POS);} void Update (int l, int R, int X, int POS) {If (L = tree [POS]. L & R = tree [POS]. r) {If (x = 0) {tree [POS]. lazy = 0; tree [POS]. sum = 0; tree [POS]. lsum1 = tree [POS]. rsum1 = tree [POS]. msum1 = 0; tree [POS]. lsum0 = tree [POS]. rsum0 = tree [POS]. msum0 = tree [POS]. r-tree [POS]. L + 1;} els E if (x = 1) {tree [POS]. lazy = 1; tree [POS]. sum = tree [POS]. r-tree [POS]. L + 1; tree [POS]. lsum1 = tree [POS]. rsum1 = tree [POS]. msum1 = tree [POS]. r-tree [POS]. L + 1; tree [POS]. lsum0 = tree [POS]. rsum0 = tree [POS]. msum0 = 0;} else {If (L! = R) // It can be directly overwritten when it is 1 or 0, but if it is 2, it must be updated downward, essentially ~ Because the following state changes with the above, Pushdown (POS); tree [POS]. lazy = 2; tree [POS]. sum = tree [POS]. r-tree [POS]. L + 1-tree [POS]. SUM; int T = tree [POS]. lsum1; tree [POS]. lsum1 = tree [POS]. lsum0; tree [POS]. lsum0 = T; t = tree [POS]. rsum1; tree [POS]. rsum1 = tree [POS]. rsum0; tree [POS]. rsum0 = T; t = tree [POS]. msum1; tree [POS]. msum1 = tree [POS]. msum0; tree [POS]. msum0 = T;} return;} Pushdown (POS); int mid = tree [POS]. mid (); If (r <= mid) Update (L, R, X, lson); else if (L> mid) Update (L, R, X, rson ); else {Update (L, mid, X, lson); Update (Mid + 1, R, X, rson);} pushup (POS);} int queryr (INT l, int R, int POS) {If (R-l + 1> = tree [POS]. rsum1) return tree [POS]. rsum1; else return r-L + 1;} int queryl (int l, int R, int POS) {If (R-l + 1> = tree [POS]. lsum1) return tree [POS]. lsum1; else return r-L + 1;} int query (int l, int R, int X, int POS) {If (L = tree [POS]. L & R = tree [POS]. r) {If (x = 3) return tree [POS]. SUM; else return tree [POS]. msum1 ;}pushdown (POS); int mid = tree [POS]. mid (); If (x = 3) {If (r <= mid) return query (L, R, X, lson); else if (L> mid) return query (L, R, X, rson); else return query (L, mid, X, lson) + query (Mid + 1, R, X, rson );} else {If (r <= mid) return query (L, R, X, lson); else if (L> mid) return query (L, R, X, rson ); else {int res = query (L, mid, X, lson); Res = max (Res, query (Mid + 1, R, X, rson )); res = max (Res, queryr (L, mid, lson) + queryl (Mid + 1, R, rson); Return res ;}} int main () {int t; scanf ("% d", & T); While (t --) {int n, m; scanf ("% d", & N, & M); Build (1, n, 1); int I; for (I = 0; I <m; I ++) {int A, B, X; scanf ("% d", & X, & A, & B); A ++, B ++; If (x <= 2) Update (, b, X, 1); else {int res = query (a, B, X, 1); printf ("% d \ n", Res );}}} return 0 ;}