結果

問題 No.60 魔法少女
ユーザー reika727reika727
提出日時 2024-02-04 02:20:49
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 5,000 ms
コード長 6,571 bytes
コンパイル時間 3,684 ms
コンパイル使用メモリ 256,908 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-09-28 11:06:31
合計ジャッジ時間 5,037 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
11,392 KB
testcase_01 AC 10 ms
11,264 KB
testcase_02 AC 9 ms
11,392 KB
testcase_03 AC 10 ms
11,520 KB
testcase_04 AC 110 ms
11,520 KB
testcase_05 AC 116 ms
12,160 KB
testcase_06 AC 185 ms
12,032 KB
testcase_07 AC 137 ms
11,776 KB
testcase_08 AC 91 ms
11,648 KB
testcase_09 AC 44 ms
11,312 KB
testcase_10 AC 174 ms
12,160 KB
testcase_11 AC 26 ms
11,444 KB
testcase_12 AC 83 ms
12,032 KB
testcase_13 AC 196 ms
12,288 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:199:14: warning: narrowing conversion of 'y' from 'int' to 'long unsigned int' [-Wnarrowing]
  199 |             {y, x},
      |              ^
main.cpp:199:17: warning: narrowing conversion of 'x' from 'int' to 'long unsigned int' [-Wnarrowing]
  199 |             {y, x},
      |                 ^
main.cpp:200:20: warning: narrowing conversion of '((y + h) + 1)' from 'int' to 'long unsigned int' [-Wnarrowing]
  200 |             {y + h + 1, x + w + 1},
      |              ~~~~~~^~~
main.cpp:200:31: warning: narrowing conversion of '((x + w) + 1)' from 'int' to 'long unsigned int' [-Wnarrowing]
  200 |             {y + h + 1, x + w + 1},
      |                         ~~~~~~^~~

ソースコード

diff #

#include"bits/stdc++.h"

/*
using namespace std;
using ll=long long;
template<class T=ll>inline T in(istream&is=cin){return*istream_iterator<T>(is);}
template<class T=ll,template<class,class>class Container=vector,template<class>class Allocator=allocator>inline Container<T,Allocator<T>>ins(istream&is=cin){return{istream_iterator<T>(is),istream_iterator<T>()};}
template<class Container>concept pushbackable=requires(Container&c){c.push_back(declval<typename Container::value_type>());};
template<class Container>concept reservable=requires(Container&c){c.reserve();};
template<class T=ll,template<class,class>class Container=vector,template<class>class Allocator=allocator>requires pushbackable<Container<T,Allocator<T>>>inline Container<T,Allocator<T>>ins(size_t n,istream&is=cin){Container<T,Allocator<T>>c;if constexpr(reservable<decltype(c)>)c.reserve(n);copy_n(istream_iterator<T>(is),n,back_inserter(c));return c;}
template<class T>inline bool chmin(T&a,T b){return a>b?a=b,true:false;}
template<class T>inline bool chmax(T&a,T b){return a<b?a=b,true:false;}
template<class T,class...Sizes>inline auto multdim_vector_v(T v,size_t size,Sizes...sizes){if constexpr(sizeof...(Sizes)==0)return vector(size,v);else return vector(size,multdim_vector_v(v,sizes...));}
template<class T,class...Sizes>inline auto multdim_vector(Sizes...sizes){return multdim_vector_v(T{},sizes...);}
*/

template <class T, size_t Dimension>
requires
    std::default_initializable<T> &&
    requires { Dimension != 0; }
class imos
    : protected std::conditional_t<
        Dimension == 1,
        std::vector<T>,
        std::vector<imos<T, Dimension - 1>>
      >
{
    friend imos<T, Dimension + 1>;

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

    imos<T, Dimension> &operator+=(const imos<T, Dimension> &ci)
    {
        for (std::size_t i = 0; i < this->size(); ++i) {
            (*this)[i] += ci[i];
        }
        return *this;
    }

public:
    explicit imos(const std::size_t size, const auto... sizes)
    requires requires { 1 + sizeof...(sizes) == Dimension; }
        : std::vector<lower_dimension>(
              size + 1,
              lower_dimension(sizes...)
          )
    {
    }

    const T& at(const std::size_t index, const auto... indices) const
    requires requires { 1 + sizeof...(indices) == Dimension; }
    {
        if constexpr (sizeof...(indices) == 0) {
            return (*this)[index];
        } else {
            return (*this)[index].at(indices...);
        }
    }

    void set(
        const std::span<const std::size_t, Dimension> begin,
        const std::span<const std::size_t, Dimension> end,
        const T weight
    )
    {
        const std::size_t begin0 = std::min(begin.front(), this->size() - 1);
        const std::size_t end0 = std::min(end.front(), this->size() - 1);
        if constexpr (Dimension > 1) {
            (*this)[begin0].set(
                begin.template last<Dimension - 1>(),
                end.template last<Dimension - 1>(),
                weight
            );
            (*this)[end0].set(
                begin.template last<Dimension - 1>(),
                end.template last<Dimension - 1>(),
                -weight
            );
        } else {
            (*this)[begin0] += weight;
            (*this)[end0] -= weight;
        }
    }

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

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

template <class T, size_t Dimension>
class cyclic_imos final : public imos<T, Dimension> {
public:
    using imos<T, Dimension>::imos;

    void set_cyclic(
        const std::span<const std::size_t, Dimension> begin,
        const std::span<const std::size_t, Dimension> end,
        const T weight
    )
    {
        this->set(begin, end, weight);
        if constexpr (Dimension > 1) {
            for (unsigned i = 1; i < (1 << Dimension); ++i) {
                std::array<std::size_t, Dimension> correction_begin, correction_end;
                for (unsigned j = 0; j < Dimension; ++j) {
                    if (i & (1 << j)) {
                        if (begin[j] <= end[j]) {
                            goto CONTINUE;
                        }
                        correction_begin[j] = 0;
                        correction_end[j] = this->size() - 1;
                    } else {
                        correction_begin[j] = begin[j];
                        correction_end[j] = end[j];
                    }
                }
                this->set(correction_begin, correction_end, weight);
            CONTINUE:
                ;
            }
        } else {
            if (begin.front() > end.front()) {
                (*this).front() += weight;
                (*this).back() -= weight;
            }
        }
    }

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

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

template <class T>
auto make_cyclic_imos(auto... sizes)
{
    return cyclic_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>(1001, 1001);
    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