結果
| 問題 |
No.60 魔法少女
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-07-01 01:11:22 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 271 ms / 5,000 ms |
| コード長 | 4,967 bytes |
| コンパイル時間 | 6,884 ms |
| コンパイル使用メモリ | 289,804 KB |
| 実行使用メモリ | 22,144 KB |
| 最終ジャッジ日時 | 2024-07-01 01:11:33 |
| 合計ジャッジ時間 | 10,884 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 10 |
ソースコード
#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:
using lower_dimension_t = std::conditional_t<
Dimension == 1,
T,
imos<T, Dimension - 1>
>;
std::vector<lower_dimension_t> 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) {
std::ranges::for_each(lower_dimensions, &lower_dimension_t::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>(1501uz, 1501uz);
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](auto acc, const auto &e) {
return acc + std::max(0, e.hp - imos.at(e.y, e.x));
}
) << std::endl;
}