http://acm.hdu.edu.cn/showproblem.php?pid=1698
Problem DescriptionIn 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.
Inputthe 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.
Outputfor, print a number in a line representing the total value of the hook after the operations. Use the format in the example.
Sample Input11021 5 25 9 3
Sample outputcase 1:the Total value of the hook is 24. The main topic: there is a stick, now to give him a layer of metal plating, (itself this stick is copper) the value of copper is 1 silver is worth 2 gold is the value of 3 to seek the final value is how much. Analysis: Use a variable e to indicate the value of this segment of the node if the node has two kinds of metal let e=-1;
#include <stdio.h>#include<math.h>#include<algorithm>#include<iostream>#include<string.h>#include<stdlib.h>using namespacestd;#defineN 101000#defineLson r<<1#defineRson r<<1|1structnode{intl,r,e; BOOL is; intmid () {return(L+R)/2; } intLen () {returnr-l+1; }}a[n*4];voidBuildtree (intRintLintR) {A[r]. L=m; A[R]. R=s; A[R].E=1; if(l==R) {return; } buildtree (Lson,l,a[r].mid ()); Buildtree (Rson,a[r].mid ()+1, R);}intQurry (intR) { if(a[r].e!=-1) returnA[r].len () *A[R].E; Else returnQurry (Lson) +Qurry (Rson);}voidUpdate (intRintLintRinte) { if(A[r]. L==l && A[r]. r==R) {a[r].e=e; return; } if(a[r].e!=-1) {A[LSON].E=a[rson].e=A[R].E; } A[R].E=-1; if(r<=A[r].mid ()) Update (lson,l,r,e); Else if(l>A[r].mid ()) Update (rson,l,r,e); Else{Update (Lson,l,a[r].mid (), E); Update (Rson,a[r].mid ()+1, r,e); }}intMain () {intt,n,m,i,t,x,y,e; scanf ("%d",&T); T=1; while(t--) {scanf ("%d",&N); Buildtree (1,1, N); scanf ("%d",&m); for(i=1; i<=m;i++) {
scanf (" %d%d%d",&x,&y,&e); Update (1, x,y,e); } printf ("Case%d:the Total value of the hook is%d.\n", T++,qurry (1)); } return 0;}
Just a hook-hdu1698 (segment tree to find interval)