C - 積雪調査 / Snow Depth Survey Editorial by admin
Claude 4.6 Opus (Thinking)Overview
Given \(N\) points, perform \(M\) range addition operations and find the number of points whose final value is at least \(K\). This can be solved efficiently using the imos method (difference array).
Analysis
Naive Approach and Its Issues
The simplest method is to increment the snowfall count for each point from \(L_i\) to \(R_i\) for every snowfall event. However, this requires up to \(O(N)\) operations per snowfall, resulting in \(O(N \times M)\) total operations. Since \(N\) can be up to \(10^6\) and \(M\) up to \(2 \times 10^5\), this leads to a worst case of \(2 \times 10^{11}\) operations, which will not finish within the time limit (TLE).
Key Insight: Range Addition Can Be Accelerated with the Imos Method
When repeatedly performing “add the same value to an entire range” operations, the imos method (difference array) is extremely effective. Using a difference array, each range addition can be processed in \(O(1)\), and the values at each point can be recovered by computing a prefix sum at the end.
Concrete Example
Consider \(N = 5\) with two snowfall events on \([2, 4]\) and \([3, 5]\).
Recording in the difference array:
- \([2, 4]\):
diff[2] += 1,diff[5] -= 1 - \([3, 5]\):
diff[3] += 1,diff[6] -= 1
Difference array: [0, 0, 1, 1, 0, -1, -1] (indices 0 through 6)
Recovering values by taking the prefix sum:
| Point | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Snowfall count | 0 | 1 | 2 | 2 | 1 |
If \(K = 2\), then points 3 and 4 qualify, giving a count of 2.
Algorithm
- Initialize a difference array
diffof size \(N+2\) with zeros. - For each snowfall \((L_i, R_i)\), set
diff[L_i] += 1anddiff[R_i + 1] -= 1. - Iterate from point \(1\) to \(N\), computing the prefix sum along the way. If the current snowfall count is at least \(K\), increment the count.
- Output the count.
How the imos method works:
- diff[L] += 1 means “snowfall begins at point \(L\).”
- diff[R+1] -= 1 means “the effect of snowfall ends starting from point \(R+1\).”
- Taking the prefix sum correctly computes “the number of currently active snowfall events” at each point.
Complexity
- Time complexity: \(O(N + M)\)
- \(O(M)\) for recording in the difference array, \(O(N)\) for computing the prefix sum and counting
- Space complexity: \(O(N)\)
- Size of the difference array
Implementation Notes
The difference array should have size
N + 2. Sincediff[R_i + 1]accessesdiff[N+1]when \(R_i = N\), we need to allocate up to index \(N+1\).By reading all input at once with
sys.stdin.buffer.read()and splitting withsplit(), fast input processing is possible even in Python. Callinginput()line by line may cause TLE when \(M\) is large.By performing the prefix sum computation and counting in a single loop, we save memory and time by avoiding the creation of an extra array.
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
K = int(input_data[idx]); idx += 1
diff = [0] * (N + 2)
for i in range(M):
L = int(input_data[idx]); idx += 1
R = int(input_data[idx]); idx += 1
diff[L] += 1
diff[R + 1] -= 1
count = 0
current = 0
for j in range(1, N + 1):
current += diff[j]
if current >= K:
count += 1
print(count)
main()
This editorial was generated by claude4.6opus-thinking.
posted:
last update: