結果

問題 No.60 魔法少女
ユーザー reika727reika727
提出日時 2024-02-15 21:52:13
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 276 ms / 5,000 ms
コード長 4,926 bytes
コンパイル時間 3,526 ms
コンパイル使用メモリ 259,208 KB
実行使用メモリ 22,272 KB
最終ジャッジ日時 2024-02-15 21:52:20
合計ジャッジ時間 6,785 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
21,376 KB
testcase_01 AC 16 ms
21,376 KB
testcase_02 AC 17 ms
21,376 KB
testcase_03 AC 18 ms
21,376 KB
testcase_04 AC 146 ms
21,376 KB
testcase_05 AC 143 ms
22,144 KB
testcase_06 AC 276 ms
22,144 KB
testcase_07 AC 178 ms
21,888 KB
testcase_08 AC 116 ms
21,760 KB
testcase_09 AC 58 ms
21,376 KB
testcase_10 AC 218 ms
22,144 KB
testcase_11 AC 35 ms
21,376 KB
testcase_12 AC 98 ms
21,888 KB
testcase_13 AC 274 ms
22,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

template <std::default_initializable T, size_t Dimension>
class imos final
    : private std::conditional_t<
        Dimension == 1,
        std::vector<T>,
        std::vector<imos<T, Dimension - 1>>
      >
{
    static_assert(Dimension != 0, "Dimension must not be 0.");
    friend imos<T, Dimension + 1>;

private:
    using lower_dimension = std::conditional_t<
        Dimension == 1,
        T,
        imos<T, Dimension - 1>
    >;

    std::size_t cast_index(const std::ptrdiff_t index) const noexcept
    {
        const std::ptrdiff_t elem_count = this->size() - 1;
        return (index % elem_count + elem_count) % elem_count;
    }

    std::ptrdiff_t floor_index(const std::ptrdiff_t index) const noexcept
    {
        const std::ptrdiff_t elem_count = this->size() - 1;
        return index >= 0 ? index / elem_count : (index + 1) / elem_count - 1;
    }

    imos<T, Dimension> &operator+=(const imos<T, Dimension> &im)
    {
        for (auto i = 0uz; i < this->size(); ++i) {
            (*this)[i] += im[i];
        }
        return *this;
    }

public:
    imos(const std::size_t size, const auto... sizes)
        : std::vector<lower_dimension>(
              size + 1,
              lower_dimension(sizes...)
          )
    {
        static_assert(
            1 + sizeof...(sizes) == Dimension,
            "Number of sizes must be equal to Dimension."
        );
    }

    const T& at(const std::ptrdiff_t index, const auto... indices) const
    {
        static_assert(
            1 + sizeof...(indices) == Dimension,
            "Number of indices must be equal to Dimension."
        );
        if constexpr (sizeof...(indices) == 0) {
            return (*this)[cast_index(index)];
        } else {
            return (*this)[cast_index(index)].at(indices...);
        }
    }

    void set(
        const std::span<const std::ptrdiff_t, Dimension> begin,
        const std::span<const std::ptrdiff_t, Dimension> end,
        const T weight
    )
    {
        if (weight == 0) {
            return;
        }
        const auto cross_count = floor_index(end.front()) - floor_index(begin.front());
        if constexpr (Dimension > 1) {
            (*this)[cast_index(begin.front())].set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                weight
            );
            (*this)[cast_index(end.front())].set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                -weight
            );
            this->front().set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                weight * cross_count
            );
            this->back().set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                -weight * cross_count
            );
        } else {
            (*this)[cast_index(begin.front())] += weight;
            (*this)[cast_index(end.front())] -= weight;
            this->front() += weight * cross_count;
            this->back() -= weight * cross_count;
        }
    }

    void set(
        const std::array<const std::ptrdiff_t, Dimension> &begin,
        const std::array<const std::ptrdiff_t, Dimension> &end,
        const T weight
    )
    {
        set(std::span{begin}, std::span{end}, weight);
    }

    void set(const std::ptrdiff_t begin, const std::ptrdiff_t end, const T weight)
    {
        static_assert(
            Dimension == 1,
            "set(ptrdiff_t, ptrdiff_t, T) can be called only when Dimension == 1."
        );
        return set(std::array{begin}, std::array{end}, weight);
    }

    void accumulate()
    {
        for (auto i = 0uz; i < this->size() - 1; ++i) {
            (*this)[i + 1] += (*this)[i];
        }
        if constexpr (Dimension > 1) {
            for (auto &child : *this) {
                child.accumulate();
            }
        }
    }
};

template <class T>
auto make_imos(auto... sizes)
{
    return imos<T, sizeof...(sizes)>(sizes...);
}

int main()
{
    struct enemy {
        int x, y, hp;
    };

    int n, k;
    std::cin >> n >> k;
    std::vector<enemy> enemies(n);
    for (int i = 0; i < n; ++i) {
        int x, y, hp;
        std::cin >> x >> y >> hp;
        enemies[i] = {
            x + 500,
            y + 500,
            hp
        };
    }

    auto imos = make_imos<int>(1501, 1501);
    for (int i = 0; i < k; ++i) {
        int x, y, w, h, d;
        std::cin >> x >> y >> w >> h >> d;
        x += 500;
        y += 500;
        imos.set(
            {y, x},
            {y + h + 1, x + w + 1},
            d
        );
    }
    imos.accumulate();

    std::cout << std::accumulate(
        enemies.begin(), enemies.end(), 0,
        [imos](int acc, const enemy &e) {
            return acc + std::max(0, e.hp - imos.at(e.y, e.x));
        }
    ) << std::endl;
}
0