標籤:des style blog http color java os io
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17214 Accepted Submission(s): 8600
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 up of several consecutive metallic sticks which are of the same length.
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks 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 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.
You may consider the original hook is 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 are no more than 10 cases.
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an 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: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
OutputFor each case, 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 一道線段樹區間更新的題目這種題目,剛開始做的時候想單點修改,結果單點修改會逾時。所以採用了區間修改。這裡用了延時標記,也就是說,當我查詢的區間滿足當前區間的條件的我就只更新我當前的區間,給子區間打標記,當下次詢問時再更新,否則每次更新區間遍曆整棵樹會逾時。
1 #include<cstdio> 2 #include<cstring> 3 #include<stdlib.h> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN=100000+10; 7 struct node 8 { 9 int l,r;10 int num;11 int col;12 int mid()13 {14 return (l+r)/2;15 }16 }a[MAXN*4];17 18 void pushup(int step)19 {20 a[step].num=a[step*2].num+a[step*2+1].num;21 }22 23 int pushdown(int x,int step)24 {25 if(a[step].col)26 {27 a[step*2].col=a[step*2+1].col=a[step].col;28 a[step*2].num=(x-x/2)*a[step].col;29 a[step*2+1].num=(x/2)*a[step].col;30 a[step].col=0;31 }32 }33 34 void btree(int l,int r,int step)35 {36 a[step].l=l;37 a[step].r=r;38 a[step].col=0;39 if(l==r)40 {41 a[step].num=1;42 return ;43 }44 int mid=a[step].mid();45 btree(l,mid,step*2);46 btree(mid+1,r,step*2+1);47 pushup(step);48 }49 50 void ptree(int l,int r,int val,int step)51 {52 if(l<=a[step].l&&a[step].r<=r)53 {54 a[step].col=val;55 a[step].num=(a[step].r-a[step].l+1)*val;56 return ;57 }58 pushdown(a[step].r-a[step].l+1,step);59 int mid=a[step].mid();60 if(l>mid)61 ptree(l,r,val,step*2+1);62 else if(r<=mid)63 ptree(l,r,val,step*2);64 else65 {66 ptree(l,r,val,step*2);67 ptree(l,r,val,step*2+1);68 }69 pushup(step);70 }71 int main()72 {73 int kase,cnt=0,ans;74 scanf("%d",&kase);75 while(kase--)76 {77 int n,Q;78 scanf("%d %d",&n,&Q);79 btree(1,n,1);80 while(Q--)81 {82 int x,y,z;83 scanf("%d %d %d",&x,&y,&z);84 ptree(x,y,z,1);85 }86 printf("Case %d: The total value of the hook is %d.\n",++cnt,a[1].num);87 }88 return 0;89 }View Code