D - モンスター討伐 / Monster Subjugation Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
This problem asks us to find the total damage taken by each monster and count how many monsters have taken damage greater than or equal to their initial HP. Using the Imos method (difference array and prefix sums) allows us to process range attacks efficiently.
Analysis
Naively, for each attack, we could update the damage dealt to monsters from the \(L_i\)-th to the \(R_i\)-th monster.
However, if every attack targets all monsters, the time complexity becomes \(O(NM)\). Under the maximum constraints, this requires as many as \(10^{11}\) updates, which will exceed the time limit.
The key observation is that we do not need the intermediate state during attacks, but only the total damage after all attacks have finished. Furthermore, monsters defeated midway are not removed from the line, so the attack ranges never change.
Therefore, instead of updating all elements within the range every time, we only record changes at the following two positions:
- Damage increases by \(A_i\) starting from the \(L_i\)-th monster.
- That increase ends starting from the \((R_i + 1)\)-th monster.
By recording these changes in a difference array and computing prefix sums from left to right at the end, we can obtain the total damage for each monster.
Algorithm
1. Record Attacks in the Difference Array
Prepare an array diff initialized with all \(0\)s.
For each attack \((L_i, R_i, A_i)\), perform the following updates:
diff[L_i] += A_idiff[R_i + 1] -= A_i
For example, when \(M=5\) and an attack deals \(3\) damage to monsters \(2\) through \(4\), the 1st through 5th elements of the difference array are as follows:
| Monster Index | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Difference | 0 | 3 | 0 | 0 | -3 |
| Prefix Sum of Differences | 0 | 3 | 3 | 3 | 0 |
Taking the prefix sums adds \(3\) only to the attack range from the 2nd to the 4th monster.
Multiple attacks can be handled in the same way by accumulating all updates into the same difference array.
2. Compute Prefix Sums and Count Defeated Monsters
Initialize damage (representing the cumulative damage) and answer (representing the number of defeated monsters) to \(0\).
Iterate through monsters \(1\) to \(M\) in order, performing the following steps:
- Add the difference at the current position to
damage. - If
damageis greater than or equal to that monster’s initial HP, incrementanswerby \(1\).
Finally, output answer.
Correctness
Focusing on a single attack \((L_i, R_i, A_i)\), its contribution to the prefix sum of the difference array is as follows:
- For \(j < L_i\), \(A_i\) has not been added yet, so the contribution is \(0\).
- For \(L_i \leq j \leq R_i\), \(A_i\) has been added, so the contribution is \(A_i\).
- For \(j > R_i\), the addition and subtraction of \(A_i\) cancel each other out, so the contribution is \(0\).
In other words, the prefix sum accurately represents the damage dealt by that attack. Since we sum these up over all attacks, damage at the position of monster \(j\) matches the total damage taken.
Therefore, checking whether damage is greater than or equal to \(H_j\) correctly counts the defeated monsters.
Complexity
- Time Complexity: \(O(N+M)\)
- Space Complexity: \(O(M)\)
Each attack requires updating only two positions and can be processed in \(O(1)\) time, while the final prefix sum and evaluation take \(O(M)\) time. We use \(O(M)\) space for the HP array and the difference array.
Implementation Details
The position to subtract is
R + 1- Monster \(R\) is also targeted by the attack, so the increase in damage must be canceled out at the next position.
The length of the difference array should be
M + 2- This ensures we can safely write to
diff[M + 1]even when \(R=M\).
- This ensures we can safely write to
Pay attention to 0-based vs. 1-based indexing
- In the code,
His 0-indexed, whilediffis 1-indexed to match the monster numbers. - Thus, we update
damage += diff[j + 1]before comparing it withH[j].
- In the code,
A monster is defeated even if the damage taken equals its HP
- The condition is
damage >= H[j].
- The condition is
Total damage can be large
The total damage can reach up to \(N \times 10^9 = 10^{14}\). Python handles arbitrarily large integers automatically, but languages with fixed-width integer types require a 64-bit integer type (e.g.,
long longin C++).Source Code
import sys
def main():
input = sys.stdin.buffer.readline
N, M = map(int, input().split())
H = []
while len(H) < M:
H.extend(map(int, input().split()))
diff = [0] * (M + 2)
for _ in range(N):
L, R, A = map(int, input().split())
diff[L] += A
diff[R + 1] -= A
damage = 0
answer = 0
for j in range(M):
damage += diff[j + 1]
if damage >= H[j]:
answer += 1
print(answer)
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: