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
| #include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; struct Tree { #define lson (x << 1) #define rson (x << 1) + 1 int presum_Max[MAXN << 2], sufsum_Max[MAXN << 2], Max[MAXN << 2]; int presum_Min[MAXN << 2], sufsum_Min[MAXN << 2], Min[MAXN << 2]; int sum[MAXN << 2], Min_val[MAXN << 2], rt; inline void pushup (int x) { presum_Max[x] = max (presum_Max[lson], sum[lson] + presum_Max[rson]); sufsum_Max[x] = max (sufsum_Max[rson], sufsum_Max[lson] + sum[rson]); presum_Min[x] = min (presum_Min[lson], sum[lson] + presum_Min[rson]); sufsum_Min[x] = min (sufsum_Min[rson], sufsum_Min[lson] + sum[rson]); sum[x] = sum[lson] + sum[rson]; Max[x] = max (Max[lson], Max[rson]); Max[x] = max (Max[x], sufsum_Max[lson] + presum_Max[rson]); Min[x] = min (Min[lson], Min[rson]); Min[x] = min (Min[x], sufsum_Min[lson] + presum_Min[rson]); Min_val[x] = min (Min_val[lson], Min_val[rson]); return; } void insert (int x, int l, int r) { if (l == r) { scanf ("%d", &sum[x]); presum_Max[x] = sum[x]; sufsum_Max[x] = sum[x]; presum_Min[x] = sum[x]; sufsum_Min[x] = sum[x]; Max[x] = sum[x]; Min[x] = sum[x]; Min_val[x] = sum[x]; return; } int mid = (l + r) >> 1; insert (lson, l, mid); insert (rson, mid + 1, r); pushup(x); return; } void change (int x, int l, int r, int k, int val) { if (l == r) { sum[x] = val; presum_Max[x] = val; sufsum_Max[x] = val; presum_Min[x] = val; sufsum_Min[x] = val; Max[x] = val; Min[x] = val; Min_val[x] = val; return; } int mid = (l + r) >> 1; if (k > mid) { change (rson, mid + 1, r, k, val); } else { change (lson, l, mid, k, val); } pushup(x); return; } }; Tree T; int n, m; int main() { scanf ("%d", &n); T.rt = 1; T.insert (T.rt, 1, n); scanf ("%d", &m); for (int i = 1; i <= m; i++) { int k, val; scanf ("%d%d", &k, &val); T.change (T.rt, 1, n, k, val); int temp; if (T.sum[T.rt] != T.Max[T.rt]) { temp = T.Max[T.rt]; } else { temp = T.sum[T.rt] - T.Min_val[T.rt]; } printf ("%d\n", max (temp, T.sum[T.rt] - T.Min[T.rt])); } return 0; }
|