#include #ifndef TEMP #define TEMP template std::ostream &operator<<(std::ostream &os, const std::pair &x) { return os << "(" << x.first << ", " << x.second << ")"; } template std::ostream &operator<<(std::ostream &os, const std::vector &vec) { os << '['; for (int _ = 0, __ = vec.size(); _ < __; _++) os << (_ ? ", " : "") << vec[_]; return os << ']'; } template std::ostream &operator<<(std::ostream &os, const std::set &s) { os << '{'; int _ = 0; for (const auto &x : s) os << (_++ ? ", " : "") << x; return os << '}'; } template std::ostream &operator<<(std::ostream &os, const std::array &arr) { os << '[' << arr[0]; for (std::size_t _ = 1; _ < _Nm; _++) os << ", " << arr[_]; return os << ']'; } template void print(std::ostream &os, const Tup &x, std::index_sequence) { using swallow = int[]; (void)swallow{(os << std::get(x) << ", ", 0)...}; } template std::ostream &operator<<(std::ostream &os, const std::tuple &x) { static constexpr std::size_t N = sizeof...(Args); os << "("; if constexpr (N >= 2) print(os, x, std::make_index_sequence()); return os << std::get(x) << ")"; } std::ostream &operator<<(std::ostream &os, std::int8_t x) { return os << (int)x; } std::ostream &operator<<(std::ostream &os, std::uint8_t x) { return os << (int)x; } std::ostream &operator<<(std::ostream &os, const __int128_t &v) { if (v == 0) os << "0"; __int128_t tmp = v < 0 ? (os << "-", -v) : v; std::string s; while (tmp) s += '0' + (tmp % 10), tmp /= 10; return std::reverse(s.begin(), s.end()), os << s; } std::ostream &operator<<(std::ostream &os, const __uint128_t &v) { if (v == 0) os << "0"; __uint128_t tmp = v; std::string s; while (tmp) s += '0' + (tmp % 10), tmp /= 10; return std::reverse(s.begin(), s.end()), os << s; } #ifdef __LOCAL const std::string COLOR_RESET = "\033[0m", BRIGHT_GREEN = "\033[1;32m", BRIGHT_RED = "\033[1;31m", BRIGHT_CYAN = "\033[1;36m", NORMAL_CROSSED = "\033[0;9;37m", ITALIC = "\033[3m", BOLD = "\033[1m", RED_BACKGROUND = "\033[1;41m", NORMAL_FAINT = "\033[0;2m"; #define func_LINE_FILE \ NORMAL_FAINT << " in " << BOLD << __func__ << NORMAL_FAINT << ITALIC \ << " (L" << __LINE__ << ") " << __FILE__ << COLOR_RESET #define checkpoint() \ std::cerr << BRIGHT_RED << "< check point! >" << func_LINE_FILE << '\n' #define debug(x) \ std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = " << (x) \ << func_LINE_FILE << '\n' #define debugArray(x, n) \ do { \ std::cerr << BRIGHT_CYAN << #x << COLOR_RESET << " = [" << x[0]; \ for (int _ = 1; _ < (int)(n); ++_) std::cerr << ", " << x[_]; \ std::cerr << "]" << func_LINE_FILE << '\n'; \ } while (0) #define debugMatrix(x, h, w) \ do { \ std::cerr << BRIGHT_CYAN << #x << "\n" << COLOR_RESET << "= "; \ for (int _ = 0; (_) < (int)(h); ++_) { \ std::cerr << ((_ ? " [" : "[[")); \ for (int __ = 0; __ < (int)(w); ++__) \ std::cerr << ((__ ? ", " : "")) << x[_][__]; \ std::cerr << "]" << (_ + 1 == (int)(h) ? "]" : ",\n"); \ } \ std::cerr << func_LINE_FILE << '\n'; \ } while (0) #else #define checkpoint() (void(0)) #define debug(x) (void(0)) #define debugArray(x, n) (void(0)) #define debugMatrix(x, h, w) (void(0)) #endif template auto compress(std::vector &v) { std::sort(v.begin(), v.end()); v.erase(std::unique(v.begin(), v.end()), v.end()); return [&v](T x) { return std::lower_bound(v.begin(), v.end(), x) - v.begin(); }; } struct ClosedSection { long long l, r; ClosedSection() : l(1), r(0) {} ClosedSection(long long l_, long long r_) : l(l_), r(r_) {} bool valid() { return l <= r; } }; template // closed [l,r] ClosedSection bin_search(const Check &isok, long long l, long long r) { bool res_l = isok(l), res_r = isok(r); if (res_l && res_r) return ClosedSection(l, r); if (!res_l && !res_r) return ClosedSection(); long long lb = l, ub = r; for (long long x; ub - lb > 1;) (isok(x = (lb + ub) / 2) == res_l ? lb : ub) = x; return res_l ? ClosedSection(l, lb) : ClosedSection(ub, r); } template ClosedSection bin_search(const Check &isok, ClosedSection cs) { return cs.valid() ? bin_search(isok, cs.l, cs.r) : cs; } #endif #ifndef HAS_CHECK #define HAS_CHECK(member, Dummy) \ template \ struct has_##member { \ template \ static std::true_type check(U *); \ static std::false_type check(...); \ static T *mClass; \ static const bool value = decltype(check(mClass))::value; \ }; #define HAS_MEMBER(member) HAS_CHECK(member, int dummy = (&U::member, 0)) #define HAS_TYPE(member) HAS_CHECK(member, class dummy = typename U::member) #endif template class KDTree { HAS_MEMBER(op); HAS_MEMBER(ti); HAS_MEMBER(mapping); HAS_MEMBER(composition); HAS_TYPE(T); HAS_TYPE(E); template using monoid = std::conjunction, has_op, has_ti>; template using dual = std::conjunction, has_E, has_mapping, has_composition>; template struct Node_B { using E = F; T val; int ch[2] = {-1, -1}; pos_t range[K][2], pos[K]; }; template struct Node_D : Node_B {}; template struct Node_D> : Node_B { typename M::T sum; }; template struct Node_D> : Node_B { typename M::E lazy; bool lazy_flg = false; }; template struct Node_D> : Node_B { typename M::T sum; typename M::E lazy; bool lazy_flg = false; }; using Node = Node_D::value, dual::value>; using T = decltype(Node::val); using E = typename Node::E; std::vector ns; public: using PosVal = std::pair, T>; using Iter = typename std::vector::iterator; using Range = std::array, K>; private: inline void pushup(int i) { ns[i].sum = ns[i].val; if (ns[i].ch[0] != -1) ns[i].sum = M::op(ns[ns[i].ch[0]].sum, ns[i].sum); if (ns[i].ch[1] != -1) ns[i].sum = M::op(ns[i].sum, ns[ns[i].ch[1]].sum); } inline void propagate(int i, const E &x) { if (i == -1) return; ns[i].lazy_flg ? (M::composition(ns[i].lazy, x), x) : (ns[i].lazy = x); M::mapping(ns[i].val, x), ns[i].lazy_flg = true; if constexpr (monoid::value) M::mapping(ns[i].sum, x); } inline void eval(int i) { if (!ns[i].lazy_flg) return; ns[i].lazy_flg = false; propagate(ns[i].ch[0], ns[i].lazy), propagate(ns[i].ch[1], ns[i].lazy); } inline void build(int &i, Iter bg, Iter ed, std::uint8_t div = 0) { if (ed - bg < 1) return; const int n = ed - bg; auto md = bg + n / 2; std::nth_element(bg, md, ed, [div](const PosVal &l, const PosVal &r) { return l.first[div] < r.first[div]; }); i = ns.size(), ns.push_back(Node{md->second, {-1, -1}}); for (std::uint8_t j = K; j--; ns[i].pos[j] = md->first[j]) { auto [mn, mx] = std::minmax_element(bg, ed, [j](const PosVal &l, const PosVal &r) { return l.first[j] < r.first[j]; }); ns[i].range[j][0] = mn->first[j], ns[i].range[j][1] = mx->first[j]; } if (std::uint8_t nex = (div + 1) % K; n > 1) build(ns[i].ch[0], bg, md, nex), build(ns[i].ch[1], md + 1, ed, nex); if constexpr (monoid::value) pushup(i); } template inline T fold(int i, const F &in, const G &inall, const H &outall) { static_assert(monoid::value, "\"fold\" is not available"); if (i == -1 || outall(ns[i].range)) return M::ti(); if (inall(ns[i].range)) return ns[i].sum; if constexpr (dual::value) eval(i); T ret = M::op(fold(ns[i].ch[0], in, inall, outall), fold(ns[i].ch[1], in, inall, outall)); return in(ns[i].pos) ? M::op(ret, ns[i].val) : ret; } template inline void apply(int i, const F &in, const G &inall, const H &outall, const E &x) { static_assert(dual::value, "\"apply\" is not available"); if (i == -1 || outall(ns[i].range)) return; if (inall(ns[i].range)) return propagate(i, x), void(); if (eval(i); in(ns[i].pos)) M::mapping(ns[i].val, x); apply(ns[i].ch[0], in, inall, outall, x); apply(ns[i].ch[1], in, inall, outall, x); if constexpr (monoid::value) pushup(i); } inline std::pair get(int i, const std::array &pos) { if (i == -1) return {M::ti(), false}; bool myself = true; for (std::uint8_t j = K; j--; myself &= pos[j] == ns[i].pos[j]) if (ns[i].range[j][1] < pos[j] || pos[j] < ns[i].range[j][0]) return {M::ti(), false}; if (myself) return {ns[i].val, true}; if constexpr (dual::value) eval(i); auto ret = get(ns[i].ch[0], pos); return !ret.second ? get(ns[i].ch[1], pos) : ret; } inline bool set(int i, const std::array &pos, const T &x) { if (i == -1) return false; bool myself = true; for (std::uint8_t j = K; j--; myself &= pos[j] == ns[i].pos[j]) if (ns[i].range[j][1] < pos[j] || pos[j] < ns[i].range[j][0]) return false; if constexpr (dual::value) eval(i); bool ret = true; if (myself) ns[i].val = x; else if (!(ret = set(ns[i].ch[0], pos, x))) ret = set(ns[i].ch[1], pos, x); if constexpr (monoid::value) pushup(i); return ret; } template static inline Range to_range(std::initializer_list... intervals) { static_assert(sizeof...(intervals) == K); static_assert(std::conjunction_v...>); Range r; std::uint8_t i = 0; for (auto &&x : {intervals...}) { std::vector tmp(x); assert(tmp.size() == 2), assert(tmp[0] <= tmp[1]); r[i][0] = tmp[0], r[i][1] = tmp[1], i++; } return r; } static inline auto funcs(const Range &r) { return std::make_tuple( [r](pos_t pos[K]) { for (std::uint8_t i = K; i--;) if (pos[i] < r[i][0] || r[i][1] < pos[i]) return false; return true; }, [r](pos_t range[K][2]) { for (std::uint8_t i = K; i--;) if (range[i][0] < r[i][0] || r[i][1] < range[i][1]) return false; return true; }, [r](pos_t range[K][2]) { for (std::uint8_t i = K; i--;) if (range[i][1] < r[i][0] || r[i][1] < range[i][0]) return true; return false; }); } public: KDTree(std::vector v) { int root; build(root, v.begin(), v.end()); } T get(std::array pos) { return get(0, pos).first; } template T get(Args... ids) { static_assert(sizeof...(ids) == K); static_assert(std::conjunction_v...>); return get(0, {ids...}).first; } bool set(T x, std::array pos) { return set(0, pos, x); } template bool set(T x, Args... ids) { static_assert(sizeof...(ids) == K); static_assert(std::conjunction_v...>); return set(0, {ids...}, x); } T fold(const Range &r) { auto [in, inall, outall] = funcs(r); return fold(0, in, inall, outall); } template T fold(std::initializer_list &&...intervals) { auto r = to_range(intervals...); auto [in, inall, outall] = funcs(r); return fold(0, in, inall, outall); } template T fold(const F &in, const G &inall, const H &outall) { return fold(0, in, inall, outall); } void apply(E x, const Range &r) { auto [in, inall, outall] = funcs(r); apply(0, in, inall, outall, x); } template void apply(E x, std::initializer_list &&...intervals) { auto r = to_range(intervals...); auto [in, inall, outall] = funcs(r); apply(0, in, inall, outall, x); } template void apply(E x, const F &in, const G &inall, const H &outall) { apply(0, in, inall, outall, x); } void debug_output(int i) { if (i == -1) return; debugArray(ns[i].pos, K); debugMatrix(ns[i].range, K, 2); debug(ns[i].val); debug(ns[i].sum); debug_output(ns[i].ch[0]); debug_output(ns[i].ch[1]); } void debug_output() { debug_output(0); } }; using namespace std; struct RASQ { struct T { long long val; int sz; }; using E = long long; static T ti() { return {0, 0}; } static T op(T l, T r) { return {l.val + r.val, l.sz + r.sz}; } static void mapping(T &v, E x) { v.val += x * v.sz; } static void composition(E &pre, E suf) { pre += suf; } }; signed main() { cin.tie(0); ios::sync_with_stdio(0); using KDT = KDTree<2, int, RASQ>; int H, W, N, M; cin >> H >> W >> N >> M; vector v; for (int i = 1; i <= H; i++) for (int j = 1; j <= W; j++) v.push_back({{i, j}, {0, 1}}); KDT kdt(v); int T[N], U[N], L[N], R[N], A[N]; for (int i = 0; i < N; i++) cin >> T[i] >> U[i] >> L[i] >> R[i] >> A[i]; while (M--) { int X, Y, B, C; cin >> X >> Y >> B >> C; kdt.apply(C, {X - B, X + B}, {Y - B, Y + B}); } int ans = 0; for (int i = 0; i < N; i++) ans += kdt.fold({T[i], U[i]}, {L[i], R[i]}).val < A[i]; cout << ans << '\n'; return 0; }