We need to solve: Given an array of n numbers, we need to compute the maximum subarray sum (classic Kadane). However, there are q updates (point updates) and we need to output the max subarray sum after each update (including initial). n, q up to 1e5.
We need a data structure to support point updates and query for maximum subarray sum. This is classic segment tree with storing for each node: sum, max prefix sum, max suffix sum, max subarray sum. That yields O(log n) per update. So we can solve.
Constraints: values up to 1e9 absolute, sums can be up to 1e14, so use 64-bit (long long). Also empty subarray sum = 0, so max subarray sum is at least 0. So we need to cap at 0? Actually classic Kadane can produce negative, but here they consider empty subarray sum = 0, so answer is max(0, max subarray sum). In segment tree, we can enforce that by taking max with 0 for the overall answer. But careful: For each node, we compute its values in a way that empty subarray within that segment is allowed? Typically we compute maximum subarray sum that must be non-empty within the segment. But if we allow empty, then overall answer is max(0, root.max_sub). So we can just output max(0, root.max_sub). However, we must ensure that node merging accounts for non-empty property? The typical implementation for maximum subarray sum (non-empty) yields the maximum among all non-empty subarrays. That is fine because if all numbers are negative, the max_non_empty is negative, but we then output 0. So it's fine.
But watch: The problem states "The sum of empty strings is 0." So the maximum value of the sum of the partial arrays (which may be empty) is max(0, maximum subarray sum). So we just do that.
Implementation: segment tree with arrays of size 4*n. Each node stores:
- sum (total sum of segment)
- pref (maximum prefix sum, non-empty? Usually we consider prefix that can be empty? But if we allow empty prefix, its sum is 0. However, when merging, if we use standard formulas, we often compute pref = max(left.pref, left.sum + right.pref). This considers prefixes that are non-empty? Actually left.pref is max sum of a prefix of left segment (non-empty). But we could include empty prefix as 0 to match the ability to start from nowhere. However, in the context of max subarray within the segment, if we allow empty subarray, we could also allow empty prefix? But for merging, we want to compute max subarray (non-empty) inside the node. For the root we take max with 0. So we don't need to consider empty subarray inside nodes. So we can just compute typical values for non-empty subarrays. The formulas for pref: max(left.pref, left.sum + right.pref). This yields the best prefix sum, which could be negative if all values are negative, but it's non-empty. That's fine because we only use it to compute max subarray inside. But when we compute overall max subarray for the whole array (non-empty), we combine as: max_sub = max(left.max_sub, right.max_sub, left.suf + right.pref). These are all non-empty. Then we take max(0, root.max_sub). That yields correct answer.
Check: Example 2: array: -1 -2 -3. Initial: all negative. Segment tree root.max_sub = -1? Actually maximum non-empty subarray sum would be -1 (the largest element). Then max(0, -1) = 0. Output 0. After update: set a1=1 => array 1 -2 -3. Max subarray non-empty is 1? Also could be 1? Actually maybe subarray [1] =1, [1,2]= -1, etc. So root.max_sub =1, max(0,1)=1. Output correct.
So standard segment tree works.
But careful: values can be up to 1e9, and n up to 1e5, sums up to 1e14, so use long long.
Implementation steps:
- Read n, q.
- Read initial array a (1-indexed).
- Build segment tree.
- For each query (k, x):
- Update position k to x.
- Compute answer = max(0, tree[1].max_sub) (root node)
- Output after each update. Also output initial before processing updates.
But wait: The problem statement says output q+1 lines: first line before any rewrite, then after each rewrite.
So we first compute and output initial answer. Then for each update, apply and output.
Complexities: O((n+q) log n).
Now we need to code segment tree. Node structure: { sum, pref, suf, best }.
Merge function:
Given node L and R:
- sum = L.sum + R.sum
- pref = max(L.pref, L.sum + R.pref)
- suf = max(R.suf, R.sum + L.suf)
- best = max(L.best, R.best, L.suf + R.pref)
We need to decide base case: leaf node for index i:
- sum = a[i]
- pref = a[i]
- suf = a[i]
- best = a[i]
This is for non-empty subarray.
Check: For a leaf with a negative number, pref = that negative, suf = negative, best = negative. That's okay.
Now, note that the merging formulas assume that pref and suf are maximum sums of non-empty prefixes/suffixes. When we combine, we get non-empty subarrays.
But is there any risk that the max subarray might be empty? No, because empty is 0 and we handle at the root.
Now, think about potential overflow? sum of two 1e14 numbers could be up to 2e14, still within 64-bit signed (9e18). So safe.
Implementation details: We'll implement iterative segment tree? Usually recursive is fine with n=1e5, recursion depth log n but building recursively may cause stack overflow if recursion depth too high? 1e5 recursion depth? No, recursion depth for building is O(log n) if we implement correctly. We can implement recursively: build(node, l, r). That's okay.
Alternatively, we can use array of nodes of size 4*n.
We'll do in C++:
struct Node {
long long sum, pref, suf, best;
};
Node tree[4*MAXN];
Node combine(const Node& left, const Node& right) {
Node res;
res.sum = left.sum + right.sum;
res.pref = max(left.pref, left.sum + right.pref);
res.suf = max(right.suf, right.sum + left.suf);
res.best = max({left.best, right.best, left.suf + right.pref});
return res;
}
void build(int node, int l, int r) {
if (l == r) {
tree[node].sum = a[l];
tree[node].pref = a[l];
tree[node].suf = a[l];
tree[node].best = a[l];
} else {
int mid = (l+r)/2;
build(node2, l, mid);
build(node2+1, mid+1, r);
tree[node] = combine(tree[node2], tree[node2+1]);
}
}
void update(int node, int l, int r, int pos, long long val) {
if (l == r) {
tree[node].sum = val;
tree[node].pref = val;
tree[node].suf = val;
tree[node].best = val;
} else {
int mid = (l+r)/2;
if (pos <= mid) update(node2, l, mid, pos, val);
else update(node2+1, mid+1, r, pos, val);
tree[node] = combine(tree[node2], tree[node2+1]);
}
}
Then query: we don't need a separate query function because we just need root after each update. So we can just after update, output max(0LL, tree[1].best).
But careful: For initial output we need tree[1].best before any updates. So after building, output max(0, tree[1].best).
Now test with given examples.
Example 1:
n=5, a=[1,2,-3,4,-5]
Compute max subarray non-empty? Actually before update, maximum subarray sum non-empty is 4? Let's compute: subarrays: [1]=1, [1,2]=3, [1,2,-3]=0, [1,2,-3,4]=4, [1,2,-3,4,-5]=-1, [2]=2, [2,-3]=-1, [2,-3,4]=3, [2,-3,4,-5]=-2, [-3]=-3, [-3,4]=1, [-3,4,-5]=-4, [4]=4, [4,-5]=-1, [-5]=-5. So max non-empty is 4. Then max with 0 gives 4. So output 4. After first update: set a3=3 (original -3) -> [1,2,3,4,-5] max subarray: [1,2,3,4]=10, also maybe [1,2,3,4,-5]=5, so max =10, output 10. After second update: set a2=-6 -> [1,-6,3,4,-5] max subarray: maybe [3,4]=7, or [4]=4, etc. So 7. Output 7. Works.
Example2: n=3, a=[-1,-2,-3] initial max non-empty = -1, output max(0,-1)=0. After update: set a1=1 -> [1,-2,-3] max non-empty = 1 -> output 1. Works.
Edge cases: all positive: sum large; all negative: answer 0.
But one nuance: The maximum prefix and suffix in the node might be computed as negative, but when merging, e.g., if left.suf is negative and right.pref is negative, their sum is even more negative; but the best will be max of children which could be less negative. That's fine.
Now we need to handle q up to 1e5, so total operations fine.
Memory: 4*n nodes each with 4 long longs = about 32 bytes per node * 4e5 = 12.8 MB, okay.
Now code in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 100005;
int n, q;
ll a[MAXN];
struct Node {
ll sum, pref, suf, best;
} tree[4*MAXN];
Node combine(Node left, Node right) {
Node res;
res.sum = left.sum + right.sum;
res.pref = max(left.pref, left.sum + right.pref);
res.suf = max(right.suf, right.sum + left.suf);
res.best = max({left.best, right.best, left.suf + right.pref});
return res;
}
void build(int node, int l, int r) {
if (l == r) {
tree[node] = {a[l], a[l], a[l], a[l]};
return;
}
int mid = (l+r)/2;
build(node2, l, mid);
build(node2+1, mid+1, r);
tree[node] = combine(tree[node2], tree[node2+1]);
}
void update(int node, int l, int r, int pos, ll val) {
if (l == r) {
tree[node] = {val, val, val, val};
return;
}
int mid = (l+r)/2;
if (pos <= mid) update(node2, l, mid, pos, val);
else update(node2+1, mid+1, r, pos, val);
tree[node] = combine(tree[node2], tree[node2+1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
build(1, 1, n);
cout << max(0LL, tree[1].best) << '\n';
while (q--) {
int k; ll x;
cin >> k >> x;
update(1, 1, n, k, x);
cout << max(0LL, tree[1].best) << '\n';
}
return 0;
}
But there is a subtlety: In some segment tree implementations for max subarray sum, they also store empty prefix/suffix for easier merging? But as said, it's fine.
But is it necessary to consider the empty subarray inside nodes to properly propagate? For example, consider merging two segments: left: [-5] (sum=-5, pref=-5, suf=-5, best=-5); right: [3] (sum=3, pref=3, suf=3, best=3). Now left.suf + right.pref = -5+3 = -2, best = max(-5,3,-2) = 3. So root.best = 3. That's correct: max subarray non-empty is 3 from right. If we allowed empty, the root.best would be max of maybe 0, but we handle at root.
Now consider if we have left: [-2, -3], right: [4]. Let's compute left segment: For [-2,-3], best = -2 (largest element), sum = -5, pref = -2? Actually max prefix non-empty: starting at first: -2 (only first), -2-3=-5 => max is -2. suf: starting at last: -3, -2-3=-5 => max is -3? Actually suffix max non-empty: from rightmost: -3, from -2,-3 = -5, so max is -3 (since -3 > -5). So left.suf = -3. right: [4] sum=4, pref=4, suf=4, best=4. Combine: left.suf + right.pref = -3+4=1. So best = max(left.best=-2, right.best=4, 1) = 4. That's correct: max subarray non-empty is 4. But what about subarray that includes left partially? Actually if we take the suffix of left [-3] and prefix of right [4], we get sum 1, which is less than 4. So fine.
Now consider if whole array is negative: left: [-5], right: [-3], best = max(-5,-3) = -3? Actually left.best=-5, right.best=-3, left.suf+right.pref=-5+(-3) = -8, so best = -3. So root.best = -3, which is the maximum element. So correct.
Thus standard works.
Potential issue: integer overflow? left.sum + right.pref could be sum up to 1e14 + 1e14 = 2e14, still within long long.
Now about input: there are spaces and lines. Use cin.
Edge: n=1. Build leaf, root.best = a[1]. Output max(0, a[1]). Update works.
Now it's straightforward.
But there is an alternative approach: using segment tree with these four values is classic.
Now we need to output q+1 lines, each value as integer.
Now double-check the input format: The example input:
5 2
1 2 -3 4 -5
3 3
2 -6
Yes.
Also note: The problem statement says "E: Cut out the sum" and there is Japanese? But we just need to implement.
Now confirm that all indexes and updates are 1-indexed, as given: 1 <= k_j <= n.
Now code.
But let's also consider if we need to use fast I/O due to large input size (2e5 numbers). We'll use ios::sync_with_stdio(false); cin.tie(0); and use '\n' not endl. Should be fine.
Now test with sample.
We can quickly simulate.
Now finalize.
But wait: there is a nuance about the empty subarray: the problem allows empty subarray with sum 0. So answer = max(0, max subarray sum). Our method does that. So correct.
Now I think this solves.
But I've seen a variation where the segment tree stores both non-empty and possibly empty variants to simplify merging for cases where we want to allow empty subarray inside? But we don't need it.
Thus answer is ready.
Now I'll write solution in Python? The problem statement did not specify language, but we need to produce answer. Usually we need to produce code. The user asked "E: Cut out the sum ..." but it's likely a coding problem. They might want solution explanation. But the instruction says "Please output q + 1 line." etc. That is the output of the program. They gave examples. So they want a program.
But this is an explanation? Actually the user might be asking for a solution (code) to this problem. But in typical ChatGPT usage, they might want a detailed explanation. However, the final answer could be code. I think we should provide both explanation and code.
Given the instruction: "You are an AI designed to assist with programming contest problems. Provide clear, concise solutions and code." So I'll provide explanation and then code in C++.
Now ensure code compiles and passes constraints.
Now let's write final answer.
But also double-check potential bug: In combine, we used max({ ... }) which requires C++11 and including . We'll include bits/stdc++.h. That's fine.
Alternatively, we can write max( max(left.best, right.best), left.suf+right.pref ). We'll use curly braces.
Now note: The initial array values can be up to 1e9, but updates also up to 1e9 absolute. Use long long.
Now provide final answer.
But before final, test with some custom cases.
Case 1: n=3, a=[-2, -3, -4] -> initial best = -2, output max(0,-2)=0. update 2 to 5 -> array [-2,5,-4] max subarray non-empty = 5? Actually [5] =5, also [-2,5]=3, [5,-4]=1, [-2,5,-4]=-1, so best=5. Output 5. Our code: after update root.best=5. Good.
Case 2: n=4, a=[2, -1, 2, -1] -> max subarray sum? Could be [2,-1,2]=3, [2]=2, [2, -1, 2, -1]=2, [2] other. Actually maybe [2,-1,2] =3, also [2, -1, 2, -1]? =2. Also [2] from third? Actually 2 at pos3. So best =3. With empty allowed, answer =3. After update set a2= -5 -> [2,-5,2,-1] max subarray? [2] =2, [2]=2 from third, [2,-5,2]= -1? Actually 2+(-5)+2 = -1, [2] from third, maybe [2,-5] = -3. So best =2. Answer 2. Our code should compute.
Now check merging: left [2,-5] -> sum = -3, pref = max(2, -3?) Actually left segment leaf? Actually if we build tree, it will compute correctly. So fine.
Now confirm that building and updates are recursive; with n=1e5 recursion depth up to log2(1e5) ~ 17, fine.
One more nuance: If we use recursion for update and combine, it's fine.
Now check potential TLE: q=1e5, each update O(log n) ~ 17 steps, total operations ~ 1.7e6, trivial.
Now output: each answer on separate line.
Thus final solution is segment tree.
Now I'll write explanation and code.
But the user may want the code only. However, typical guideline: Provide solution with explanation. So I'll give explanation and then code.
Now I'll produce final answer. The problem requires maintaining the maximum subarray sum (allowing empty subarray with sum 0) of a sequence under point updates. A segment tree can store for each segment the total sum, maximum non‑empty prefix sum (pref), maximum non‑empty suffix sum (suf), and maximum non‑empty subarray sum (best). Merging two children uses standard formulas. After each update we take max(0, root.best) as the answer.
Complexity: (O((n + q) \log n)) time, (O(n)) memory.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
int n, q;
ll a[MAXN];
struct Node {
ll sum, pref, suf, best;
} tree[4 * MAXN];
Node combine(const Node &l, const Node &r) {
Node res;
res.sum = l.sum + r.sum;
res.pref = max(l.pref, l.sum + r.pref);
res.suf = max(r.suf, r.sum + l.suf);
res.best = max({l.best, r.best, l.suf + r.pref});
return res;
}
void build(int node, int l, int r) {
if (l == r) {
tree[node] = {a[l], a[l], a[l], a[l]};
return;
}
int mid = (l + r) / 2;
build(node * 2, l, mid);
build(node * 2 + 1, mid + 1, r);
tree[node] = combine(tree[node * 2], tree[node * 2 + 1]);
}
void update(int node, int l, int r, int pos, ll val) {
if (l == r) {
tree[node] = {val, val, val, val};
return;
}
int mid = (l + r) / 2;
if (pos <= mid) update(node * 2, l, mid, pos, val);
else update(node * 2 + 1, mid + 1, r, pos, val);
tree[node] = combine(tree[node * 2], tree[node * 2 + 1]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
build(1, 1, n);
cout << max(0LL, tree[1].best) << '\n';
while (q--) {
int k; ll x;
cin >> k >> x;
update(1, 1, n, k, x);
cout << max(0LL, tree[1].best) << '\n';
}
return 0;
}