C - 照明スイッチの操作 / Light Switch Operation 解説 by admin
Claude 4.6 Opus (Thinking)Overview
This problem asks you to determine the number of lights that are on after performing \(M\) range toggle operations on \(N\) lights. It can be solved efficiently using the imos method (difference array).
Analysis
Naive Approach and Its Issues
The simplest method is to toggle each light in the range \([L_j, R_j]\) one by one for each operation. However, this takes up to \(O(N)\) time per operation, resulting in \(O(NM)\) overall. Since \(N\) can be up to \(10^6\) and \(M\) up to \(2 \times 10^5\), this could require up to \(2 \times 10^{11}\) operations in the worst case, leading to TLE (Time Limit Exceeded).
Key Observation
For each light \(i\), whether it is ultimately on or off depends solely on how many times that light was toggled.
- If the toggle count is odd → the light is on
- If the toggle count is even → the light is off
In other words, we just need to efficiently compute the toggle count for each light.
Speeding Up with the Imos Method
The imos method (difference array) is a technique for efficiently processing operations that uniformly add \(+1\) to a range \([L, R]\).
We prepare a difference array \(\text{diff}\) and represent the addition to range \([L, R]\) with the following two operations:
- \(\text{diff}[L] += 1\)
- \(\text{diff}[R+1] -= 1\)
After processing all operations, taking the prefix sum of the difference array gives us the toggle count for each light.
Concrete Example
For \(N = 5\), \(M = 2\), with operations \([2, 4]\) and \([3, 5]\):
| Operation | diff[1] | diff[2] | diff[3] | diff[4] | diff[5] | diff[6] |
|---|---|---|---|---|---|---|
| Initial | 0 | 0 | 0 | 0 | 0 | 0 |
| \([2,4]\) | 0 | +1 | 0 | 0 | -1 | 0 |
| \([3,5]\) | 0 | +1 | +1 | 0 | -1 | -1 |
Taking the prefix sum:
| Light | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Toggle count | 0 | 1 | 2 | 2 | 1 |
The lights toggled an odd number of times are \(2\) and \(5\), so the answer is 2.
Algorithm
- Initialize a difference array \(\text{diff}\) of size \(N+2\) with \(0\)
- For each operation \((L_j, R_j)\):
- \(\text{diff}[L_j] += 1\)
- \(\text{diff}[R_j + 1] -= 1\)
- Compute the prefix sum of \(\text{diff}\) to obtain the toggle count for each light \(i\) (\(1 \leq i \leq N\))
- Count and output the number of lights with an odd toggle count
Complexity
- Time complexity: \(O(N + M)\)
- Writing to the difference array takes \(O(M)\), and computing the prefix sum and counting takes \(O(N)\)
- Space complexity: \(O(N)\)
- For the difference array
Implementation Notes
By making the difference array size \(N+2\), we prevent out-of-bounds access even when \(R+1 = N+1\)
Using
sys.stdin.buffer.read()to read all input at once enables fast input processing even in PythonManaging the prefix sum with a variable
currentand counting without creating a separate array improves memory efficiencySource 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 + 2)
for _ 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 i in range(1, N + 1):
current += diff[i]
if current % 2 == 1:
count += 1
print(count)
main()
This editorial was generated by claude4.6opus-thinking.
投稿日時:
最終更新: