HDU- 1698 Just a Hook
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 13918 Accepted Submission(s): 6886
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 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.
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.
Input
The 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.
Output
For 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 Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.
1 #include<stdio.h> 2 #define maxn 500004 3 struct node 4 { 5 int left,right; 6 int num; 7 }; 8 node tree[3*maxn]; 9 void build(int left,int right,int i) 10 { 11 tree[i].left =left; 12 tree[i].right =right; 13 tree[i].num =1; 14 if(tree[i].left ==tree[i].right ) 15 return ; 16 build(left,(left+right)/2,2*i); 17 build((left+right)/2+1,right,2*i+1); 18 } 19 void update(int r,int i,int j,int k) 20 { 21 int mid; 22 //if(tree[r].num==k) 23 // return ;//注意如果不要更新就直接返回。细节。 24 mid=(tree[r].left+tree[r].right)/2; 25 if(tree[r].left==i&&tree[r].right==j) 26 { 27 tree[r].num=k; 28 return ; 29 } 30 if(tree[r].num!=-1) 31 { 32 tree[2*r+1].num=tree[2*r].num=tree[r].num; 33 tree[r].num=-1; 34 } 35 //如果更新区域在右边 36 if(i>mid) 37 update(2*r+1,i,j,k); 38 //如果更细区域在左边 39 else if(j<=mid) 40 update(r*2,i,j,k); 41 else 42 { 43 update(2*r,i,mid,k); 44 update(2*r+1,mid+1,j,k); 45 } 46 } 47 int sum(int r) 48 { 49 if(tree[r].num!=-1) 50 return (tree[r].right-tree[r].left+1)*tree[r].num; 51 else 52 return sum(r*2)+sum(2*r+1); 53 } 54 int main() 55 { 56 int t,n,q,i,a,b,val,s,j; 57 while(~scanf("%d",&t)) 58 { 59 for(j=1;j<=t;j++) 60 { 61 s=0; 62 scanf("%d%d",&n,&q); 63 build(1,n,1); 64 for(i=0;i<q;i++) 65 { 66 scanf("%d%d%d",&a,&b,&val); 67 update(1,a,b,val); 68 } 69 70 printf("Case %d: The total value of the hook is %d.\n",j,sum(1)); 71 } 72 } 73 return 0; 74 } 75 76 77 78 79 80