HDU 3397 sequence operation (line segment tree, segment update, interval merge)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3397

Sequence operation Time Limit: 10000/5000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 5801 accepted submission (s): 1713


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

It means you do not want to see the line segment tree any more> _ <

Question:

0 a B: Set [a, B] to 0;

1 a B: Set [a, B] to 1;

2 a B: perform an exception or operation on the range [a, B;

3 a B: query the number of 1 in the interval [a, B;

4 a B: ask how long the longest continuous 1 in the interval [a, B] is.

Analysis:

Set "0/1" to "Update lazy" in a simple segment. Then, you can ask how many "1" is a sum and maintain the sum value in the interval. The longest continuous 1 is a merge of intervals. We need to maintain three values, the longest continuous 1 starting from the left vertex of the interval, and the longest contact 1 starting from the right vertex, the maximum number of consecutive nodes in the interval is 1. When maintaining the parent node, pay attention to whether the left and right children of the parent node are consecutive.

An exception or operation is complicated. We need to maintain not only information of 1, but also information of 0. If the values in this range are the same (setv! =-1), then setv ^ = 1 and the value of 0/1 information is exchanged; otherwise, XOR ^ = 1 and the value of 0/1 information is exchanged. Pushdown is troublesome. If setv [k]! =-1 (k is the current node, LC is the left child, RC is the right child), then the differences or operations before the two children will be overwritten, so the next step is setv [K], and the XOR mark of the two children is cleared; If XOR [k] = 1, the two children will exchange 0/1 of their own information and pass the XOR to the left and right children.


# Include <cstdio> # include <iostream> # include <cstdlib> # include <algorithm> # include <ctime> # include <cctype> # include <cmath> # include <string> # include <cstring> # include <stack> # include <queue> # include <list> # include <vector> # include <map> # include <set> # define sqr (x) (x) * (x) # define ll long # define ITN int # define INF 0x3f3f3f3f # define PI 3.1415926535897932384626 # define EPS 1e-10 # define maxm # define maxn limit 7us Ing namespace STD; int XOR [maxn <2], setv [maxn <2], lm1 [maxn <2], RM1 [maxn <2], lm0 [maxn <2], rm0 [maxn <2], sum [maxn <2], msum1 [maxn <2], msum0 [maxn <2]; inline void swap (int K, int L, int R) {swap (lm1 [K], lm0 [k]); swap (RM1 [K], rm0 [k]); swap (msum1 [K], msum0 [k]); sum [k] = R-l-sum [k];} inline void pushup (int K, int l, int R) {int lc = K * 2 + 1, Rc = K * 2 + 2, M = L + r> 1; sum [k] = sum [LC] + sum [RC]; If (lm1 [LC] = M-l) lm1 [k] = lm1 [LC] + lm1 [RC]; Else lm1 [k] = lm1 [LC]; If (RM1 [RC] = r-m) RM1 [k] = Rm1 [RC] + RM1 [LC]; else RM1 [k] = Rm1 [RC]; msum1 [k] = max (RM1 [LC] + lm1 [RC], max (msum1 [LC], msum1 [RC]); If (lm0 [LC] = M-l) lm0 [k] = lm0 [LC] + lm0 [RC]; else lm0 [k] = lm0 [LC]; If (rm0 [RC] = r-m) rm0 [k] = rm0 [RC] + rm0 [LC]; else rm0 [k] = rm0 [RC]; msum0 [k] = max (rm0 [LC] + lm0 [RC], max (msum0 [LC], msum0 [RC]);} inline void Pushdown (int K, int L, int R) {int lc = K * 2 + 1, Rc = K * 2 + 2, M = L + r> 1; if (Setv [k]! =-1) {setv [LC] = setv [RC] = setv [k]; XOR [LC] = XOR [RC] = 0; lm1 [LC] = Rm1 [LC] = msum1 [LC] = sum [LC] = setv [k]? M-l: 0; lm0 [LC] = rm0 [LC] = msum0 [LC] = setv [k]? 0: M-l; lm1 [RC] = Rm1 [RC] = msum1 [RC] = sum [RC] = setv [k]? R-M: 0; lm0 [RC] = rm0 [RC] = msum0 [RC] = setv [k]? 0: r-m; setv [k] =-1;} If (XOR [k]) {If (setv [LC]! =-1) setv [LC] ^ = 1; else XOR [LC] ^ = 1; if (setv [RC]! =-1) setv [RC] ^ = 1; else XOR [RC] ^ = 1; swap (LC, l, m); swap (RC, M, R ); XOR [k] = 0 ;}} void Update (int A, int B, int V, int K, int L, int R) {If (B <= L | r <= A) return; if (a <= L & R <= B) {setv [k] = V; lm1 [k] = Rm1 [k] = msum1 [k] = sum [k] = V? R-l: 0; lm0 [k] = rm0 [k] = msum0 [k] = V? 0: R-l; XOR [k] = 0;} else {Pushdown (K, L, R); int M = L + r> 1; update (, b, V, K * 2 + 1, l, m); Update (a, B, V, K * 2 + 2, M, R); pushup (k, l, r) ;}} void change (int A, int B, int K, int L, int R) {If (B <= L | r <= A) return; if (A <= L & R <= B) {If (setv [k]! =-1) setv [k] ^ = 1; else XOR [k] ^ = 1; swap (K, L, R);} else {Pushdown (k, l, r); int M = L + r> 1; change (a, B, K * 2 + 1, l, m); Change (A, B, K * 2 + 2, M, R); pushup (K, L, R) ;}} int query_sum (int A, int B, int K, int L, int R) {If (B <= L | r <= A) return 0; if (a <= L & R <= B) Return sum [k]; if (R-l! = 1) Pushdown (K, L, R); int M = L + r> 1, V1 = 0, V2 = 0; V1 = query_sum (A, B, K * 2 + 1, l, m); v2 = query_sum (a, B, K * 2 + 2, M, R); Return V1 + V2 ;} int query_lc1 (itn a, int B, int K, int L, int R) {If (B <= L | r <= A) return 0; if (A <= L & R <= B) Return msum1 [k]; If (R-l! = 1) Pushdown (K, L, R); int M = L + r> 1, V1 = 0, V2 = 0, V3 = 0; V1 = query_lc1 (, b, K * 2 + 1, l, m); v2 = query_lc1 (a, B, K * 2 + 2, M, R); V3 = min (B, m + lm1 [K * 2 + 2])-max (A, m-rm1 [K * 2 + 1]); Return max (V3, max (V1, V2 ));} int main () {# ifndef online_judge freopen ("/home/fcbruce/documentation/code/T", "r", stdin); # endif/online_judge int t_t, n, m, X, A, B, OP; scanf ("% d", & t_t); While (t_t --) {scanf ("% d", & N, & M); For (INT I = 0; I <n; I ++) {scanf ("% d", & X); Update (I, I + 1, x, 0, 0, n) ;}while (M --) {scanf ("% d", & OP, & A, & B ); if (OP = 0) {update (A, B +, n); continue;} If (OP = 1) {update (A, B +, 0, 0, n); continue;} If (OP = 2) {change (A, B + 1, 0, n); continue;} If (OP = 3) {printf ("% d \ n", query_sum (A, B +, 0, 0, n); continue;} If (OP = 4) {printf ("% d \ n", query_lc1 (A, B +, 0, 0, n); Continue ;}} return 0 ;}


HDU 3397 sequence operation (line segment tree, segment update, interval merge)

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.