forked from nanwan03/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2750 Potted Flower.java
93 lines (85 loc) · 2.6 KB
/
2750 Potted Flower.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import java.io.*;
import java.util.*;
class SegmentTree {
int val;
int lMax;
int rMax;
int cMax;
int lMin;
int rMin;
int cMin;
SegmentTree(int val) {
this.val = val;
this.lMax = val;
this.rMax = val;
this.cMax = val;
this.lMin = val;
this.rMin = val;
this.cMin = val;
}
public void update(int val) {
this.val = val;
this.lMax = val;
this.rMax = val;
this.cMax = val;
this.lMin = val;
this.rMin = val;
this.cMin = val;
}
}
public class Main {
private static SegmentTree tree[];
private static final int NUM = 100010;
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
tree = new SegmentTree[NUM << 2];
for (int i = 0; i < NUM << 2; ++i) {
tree[i] = new SegmentTree(0);
}
int number = in.nextInt();
build(1, number, 1);
int query = in.nextInt();
for (int i = 1; i <= query; ++i) {
int index = in.nextInt();
int cur = in.nextInt();
update(index, cur, 1, number, 1);
if (tree[1].val == tree[1].cMax) {
System.out.println(tree[1].val - tree[1].cMin);
} else {
System.out.println(Math.max(tree[1].cMax, tree[1].val - tree[1].cMin));
}
}
}
private static void build(int left, int right, int index) {
if (left == right) {
tree[index] = new SegmentTree(in.nextInt());
return;
}
int mid = left + (right - left) / 2;
build(left, mid, index << 1);
build(mid + 1, right, (index << 1) | 1);
pushUp(index);
}
private static void update(int pos, int cur, int left, int right, int index) {
if (left == right && left == pos) {
tree[index].update(cur);
return;
}
int mid = left + (right - left) / 2;
if (pos <= mid) {
update(pos, cur, left, mid, index << 1);
} else {
update(pos, cur, mid + 1, right, (index << 1) | 1);
}
pushUp(index);
}
private static void pushUp(int index) {
tree[index].val = tree[index << 1].val + tree[(index << 1) | 1].val;
tree[index].lMax = Math.max(tree[index << 1].lMax, tree[index << 1].val + tree[(index << 1) | 1].lMax);
tree[index].rMax = Math.max(tree[(index << 1) | 1].rMax, tree[(index << 1) | 1].val + tree[index << 1].rMax);
tree[index].cMax = Math.max(tree[index << 1].cMax, Math.max(tree[(index << 1) | 1].cMax, tree[index << 1].rMax + tree[(index << 1) | 1].lMax));
tree[index].lMin = Math.min(tree[index << 1].lMin, tree[index << 1].val + tree[(index << 1) | 1].lMin);
tree[index].rMin = Math.min(tree[(index << 1) | 1].rMin, tree[(index << 1) | 1].val + tree[index << 1].rMin);
tree[index].cMin = Math.min(tree[index << 1].cMin, Math.min(tree[(index << 1) | 1].cMin, tree[index << 1].rMin + tree[(index << 1) | 1].lMin));
}
}