#include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define REP(i,n) for(int i=0; i<(n); ++i) #define VAR(v,i) __typeof(i) v = (i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; template<class C> void mini(C&a4, C b4) { a4 = min(a4,b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4,b4); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; const int N = 2e5 + 3; const LL INF = 2e18; int n, m; int w, h; struct El { int x, y, i, v, t; }; El e[N*2]; LL toE1(int x, int y) { return LL(x) * h + LL(y) * w; } LL toE2(int x, int y) { return LL(x) * (-h) + LL(y) * w; } bool cmp1(int a, int b) { LL A = toE1(e[a].x, e[a].y); LL B = toE1(e[b].x, e[b].y); if (A != B) return A < B; return e[a].t < e[b].t; } bool cmp2(int a, int b) { LL A = toE2(e[a].x, e[a].y); LL B = toE2(e[b].x, e[b].y); if (A != B) return A < B; return e[a].t < e[b].t; } const int S = N*4; int size; struct V { LL inc; LL max; }; V t[S*2]; void insertMax(int p, LL v) { p += size; int q = p; while (q) { v -= t[q].inc; q /= 2; } t[p].max = v; p /= 2; while (p) { t[p].max = max(t[p*2].max + t[p*2].inc, t[p*2+1].max + t[p*2+1].inc); p /= 2; } } void insertSum(int p, int a, int b, int A, int B, LL v) { if ((b <= A) || (B <= a)) return; if ((a <= A) && (B <= b)) { t[p].inc += v; return; } insertSum(p*2, a, b, A, (A+B)/2, v); insertSum(p*2+1, a, b, (A+B)/2, B, v); t[p].max = max(t[p*2].max + t[p*2].inc, t[p*2+1].max + t[p*2+1].inc); } LL queryMax(int p, int a, int b, int A, int B) { if ((b <= A) || (B <= a)) return -INF; if ((a <= A) && (B <= b)) { return t[p].max + t[p].inc; } return max(queryMax(p*2, a, b, A, (A+B)/2), queryMax(p*2+1, a, b, (A+B)/2, B)) + t[p].inc; } LL queryMax(int p) { return queryMax(1, p, size, 0, size); } void insertSum(int p, LL v) { insertSum(1, 0, p+1, 0, size, v); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); scanf("%d %d", &n, &m); scanf("%d %d", &w, &h); int c = 0; REP(i, n) { scanf("%d %d %d", &e[c].x, &e[c].y, &e[c].v); e[c].t = 0; ++c; } REP(i, m) { scanf("%d %d %d", &e[c].x, &e[c].y, &e[c].v); e[c].t = 1; ++c; } VI ind; REP(i, c) ind.PB(i); sort(ALL(ind), cmp1); REP(i, SZ(ind)) e[ind[i]].i = i+1; sort(ALL(ind), cmp2); size = 1; while (size < c) size *= 2; REP(i, size*2) { t[i].inc = 0; t[i].max = -INF; } for (int i: ind) { if (e[i].t == 0) { LL curr = max(0ll, queryMax(e[i].i)); insertMax(e[i].i, curr); insertSum(e[i].i, e[i].v); } else { insertSum(e[i].i, -e[i].v); } } printf("%lld\n", queryMax(0)); 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | #include<algorithm> #include<bitset> #include<cassert> #include<complex> #include<cstring> #include<cstdio> #include<iomanip> #include<map> #include<iostream> #include<queue> #include<set> #include<stack> #include<string> #include<vector> #define FOR(i,a,b) for(int i=(a); i<=(b); ++i) #define FORD(i,a,b) for(int i=(a); i>=(b); --i) #define REP(i,n) for(int i=0; i<(n); ++i) #define VAR(v,i) __typeof(i) v = (i) #define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i) #define ALL(x) (x).begin(),(x).end() #define SZ(x) ((int)(x).size()) #define PB push_back #define MP make_pair #define X first #define Y second #define debug(x) { cerr << #x <<" = " << (x) << endl; } #define debugv(x) { cerr << #x << " = "; FORE(it, x) cerr << *it << ", "; cerr << endl; } #define dprintf(...) fprintf(stderr, __VA_ARGS__) using namespace std; typedef pair<int, int> PII;; typedef vector<int> VI; typedef long long LL; typedef long double LD; template<class C> void mini(C&a4, C b4) { a4 = min(a4,b4); } template<class C> void maxi(C&a4, C b4) { a4 = max(a4,b4); } template<class T1, class T2> ostream& operator<<(ostream &out, pair<T1, T2> pair) { return out << "(" << pair.X << ", "<< pair.Y << ")"; }; const int N = 2e5 + 3; const LL INF = 2e18; int n, m; int w, h; struct El { int x, y, i, v, t; }; El e[N*2]; LL toE1(int x, int y) { return LL(x) * h + LL(y) * w; } LL toE2(int x, int y) { return LL(x) * (-h) + LL(y) * w; } bool cmp1(int a, int b) { LL A = toE1(e[a].x, e[a].y); LL B = toE1(e[b].x, e[b].y); if (A != B) return A < B; return e[a].t < e[b].t; } bool cmp2(int a, int b) { LL A = toE2(e[a].x, e[a].y); LL B = toE2(e[b].x, e[b].y); if (A != B) return A < B; return e[a].t < e[b].t; } const int S = N*4; int size; struct V { LL inc; LL max; }; V t[S*2]; void insertMax(int p, LL v) { p += size; int q = p; while (q) { v -= t[q].inc; q /= 2; } t[p].max = v; p /= 2; while (p) { t[p].max = max(t[p*2].max + t[p*2].inc, t[p*2+1].max + t[p*2+1].inc); p /= 2; } } void insertSum(int p, int a, int b, int A, int B, LL v) { if ((b <= A) || (B <= a)) return; if ((a <= A) && (B <= b)) { t[p].inc += v; return; } insertSum(p*2, a, b, A, (A+B)/2, v); insertSum(p*2+1, a, b, (A+B)/2, B, v); t[p].max = max(t[p*2].max + t[p*2].inc, t[p*2+1].max + t[p*2+1].inc); } LL queryMax(int p, int a, int b, int A, int B) { if ((b <= A) || (B <= a)) return -INF; if ((a <= A) && (B <= b)) { return t[p].max + t[p].inc; } return max(queryMax(p*2, a, b, A, (A+B)/2), queryMax(p*2+1, a, b, (A+B)/2, B)) + t[p].inc; } LL queryMax(int p) { return queryMax(1, p, size, 0, size); } void insertSum(int p, LL v) { insertSum(1, 0, p+1, 0, size, v); } int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); scanf("%d %d", &n, &m); scanf("%d %d", &w, &h); int c = 0; REP(i, n) { scanf("%d %d %d", &e[c].x, &e[c].y, &e[c].v); e[c].t = 0; ++c; } REP(i, m) { scanf("%d %d %d", &e[c].x, &e[c].y, &e[c].v); e[c].t = 1; ++c; } VI ind; REP(i, c) ind.PB(i); sort(ALL(ind), cmp1); REP(i, SZ(ind)) e[ind[i]].i = i+1; sort(ALL(ind), cmp2); size = 1; while (size < c) size *= 2; REP(i, size*2) { t[i].inc = 0; t[i].max = -INF; } for (int i: ind) { if (e[i].t == 0) { LL curr = max(0ll, queryMax(e[i].i)); insertMax(e[i].i, curr); insertSum(e[i].i, e[i].v); } else { insertSum(e[i].i, -e[i].v); } } printf("%lld\n", queryMax(0)); return 0; } |