#include <unordered_set> #include <unordered_map> #include <functional> #include <algorithm> //#include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <cstdio> #include <vector> #include <queue> //#include <cmath> #include <set> #include <map> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b);i>=(e);--i) #define PB push_back #define ALL(V) (V).begin(),(V).end() #define SIZE(V) ((int)(V).size()) #define MP make_pair #define ST first #define ND second #define DBG #ifdef DBG #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif int __stmp; #define scanf __stmp=scanf const int INF = 1000000001; const int MAX = 200010; class tree { public: void init(int n) { for(size=1;size<n;size*=2); sum.assign(2*size+3, 0); } void add(int x, LL v) { for(x+=size;x;x/=2) sum[x] += v; } LL query(int e, LL cap) { assert(cap > 0); return _query(1, 0, size, 0, e+1, cap); } private: int size; VLL sum; LL _query(int v, int b, int e, int k, int l, LL cap) { if(e <= k || l <= b) return 0; if(k <= b && e <= l) { LL res = 0; if(v < size) { if(sum[2*v+1]) { LL r = _query(2*v+1, (b+e)/2, e, k, l, cap); res += r; } if(res < cap && sum[2*v]) { LL r = _query(2*v, b, (b+e)/2, k, l, cap-res); res += r; } sum[v] = sum[2*v] + sum[2*v+1]; } else { res = min(sum[v], cap); sum[v] -= res; } return res; } LL res = 0; if(sum[2*v+1]) res = _query(2*v+1, (b+e)/2, e, k, l, cap); if(res < cap && sum[2*v]) { LL r = _query(2*v, b, (b+e)/2, k, l, cap-res); res += r; } sum[v] = sum[2*v] + sum[2*v+1]; return res; } }; struct point { LL x, y, c; int type; bool operator<(const point &rhs) const { if(y != rhs.y) return y < rhs.y; if(type != rhs.type) return type < rhs.type; return x < rhs.x; } }; tree T; point arr[2*MAX]; int n, m; LL w, h; int main(int argc, char *argv[]) { scanf("%d %d", &n, &m); scanf("%lld %lld", &w, &h); VLL coords; LL res = 0; REP(i,n+m) { LL _x, _y, c; scanf("%lld %lld %lld", &_x, &_y, &c); LL x = _y*w + _x*h; LL y = _y*w - _x*h; coords.PB(x); arr[i] = point{x, y, c, 1}; if(i < n) { res += c; arr[i].type = 0; } } sort(ALL(coords)); coords.resize(unique(ALL(coords)) - coords.begin()); REP(i,n+m) arr[i].x = lower_bound(ALL(coords), arr[i].x) - coords.begin(); T.init(SIZE(coords)); sort(arr, arr+n+m); REP(i,n+m) { int x = arr[i].x; int type = arr[i].type; LL c = arr[i].c; if(type == 0) { T.add(x, c); } else { res -= T.query(x, c); } } printf("%lld\n", res); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | #include <unordered_set> #include <unordered_map> #include <functional> #include <algorithm> //#include <iostream> #include <numeric> #include <cassert> #include <cstring> #include <cstdio> #include <vector> #include <queue> //#include <cmath> #include <set> #include <map> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef vector<int> VI; typedef vector<LL> VLL; typedef vector<VI> VVI; typedef pair<int,int> PII; typedef vector<PII> VPII; #define REP(i,n) for(int i=0;i<(n);++i) #define FOR(i,b,e) for(int i=(b);i<=(e);++i) #define FORD(i,b,e) for(int i=(b);i>=(e);--i) #define PB push_back #define ALL(V) (V).begin(),(V).end() #define SIZE(V) ((int)(V).size()) #define MP make_pair #define ST first #define ND second #define DBG #ifdef DBG #define debug(...) fprintf(stderr, __VA_ARGS__) #else #define debug(...) #endif int __stmp; #define scanf __stmp=scanf const int INF = 1000000001; const int MAX = 200010; class tree { public: void init(int n) { for(size=1;size<n;size*=2); sum.assign(2*size+3, 0); } void add(int x, LL v) { for(x+=size;x;x/=2) sum[x] += v; } LL query(int e, LL cap) { assert(cap > 0); return _query(1, 0, size, 0, e+1, cap); } private: int size; VLL sum; LL _query(int v, int b, int e, int k, int l, LL cap) { if(e <= k || l <= b) return 0; if(k <= b && e <= l) { LL res = 0; if(v < size) { if(sum[2*v+1]) { LL r = _query(2*v+1, (b+e)/2, e, k, l, cap); res += r; } if(res < cap && sum[2*v]) { LL r = _query(2*v, b, (b+e)/2, k, l, cap-res); res += r; } sum[v] = sum[2*v] + sum[2*v+1]; } else { res = min(sum[v], cap); sum[v] -= res; } return res; } LL res = 0; if(sum[2*v+1]) res = _query(2*v+1, (b+e)/2, e, k, l, cap); if(res < cap && sum[2*v]) { LL r = _query(2*v, b, (b+e)/2, k, l, cap-res); res += r; } sum[v] = sum[2*v] + sum[2*v+1]; return res; } }; struct point { LL x, y, c; int type; bool operator<(const point &rhs) const { if(y != rhs.y) return y < rhs.y; if(type != rhs.type) return type < rhs.type; return x < rhs.x; } }; tree T; point arr[2*MAX]; int n, m; LL w, h; int main(int argc, char *argv[]) { scanf("%d %d", &n, &m); scanf("%lld %lld", &w, &h); VLL coords; LL res = 0; REP(i,n+m) { LL _x, _y, c; scanf("%lld %lld %lld", &_x, &_y, &c); LL x = _y*w + _x*h; LL y = _y*w - _x*h; coords.PB(x); arr[i] = point{x, y, c, 1}; if(i < n) { res += c; arr[i].type = 0; } } sort(ALL(coords)); coords.resize(unique(ALL(coords)) - coords.begin()); REP(i,n+m) arr[i].x = lower_bound(ALL(coords), arr[i].x) - coords.begin(); T.init(SIZE(coords)); sort(arr, arr+n+m); REP(i,n+m) { int x = arr[i].x; int type = arr[i].type; LL c = arr[i].c; if(type == 0) { T.add(x, c); } else { res -= T.query(x, c); } } printf("%lld\n", res); return 0; } |