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.
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 Input
1
10
2
1 5 2
5 9 3
Sample Output
Case 1: The total value of the hook is 24.题意:一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2,铜为1),最初可当做全为铜;最后求这条线段的总价值。题解:简单的线段树的区间更新。
1 #include<cstdio>
2 #include<cstring>
3 #include<cmath>
4 #include<iostream>
5 #include<algorithm>
6 using namespace std;
7 const int MAXN=2e5+10;
8 typedef long long ll;
9 #define lson l,m,i<<1
10 #define rson m+1,r,i<<1|1
11 typedef struct Node
12 {
13 ll l,r;
14 ll mid()
15 {
16 return (l+r)/2.0;
17 }
18 ll value;
19 } Node;
20 Node node[MAXN<<2];
21 ll sum[MAXN<<2];//用来记录每个节点的值(区间和)
22 ll add[MAXN<<2];//延迟标记数组
23 void push_up(ll i)
24 {
25 sum[i]=sum[i<<1]+sum[i<<1|1];//该节点的和等于左儿子加幼儿子的和
26 }
27 void Build(ll l,ll r,ll i)
28 {
29 node[i].l=l;
30 node[i].r=r;
31 node[i].value=0;
32 sum[i]=0;
33 add[i]=0;
34 if(l==r)
35 {
36 sum[i]=1;
37 node[i].value=1;
38 return ;
39 }
40 ll m=node[i].mid();
41 Build(lson);//建立左子树
42 Build(rson);//建立右子树
43 push_up(i);
44 }
45 ll M;
46 void Push_down(ll i,ll len)//延迟标记下压
47 {
48 if(add[i])
49 {
50 add[i<<1]=add[i];//把该节点的标记赋给他的左儿子 51 add[i<<1|1]=add[i];//把该节点的标记赋给他的右儿子
52 sum[i<<1]=add[i]*(len-(len>>1));//把左儿子更新
53 sum[i<<1|1]=add[i]*(len>>1);//把右儿子更新
54 add[i]=0;//把该节点的延迟标记删除
55 }
56 }
57
58 void update(ll l,ll r,ll i,ll v)
59 {
60 if(node[i].r==r&&node[i].l==l)
61 {
62 add[i]=v;//延迟标记
63 sum[i]=v*(r-l+1);//给该节点赋值
64 return;
65 }
66 ll m=node[i].mid();
67 Push_down(i,node[i].r-node[i].l+1);//来判断节点是否有延迟标记,若有则更新
68 if(r<=m)
69 update(l,r,i<<1,v);//查询区间在该节点的左子树上 70 else
71 {
72 if(l>m)
73 update(l,r,i<<1|1,v);///查询区间在该节点的右子树上
74
75 else
76 {
77 update(l,m,i<<1,v);//查询区间在该节点的两颗树上
78 update(m+1,r,i<<1|1,v);
79 }
80 }
81 push_up(i);//归的第一步,向上更新
82 }
83 int main()
84 {
85 ll m,n,a,b,T,c;
86 ll flag=0;
87 scanf("%lld",&T);
88 while(T--)
89 {
90 sum[0]=0;
91
92 scanf("%lld%lld",&m,&n);
93 Build(1,m,1);//建树
94 flag++;
95 while(n--)
96 {
97
98 scanf("%lld%lld%lld",&a,&b,&c);
99 update(a,b,1,c);//更新树
100 }
101 printf("Case %lld: The total value of the hook is %lld.\n",flag,sum[1]);//sum[1],表示根节点的值即区间的和
102 }
103 return 0;
104 }