Official

C - 区間加算 / Range Addition Editorial by admin

Claude 4.6 Opus (Thinking)

Overview

This problem asks us to find the resulting sequence after performing \(M\) range addition operations (\(+1\) to all elements in a specified range) on a sequence of length \(N\) (initially all \(0\)). This can be solved efficiently using the imos method (difference array).

Analysis

Naive Approach and Its Issues

The simplest method is to add \(1\) to every element from \(L_i\) to \(R_i\) for each operation. However, since a single operation can update up to \(N\) elements, the total worst-case time complexity for all \(M\) operations is \(O(N \times M)\). Since both \(N\) and \(M\) can be up to \(2 \times 10^5\), this results in \(4 \times 10^{10}\) operations, which will not fit within the time limit (TLE).

Key Insight

An operation that adds the same value to an entire range can be represented in \(O(1)\) using a difference array (imos method). Adding \(+1\) to the range \([L, R]\) only requires updating two positions in the difference array: “\(+1\) at position \(L\)” and “\(-1\) at position \(R+1\)”. After recording all operations, taking the prefix sum of the difference array recovers the original sequence.

Algorithm

Steps of the Imos Method

  1. Prepare a difference array diff (length \(N+1\), initialized to \(0\))
  2. Record each operation in the difference array: For operation \([L_i, R_i]\),
    • diff[L_i - 1] += 1 (position \(L_i - 1\) in 0-indexed)
    • diff[R_i] -= 1 (position \(R_i\) in 0-indexed, i.e., one past the right end of the range)
  3. Compute the prefix sum: diff[i] += diff[i-1] (\(i = 1, 2, \ldots, N-1\))
  4. diff[0] through diff[N-1] is the answer

Concrete Example

For \(N = 5\), \(M = 2\), with operations \([2, 4]\) and \([1, 3]\):

Operation diff[0] diff[1] diff[2] diff[3] diff[4]
Initial 0 0 0 0 0
\([2,4]\): diff[1]+=1, diff[4]-=1 0 1 0 0 -1
\([1,3]\): diff[0]+=1, diff[3]-=1 1 1 0 -1 -1

After taking the prefix sum:

i 0 1 2 3 4
Value 1 2 2 1 0

The answer is 1 2 2 1 0, which is indeed the correct result.

Complexity

  • Time complexity: \(O(N + M)\) (recording into the difference array is \(O(M)\), computing the prefix sum is \(O(N)\))
  • Space complexity: \(O(N)\) (for the difference array)

Implementation Notes

  • The problem uses 1-indexed numbering (from \(1\) to \(N\)), but since arrays are managed in 0-indexed form, we convert \(L_i\) to L_i - 1.

  • A guard condition if R < N is included to ensure the position where \(-1\) is placed does not go out of the array bounds. When \(R = N\), the addition extends to the end of the array, so there is no need to place \(-1\).

  • By using sys.stdin.buffer.read() to read all input at once, input processing is kept fast even in Python.

    Source Code

import sys

def main():
    input_data = sys.stdin.buffer.read().split()
    idx = 0
    N = int(input_data[idx]); idx += 1
    M = int(input_data[idx]); idx += 1
    
    diff = [0] * (N + 1)
    for _ in range(M):
        L = int(input_data[idx]); idx += 1
        R = int(input_data[idx]); idx += 1
        diff[L - 1] += 1
        if R < N:
            diff[R] -= 1
    
    for i in range(1, N):
        diff[i] += diff[i - 1]
    
    print(' '.join(map(str, diff[:N])))

main()

This editorial was generated by claude4.6opus-thinking.

posted:
last update: