Hdoj Just a Hook 1698 (value of segment tree transform interval sum) Good question

Source: Internet
Author: User
Just a Hook Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 23780 Accepted Submission (s): 11900


Problem Description in the game of DotA, Pudge's Meat hook is actually the most horrible thing for most of the heroes. The hook is made to several consecutive metallic sticks which is of the same length.



Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hooks from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver s Ticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each of the silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after performing the operations.
Consider the original hook is a made up of cupreous sticks.

Input the input consists of several test cases. The first line of the input is the number of the cases. There is no more than cases.
For each case, the first line contains a integer N, 1<=n<=100,000, which is the number of the sticks of Pudge ' s MEA T Hook and the second line contains a integer Q, 0<=q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=x<=y<=n, Z, 1<=z<=3, which defines an operation:c Hange the sticks numbered from X to Y into the metal kind Z, where z=1 represents the Cupreous kind, z=2 represents the SI Lver Kind and z=3 represents the golden kind.

Print a number in a line representing the total value of the hook after the operations. Use the format in the example.

Sample Input
1 10 2 1 5 2 5 9 3
Sample Output
Case 1:the total value of the hook is 24.

Test instructions

Give you a sequence with n number, and the initial value of the sequence is 1. For M operations, enter three numbers per operation, x, Y, Z. Indicates that the values in the interval x through Y are updated to Z. Finally, the sum of the n number is calculated.

#include <stdio.h> #include <string.h> #include <algorithm> #define N 100010 using namespace std;
	struct ZZ {int l;
	int R;
int m;
}q[n<<2];
	void build (int gen,int l,int r) {q[gen].l=l;
	Q[gen].r=r;
	Q[gen].m=1;
	if (l==r) return;
	int mid= (L+R) >>1;
	Build (Gen<<1,l,mid);
Build (Gen<<1|1,mid+1,r);
	The void update (int gen,int l,int R,int z) {if (q[gen].m==z)//is the same without modification, a small optimization return;
		if (q[gen].l==l&&q[gen].r==r)//Find the interval, directly update {q[gen].m=z;
	return; } if (Q[gen].m!=-1)//This block is key: If the interval has only one number {q[gen<<1].m=q[gen<<1|1].m=q[gen].m;//because of a subsequent subtree operation,
	So the value of the updated subtree is equal to the father's value q[gen].m=-1;
	}//parent interval for multiple numbers operations on all child nodes int mid= (Q[GEN].L+Q[GEN].R) >>1;
	if (l>mid) update (GEN&LT;&LT;1|1,L,R,Z);
	else if (r<=mid) update (GEN&LT;&LT;1,L,R,Z);
		else {update (GEN&LT;&LT;1,L,MID,Z);
	Update (GEN&LT;&LT;1|1,MID+1,R,Z);
	}} int query (int gen)//interval sum {if (q[gen].m!=-1)//Only one number directly calculates this interval return (q[gen].r-q[gen].l+1) *q[gen].m; Else//otherwise in the left and right sub-tree to find return query (gen<<1) +query (gen<<1|1);
	} int main () {int t,t=1;
	int n,m,i,j,k,x,y,z;
	scanf ("%d", &t);
		while (t--) {scanf ("%d", &n);
		Build (1,1,n);
		scanf ("%d", &m);
			while (m--) {scanf ("%d%d%d", &x,&y,&z);
		Update (1,X,Y,Z);
	} printf ("Case%d:the total value of the hook is%d.\n", T++,query (1));
} 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.