C - 花壇の水やり / Watering the Flower Bed 解説 by admin
Claude 4.5 OpusOverview
This problem involves multiple range addition queries on consecutive elements, where we need to find the value of each element after all operations. This can be solved efficiently using the “imos method” (cumulative sum technique).
Analysis
Problems with the Naive Approach
A straightforward approach would be, for each watering query \((L_j, R_j)\), to add \(K\) to all flower beds from \(L_j\) to \(R_j\).
for j in range(Q):
for i in range(L[j], R[j]+1):
C[i] += K
However, this method has a worst-case time complexity of \(O(N \times Q)\). When both \(N\) and \(Q\) are at most \(2 \times 10^5\), this requires \(4 \times 10^{10}\) operations, resulting in TLE (Time Limit Exceeded).
Key Insight
We realize that we only need to record the count of “how many times watering occurred” for each query, and multiply by \(K\) at the end. In other words, for each flower bed \(i\), we find the “number of times watered” and finally calculate \(C_i + (\text{count}) \times K\).
The imos method can be used as a technique to “efficiently process range additions.”
Algorithm
What is the Imos Method?
It’s a technique that records range \([L, R]\) additions in \(O(1)\) using a difference array, then restores the value at each position by taking the cumulative sum at the end.
Specifically:
1. Prepare a difference array diff (all initial values are 0)
2. When you want to add +1 to range \([L, R]\):
- diff[L] += 1 (the +1 starts from this position)
- diff[R+1] -= 1 (the +1 ends at this position)
3. Taking the cumulative sum of diff gives the number of additions at each position
Concrete Example
For \(N = 5\), watering ranges \([2, 4]\) and \([3, 5]\):
Initial: diff = [0, 0, 0, 0, 0, 0] (size N+1)
+1 to [2,4]: diff[1] += 1, diff[4] -= 1
diff = [0, 1, 0, 0, -1, 0]
+1 to [3,5]: diff[2] += 1, diff[5] -= 1
diff = [0, 1, 1, 0, -1, -1]
Cumulative sum: count = [0, 1, 2, 2, 1, 0]
→ Flower bed 1: 1 time, bed 2: 2 times, bed 3: 2 times, bed 4: 1 time, bed 5: 0 times
(※ For 0-indexed. The code uses L-1 and R)
Complexity
- Time Complexity: \(O(N + Q)\)
- Processing each query: \(O(1) \times Q = O(Q)\)
- Cumulative sum calculation and output: \(O(N)\)
- Space Complexity: \(O(N)\)
- \(O(N)\) for the difference array
diffand result arrayresult
- \(O(N)\) for the difference array
Implementation Notes
Index Conversion: The problem uses 1-indexed flower beds, but Python arrays are 0-indexed, so we use
diff[L-1]anddiff[R].Difference Array Size: Since we access
diff[R], the size needs to be \(N+1\) (to accessdiff[N]when \(R\) is \(N\)).Overflow Prevention: The product of \(K\) and the watering count can become large, but Python has no integer overflow, so there’s no need to worry.
Fast Input: Since \(N\) and \(Q\) are large, reading all input at once with
sys.stdin.read()is faster.
Source Code
import sys
def main():
input_data = sys.stdin.read().split()
idx = 0
N = int(input_data[idx]); idx += 1
K = int(input_data[idx]); idx += 1
Q = int(input_data[idx]); idx += 1
C = [int(input_data[idx + i]) for i in range(N)]
idx += N
# Difference array for imos method
diff = [0] * (N + 1)
for _ in range(Q):
L = int(input_data[idx]); idx += 1
R = int(input_data[idx]); idx += 1
diff[L - 1] += 1
diff[R] -= 1
# Calculate cumulative sum to get watering count for each flower bed
count = 0
result = []
for i in range(N):
count += diff[i]
result.append(C[i] + count * K)
print(' '.join(map(str, result)))
if __name__ == '__main__':
main()
This editorial was generated by claude4.5opus.
投稿日時:
最終更新: