hdu1698 Just a Hook

http://acm.hdu.edu.cn/showproblem.php?pid=1698

线段树,成段更新

只查询全部的和sum[1],没写query

 1 #include <stdio.h>
 2 
 3 #define lson l, m, root<<1
 4 #define rson m+1, r, root<<1|1
 5 #define N 100100
 6 
 7 int sum[N<<2], color[N<<2];
 8 
 9 void push_up(int root)
10 {
11     sum[root] = sum[root<<1] + sum[root<<1|1];
12 }
13 
14 void push_down(int root, int x)
15 {
16     if(color[root])
17     {
18         color[root<<1] = color[root<<1|1] = color[root];
19         sum[root<<1] = (x-(x>>1)) * color[root];
20         sum[root<<1|1] = (x>>1) * color[root];
21         color[root] = 0;
22     }
23 }
24 
25 void build(int l, int r, int root)
26 {
27     int m;
28     color[root] = 0;
29     if(l == r)
30     {
31         sum[root] = 1;
32         return;
33     }
34     m = (l + r) >> 1;
35     build(lson);
36     build(rson);
37     push_up(root);
38 }
39 
40 void update(int L, int R, int x, int l, int r, int root)
41 {
42     int m;
43     if(L<=l && r<=R)
44     {
45         color[root] = x;
46         sum[root] = (r-l+1) * x;
47         return;
48     }
49     push_down(root, r-l+1);
50     m = (l + r) >> 1;
51     if(L <= m)
52     {
53         update(L, R, x, lson);
54     }
55     if(m+1 <= R)
56     {
57         update(L, R, x, rson);
58     }
59     push_up(root);
60 }
61 
62 int main()
63 {
64     int t, cases, n, q, x, y, z;
65     scanf("%d", &t);
66     for(cases=1; cases<=t && scanf("%d%d", &n, &q); cases++)
67     {
68         build(1, n, 1);
69         while(q-- && scanf("%d%d%d", &x, &y, &z))
70         {
71             update(x, y, z, 1, n, 1);
72         }
73         printf("Case %d: The total value of the hook is %d.\n", cases, sum[1]);
74     }
75     return 0;
76 }
原文地址:https://www.cnblogs.com/yuan1991/p/hdu1698.html