D - モンスター討伐 / Monster Subjugation Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
This problem asks us to find the number of monsters whose total accumulated damage is greater than or equal to their initial health \(H_j\) after performing \(N\) range-damage attacks on \(M\) monsters.
Analysis
A Naive Approach and Its Limitations
A straightforward method that first comes to mind is to iterate over each attack and add the damage \(A_i\) to each monster in the range \([L_i, R_i]\). However, since a single attack can damage up to \(M\) monsters, the worst-case time complexity would be \(O(N \times M)\). Given the constraints \(N \leq 10^5, M \leq 10^6\), this could require up to approximately \(10^{11}\) operations, exceeding the time limit and resulting in TLE (Time Limit Exceeded).
Solution: Using the Imos Method
In this problem, we do not need to inspect the state after each attack; it is sufficient to know only the “final total damage after all attacks have been completed.” When we want to find the final values all at once after all range addition queries have been processed, using the Imos method (difference management using 1D prefix sums) is extremely effective.
Algorithm
By using the Imos method, each range addition can be performed in \(O(1)\) time.
- Preparing the difference array:
Prepare an array
diffof size \(M + 2\) and initialize all elements to \(0\). - Recording differences:
For an attack dealing \(A_i\) damage to the range \([L_i, R_i]\), we only update two endpoints as follows:
diff[L_i] += A_i(damage starts at the beginning of the interval)diff[R_i + 1] -= A_i(the effect of the damage ends right after the end of the interval) We repeat this for all \(N\) attacks. The time complexity per attack is \(O(1)\).
- Reconstructing damage via prefix sums and checking conditions:
By computing the cumulative sum (prefix sum) of
diffsequentially from monster \(1\) to \(M\), we can obtain the total damage taken by monster \(j\). If the total damage is greater than or equal to the initial health \(H_j\), increment the count of defeated monsters by \(1\).
Concrete Example
Consider an example with 4 monsters (\(M = 4\)) where damage \(5\) is applied to the range \([2, 3]\):
- We record diff[2] += 5 and diff[4] -= 5.
- Taking the prefix sum from left to right, the damage to each monster is as follows:
- Monster 1: \(0\)
- Monster 2: \(0 + 5 = 5\)
- Monster 3: \(5 + 0 = 5\)
- Monster 4: \(5 - 5 = 0\)
We can see that \(5\) has been correctly added only to the range \([2, 3]\).
Complexity
- Time Complexity: \(O(N + M)\)
- Recording differences takes \(O(N)\) time, and computing the final prefix sums and checking conditions takes \(O(M)\) time. Since \(N \leq 10^5, M \leq 10^6\), the total number of operations is around \(1.1 \times 10^6\), which easily runs well within the time limit.
- Space Complexity: \(O(M)\)
- We use \(O(M)\) memory to store the health array \(H\) and the difference array
diff.
- We use \(O(M)\) memory to store the health array \(H\) and the difference array
Implementation Notes
Fast I/O: Since the input size is large (the number of input integers is \(2 + M + 3N \approx 1.3 \times 10^6\)), reading the entire input at once using
sys.stdin.read().split()in Python avoids I/O bottlenecks.Array Size: When \(R_i = M\),
diff[R_i + 1](i.e.,diff[M + 1]) is accessed. To prevent out-of-bounds index errors, allocate an array of size at least \(M + 2\).Magnitude of Values: The total damage can reach up to \(N \times \max(A_i) = 10^5 \times 10^9 = 10^{14}\). In Python, arbitrary-precision integers are handled automatically, so there is no risk of overflow. However, when implementing in languages like C++, a 64-bit integer type (
long long) must be used.Source Code
import sys
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
M = int(input_data[1])
H = [int(x) for x in input_data[2 : 2 + M]]
diff = [0] * (M + 2)
idx = 2 + M
for _ in range(N):
l = int(input_data[idx])
r = int(input_data[idx + 1])
a = int(input_data[idx + 2])
diff[l] += a
diff[r + 1] -= a
idx += 3
ans = 0
current_damage = 0
for j in range(1, M + 1):
current_damage += diff[j]
if current_damage >= H[j - 1]:
ans += 1
print(ans)
if __name__ == "__main__":
main()
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: