Sereja has got an array, consisting of n integers, a1, a2, ..., an. Sereja is an active boy, so he is now going to complete m operations. Each operation will have one of the three forms:
Make vi-th array element equal to xi. In other words, perform the assignment avi = xi.
Increase each array element by yi. In other words, perform n assignments ai = ai + yi (1 ≤ i ≤ n).
Take a piece of paper and write out the qi-th array element. That is, the element aqi.
Help Sereja, complete all his operations.
Input
The first line contains integers n, m (1 ≤ n, m ≤ 105). The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the original array.
Next m lines describe operations, the i-th line describes the i-th operation. The first number in the i-th line is integer ti (1 ≤ ti ≤ 3) that represents the operation type. If ti = 1, then it is followed by two integers vi and xi, (1 ≤ vi ≤ n, 1 ≤ xi ≤ 109). If ti = 2, then it is followed by integer yi (1 ≤ yi ≤ 104). And if ti = 3, then it is followed by integer qi (1 ≤ qi ≤ n).
Output
For each third type operation print value aqi. Print the values in the order, in which the corresponding queries follow in the input.
We are asked to implement operations on an array of size n up to 1e5, with m operations up to 1e5. Operation types:
1: set a[vi] = xi (direct assignment)
2: add yi to all elements (global addition)
3: query a[qi] and output.
This is a classic problem of handling point updates and range updates (here global additions) and point queries. Since all operations are point assignments, global additions, and point queries, we can manage it with a lazy offset plus tracking of last assignment times. Approach: maintain a variable 'add' which is the total amount added to all elements via type 2 operations. But careful: When we assign a value to a specific element after some global additions, the effect of prior global adds should not apply to that element? Actually, let's think:
We have an array initially given. Then we do sequence of operations. For operation type 2 (add yi to all elements), it's like we increment a global offset. For operation type 1 (set vi to xi), we need to assign the value xi to that element. But what about the global offset? If we maintain a global offset, then the actual value of an element is its "base value" plus offset. However, assignments complicate: When we set a[vi] = xi, we need to define what that means in presence of global offset. The intuitive interpretation: at the moment of assignment, we assign the actual value of that element to be xi, overriding any previous value and ignoring any prior global increments? But careful: The operations are sequential. Suppose we have global offset variable offset that tracks total sum added to all elements via type 2 operations since start (but we have to consider previous assignments). A typical approach: store for each element the last "set" value and the time of that set relative to the offset. Alternatively, we can use a lazy approach:
Let global_add = total amount added to all elements via type 2 operations. For each element, we store its value as if no global addition? But global addition affects all elements uniformly, including those set later? Consider: Initially, a = [a1, a2, ...]. We start with offset = 0. When we do type 2 add y, we increment offset by y. Then the actual value of any element is base_val + offset. But if we later do a type 1 set vi = xi, we want that element's actual value to become xi. However, if we just set its base_val = xi - offset, then later when offset changes, its value will be (xi - offset_old) + offset_new = xi + (offset_new - offset_old) which is not xi unless offset doesn't change. Actually we want: after setting, the element's value is xi, and then subsequent global adds should also affect it because they affect all elements. So after setting to xi, if we later add y, then the element should become xi + y. So the behavior is: The element's actual value = (its "base" value) + (global offset). If we set at time when global offset = cur_offset, we need to set base_val such that base_val + cur_offset = xi => base_val = xi - cur_offset. That works: later when offset becomes larger, the actual value will be (xi - cur_offset) + new_offset = xi + (new_offset - cur_offset). That's exactly as expected: after setting to xi, any subsequent global adds are applied to it. But also note: what about previous global adds that were already applied? They are incorporated in offset. So the approach: maintain a global variable add (sum of all type 2 operations so far). For each index i, we store a value base[i] (or a timestamp/last set value). Initially, base[i] = a[i] because add=0 => actual = base[i] + add = a[i]. Then for type 2: add += yi. For type 1: we set base[vi] = xi - add. For type 3: output base[qi] + add.
This is O(1) per operation. n up to 1e5, m up to 1e5, simple.
But wait: the constraints: yi up to 1e4, xi up to 1e9, a[i] up to 1e9. Base values might become negative? Not in problem constraints, but actual values must be >=1? The problem says 1 ≤ ai (the elements) initially, but assignments set xi≥1, and add yi≥1. So actual values will stay positive, but base[i] could be negative because if add is large, base = xi - add could be negative. That's fine, we just store it.
Edge Cases: multiple assignments to same index; base values overwritten; queries after many adds.
Complexity: O(m) time, O(n) memory.
Let's confirm constraints: n,m ≤ 1e5, easy.
Implementation details: read n,m; read array a of length n; store base = a copy (maybe as list of ints). add=0. For each operation: read t; if t==1: read vi, xi; base[vi-1] = xi - add (since zero-index). t==2: read yi; add += yi. t==3: read qi; print(base[qi-1] + add). Use fast I/O because m up to 1e5, print many lines. In Python, use sys.stdin.read() parse ints.
Now produce final answer.
But wait, the problem statement says "Make vi-th array element equal to xi. In other words, perform the assignment avi = xi." This implies that we directly set the value, not that we set base and then later adds affect it? However, sequential semantics: if we set a[vi]=xi, then later we do a global add, that add should affect all elements, including this one. So the described method is correct. There's no nuance like the assignment should ignore future global adds? It shouldn't, because global add says "increase each array element by yi", which means it's applied to whatever current values are. So yes.
Thus we can solve.
Now write final solution explanation in appropriate style.
But the user asked: "Sereja has got an array... Help Sereja, complete all his operations." So we need to produce the output. However, as ChatGPT, we are to provide code and explanation? The user just posted the problem statement. Typically they expect a solution description or code. Since it's a problem from Codeforces maybe (Sereja and ...), but we can provide a solution. However, the instruction says: "You are an AI assistant that responds to user queries." and the user said: "Sereja has got an array... Help Sereja, complete all his operations." So they want help with solving the problem, likely code. They might be expecting a programming solution. The output should be code that reads input and prints output.
Given the environment: It's not an interactive coding environment, but we can produce the solution in Python.
Thus final answer: explanation and code.
We'll produce a clear answer: Approach, then code.
Potential pitfalls: Large numbers but within Python int.
Now, I'll respond. We need to support three operations on an array:
Set a specific element to a given value.
Add a value to all elements.
Query a specific element.
The operations are applied sequentially. A naive implementation would be O(n) for type 2, which is too slow for n, m up to 1e5. Instead we maintain a global additive offset add that accumulates all type‑2 increments. For each index we store a base value that represents its value when add = 0. Then:
Initially add = 0 and base[i] = a[i].
Type 1 (set vi to xi): we want the actual value to become xi at this moment. Since the actual value = base[vi] + add, we set base[vi] = xi - add.
Type 2 (add yi to all): simply increase add by yi.
Type 3 (query qi): output base[qi] + add.
All operations become O(1).
Complexity
Time: O(n + m)
Space: O(n)
Here is an implementation in Python:
python
import sys
def main():
data = sys.stdin.buffer.read().split()
it = iter(data)
n = int(next(it))
m = int(next(it))
base = [0] * n
for i in range(n):
base[i] = int(next(it))
add = 0
out_lines = []
for _ in range(m):
t = int(next(it))
if t == 1:
vi = int(next(it)) - 1
xi = int(next(it))
base[vi] = xi - add
elif t == 2:
yi = int(next(it))
add += yi
else: # t == 3
qi = int(next(it)) - 1
out_lines.append(str(base[qi] + add))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
main()
This code uses fast input/output and processes up to 1e5 operations efficiently.