操作:

1,更新某个位置的值。

2,查询区间最大连续和。

更新位置然后PushUp。

查询同【SPOJ】1043 Can you answer these queries I

 1 #include<cstdio>
 2 #define MAX(a,b) ((a)>(b)?(a):(b))
 3 #define MAXN 50010
 4 #define oo 1000000000
 5 struct node {
 6     int left, right, sum, val;
 7     void Init() {
 8         sum = 0;
 9         left = right = val = -oo;
10     }
11 };
12 node tree[MAXN << 2];
13 inline void PushUp(int rt) {
14     tree[rt].left = MAX(tree[rt << 1].left,
15             tree[rt << 1].sum + tree[rt << 1 | 1].left);
16     tree[rt].right = MAX(tree[rt << 1 | 1].right,
17             tree[rt << 1 | 1].sum + tree[rt << 1].right);
18     tree[rt].sum = tree[rt << 1].sum + tree[rt << 1 | 1].sum;
19     tree[rt].val = MAX(tree[rt << 1].val, tree[rt << 1 | 1].val);
20     tree[rt].val = MAX(tree[rt].val,
21             tree[rt << 1].right + tree[rt << 1 | 1].left);
22 }
23 void Build(int L, int R, int rt) {
24     if (L == R) {
25         scanf("%d", &tree[rt].val);
26         tree[rt].left = tree[rt].right = tree[rt].sum = tree[rt].val;
27     } else {
28         int mid = (L + R) >> 1;
29         Build(L, mid, rt << 1);
30         Build(mid + 1, R, rt << 1 | 1);
31         PushUp(rt);
32     }
33 }
34 void Update(int x, int val, int L, int R, int rt) {
35     if (L == R)
36         tree[rt].left = tree[rt].right = tree[rt].sum = tree[rt].val = val;
37     else {
38         int mid = (L + R) >> 1;
39         if (x <= mid)
40             Update(x, val, L, mid, rt << 1);
41         else
42             Update(x, val, mid + 1, R, rt << 1 | 1);
43         PushUp(rt);
44     }
45 }
46 node Query(int x, int y, int L, int R, int rt) {
47     if (x <= L && R <= y)
48         return tree[rt];
49     int mid = (L + R) >> 1;
50     node a, b, res;
51     a.Init(), b.Init(), res.Init();
52     if (x <= mid)
53         a = Query(x, y, L, mid, rt << 1);
54     if (y > mid)
55         b = Query(x, y, mid + 1, R, rt << 1 | 1);
56     res.left = MAX(a.left, a.sum + b.left);
57     res.right = MAX(b.right, b.sum + a.right);
58     res.sum = a.sum + b.sum;
59     res.val = MAX(a.val, b.val);
60     res.val = MAX(res.val, a.right + b.left);
61     return res;
62 }
63 int main() {
64     int n, q, cmd, x, y;
65     while (~scanf("%d", &n)) {
66         Build(1, n, 1);
67         scanf("%d", &q);
68         while (q--) {
69             scanf("%d%d%d", &cmd, &x, &y);
70             if (cmd)
71                 printf("%d\n", Query(x, y, 1, n, 1).val);
72             else
73                 Update(x, y, 1, n, 1);
74         }
75     }
76     return 0;
77 }
posted on 2012-08-29 00:16  DrunBee  阅读(333)  评论(0编辑  收藏  举报