Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 5e5 + 10;
- const ll INF = 1e9;
- int a[maxn];
- int n;
- struct node {
- ll sum, pref_sum, suff_sum, max_sum;
- node () {}
- node(ll x) {
- sum = x;
- pref_sum = x;
- suff_sum = x;
- max_sum = x;
- }
- };
- node segment_tree[3 * maxn];
- node merge_two_nodes(node A, node B) {
- node C;
- C.sum = A.sum + B.sum;
- C.pref_sum = max(A.pref_sum, A.sum + B.pref_sum);
- C.suff_sum = max(B.suff_sum, B.sum + A.suff_sum);
- C.max_sum = max(A.max_sum, max(B.max_sum, A.suff_sum + B.pref_sum));
- return C;
- }
- void build(int L, int R, int pos) {
- if(L == R) {
- segment_tree[pos] = node(a[L]);
- }
- else {
- int middle = (L + R) / 2;
- build(L, middle, 2 * pos);
- build(middle + 1, R, 2 * pos + 1);
- segment_tree[pos] = merge_two_nodes(segment_tree[2 * pos], segment_tree[2 * pos + 1]);
- }
- }
- node query(int L, int R, int pos, int i, int j) {
- // L R i L R j L R
- if(i <= L and R <= j) {
- return segment_tree[pos];
- }
- if(R < i or j < L) {
- return node(-INF);
- }
- int middle = (L + R) / 2;
- return merge_two_nodes(query(L, middle, 2 * pos, i, j), query(middle + 1, R, 2 * pos + 1, i, j));
- }
- void update(int L, int R, int pos, int idx, int value) {
- if(L == R) {
- segment_tree[pos] = node(value);
- return;
- }
- int middle = (L + R) / 2;
- if(idx <= middle) {
- update(L, middle, 2 * pos, idx, value);
- }
- else {
- update(middle + 1, R, 2 * pos + 1, idx, value);
- }
- segment_tree[pos] = merge_two_nodes(segment_tree[2 * pos], segment_tree[2 * pos + 1]);
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- int q;
- cin >> n >> q;
- for(int i = 0; i < n; i++) {
- cin >> a[i];
- }
- build(0, n - 1, 1);
- while(q--) {
- int a, b;
- cin >> a >> b;
- a--;
- update(0, n - 1, 1, a, b);
- ll res = query(0, n - 1, 1, 0, n - 1).max_sum;
- if(res < 0) {
- res = 0;
- }
- cout << res << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement