Official

D - モンスター討伐 / Monster Subjugation Editorial by admin Editorial for printing

Claude Opus 5 (High)

Overview

In this problem, we perform an operation of dealing a uniform amount of damage to a range \(N\) times, and finally count the number of monsters whose cumulative damage is greater than or equal to their health (cumulative damage \(\geq\) health). This is a textbook problem using the so-called Imos method (difference array + prefix sums).

Analysis

Naive Approach and Its Issues

The first implementation that comes to mind is to iterate from \(L_i\) to \(R_i\) for each attack and add the damage directly.

for i in 1..N:
    for j in L_i..R_i:
        damage[j] += A_i

However, this approach could potentially update up to \(M = 10^6\) elements per attack. Since there are \(N = 10^5\) attacks, this would require up to \(10^{11}\) operations in the worst case. This will easily exceed the time limit and result in TLE.

Key Observations

There are two important observations here:

  1. Monsters are not removed even if they are defeated midway, so there is no need to track when each monster was defeated. It is sufficient to know only the total damage dealt to each monster at the end. In other words, the order of attacks does not affect the final result at all.
  2. The total damage is the sum of \(A_i\) across all attacks covering that monster, which can be computed efficiently if we can process range additions collectively.

This is where the difference array (Imos method) comes in. We can replace the operation of adding \(A\) to the interval \([L, R]\) with updates to just two positions:

  • \(\mathrm{diff}[L] \mathrel{+}= A\)
  • \(\mathrm{diff}[R+1] \mathrel{-}= A\)

After processing all attacks, taking the prefix sums of \(\mathrm{diff}\) yields the exact total damage taken by monster \(j\) at position \(j\).

Concrete Example

Suppose \(M = 5\), and there are two attacks: “add \(3\) to \([2,4]\)” and “add \(5\) to \([1,3]\)”.

The difference array (indices \(1..6\)) is updated as follows:

  • 1st attack: \(\mathrm{diff}[2] {+}{=}3\), \(\mathrm{diff}[5] {-}{=}3\)
  • 2nd attack: \(\mathrm{diff}[1] {+}{=}5\), \(\mathrm{diff}[4] {-}{=}5\)

Thus, \(\mathrm{diff} = [5, 3, 0, -5, -3, 0]\). Taking the prefix sums gives:

\[\text{damage} = [5,\ 8,\ 8,\ 3,\ 0]\]

Indeed, monster 1 takes \(5\), monsters 2 and 3 take \(3+5=8\), monster 4 takes \(3\), and monster 5 takes \(0\) damage, which is correct.

Finally, we simply compare this with \(H_j\) and count the number of monsters satisfying \(\text{damage}_j \geq H_j\).

Algorithm

  1. Initialize a difference array \(\mathrm{diff}\) of length \(M+2\) with \(0\).
  2. For each attack \((L_i, R_i, A_i)\), perform \(\mathrm{diff}[L_i] \mathrel{+}= A_i\) and \(\mathrm{diff}[R_i+1] \mathrel{-}= A_i\).
  3. Set \(\mathrm{cur} = 0\) and compute \(\mathrm{cur} \mathrel{+}= \mathrm{diff}[j]\) in order for \(j = 1, 2, \dots, M\) (this represents the total damage to monster \(j\)).
  4. If \(\mathrm{cur} \geq H_j\), increment the answer counter by 1.
  5. Output the counter.

By comparing with \(H_j\) on the fly while computing the prefix sums, there is no need to even maintain a separate damage array.

Complexity

  • Time complexity: \(O(N + M)\)
  • Space complexity: \(O(M)\)

Implementation Notes

  • Beware of integer overflow: The total damage can reach up to \(N \times \max A = 10^5 \times 10^9 = 10^{14}\). This will overflow a 32-bit integer, so be sure to use 64-bit integers for the difference array and prefix sums (long long in C++; in Python, you don’t need to worry about this).

  • Array size: When \(R_i = M\), the algorithm accesses \(\mathrm{diff}[M+1]\), so allocating \(M+2\) elements is safe (when using 1-based indexing).

  • Fast I/O: The input size is relatively large, reading \(M \leq 10^6\) health values and \(N \leq 10^5\) attacks. In C++, untie cin (e.g., ios::sync_with_stdio(false)) or read in bulk with fread and parse manually as in the code above to be safe. In Python, use sys.stdin.buffer.read().split().

  • Comparison is “greater than or equal to”: A monster is also defeated when the damage is exactly equal to \(H_j\). Be sure to use >= instead of >.

  • The core insight is reading from the problem statement that neither the order of attacks nor whether a monster was defeated midway matters. Misinterpreting this can lead to an unnecessarily complicated implementation.

    Source Code

#include <bits/stdc++.h>
using namespace std;

static char buf[1 << 25];

int main() {
    size_t len = fread(buf, 1, sizeof(buf) - 1, stdin);
    buf[len] = '\0';
    char *p = buf;
    auto readInt = [&]() -> long long {
        while (*p && (*p < '0' || *p > '9') && *p != '-') ++p;
        bool neg = false;
        if (*p == '-') { neg = true; ++p; }
        long long x = 0;
        while (*p >= '0' && *p <= '9') { x = x * 10 + (*p - '0'); ++p; }
        return neg ? -x : x;
    };

    long long N = readInt();
    long long M = readInt();
    vector<int> H(M + 1);
    for (long long j = 1; j <= M; ++j) H[j] = (int)readInt();
    vector<long long> diff(M + 2, 0);
    for (long long i = 0; i < N; ++i) {
        long long L = readInt(), R = readInt(), A = readInt();
        diff[L] += A;
        diff[R + 1] -= A;
    }
    long long cur = 0;
    long long cnt = 0;
    for (long long j = 1; j <= M; ++j) {
        cur += diff[j];
        if (cur >= (long long)H[j]) ++cnt;
    }
    printf("%lld\n", cnt);
    return 0;
}

This editorial was generated by claude5opus-high.

posted:
last update: