A - ポイントカードの残高管理 / Point Card Balance Management Editorial by admin
GPT 5.2 HighOverview
This problem asks you to perform \(M\) additions (which may also be subtractions) sequentially on each member’s point balance, rounding to \(0\) whenever the balance would become negative, and then output the final results.
Analysis
- Each operation is a simple single-point update: “Add \(V_j\) to the balance of member \(T_j\). If the result is less than \(0\), set it to \(0\).”
- The key observation is that there are no complex dependencies between operations — you only need to update the specified member’s balance on the spot. In other words, it suffices to maintain the balances in an array (list) and update the corresponding element for each query.
- A common mistake (WA) is keeping a negative value as-is after a negative addition. According to the problem statement, the balance never becomes negative, so you must always compute \(A = \max(0, A + V)\).
- Since \(N, M \le 10^5\), performing each operation one at a time in \(O(M)\) is fast enough. However, if you iterate over all members for each operation (e.g., recomputing the entire array each time), that would be \(O(NM)\), which is up to \(10^{10}\) and will certainly not finish in time (TLE).
Example:
- Initial \(S = [5, 1]\)
- Operation: \((T=2, V=-3)\)
\(1 + (-3) = -2\), so round to \(0\), giving \(S = [5, 0]\)
Algorithm
- Store the initial balances in an array \(S\) of length \(N\).
- For each operation \((T, V)\), convert from 1-indexed to 0-indexed: \(t = T - 1\).
- Update as \(S[t] \leftarrow \max(0, S[t] + V)\).
- Finally, output \(S_1, \dots, S_N\) separated by spaces.
Complexity
- Time complexity: \(O(N + M)\) (reading input \(O(N + M)\), \(M\) updates)
- Space complexity: \(O(N)\) (balance array)
Implementation Notes
1-indexed → 0-indexed: Since the member number \(T_j\) in the input starts from \(1\), use
t = T - 1to index into a Python list.Preventing negative values: Always update with
S[t] = max(0, S[t] + v)(or an equivalent conditional branch in code).Fast I/O: With input on the order of \(10^5\) lines, reading everything at once with
sys.stdin.buffer.read()and outputting withjoinat the end ensures stable and fast performance.Source Code
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
N = next(it)
M = next(it)
S = [next(it) for _ in range(N)]
for _ in range(M):
t = next(it) - 1
v = next(it)
nv = S[t] + v
S[t] = nv if nv > 0 else 0
sys.stdout.write(" ".join(map(str, S)))
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.2-high.
posted:
last update: