D - モンスター討伐 / Monster Subjugation Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
This problem asks us to find the total number of monsters whose “total damage received \(\geq\) initial health” after performing \(N\) area-of-effect attacks (range additions) on \(M\) monsters.
Analysis
Problem Observations
An important point to note is stated in the problem: “Monsters defeated during an attack are not removed from the line and remain targets for subsequent attacks.” This implies that we do not need to simulate whether a monster is defeated after each individual attack; rather, we only need to compute the total damage each monster has received after all attacks have finished.
Naive Approach and Its Limitations
The most straightforward approach is to loop over each attack \(i\) and increase the damage of every monster in the interval \([L_i, R_i]\) by \(A_i\). However, since each attack can update the health of up to \(M\) monsters, the worst-case time complexity is \(O(N \times M)\). Given the constraints \(N \leq 10^5\) and \(M \leq 10^6\), the total number of operations can reach up to \(10^5 \times 10^6 = 10^{11}\) (100 billion), which will not fit within the time limit (typically around 2 seconds) and will result in TLE (Time Limit Exceeded).
Speedup via Difference Arrays (Imos Method)
To perform range additions efficiently, we use a difference array (the Imos method: recording differences and taking prefix sums at the end).
When we want to add a value \(A_i\) to the interval \([L_i, R_i]\), instead of updating all elements in the range, we only record the changes at the “boundaries (endpoints)”.
Specifically, we prepare a difference array diff and update only \(2\) positions:
diff[L_i] += A_i(damage increases at the start of the interval)diff[R_i + 1] -= A_i(damage decreases right after the end of the interval)
This allows each attack to be processed in \(O(1)\) time. After recording all attacks, we can reconstruct the total damage received by each monster all at once by computing the prefix sums from left to right.
Algorithm
- Initialize the Difference Array:
Prepare an array
diffof size \(M + 2\), initialized with all \(0\)s. - Record Attack Differences:
For each attack \((L_i, R_i, A_i)\), perform the following (Complexity: \(O(N)\)):
diff[L_i] += A_idiff[R_i + 1] -= A_i
- Reconstruct Total Damage and Count:
Iterate through monsters \(1\) to \(M\) in order (Complexity: \(O(M)\)):
- Accumulate
diff[i]into a variablecurrent_damageto compute the prefix sum, which represents the total damage monster \(i\) received. - If
current_damage >= H_i, increment the count of defeated monsters by \(1\).
- Accumulate
- Output the final count.
Concrete Example
For example, suppose \(M = 4\) with initial health values \([3, 5, 2, 4]\).
If an attack deals \(2\) damage to monsters \(1 \sim 3\):
- diff[1] += 2
- diff[4] -= 2 (at position \(3 + 1 = 4\))
The diff array after the attack: [0, +2, 0, 0, -2] (1-indexed).
Taking the prefix sums gives the total damages [2, 2, 2, 0], which correctly reflects that \(2\) damage was dealt to the interval \([1, 3]\).
Complexity
- Time Complexity: \(O(N + M)\)
- Recording differences takes \(O(N)\), and the final prefix sum and checks take \(O(M)\). With \(N \leq 10^5\) and \(M \leq 10^6\), this requires around \(10^6\) operations, which easily finishes within a few to several tens of milliseconds.
- Space Complexity: \(O(M)\)
- Both the array storing monster healths and the difference array require \(O(M)\) space.
Implementation Notes
Watch Out for Integer Overflow: The maximum damage a monster can receive can reach \(N \times \max(A_i) = 10^5 \times 10^9 = 10^{14}\). This significantly exceeds the maximum value of a standard 32-bit signed integer (
intin C++, which is about \(2 \times 10^9\)), so be sure to use a 64-bit integer type (long longin C++).Preventing Out-of-Bounds Access: When \(R_i = M\), the code accesses index \(R_i + 1 = M + 1\). When using 1-based indexing, ensure that the array size is allocated to at least \(M + 2\).
Fast I/O: Because the input size is large, in C++, it is recommended to speed up standard I/O by including
cin.tie(nullptr); ios::sync_with_stdio(false);.Source Code
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<long long> h(m + 1);
for (int i = 1; i <= m; ++i) {
cin >> h[i];
}
vector<long long> diff(m + 2, 0);
for (int i = 0; i < n; ++i) {
int l, r;
long long a;
cin >> l >> r >> a;
diff[l] += a;
diff[r + 1] -= a;
}
int ans = 0;
long long current_damage = 0;
for (int i = 1; i <= m; ++i) {
current_damage += diff[i];
if (current_damage >= h[i]) {
ans++;
}
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: