HDU 1698 Just a Hook

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1698

解题思路:线段树区间更新。写的有点杂,不过整体来说题目不算难。

注意最后那个句号。

代码:

 1 const int maxn = 1e5 + 5;
 2 int tree[maxn * 4], tag[maxn * 4];
 3 int n, q;
 4 
 5 void build(int l, int r, int k){
 6     tree[k] = (r - l + 1);
 7     tag[k] = 1;
 8     if(l == r) return;
 9     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
10     build(l, mid, lc);
11     build(mid + 1, r, rc);
12 }
13 void update(int ul, int ur, int x, int l, int r, int k){
14     if(ul <= l && ur >= r) {
15         tree[k] = (r - l + 1) * x;
16         tag[k] = x;
17         return;
18     }
19     if(ul > r || ur < l) return;
20     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
21     if(tag[k] != 0){
22         tag[lc] = tag[k]; tag[rc] = tag[k];
23         tree[lc] = (mid - l + 1) * tag[k]; tree[rc] = (r - mid) * tag[k];
24         tag[k] = 0;
25     }
26     update(ul, ur, x, l, mid, lc);
27     update(ul, ur, x, mid + 1, r, rc);
28     tree[k] = tree[lc] + tree[rc];
29 }
30 
31 int main(){
32     int T;
33     scanf("%d", &T);
34     for(int t = 1; t <= T; t++){
35         scanf("%d %d", &n, &q);
36         build(1, n, 1);
37         while(q--){
38             int u, v, x;
39             scanf("%d %d %d", &u, &v, &x);
40             update(u, v, x, 1, n, 1);
41         }
42         int ans = tree[1];
43         printf("Case %d: The total value of the hook is %d.\n", t, ans);
44     }
45 }

题目:

Just a Hook

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33993    Accepted Submission(s): 16588


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.
 

 

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.
 

 

Source
posted @ 2017-08-07 17:35  EricJeffrey  阅读(97)  评论(0编辑  收藏  举报