HDU 3379 Sequence operation (segment update, interval merge)

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=3397


The line tree is a good question. The knowledge points involved: The lazy operation handles XOR operation and 01, interval merging.

There are five types of operations:

0 A B changes [A, a] to 0

1 A B changes [A, a] to 1

2 A B takes [A, a, a] counter

3 a B output [A, a] 1 of the number

4 a B output [A, a] maximum number of consecutive 1

The operation of the interval is similar to the POJ 3225. Set 01 and the inverse is mutually exclusive, a period can only have a mark, discuss inside also said to take the reverse when recursion to the interval is all 0 or 1 when the reverse violates the nature of the line segment tree. Therefore, for these two operations to set a variable lazy, its value has -1,0,1,2,-1 to indicate that there is no operation, 0 and 1 are covered by 0 or 1, 2 indicates the interval inversion. Because two times after the equivalent of no take, the lazy tag here to play a big role, to take the inverse of this interval, just put its lazy to 2, the next time to take the inverse of the original, that is, 1.


when asking for the number of 1 in the interval, only sum of maintenance intervals is required. For a continuous length of 1, interval merging is required, and the number of consecutive 0 in the interval is also recorded because of the inverse operation.


#include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> # Include <stack> #include <vector> #include <math.h> #include <string.h> #include <queue> # Include <string> #include <stdlib.h> #include <algorithm>//#define LL Long long #define LL __int64 #def
INE EPS 1e-12 #define PI acos ( -1.0) using namespace std;
const int INF = 0X3F3F3F3F;

const int MAXN = 100010;
/* Nodes have the following information: lazy: operation or coverage in the tag interval, sum: Interval and ll[2] indicates the number of consecutive 0 and 1 from the left, rr[2] represents the number of consecutive 0 and 1 from the right, LONG0, long1, respectively, represent the longest consecutive 0 and 1 lengths.
	*/struct Node {int l,r;
	int lazy;
	int sum;
	int ll[2],rr[2];
int LONG1,LONG0;

}TREE[MAXN*4];
int A[MAXN];
	/* Update up, update the parent node's and and ll[],rr[],long0,long1 */void push_up (int v) {int L = Tree[v].l,r = TREE[V].R;
	int ls = V*2,rs = v*2+1;

	int mid = (l+r) >>1;
	Tree[v].sum = Tree[ls].sum + tree[rs].sum;
	Tree[v].ll[0] = tree[ls].ll[0];
	if (tree[ls].sum = = 0) tree[v].ll[0] + = tree[rs].ll[0]; TREE[V].LL[1] = Tree[ls].ll[1];

	if (tree[ls].sum = = mid-l+1) tree[v].ll[1] + + tree[rs].ll[1];
	Tree[v].rr[0] = tree[rs].rr[0];
	if (tree[rs].sum = = 0) tree[v].rr[0] + = tree[ls].rr[0];
	TREE[V].RR[1] = tree[rs].rr[1];

	if (tree[rs].sum = = R-mid) tree[v].rr[1] + + tree[ls].rr[1];
	TREE[V].LONG0 = Max (max (TREE[LS].LONG0,TREE[RS].LONG0), tree[ls].rr[0]+tree[rs].ll[0]);
TREE[V].LONG1 = Max (max (tree[ls].long1,tree[rs].long1), tree[ls].rr[1]+tree[rs].ll[1]); }/* Down update, lazy to 0 or 1 o'clock directly overwrite the sub-range, for 2 o'clock to see whether the sub-range is completely covered, if it is directly reversed, otherwise the lazy of the child node/void Push_down (int v) {if (tree[v].l = = TREE[V].R | | tr
	Ee[v].lazy = =-1) return;
	int L = Tree[v].l,r = TREE[V].R;
	int ls = V*2,rs = v*2+1;
	int mid = (L + r) >>1;
		if (Tree[v].lazy = = 0) {tree[ls].lazy = Tree[rs].lazy = 0;
		tree[ls].sum = tree[rs].sum = 0;
		tree[ls].long1 = tree[rs].long1 = 0;
		TREE[LS].LL[1] = tree[ls].rr[1] = tree[rs].ll[1] = tree[rs].rr[1] = 0;
		Tree[ls].ll[0] = tree[ls].rr[0] = TREE[LS].LONG0 = mid-l+1; Tree[rs].ll[0] = tree[rs].rr[0] = Tree[rS].LONG0 = R-mid;
		} else if (Tree[v].lazy = = 1) {Tree[ls].lazy = Tree[rs].lazy = 1;
		Tree[ls].sum = mid-l+1;
		Tree[rs].sum = R-mid;
		TREE[LS].LONG0 = TREE[RS].LONG0 = 0;
		Tree[ls].ll[0] = tree[ls].rr[0] = tree[rs].ll[0] = tree[rs].rr[0] = 0;
		TREE[LS].LL[1] = tree[ls].rr[1] = Tree[ls].long1 = mid-l+1;
	TREE[RS].LL[1] = tree[rs].rr[1] = Tree[rs].long1 = R-mid;
			} else if (Tree[v].lazy = = 2) {if (Tree[ls].lazy = =-1 | | tree[ls].lazy = = 2) {tree[ls].lazy = 1-tree[ls].lazy;
			Tree[ls].sum = Mid-l+1-tree[ls].sum;
			Swap (TREE[LS].LONG0,TREE[LS].LONG1);
			Swap (tree[ls].ll[0],tree[ls].ll[1]);
		Swap (tree[ls].rr[0],tree[ls].rr[1]);
			} else if (Tree[ls].lazy = = 1) {tree[ls].lazy = 0;
			tree[ls].sum = 0;
			TREE[LS].LONG0 = tree[ls].ll[0] = tree[ls].rr[0] = mid-l+1;
		Tree[ls].long1 = tree[ls].ll[1] = tree[ls].rr[1] = 0;
			} else if (Tree[ls].lazy = = 0) {tree[ls].lazy = 1;
			Tree[ls].sum = mid-l+1;
TREE[LS].LONG0 = tree[ls].ll[0] = tree[ls].rr[0] = 0;			Tree[ls].long1 = tree[ls].ll[1] = tree[ls].rr[1] = mid-l+1;
			} if (Tree[rs].lazy = =-1 | | tree[rs].lazy = = 2) {tree[rs].lazy = 1-tree[rs].lazy;
			Tree[rs].sum = R-mid-tree[rs].sum;
			Swap (TREE[RS].LONG0,TREE[RS].LONG1);
			Swap (tree[rs].ll[0],tree[rs].ll[1]);
		Swap (tree[rs].rr[0],tree[rs].rr[1]);
			} else if (Tree[rs].lazy = = 1) {tree[rs].lazy = 0;
			tree[rs].sum = 0;
			TREE[RS].LONG0 = tree[rs].ll[0] = tree[rs].rr[0] = R-mid;
		Tree[rs].long1 = tree[rs].ll[1] = tree[rs].rr[1] = 0;
			} else if (Tree[rs].lazy = = 0) {tree[rs].lazy = 1;
			Tree[rs].sum = R-mid;
			TREE[RS].LONG0 = tree[rs].ll[0] = tree[rs].rr[0] = 0;
		Tree[rs].long1 = tree[rs].ll[1] = tree[rs].rr[1] = R-mid;
}} Tree[v].lazy =-1;
	} void Build (int v, int l, int r) {tree[v].l = l;
	TREE[V].R = R;
	Tree[v].lazy =-1;
	tree[v].sum = 0;
	Tree[v].ll[0] = tree[v].rr[0] = TREE[V].LONG0 = 0;
	TREE[V].LL[1] = tree[v].rr[1] = tree[v].long1 = 0;
		if (L = = r) {tree[v].sum = a[l]; if (a[L] = = 0) tree[v].ll[0] = tree[v].rr[0] = TREE[V].LONG0 = 1;
		else tree[v].ll[1] = tree[v].rr[1] = tree[v].long1 = 1;
	Return
	} int mid = (l+r) >>1;
	Build (V*2,l,mid);
	Build (V*2+1,mid+1,r);
Push_up (v); } void Update (int v, int l, int r, int op) {if (TREE[V].L = = L && TREE[V].R = r) {if (op = = 0) {tree[v
			].lazy = tree[v].sum = 0;
			Tree[v].ll[0] = tree[v].rr[0] = TREE[V].LONG0 = tree[v].r-tree[v].l + 1;
		TREE[V].LL[1] = tree[v].rr[1] = tree[v].long1 = 0;
			} else if (op = = 1) {tree[v].lazy = 1;
			Tree[v].sum = tree[v].r-tree[v].l + 1;
			TREE[V].LL[1] = tree[v].rr[1] = tree[v].long1 = tree[v].r-tree[v].l + 1;
		Tree[v].ll[0] = tree[v].rr[0] = TREE[V].LONG0 = 0;
				} else if (op = = 2) {if (Tree[v].lazy = = 0) {tree[v].lazy = 1;
				Tree[v].sum = tree[v].r-tree[v].l + 1;
				TREE[V].LL[1] = tree[v].rr[1] = Tree[v].long1 = tree[v].r-tree[v].l+1;
			Tree[v].ll[0] = tree[v].rr[0] = TREE[V].LONG0 = 0; } else if (Tree[v]. Lazy = = 1) {tree[v].lazy = Tree[v].sum = 0;
				TREE[V].LL[1] = tree[v].rr[1] = tree[v].long1 = 0;
			Tree[v].ll[0] = tree[v].rr[0] = TREE[V].LONG0 = tree[v].r-tree[v].l+1;
				} else if (Tree[v].lazy = =-1 | | tree[v].lazy = = 2) {tree[v].lazy = 1-tree[v].lazy;
				Tree[v].sum = Tree[v].r-tree[v].l+1-tree[v].sum;
				Swap (TREE[V].LONG0,TREE[V].LONG1);
				Swap (tree[v].ll[0],tree[v].ll[1]);
			Swap (tree[v].rr[0],tree[v].rr[1]);
	}} return;
	} push_down (v);
	int mid = (tree[v].l + tree[v].r) >> 1;
	if (r <= mid) update (V*2,L,R,OP);
	else if (L > Mid) update (V*2+1,L,R,OP);
		else {update (V*2,L,MID,OP);
	Update (V*2+1,MID+1,R,OP);
} push_up (v); } int query (int v, int l, int r,int op) {if (TREE[V].L = = L && TREE[V].R = = r) {if (op = = 3) return Tree[v
		].sum;
	else return tree[v].long1;
	} push_down (v);
	int mid = (tree[v].l + tree[v].r) >> 1;
	if (R <= mid) return query (V*2,L,R,OP); else if (L > Mid) return querY (V*2+1,L,R,OP);
		else {if (op = = 3) return query (V*2,L,MID,OP) + query (V*2+1,MID+1,R,OP);
			else {//the longest continuous in the interval [l,r] is the length of 1.
			int tmp = MIN (tree[v*2].rr[1],mid-l+1) + min (tree[v*2+1].ll[1],r-mid);
		Return Max (Tmp,max (Query (V*2,L,MID,OP), query (V*2+1,MID+1,R,OP)));
	}}} int main () {int test;
	int n,m;

	int op,l,r;
	scanf ("%d", &test);
		while (test--) {scanf ("%d%d", &n,&m);
		for (int i = 1; I <= n; i++) scanf ("%d", &a[i]);
		Build (1,1,n);
			while (m--) {scanf ("%d%d%d", &op,&l,&r);
			l++;
			r++;
			if (OP <= 2) {update (1,L,R,OP);
				} else {int ans = query (1,L,R,OP);
			printf ("%d\n", ans);
}}} 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.