HDU 1698 - Just a Hook

区间修改,代码还是比较清晰的,接下来就是不断去熟练它了。

 1 #include <cstdio>
 2 using namespace std;
 3 
 4 #define MAXV    131072
 5 
 6 unsigned _v[MAXV << 1], *const v = &_v[-1];
 7 unsigned _dirty[MAXV << 1], *const dirty = &_dirty[-1];
 8 
 9 #define lsi        i<<1
10 #define rsi        lsi | 1
11 #define recursive_def    int l, int r, int i
12 #define lso        l, m, lsi
13 #define rso        m+1, r, rsi
14 
15 #define pushup        (v[i] = v[lsi] + v[rsi])
16 #define pushdown    if (dirty[i]) { \
17     dirty[lsi] = dirty[rsi] = dirty[i];    \
18     v[lsi] = (m-l+1) * dirty[i],    \
19     v[rsi] = (r-m) * dirty[i];    \
20     dirty[i] = 0;}
21 
22 void build(recursive_def)
23 {
24     dirty[i] = 0;
25     if (l == r) v[i] = 1; // scanf("%d", &v[i]);
26     else {
27         int m = l+r >> 1;
28         build(lso), build(rso);
29         pushup;
30     }
31 }
32 
33 void update(int L, int R, int val, recursive_def)
34 {
35     if (L<=l && r<=R) v[i] = (r-l+1) * (dirty[i] = val);
36     else {
37         int m = l+r >> 1;
38         pushdown;
39         if (L <= m) update(L, R, val, lso);
40         if (m < R) update(L, R, val, rso);
41         pushup;
42     }
43 }
44 
45 /*
46 int query(int L, int R, recursive_def)
47 {
48     if (L<=l && r<=R) return v[i];
49     else {
50         int m = l+r >> 1;
51         pushdown;
52         int result = 0;
53         if (L <= m) result += query(L, R, lso);
54         if (m < R) result += query(L, R, rso);
55         return result;
56     }
57 }
58 */
59 
60 int main(void)
61 {
62 //    freopen("hdu1698.txt", "r", stdin);
63     unsigned T;
64     scanf("%d", &T);
65     for(unsigned t=1; t<=T; ++t) {
66         unsigned N, Q;
67         scanf("%d%d", &N, &Q);
68         build(1, N, 1);
69         while(Q) {
70             unsigned L, R, val;
71             scanf("%d%d%d", &L, &R, &val);
72             update(L, R, val, 1, N, 1);
73             --Q;
74         }
75         printf("Case %u: The total value of the hook is %u.\n", t, v[1]);
76     }
77     return 0;
78 }

 

2014-06-08 16:20:03 Accepted 1698 812MS 2256K 1321 B G++
posted @ 2014-06-05 22:16  e0e1e  阅读(169)  评论(0编辑  收藏  举报