#include <cstdio> #include <algorithm> using namespace std; struct point { int x, y; point(int X = 0, int Y = 0) : x(X), y(Y) { } }; long long w, h; inline bool operator<(const point &a, const point &b) { int x = a.x - b.x, y = a.y - b.y; return x * h < y * w; } const int NM = 400000; int x[NM]; int y[NM]; int v[NM]; point p[NM]; point sorted[NM]; pair<int, int> order[NM]; const int base = 1 << 19; long long sum[base << 1]; long long mx[base << 1]; const long long INF = 1000000000000000LL; int pa, pb; long long pk; void _insert(int v, int a, int b) { if(b <= pa || a >= pb) return; if(a >= pa && b <= pb) { sum[v] += pk; mx[v] += pk; } else { _insert(v * 2, a, (a + b) / 2); _insert(v * 2 + 1, (a + b) / 2, b); mx[v] = max(mx[v * 2], mx[v * 2 + 1]) + sum[v]; } } void insert(int a, int b, long long k) { pa = a; pb = b + 1; pk = k; _insert(1, 0, base); } long long _findMax(int v, int a, int b) { if(b <= pa || a >= pb) return -INF; if(a >= pa && b <= pb) return mx[v]; return max(_findMax(v * 2, a, (a + b) / 2), _findMax(v * 2 + 1, (a + b) / 2, b)) + sum[v]; } long long findMax(int a, int b) { pa = a; pb = b + 1; return _findMax(1, 0, base); } int main() { int n, m; scanf("%d %d", &n, &m); scanf("%lld %lld", &w, &h); for(int i = 0; i < n + m; i++) { scanf("%d %d %d", &p[i].x, &p[i].y, v + i); sorted[i] = p[i]; } sort(sorted, sorted + n + m); for(int i = 0; i < n + m; i++) x[i] = lower_bound(sorted, sorted + n + m, p[i]) - sorted; w = -w; sort(sorted, sorted + n + m); for(int i = 0; i < n + m; i++) y[i] = lower_bound(sorted, sorted + n + m, p[i]) - sorted; for(int i = 0; i < n + m; i++) order[i] = make_pair(x[i], -i); sort(order, order + n + m); for(int i = 0; i < n + m; i++) { int k = -order[i].second, val = v[k]; if(k >= n) { val = -val; long long m1 = findMax(0, y[k] + 1), m2 = findMax(y[k] + 1, y[k] + 1); insert(y[k] + 1, y[k] + 1, m1 - m2); } insert(0, y[k], val); } printf("%lld\n", mx[1]); 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 | #include <cstdio> #include <algorithm> using namespace std; struct point { int x, y; point(int X = 0, int Y = 0) : x(X), y(Y) { } }; long long w, h; inline bool operator<(const point &a, const point &b) { int x = a.x - b.x, y = a.y - b.y; return x * h < y * w; } const int NM = 400000; int x[NM]; int y[NM]; int v[NM]; point p[NM]; point sorted[NM]; pair<int, int> order[NM]; const int base = 1 << 19; long long sum[base << 1]; long long mx[base << 1]; const long long INF = 1000000000000000LL; int pa, pb; long long pk; void _insert(int v, int a, int b) { if(b <= pa || a >= pb) return; if(a >= pa && b <= pb) { sum[v] += pk; mx[v] += pk; } else { _insert(v * 2, a, (a + b) / 2); _insert(v * 2 + 1, (a + b) / 2, b); mx[v] = max(mx[v * 2], mx[v * 2 + 1]) + sum[v]; } } void insert(int a, int b, long long k) { pa = a; pb = b + 1; pk = k; _insert(1, 0, base); } long long _findMax(int v, int a, int b) { if(b <= pa || a >= pb) return -INF; if(a >= pa && b <= pb) return mx[v]; return max(_findMax(v * 2, a, (a + b) / 2), _findMax(v * 2 + 1, (a + b) / 2, b)) + sum[v]; } long long findMax(int a, int b) { pa = a; pb = b + 1; return _findMax(1, 0, base); } int main() { int n, m; scanf("%d %d", &n, &m); scanf("%lld %lld", &w, &h); for(int i = 0; i < n + m; i++) { scanf("%d %d %d", &p[i].x, &p[i].y, v + i); sorted[i] = p[i]; } sort(sorted, sorted + n + m); for(int i = 0; i < n + m; i++) x[i] = lower_bound(sorted, sorted + n + m, p[i]) - sorted; w = -w; sort(sorted, sorted + n + m); for(int i = 0; i < n + m; i++) y[i] = lower_bound(sorted, sorted + n + m, p[i]) - sorted; for(int i = 0; i < n + m; i++) order[i] = make_pair(x[i], -i); sort(order, order + n + m); for(int i = 0; i < n + m; i++) { int k = -order[i].second, val = v[k]; if(k >= n) { val = -val; long long m1 = findMax(0, y[k] + 1), m2 = findMax(y[k] + 1, y[k] + 1); insert(y[k] + 1, y[k] + 1, m1 - m2); } insert(0, y[k], val); } printf("%lld\n", mx[1]); return 0; } |