Just a Hook
Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 18378 Accepted Submission (s): 9213
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.
Source2008 "Sunline Cup" National Invitational Contest
Recommendwangye
Segment Tree function : Update: segment Substitution ( because only the total interval of the query, you can directly output 1 node information )
1#include <cstdio>2#include <algorithm>3 4 #defineLson l,m,rt<<15 #defineRson m+1,r,rt<<1|16 7 Const intmaxn=1e5+3511;8 using namespacestd;9 Ten intsum[maxn<<2],lazy[maxn<<2]; One A voidPushup (intRT) - { -sum[rt]=sum[rt<<1]+sum[rt<<1|1]; the } - - voidBuildintLintRintRT) - { + intm; -lazy[rt]=0; +sum[rt]=1; A at if(l==r) { - return ; - } - -M= (l+r) >>1; - build (Lson); in build (Rson); - pushup (RT); to } + - voidPushdown (intRtintm) the { * if(Lazy[rt]) { $lazy[rt<<1]=lazy[rt<<1|1]=Lazy[rt];Panax Notoginsengsum[rt<<1]= (M-(m>>1))*Lazy[rt]; -sum[rt<<1|1]= (m>>1)*Lazy[rt]; thelazy[rt]=0; + } A } the + voidUpdata (intLintRintCintLintRintRT) - { $ intm; $ - if(L<=l && r<=R) { -lazy[rt]=C; theSum[rt]= (r-l+1)*C; - return;Wuyi } the -Pushdown (rt,r-l+1); WuM= (l+r) >>1; - if(l<=m) updata (L,r,c,lson); About if(r>m) updata (L,r,c,rson); $ pushup (RT); - } - - intMain () A { + intCas,t,q,x,y,z,n; the -scanf"%d",&T); $ for(intcas=1; cas<=t;cas++) { thescanf"%d",&n); theBuild1N1); thescanf"%d",&Q); the while(q--) { -scanf"%d%d%d",&x,&y,&z); inUpdata (x, Y, Z,1N1); the the } About theprintf"Case%d:the Total value of the hook is%d.\n", cas,sum[1]); the } the + return 0; -}
[HDU] 1698 Just a Hook [segment tree interval substitution]