#include using namespace std; const int MAXN = 200000; int n; int a[MAXN + 1]; struct Node { int max_subseg_sum, max_prefix_sum, max_suffix_sum, seg_sum; Node() {} Node(int x) { max_subseg_sum = max_prefix_sum = max_suffix_sum = max(0, x); seg_sum = x; } Node(const Node& l, const Node& r) { max_subseg_sum = max({l.max_subseg_sum, r.max_subseg_sum, l.max_suffix_sum + r.max_prefix_sum}); max_prefix_sum = max({l.max_prefix_sum, l.seg_sum + r.max_prefix_sum}); max_suffix_sum = max({r.max_suffix_sum, r.seg_sum + l.max_suffix_sum}); seg_sum = l.seg_sum + r.seg_sum; } }; Node tree[4 * MAXN]; void build(int node, int start, int end) { if (start == end) { tree[node] = Node(a[start]); } else { int mid = (start + end) / 2; build(2 * node, start, mid); build(2 * node + 1, mid + 1, end); tree[node] = Node(tree[2 * node], tree[2 * node + 1]); } } void update(int node, int start, int end, int pos, int val) { if (start == end) { tree[node] = Node(val); } else { int mid = (start + end) / 2; if (pos <= mid) { update(2 * node, start, mid, pos, val); } else { update(2 * node + 1, mid + 1, end, pos, val); } tree[node] = Node(tree[2 * node], tree[2 * node + 1]); } } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, 1, n); int q; cin >> q; while (q--) { int l, r; cin >> l >> r; update(1, 1, n, l, *max_element(a + l, a + r + 1)); cout << tree[1].max_subseg_sum << '\n'; } return 0; }