Official

D - 魔物の一掃 / Sweeping Out Monsters Editorial by admin Editorial for printing

Claude Opus 5 (High)

Overview

You are given \(N\) monsters and \(M\) scrolls. Scroll \(j\) deals \(C_j\) damage to monsters in the range \([L_j, R_j]\) at the cost of \(W_j\). The problem asks to find the minimum total cost to choose a set of scrolls such that all monsters are defeated (the total damage taken by each monster is greater than or equal to its health).

Analysis

Key Observation 1: \(M\) is very small

Looking closely at the constraints, we have \(M \leq 15\). This means there are at most \(2^{15} = 32768\) combinations of which scrolls to use. This is small enough to perform an exhaustive search (brute-force).

In competitive programming, a small constraint like \(M \leq 20\) is often a hint for “subset enumeration (bit brute-force)”.

Key Observation 2: Each scroll is simply “used or not used”

Since each scroll can be used at most once, the decision is binary: “use” or “do not use”. Furthermore, the order in which scrolls are used does not affect the outcome (damage values are simply summed up). Therefore, the state is completely determined solely by the set of scrolls used.

Pitfalls of a Naive Approach

If we consider a greedy approach such as “using cheaper scrolls for monsters with higher health first,” we can easily construct counterexamples because ranges overlap.

For example, suppose there are \(3\) monsters with health values \((5, 5, 5)\), and: - Scroll A: range \([1,3]\), damage \(5\), cost \(10\) - Scroll B: range \([1,1]\), damage \(5\), cost \(4\) - Scroll C: range \([2,3]\), damage \(5\), cost \(4\)

In this case, choosing B + C (cost \(8\)) is cheaper than choosing A alone (cost \(10\)). Because the optimal solution depends on how the ranges overlap and combine, a greedy approach does not work correctly. On the other hand, bit brute-force is safe because it tries all possible combinations.

How to Check Each Subset

When a subset \(S\) is chosen, we calculate the total damage \(D_i\) taken by each monster \(i\) and check whether \(D_i \geq A_i\) holds for all \(i\).

If we naively loop from \(L_j\) to \(R_j\) to add damage for each chosen scroll, it takes \(O(NM)\) in the worst case for a single subset, leading to an overall time complexity of \(O(2^M N M) = 32768 \times 200 \times 15 \approx 10^8\). While this might barely pass, using the Imos method (difference array) allows us to perform range additions in \(O(1)\), reducing the overall time complexity to \(O(2^M (N + M))\).

Algorithm

Bit Brute-Force + Imos Method (Prefix Sums on Difference Array)

  1. For each \(s = 0, 1, \ldots, 2^M - 1\), interpret that scroll \(j\) is used if the \(j\)-th bit of \(s\) is \(1\).
  2. Compute the cost \(\mathrm{cost} = \sum_{j \in s} W_j\).
  3. Prepare a difference array diff, and for each chosen scroll, perform:
    • diff[L_j] += C_j
    • diff[R_j + 1] -= C_j

(Imos method). 4. By taking the cumulative sum (prefix sum) of diff from left to right, the accumulated value at position \(i\) gives the total damage \(D_i\) dealt to monster \(i\). 5. If \(D_i \geq A_i\) for all \(i\), this subset is “valid”. Update \(\mathrm{best} = \min(\mathrm{best}, \mathrm{cost})\). 6. Finally, if \(\mathrm{best}\) was never updated, output \(-1\); otherwise, output \(\mathrm{best}\).

Notes on the Imos Method

When we want to add \(C\) uniformly to a range \([L, R]\), we record \(+C\) at index \(L\) and \(-C\) at index \(R+1\) in a difference array. Taking the prefix sum from the front yields the array where \(C\) has been added precisely to the range \([L, R]\). This allows recording \(M\) range additions in \(O(M)\) and reconstructing the values in \(O(N)\), making it independent of the range lengths.

Pruning

If the total cost \(\mathrm{cost}\) is already greater than or equal to the current tentative minimum \(\mathrm{best}\), this subset cannot improve the answer, so there is no need to check it. Therefore, skipping it with if (cost >= best) continue; before computing the prefix sums further reduces the execution time (while this does not change the asymptotic time complexity, it is practically effective).

Complexity

  • Time complexity: \(O(2^M (N + M))\) — roughly \(32768 \times 215 \approx 7 \times 10^6\) operations, which is sufficiently fast
  • Space complexity: \(O(N + M)\)

Key Implementation Points

  • Watch out for overflow: When up to \(15\) scrolls with \(C_j \leq 10^9\) overlap, the damage can reach up to \(1.5 \times 10^{10}\), and the cost can also reach up to \(1.5 \times 10^{10}\) (\(W_j \leq 10^9 \times 15\)). Since this exceeds the range of a 32-bit integer, use 64-bit integers (long long) for health, damage, and cost.

  • Difference array size: Because diff[R_j + 1] is accessed, the index becomes \(N+1\) when \(R_j = N\). Ensure the array size is at least \(N+2\). Also, remember to reset it to \(0\) at the beginning of each iteration of \(s\).

  • Initial value of the answer: Initialize best to a sufficiently large value such as LLONG_MAX to represent “impossible”, and output \(-1\) if it is never updated. Note that for \(s = 0\) (using no scrolls), since \(A_i \geq 1\), the validation will always fail, so cost \(0\) will never be mistakenly accepted as the answer.

  • Early exit during validation: Breaking early as soon as \(D_i < A_i\) for any monster avoids unnecessary computations.

    Source Code

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

int main(){
    int N,M;
    scanf("%d %d",&N,&M);
    vector<long long> A(N+1);
    for(int i=1;i<=N;i++) scanf("%lld",&A[i]);
    vector<int> L(M),R(M);
    vector<long long> C(M),W(M);
    for(int j=0;j<M;j++){
        scanf("%d %d %lld %lld",&L[j],&R[j],&C[j],&W[j]);
    }
    long long best = LLONG_MAX;
    vector<long long> diff(N+2);
    for(int s=0;s<(1<<M);s++){
        long long cost=0;
        for(int i=0;i<=N+1;i++) diff[i]=0;
        for(int j=0;j<M;j++){
            if(s>>j&1){
                cost+=W[j];
                diff[L[j]]+=C[j];
                diff[R[j]+1]-=C[j];
            }
        }
        if(cost>=best) continue;
        long long cur=0;
        bool ok=true;
        for(int i=1;i<=N;i++){
            cur+=diff[i];
            if(cur<A[i]){ok=false;break;}
        }
        if(ok) best=cost;
    }
    if(best==LLONG_MAX) printf("-1\n");
    else printf("%lld\n",best);
    return 0;
}

This editorial was generated by claude5opus-high.

posted:
last update: