結果

問題 No.60 魔法少女
ユーザー reika727reika727
提出日時 2024-03-06 23:07:26
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 231 ms / 5,000 ms
コード長 4,978 bytes
コンパイル時間 3,221 ms
コンパイル使用メモリ 259,344 KB
実行使用メモリ 22,272 KB
最終ジャッジ日時 2024-03-06 23:07:33
合計ジャッジ時間 6,185 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
21,376 KB
testcase_01 AC 16 ms
21,376 KB
testcase_02 AC 16 ms
21,376 KB
testcase_03 AC 17 ms
21,376 KB
testcase_04 AC 145 ms
21,376 KB
testcase_05 AC 139 ms
22,144 KB
testcase_06 AC 223 ms
22,144 KB
testcase_07 AC 173 ms
21,888 KB
testcase_08 AC 122 ms
21,760 KB
testcase_09 AC 59 ms
21,376 KB
testcase_10 AC 204 ms
22,144 KB
testcase_11 AC 34 ms
21,376 KB
testcase_12 AC 98 ms
21,888 KB
testcase_13 AC 231 ms
22,272 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In instantiation of 'imos<T, Dimension>::imos(std::size_t, const auto:55 ...) [with auto:55 = {int}; T = int; long unsigned int Dimension = 2; std::size_t = long unsigned int]':
main.cpp:133:12:   required from 'auto make_imos(auto:57 ...) [with T = int; auto:57 = {int, int}]'
main.cpp:155:31:   required from here
main.cpp:39:39: warning: narrowing conversion of '(int)sizes#0' from 'int' to 'std::size_t' {aka 'long unsigned int'} [-Wnarrowing]
   39 |         : lower_dimensions(size + 1, {sizes...})
      |                                       ^~~~~

ソースコード

diff #

#include "bits/stdc++.h"

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

private:
    std::vector<
        std::conditional_t<
            Dimension == 1,
            T,
            imos<T, Dimension - 1>
        >
    > lower_dimensions;

    std::size_t cast_index(const std::ptrdiff_t index) const noexcept
    {
        const std::ptrdiff_t elem_count = lower_dimensions.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 = lower_dimensions.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 < lower_dimensions.size(); ++i) {
            lower_dimensions[i] += im.lower_dimensions[i];
        }
        return *this;
    }

public:
    imos(const std::size_t size, const auto... sizes)
        : lower_dimensions(size + 1, {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 lower_dimensions[cast_index(index)];
        } else {
            return lower_dimensions[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) {
            lower_dimensions[cast_index(begin.front())].set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                weight
            );
            lower_dimensions[cast_index(end.front())].set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                -weight
            );
            lower_dimensions.front().set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                weight * cross_count
            );
            lower_dimensions.back().set(
                begin.template subspan<1>(),
                end.template subspan<1>(),
                -weight * cross_count
            );
        } else {
            lower_dimensions[cast_index(begin.front())] += weight;
            lower_dimensions[cast_index(end.front())] -= weight;
            lower_dimensions.front() += weight * cross_count;
            lower_dimensions.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 < lower_dimensions.size() - 1; ++i) {
            lower_dimensions[i + 1] += lower_dimensions[i];
        }
        if constexpr (Dimension > 1) {
            for (auto &lower_dimension : lower_dimensions) {
                lower_dimension.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