D - モンスター討伐 / Monster Subjugation Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We find the total damage dealt to each monster and count how many monsters receive damage greater than or equal to their initial HP. We use the imos method (difference array and prefix sums) to efficiently process range additions.
Analysis
Direct Simulation of Attacks is Too Slow
Consider an approach that iterates through monsters from \(L_i\) to \(R_i\) and adds damage for each attack.
In this approach, each attack updates up to \(M\) monsters, resulting in an overall time complexity of \(O(NM)\). Under the maximum constraints, this requires as many as \(10^{11}\) updates, which will not finish within the time limit.
Recording Only the Endpoints of the Intervals
In this problem, monsters defeated during the attacks are not removed from the line. Therefore, there is no need to track their HP at intermediate steps; it is sufficient to determine the total damage dealt to each monster after all attacks are finished.
Thus, instead of updating all elements within the range, we record only at the following two positions in a difference array diff:
- At the start position of the attack \(L_i\), increase the damage by \(A_i\).
- Immediately after the end position of the attack \(R_i+1\), decrease the damage by \(A_i\).
Finally, taking the prefix sum from left to right yields the total damage at each position.
For example, when \(M=5\) and an attack deals \(3\) damage to the range \([2,4]\), we add \(3\) to diff[2] and \(-3\) to diff[5].
| Position | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Difference | 0 | 3 | 0 | 0 | -3 |
| Prefix Sum | 0 | 3 | 3 | 3 | 0 |
In this way, \(3\) damage is applied only to the range \([2,4]\). Multiple attacks can be processed in the same manner by accumulating these differences.
Algorithm
- Store the initial HP of each monster in an array
H. - Prepare a difference array
diffof length \(M+2\), initialized to \(0\). - For each attack \((L_i,R_i,A_i)\), perform the following updates:
diff[L_i] += A_idiff[R_i + 1] -= A_i
- Initialize a variable
damagerepresenting the accumulated damage andanswerrepresenting the number of defeated monsters to \(0\). - Iterate through the monsters in the order \(j=1,2,\ldots,M\):
- Update
damage += diff[j]to compute the total damage at that position. - If
damage >= H[j], incrementanswerby \(1\).
- Update
- Output
answer.
Why This Works
Consider a single attack \((L_i,R_i,A_i)\).
The contribution of this attack to the prefix sums of the difference array is as follows:
- For \(j<L_i\), \(A_i\) has not been added yet, so it is \(0\).
- For \(L_i\leq j\leq R_i\), \(A_i\) has been added, so it is \(A_i\).
- For \(j>R_i\), \(A_i\) and \(-A_i\) cancel each other out, so it is \(0\).
In other words, \(A_i\) is correctly added only to the monsters targeted by this attack. Since this holds when summing over all attacks, the prefix sum matches the total damage received by each monster.
Therefore, by counting the monsters whose total damage is at least \(H_j\), we can find the number of defeated monsters.
Complexity
- Time complexity: \(O(N+M)\)
- Space complexity: \(O(M)\)
Each attack requires updating only two positions, taking \(O(1)\) time, and the final prefix sum and checks examine each monster once, taking \(O(M)\) time.
Key Implementation Points
Use 64-bit integers for the total damage.
A monster can receive up to \(10^5\times10^9=10^{14}\) damage. Since this does not fit in a 32-bit integer, use types like C++’slong longfordiffanddamage.Subtract at \(R_i+1\), not \(R_i\).
Monster \(R_i\) is also targeted by the attack, so the effect of the addition must be canceled at the next position.Size the difference array large enough to access index \(M+1\).
When \(R_i=M\),diff[M + 1]is updated. Therefore, in 1-indexed code, allocate a size of at least \(M+2\).A monster is defeated even if the damage equals its HP.
The condition isdamage >= H[j], notdamage > H[j].Source Code
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, M;
cin >> N >> M;
vector<long long> H(M + 1);
for (int j = 1; j <= M; ++j) {
cin >> H[j];
}
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;
}
long long damage = 0;
int answer = 0;
for (int j = 1; j <= M; ++j) {
damage += diff[j];
if (damage >= H[j]) {
++answer;
}
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: