just a hook hdu 1689线段树

Just a Hook

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


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.
 1 /*线段树求区间和, 区间成段更新 
*此题刚开始写的时候, TLE了, 是用最朴素的线段树, 附加信息为区间的和,
2 * 然后才知道, 线段树还有一个重要的思想lazy,lazy思想的附加信息不是区间的和,
3 * 而是该区间每个元素的值,如本题中c初始化为1,因为题目中每个元素的默认值就为1
4 * 如果你要更新的区间恰好为线段树中的一线段区间, 如一颗区间为【1,10】的线段树,
5 * 如果你要更新的命令为1 5 2,则只需把2更新给c,不用在往下更新每个元素,减少了
6 * 大量的不必要操作,当然你如果你下ci更新的命令为3 7 3, 则【1 5】父亲节点先会把原来c值
7 * 往下传递给其左右孩子, 然后再用c更新相应区间。
8 * 最后当然还需要要写个求和函数,朴素的线段树唯一的优点就是不需求和了
9 **/
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13
14 struct line {
15 int left , right, c;
16 }T[2000000];
17
18 //建树函数
19 void creat( int l, int r, int n)
20 {
21 int mid;
22 mid = (l + r) / 2;
23 T[n].left = l, T[n].right = r, T[n].c = 1;
24 if (l == r)
25 return;
26 creat(l, mid , 2 * n );
27 creat(mid + 1, r, 2 * n + 1);
28 }
29
30 //更新函数
31 void insert(int l, int r, int val, int n)
32 {
33 if (T[n].c == val)
34 return;
35 int mid = (T[n].left + T[n].right)/ 2;
36 if ( T[n].left == l && T[n].right == r) {
37 T[n].c = val;
38 return;
39 }
40 if (T[n].c != -1) {
41 T[2 * n + 1].c = T[ 2 * n ].c = T[n].c;
42 }
43 T[n].c = -1;
44 if (mid >= r)
45 insert(l, r, val, 2 * n);
46 else if (mid < l)
47 insert(l, r, val, 2 * n + 1);
48 else
49 insert(l, mid, val, 2 *n), insert(mid + 1, r, val, 2 * n + 1);
50 }
51
52 //求和函数
53 int sum(int l, int r, int n)
54 {
55 int mid = (T[n].left + T[n].right) / 2;
56 if (T[n].c > 0)
57 return (r - l + 1) * T[n].c;
58 else
59 return sum(l,mid,2 * n) + sum(mid + 1, r, 2 * n + 1);
60 }
61 /*
62 void print( )
63 {
64 int i;
65 for(i = 1; i <= 40 ; i++)
66 printf("%d %d %d\n",T[i].left, T[i].right, T[i].c);
67 }
68 */
69
70 int main( )
71 {
72 int T1, N, M, x, y, val, l =0;
73 scanf("%d", &T1);
74 while (T1--)
75 {
76 l ++;
77 scanf("%d%d", &N, &M);
78 creat(1, N, 1);
79 //printf("*********");
80 //print();
81 while (M--)
82 {
83 scanf("%d%d%d", &x, &y, &val);
84 insert(x, y, val, 1);
85 //print();
86 }
87 //printf("ssssssssss");
88 printf("Case %d: The total value of the hook is %d.\n", l, sum(1, N, 1));
89 }
90 return 0;
91 }

posted on 2011-08-15 19:15  more think, more gains  阅读(267)  评论(0编辑  收藏  举报

导航