hdu just a hook(线段树,区间修改)
Just a Hook
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 24102 Accepted Submission(s): 12063
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.
![](http://acm.hdu.edu.cn/data/images/C116-1010-1.JPG)
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.
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.
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.
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.
一开始忘记了r--,l--
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <cstring> 6 using namespace std; 7 const int Max=100000+3; 8 int segTree[Max<<2]; 9 int change[Max<<2]; 10 int T,q,r,l,z,n; 11 int PushUp(int node) //回溯,节点更新 12 { 13 segTree[node]=(segTree[node<<1]+segTree[node<<1|1]); 14 return 0; 15 } 16 int PushDown(int node,int arange) 17 { 18 if(change[node]){ 19 change[node<<1]=change[node<<1|1]=change[node]; 20 segTree[node<<1]=change[node]*(arange-(arange>>1)); 21 segTree[node<<1|1]=change[node]*(arange>>1); 22 change[node]=0; 23 } 24 return 0; 25 } 26 void build(int node,int begin,int end) 27 { 28 segTree[node]=1; 29 change[node]=0; 30 if(begin==end) 31 return; 32 int m=(begin+end)>>1; 33 build(node<<1,begin,m); 34 build(node<<1|1,m+1,end); 35 PushUp(node); 36 return; 37 } 38 int updata(int node,int begin,int end) 39 { 40 if(l<=begin&&end<=r) 41 { 42 segTree[node]=z*(end-begin+1); 43 change[node]=z; 44 return 0; 45 } 46 PushDown(node,end-begin+1); 47 int mid=(begin+end)>>1; 48 if(mid>=l) updata(node<<1,begin,mid); 49 if(mid<r) updata(node<<1|1,mid+1,end); 50 PushUp(node); 51 } 52 int main() 53 { 54 int i,j; 55 freopen("in.txt","r",stdin); 56 cin>>T; 57 int k=T; 58 while(T--) 59 { 60 scanf("%d%d",&n,&q); 61 build(1,0,n-1); 62 for(i=0;i<q;i++) 63 { 64 scanf("%d%d%d",&l,&r,&z); 65 l--,r--; 66 updata(1,0,n-1); 67 } 68 printf("Case %d: The total value of the hook is %d.\n",k-T,segTree[1]); 69 } 70 }
更简单的代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 using namespace std; 3 #define maxN 100005 4 #define m ((L+R)>>1) 5 int T, N, Q, X, Y, Z, i; 6 int val[4 * maxN]; 7 void push_down(int o){ 8 val[o * 2] = val[o * 2 + 1] = val[o]; 9 val[o] = 0; 10 } 11 void update(int o, int L, int R){ 12 if (X <= L&&R <= Y) val[o] = Z; 13 else{ 14 if (val[o]) push_down(o); 15 if (X <= m) update(o * 2, L, m); 16 if (Y > m) update(o * 2 + 1, m + 1, R); 17 } 18 } 19 int sum(int o,int L,int R){ 20 if (val[o]) return (R-L+1)*val[o]; 21 return sum(o * 2, L, m) + sum(o * 2 + 1, m + 1, R); 22 } 23 int main(){ 24 scanf("%d", &T); 25 for (int Case = 1; Case <= T; Case++){ 26 val[1] = 1; 27 scanf("%d%d", &N, &Q); 28 for (i = 0; i < Q; i++){ 29 scanf("%d%d%d", &X, &Y, &Z); 30 update(1, 1, N); 31 } 32 printf("Case %d: The total value of the hook is %d.\n",Case,sum(1,1,N)); 33 } 34 return 0; 35 }