#define debug if(0) // Grzegorz Guspiel #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<int(n);++i) #define SIZE(c) ((int)((c).size())) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define ALL(v) (v).begin(), (v).end() #define VAR(x) #x << " " << x << " " #define pb push_back #define mp make_pair #define st first #define nd second template<typename T> void maxE(T& a, const T& b) { a = max(a, b); } template<typename T> void minE(T& a, const T& b) { a = min(a, b); } typedef long long LL; const LL INF = 5LL * 1000* 1000 * 1000 * 1000 * 1000 * 1000LL; struct Point { LL x, y; int value; pair<LL, LL> key() const { return mp(x, y); } }; bool operator<(const Point& a, const Point& b) { return a.key() < b.key(); } // 200 000 eksponatow - 200 000 wspolrzednych y // 200 000 straznikow - 200 000 wspolrzednych y const int MAX_TREE = 2097152 + 100; struct Tree { int s; LL t[MAX_TREE]; LL d[MAX_TREE]; void init(int s_) { s = 1; while (s <= s_) s *= 2; } LL val(int a) { return t[a] + d[a]; } void fix(int a) { t[a] = -INF; maxE(t[a], val(2 * a)); maxE(t[a], val(2 * a + 1)); } void put(int a, int b, LL delta) { a += s; b += s; a--; b++; while (a / 2 != b / 2) { if (a + 1 != b) { if (a % 2 == 0) { d[a + 1] += delta; } if (b % 2 == 1) { d[b - 1] += delta; } } fix(a / 2); fix(b / 2); a /= 2; b /= 2; } while (a / 2) { fix(a / 2); a /= 2; } } LL get(int a, int b) { a += s; b += s; a--; b++; LL lr = -INF; LL rr = -INF; bool leftOk = 0; bool rightOk = 0; while (a / 2 != b / 2) { if (a % 2 == 0) leftOk = 1; if (b % 2 == 1) rightOk = 1; if (a + 1 != b) { if (a % 2 == 0) maxE(lr, val(a + 1)); if (b % 2 == 1) maxE(rr, val(b - 1)); } if (leftOk) lr += d[a / 2]; if (rightOk) rr += d[b / 2]; a /= 2; b /= 2; } LL r = max(lr, rr); while (a / 2) { r += d[a / 2]; a /= 2; } return r; } void updateAllFromWithDelta(int from, LL delta) { put(from, s - 2, delta); } LL getMaxFrom(int from) { LL rr = get(from, s - 2); return rr; } void updateValue(int a, long long b) { LL prev = get(a, a); put(a, a, b - prev); } }; vector<Point> points; Tree tree; LL solve() { LL result = 0; sort(ALL(points)); FOREACH (p, points) { tree.updateValue(p->y - 1, tree.getMaxFrom(p->y - 1)); tree.updateAllFromWithDelta(p->y, p->value); LL value = tree.getMaxFrom(p->y); maxE(result, value); tree.updateValue(p->y, value); } return result; } int main() { ios_base::sync_with_stdio(0); int n, m, w, h; cin >> n >> m >> w >> h; REP (i, n) { Point point; cin >> point.x >> point.y >> point.value; points.pb(point); } REP (i, m) { Point point; cin >> point.x >> point.y >> point.value; point.value *= -1; points.pb(point); } REP (i, SIZE(points)) { Point p = points[i]; Point np; np.value = p.value; np.x = -h * p.x - w * p.y; np.y = h * p.x - w * p.y; points[i] = np; } vector<LL> coordsX, coordsY; FOREACH (p, points) { coordsX.pb(p->x); coordsY.pb(p->y); } coordsY.pb(-INF); coordsY.pb(-INF + 1); coordsY.pb(INF); sort(ALL(coordsX)); sort(ALL(coordsY)); coordsX.resize(unique(ALL(coordsX)) - coordsX.begin()); coordsY.resize(unique(ALL(coordsY)) - coordsY.begin()); tree.init(SIZE(coordsY)); FOREACH (p, points) { p->x = lower_bound(ALL(coordsX), p->x) - coordsX.begin(); p->y = lower_bound(ALL(coordsY), p->y) - coordsY.begin(); } cout << solve() << endl; 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 | #define debug if(0) // Grzegorz Guspiel #include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i=0;i<int(n);++i) #define SIZE(c) ((int)((c).size())) #define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i) #define ALL(v) (v).begin(), (v).end() #define VAR(x) #x << " " << x << " " #define pb push_back #define mp make_pair #define st first #define nd second template<typename T> void maxE(T& a, const T& b) { a = max(a, b); } template<typename T> void minE(T& a, const T& b) { a = min(a, b); } typedef long long LL; const LL INF = 5LL * 1000* 1000 * 1000 * 1000 * 1000 * 1000LL; struct Point { LL x, y; int value; pair<LL, LL> key() const { return mp(x, y); } }; bool operator<(const Point& a, const Point& b) { return a.key() < b.key(); } // 200 000 eksponatow - 200 000 wspolrzednych y // 200 000 straznikow - 200 000 wspolrzednych y const int MAX_TREE = 2097152 + 100; struct Tree { int s; LL t[MAX_TREE]; LL d[MAX_TREE]; void init(int s_) { s = 1; while (s <= s_) s *= 2; } LL val(int a) { return t[a] + d[a]; } void fix(int a) { t[a] = -INF; maxE(t[a], val(2 * a)); maxE(t[a], val(2 * a + 1)); } void put(int a, int b, LL delta) { a += s; b += s; a--; b++; while (a / 2 != b / 2) { if (a + 1 != b) { if (a % 2 == 0) { d[a + 1] += delta; } if (b % 2 == 1) { d[b - 1] += delta; } } fix(a / 2); fix(b / 2); a /= 2; b /= 2; } while (a / 2) { fix(a / 2); a /= 2; } } LL get(int a, int b) { a += s; b += s; a--; b++; LL lr = -INF; LL rr = -INF; bool leftOk = 0; bool rightOk = 0; while (a / 2 != b / 2) { if (a % 2 == 0) leftOk = 1; if (b % 2 == 1) rightOk = 1; if (a + 1 != b) { if (a % 2 == 0) maxE(lr, val(a + 1)); if (b % 2 == 1) maxE(rr, val(b - 1)); } if (leftOk) lr += d[a / 2]; if (rightOk) rr += d[b / 2]; a /= 2; b /= 2; } LL r = max(lr, rr); while (a / 2) { r += d[a / 2]; a /= 2; } return r; } void updateAllFromWithDelta(int from, LL delta) { put(from, s - 2, delta); } LL getMaxFrom(int from) { LL rr = get(from, s - 2); return rr; } void updateValue(int a, long long b) { LL prev = get(a, a); put(a, a, b - prev); } }; vector<Point> points; Tree tree; LL solve() { LL result = 0; sort(ALL(points)); FOREACH (p, points) { tree.updateValue(p->y - 1, tree.getMaxFrom(p->y - 1)); tree.updateAllFromWithDelta(p->y, p->value); LL value = tree.getMaxFrom(p->y); maxE(result, value); tree.updateValue(p->y, value); } return result; } int main() { ios_base::sync_with_stdio(0); int n, m, w, h; cin >> n >> m >> w >> h; REP (i, n) { Point point; cin >> point.x >> point.y >> point.value; points.pb(point); } REP (i, m) { Point point; cin >> point.x >> point.y >> point.value; point.value *= -1; points.pb(point); } REP (i, SIZE(points)) { Point p = points[i]; Point np; np.value = p.value; np.x = -h * p.x - w * p.y; np.y = h * p.x - w * p.y; points[i] = np; } vector<LL> coordsX, coordsY; FOREACH (p, points) { coordsX.pb(p->x); coordsY.pb(p->y); } coordsY.pb(-INF); coordsY.pb(-INF + 1); coordsY.pb(INF); sort(ALL(coordsX)); sort(ALL(coordsY)); coordsX.resize(unique(ALL(coordsX)) - coordsX.begin()); coordsY.resize(unique(ALL(coordsY)) - coordsY.begin()); tree.init(SIZE(coordsY)); FOREACH (p, points) { p->x = lower_bound(ALL(coordsX), p->x) - coordsX.begin(); p->y = lower_bound(ALL(coordsY), p->y) - coordsY.begin(); } cout << solve() << endl; return 0; } |