题目链接:https://vjudge.net/problem/HDU-1698
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.
题解:
经典的区间染色问题。
代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const double EPS = 1e-8; 15 const int INF = 2e9; 16 const LL LNF = 2e18; 17 const int MAXN = 1e5+10; 18 19 int sum[MAXN<<2], setv[MAXN<<2]; 20 21 void push_up(int u) 22 { 23 sum[u] = sum[u*2] + sum[u*2+1]; 24 } 25 26 void push_down(int u, int l, int r) 27 { 28 if(setv[u]>0) 29 { 30 setv[u*2] = setv[u*2+1] = setv[u]; 31 sum[u*2] = (r-l+1+1)/2*setv[u]; 32 sum[u*2+1] = (r-l+1)/2*setv[u]; 33 setv[u] = 0; 34 } 35 } 36 37 void build(int u, int l, int r) 38 { 39 if(l==r) 40 { 41 setv[u] = 1; //每个鱼钩初始化为金属1 42 sum[u] = 1; 43 return; 44 } 45 46 int mid = (l+r)>>1; 47 build(u*2, l, mid); 48 build(u*2+1, mid+1, r); 49 push_up(u); 50 setv[u] = 0; 51 } 52 53 void set_val(int u, int l, int r, int x, int y, int val) 54 { 55 if(x<=l && r<=y) 56 { 57 sum[u] = (r-l+1)*val; 58 setv[u] = val; 59 return; 60 } 61 62 push_down(u, l, r); 63 int mid = (l+r)>>1; 64 if(x<=mid) set_val(u*2, l, mid, x, y, val); 65 if(y>=mid+1) set_val(u*2+1, mid+1, r, x, y, val); 66 push_up(u); 67 } 68 69 int query(int u, int l, int r, int x, int y) 70 { 71 if(x<=l && r<=y) 72 return sum[u]; 73 74 push_down(u, l, r); 75 int ret = 0; 76 int mid = (l+r)>>1; 77 if(x<=mid) ret += query(u*2, l, mid, x, y); 78 if(y>=mid+1) ret += query(u*2+1, mid+1, r, x, y); 79 return ret; 80 } 81 82 int main() 83 { 84 int T, n, m; 85 scanf("%d", &T); 86 for(int kase = 1; kase<=T; kase++) 87 { 88 scanf("%d%d", &n, &m); 89 90 build(1, 1, n); 91 for(int i = 1; i<=m; i++) 92 { 93 int x, y, z; 94 scanf("%d%d%d", &x, &y, &z); 95 set_val(1, 1, n, x, y, z); 96 } 97 98 int ans = query(1, 1, n, 1, n); 99 printf("Case %d: The total value of the hook is %d.\n", kase, ans); 100 } 101 }