#include "bits/stdc++.h" template class imos final { static_assert(Dimension != 0, "Dimension must not be 0."); friend imos; private: std::vector< std::conditional_t< Dimension == 1, T, imos > > 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 &operator+=(const imos &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 begin, const std::span 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 &begin, const std::array &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 auto make_imos(auto... sizes) { return imos(sizes...); } int main() { struct enemy { int x, y, hp; }; int n, k; std::cin >> n >> k; std::vector 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(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; }