結果

問題 No.60 魔法少女
ユーザー reika727reika727
提出日時 2024-02-01 21:15:42
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 5,000 ms
コード長 7,100 bytes
コンパイル時間 4,205 ms
コンパイル使用メモリ 259,092 KB
実行使用メモリ 21,504 KB
最終ジャッジ日時 2024-02-01 21:15:49
合計ジャッジ時間 6,612 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
19,328 KB
testcase_01 AC 14 ms
19,328 KB
testcase_02 AC 14 ms
19,328 KB
testcase_03 AC 15 ms
19,328 KB
testcase_04 AC 120 ms
19,456 KB
testcase_05 AC 125 ms
21,376 KB
testcase_06 AC 205 ms
21,376 KB
testcase_07 AC 151 ms
20,736 KB
testcase_08 AC 101 ms
20,480 KB
testcase_09 AC 50 ms
19,584 KB
testcase_10 AC 191 ms
21,376 KB
testcase_11 AC 32 ms
19,712 KB
testcase_12 AC 89 ms
20,736 KB
testcase_13 AC 205 ms
21,504 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:234:18: warning: narrowing conversion of 'y' from 'll' {aka 'long long int'} to 'long unsigned int' [-Wnarrowing]
  234 |                 {y,x},
      |                  ^
main.cpp:234:20: warning: narrowing conversion of 'x' from 'll' {aka 'long long int'} to 'long unsigned int' [-Wnarrowing]
  234 |                 {y,x},
      |                    ^
main.cpp:235:26: warning: narrowing conversion of '(long long int)std::min<long long int>(((y + h) + 1), 1001)' from 'long long int' to 'long unsigned int' [-Wnarrowing]
  235 |                 {std::min(y+h+1,1001ll),std::min(x+w+1,1001ll)}
      |                  ~~~~~~~~^~~~~~~~~~~~~~
main.cpp:235:49: warning: narrowing conversion of '(long long int)std::min<long long int>(((x + w) + 1), 1001)' from 'long long int' to 'long unsigned int' [-Wnarrowing]
  235 |                 {std::min(y+h+1,1001ll),std::min(x+w+1,1001ll)}
      |                                         ~~~~~~~~^~~~~~~~~~~~~~

ソースコード

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>
class cyclic_imos final : private std::vector<cyclic_imos<T, Dimension - 1>> {
    friend cyclic_imos<T, Dimension + 1>;

public:
    using coordinate = std::array<std::size_t, Dimension>;

    struct segment {
        coordinate begin, end;
    };

    constexpr inline static std::size_t dimension = Dimension;

    using std::vector<cyclic_imos<T, Dimension - 1>>::begin;
    using std::vector<cyclic_imos<T, Dimension - 1>>::end;
    using std::vector<cyclic_imos<T, Dimension - 1>>::operator[];

private:
    using std::vector<cyclic_imos<T, Dimension - 1>>::size;

    T& get(const std::span<const std::size_t, Dimension> idxs)
    {
        return (*this)[idxs.front()].get(idxs.template last<Dimension - 1>());
    }

    void set(
        const std::span<const std::size_t, Dimension> begin,
        const std::span<const std::size_t, Dimension> end,
        const T weight
    )
    {
        (*this)[begin.front()].set(
            begin.template last<Dimension - 1>(),
            end.template last<Dimension - 1>(),
            weight
        );
        (*this)[end.front()].set(
            begin.template last<Dimension - 1>(),
            end.template last<Dimension - 1>(),
            -weight
        );
    }

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

public:
    template <class... Sizes>
    cyclic_imos(const std::size_t size, const Sizes... sizes)
        : std::vector<cyclic_imos<T, Dimension - 1>>(
              size + 1,
              cyclic_imos<T, sizeof...(Sizes)>(sizes...)
          )
    {
    }

    T& get(const coordinate &c)
    {
        return get(std::span{c});
    }

    void set(const segment &s, const T weight)
    {
        set(std::span{s.begin}, std::span{s.end}, weight);
    }

    void set_cyclic(const segment &s, const T weight)
    {
        set(s, weight);
        for (unsigned i = 1; i < (1 << Dimension); ++i) {
            segment correction;
            for (unsigned j = 0; j < Dimension; ++j) {
                if (i & (1 << j)) {
                    if (s.begin[j] <= s.end[j]) {
                        goto CONTINUE;
                    }
                    correction.begin[j] = 0;
                    correction.end[j] = size() - 1;
                } else {
                    correction.begin[j] = s.begin[j];
                    correction.end[j] = s.end[j];
                }
            }
            set(correction, weight);
        CONTINUE:
            ;
        }
    }

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

template <class T>
class cyclic_imos<T, 1> final : private std::vector<T> {
    friend cyclic_imos<T, 2>;

public:
    using coordinate = std::array<std::size_t, 1>;

    struct segment {
        coordinate begin, end;
    };

    constexpr inline static std::size_t dimension = 1;

    using std::vector<T>::begin;
    using std::vector<T>::end;
    using std::vector<T>::operator[];

private:
    using std::vector<T>::size;
    T& get(const std::span<const std::size_t, 1> idxs)
    {
        return (*this)[idxs.front()];
    }

    void set(
        const std::span<const std::size_t, 1> begin,
        const std::span<const std::size_t, 1> end,
        const T weight
    )
    {
        (*this)[begin.front()] += weight;
        (*this)[end.front()] -= weight;
    }

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

public:
    cyclic_imos(const std::size_t size)
        : std::vector<T>(size + 1)
    {
    }

    T& get(const coordinate &c)
    {
        return (*this)[c.front()];
    }

    T& get(const std::size_t idx)
    {
        return (*this)[idx];
    }

    void set(const segment &s, const T weight)
    {
        set(std::span{s.begin}, std::span{s.end}, weight);
    }

    void set(const size_t begin, const size_t end, const T weight)
    {
        set({.begin = begin, .end = end}, weight);
    }

    void set_cyclic(const segment &s, const T weight)
    {
        set(s, weight);
        if (s.begin.front() > s.end.front()) {
            (*this).front() += weight;
            (*this).back() -= weight;
        }
    }

    void set_cyclic(const std::size_t begin, const std::size_t end, const T weight)
    {
        set_cyclic({.begin = begin, .end = end}, weight);
    }

    void accumulate()
    {
        for (std::size_t i = 0; i < size() - 1; ++i) {
            (*this)[i + 1] += (*this)[i];
        }
    }
};

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

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

    ll n=in(),k=in();
    vector<enemy>enemies(n);
    for(ll i=0;i<n;++i){
        ll x=in(),y=in(),hp=in();
        enemies[i]={x+500,y+500,hp};
    }
    auto imos=make_cyclic_imos<ll>(1002,1002);
    for(ll i=0;i<k;++i){
        ll x=in(),y=in(),w=in(),h=in(),d=in();
        x+=500;
        y+=500;
        imos.set(
            {
                {y,x},
                {std::min(y+h+1,1001ll),std::min(x+w+1,1001ll)}
            },
            d
        );
    }
    imos.accumulate();
    cout<<accumulate(
        enemies.begin(),enemies.end(), 0ll,
        [imos](ll acc,const auto&e){
            return acc+std::max(0ll,e.hp-imos[e.y][e.x]);
        }
    )<<endl;
}
0