D - モンスター討伐 / Monster Subjugation Editorial by admin Editorial for printing
Claude Opus 5 (High)Overview
In this problem, after performing \(N\) range additions, we need to count the number of monsters whose total damage taken is greater than or equal to their HP. This is a typical problem utilizing the Imos method (range additions using a difference array).
Analysis
Naive Approach
A straightforward idea is to iterate over each attack and actually add \(A_i\) to the damage of each monster from \(L_i\) to \(R_i\).
for i in 1..N:
for j in L_i..R_i:
damage[j] += A_i
However, this approach updates up to \(M = 10^6\) elements per attack, resulting in a worst-case time complexity of \(O(NM) = 10^5 \times 10^6 = 10^{11}\), which will easily exceed the time limit (TLE).
Key Observations
Here are two important observations:
- Monsters defeated mid-way are not removed, so the order of attacks does not affect the outcome at all. We only need to find the “final cumulative damage” for each monster.
- We only need the total accumulated damage at each position, and we do not need to know any intermediate states.
In other words, we only need to reconstruct the array once after all \(N\) range additions are performed, which can be done very efficiently using differences (a difference array).
Concept of Difference Array (Imos Method)
Consider an array \(D\) of length \(M\) (all initialized to \(0\), with an extra element \(D[M+1]\) as a sentinel). Instead of adding \(A\) to the interval \([L, R]\), we update only two positions:
- \(D[L] \mathrel{+}= A\)
- \(D[R+1] \mathrel{-}= A\)
After processing all attacks, computing the prefix sums of \(D\) from left to right:
\[ \text{damage}[j] = D[1] + D[2] + \dots + D[j] \]
gives the total damage taken by monster \(j\).
Concrete Example
Suppose \(M = 5\), and there are two attacks: \((L,R,A) = (2,4,3)\) and \((1,3,5)\).
Difference array (indices 1 to 6): - 1st attack: \(D[2] {+}{=} 3\), \(D[5] {-}{=} 3\) → \(D = [0, 3, 0, 0, -3, 0]\) - 2nd attack: \(D[1] {+}{=} 5\), \(D[4] {-}{=} 5\) → \(D = [5, 3, 0, -5, -3, 0]\)
Taking the prefix sums yields \([5, 8, 8, 3, 0]\). Indeed, monster 1 took \(5\) damage, monsters 2–3 took \(3+5=8\) damage, monster 4 took \(3\) damage, and monster 5 took \(0\) damage, which is correct.
Algorithm
- Initialize a difference array
diffof length \(M+2\) with \(0\). - For each attack \((L_i, R_i, A_i)\), perform
diff[L_i] += A_ianddiff[R_i + 1] -= A_i. - For \(j = 1, 2, \dots, M\) in order, update the running prefix sum with
cur += diff[j]. Ifcur >= H[j], increment the answer counter by 1. - Output the counter.
A key point is that there is no need to create a separate array for prefix sums; we can maintain a single running sum variable and check the condition simultaneously.
Complexity
- Time Complexity: \(O(N + M)\)
- Space Complexity: \(O(N + M)\) (for the difference array and storing the input)
Since \(N \le 10^5\) and \(M \le 10^6\), this is fast enough.
Implementation Notes
Allocating Sentinels: When \(R_i = M\),
diff[R_i + 1]accesses index \(M+1\), so the array size must be at least \(M+2\) (allowing access up to index \(M+1\) with 0-indexing). Forgetting this will cause an out-of-bounds error.Integer Overflow: Since the total damage can reach up to \(10^5 \times 10^9 = 10^{14}\), languages like C++ require the use of
long long. In Python, integers have arbitrary precision automatically, so you don’t need to worry about it.Fast I/O: Because the input size is large (\(M \le 10^6\), \(N \le 10^5\)), calling
input()each time in Python will be too slow; reading the entire input at once usingsys.stdin.buffer.read().split()is essential.Avoiding Unnecessary Conversions: In the code mentioned above, instead of converting all HP values \(H\) to
intinitially, keeping them as byte strings and converting withint(H[j-1])only at the moment of comparison reduces redundant operations. (Of course, converting all at once withlist(map(int, ...))often passes as well, but constant factor optimizations can be effective in Python).Check for “Greater than or Equal to”: A monster is defeated when “the total damage is at least \(H_j\),” so the comparison operator is
>=. Be careful not to use>.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
idx = 0
n = int(data[idx]); idx += 1
m = int(data[idx]); idx += 1
H = data[idx:idx + m]
idx += m
diff = [0] * (m + 2)
for _ in range(n):
l = int(data[idx]); r = int(data[idx + 1]); a = int(data[idx + 2])
idx += 3
diff[l] += a
diff[r + 1] -= a
ans = 0
cur = 0
for j in range(1, m + 1):
cur += diff[j]
if cur >= int(H[j - 1]):
ans += 1
print(ans)
main()
This editorial was generated by claude5opus-high.
posted:
last update: