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.
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.
题意:一个钩子有多段金属组成,初始材质为铜,即 1,进行操作将材质改为银或金(即2 或 3),求整个钩子上每一段对应的数字和。
用线段树+区间更新即可,叶子节点表示每段金属的数值,非叶子结点表示对应区间的树枝和,最终输出根节点的sum即可,在区间更新时采用lazy-tag标记(不会QAQ)。
1 #include <map> 2 #include <stack> 3 #include <queue> 4 #include <cmath> 5 #include <string> 6 #include <limits> 7 #include <cstdio> 8 #include <cstdlib> 9 #include <cstring> 10 #include <iostream> 11 #include <algorithm> 12 #define Scc(c) scanf("%c",&c) 13 #define Scs(s) scanf("%s",s) 14 #define Sci(x) scanf("%d",&x) 15 #define Sci2(x, y) scanf("%d%d",&x,&y) 16 #define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z) 17 #define Scl(x) scanf("%I64d",&x) 18 #define Scl2(x, y) scanf("%I64d%I64d",&x,&y) 19 #define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z) 20 #define Pri(x) printf("%d ",x) 21 #define Prl(x) printf("%I64d ",x) 22 #define Prc(c) printf("%c ",c) 23 #define Prs(s) printf("%s ",s) 24 #define For(i,x,y) for(int i=x;i<y;i++) 25 #define For_(i,x,y) for(int i=x;i<=y;i++) 26 #define FFor(i,x,y) for(int i=x;i>y;i--) 27 #define FFor_(i,x,y) for(int i=x;i>=y;i--) 28 #define Mem(f, x) memset(f,x,sizeof(f)) 29 #define LL long long 30 #define ULL unsigned long long 31 #define MAXSIZE 100005 32 #define INF 0x3f3f3f3f 33 const int mod=1e9+7; 34 const double PI = acos(-1.0); 35 36 using namespace std; 37 struct node 38 { 39 int left,right; 40 int lazy; 41 int tag; 42 int sum; 43 } tree[MAXSIZE*4]; 44 void build(int i,int l,int r) 45 { 46 tree[i].left=l; 47 tree[i].right=r; 48 tree[i].lazy=0; 49 tree[i].tag=0; 50 if(l==r) 51 { 52 tree[i].sum=1; 53 return ; 54 } 55 int mid=(l+r)/2; 56 build(i<<1,l,mid); 57 build(i<<1|1,mid+1,r); 58 tree[i].sum=tree[i*2].sum+tree[i<<1|1].sum; 59 } 60 void Update(int i,int l,int r,int v) 61 { 62 if(tree[i].left==l&&tree[i].right==r) 63 { 64 tree[i].sum=(r-l+1)*v; 65 tree[i].tag=v; 66 tree[i].lazy=1; 67 return ; 68 } 69 int mid=(tree[i].left+tree[i].right)/2; 70 if(tree[i].lazy==1) 71 { 72 tree[i].lazy=0; 73 Update(i<<1,tree[i].left,mid,tree[i].tag); 74 Update(i<<1|1,mid+1,tree[i].right,tree[i].tag); 75 tree[i].tag=0; 76 } 77 if(r<=mid) 78 Update(i<<1,l,r,v); 79 else if(l>mid) 80 Update(i<<1|1,l,r,v); 81 else 82 { 83 Update(i<<1,l,mid,v); 84 Update(i<<1|1,mid+1,r,v); 85 } 86 tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum; 87 } 88 int main() 89 { 90 int t; 91 Sci(t); 92 int k=1; 93 while(t--) 94 { 95 int n; 96 Sci(n); 97 int m; 98 Sci(m); 99 build(1,1,n); 100 int a,b,c; 101 while(m--) 102 { 103 Sci3(a,b,c); 104 Update(1,a,b,c); 105 } 106 printf("Case %d: The total value of the hook is %d. ",k++,tree[1].sum); 107 } 108 return 0; 109 }