#include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <ctime> #include <cstdlib> #include <cstdio> #include <utility> #include <iomanip> #include <assert.h> #define MP make_pair #define PB push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #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 #ifdef LOCAL #define debug(x) {cerr <<#x <<" = " <<x <<"\n"; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(x) #define debugv(x) #endif #define make(type, x) type x; cin>>x; #define make2(type, x, y) type x, y; cin>>x>>y; #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z; using namespace std; typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; 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.first << ", " << pair.second << ")";} const int N = 5e5 + 5; struct Point { ll x, y, nr, val, hei; Point(ll x_, ll y_, ll val_, ll hei_) : x(x_), y(y_), val(val_), hei(hei_) {} Point() : x(0), y(0), val(0), hei(0) {}; }; ll w, h; bool cmp1(const Point& p1, const Point& p2) { ll cross = (p2.y - p1.y) * w + (p2.x - p1.x) * h; if (cross) { return cross > 0; } return p1.val > p2.val; } bool cmp2(const Point& p1, const Point& p2) { ll cross = -(p2.y - p1.y) * w + (p2.x - p1.x) * h; if (cross) { return cross > 0; } return p1.val < p2.val; } const int M = 1 << 19; struct Node { ll val; ll min_down_bound; Node() : val(0), min_down_bound(0) {} }; Node drz[2 * M + 5]; ll Read(int ind) { ind += M - 1; ll res = 0; while (ind) { res += drz[ind].val; maxi(res, drz[ind].min_down_bound); //cerr<<res<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; ind /= 2; } return res; } const ll kInf = 2e18;; void PushDownBound(int i) { maxi(drz[2 * i].min_down_bound, drz[i].min_down_bound - drz[i].val); maxi(drz[2 * i + 1].min_down_bound, drz[i].min_down_bound - drz[i].val); drz[i].min_down_bound = -kInf; } void Add(int ind, int left, int right, int a, int b, ll val) { if (a > b) { return; } if (a == left && b == right) { // cerr<<"AddFit "<<a<<" "<<b<<endl; drz[ind].val += val; drz[ind].min_down_bound += val; // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; return; } PushDownBound(ind); int mid = (right - left) / 2; REP (i, 2) { int new_a = a; int new_b = b; maxi(new_a, left + i * (mid + 1)); mini(new_b, left + i * (mid + 1) + mid); Add(2 * ind + i, left + i * (mid + 1), left + i * (mid + 1) + mid, new_a, new_b, val); } //drz[ind].min_down_bound = drz[ind].val + min(drz[2 * ind].min_down_bound, drz[2 * ind + 1].min_down_bound); // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; } void Maxuj(int ind, int left, int right, int a, int b, ll val) { if (a > b) { return; } if (a == left && b == right) { // // cerr<<"MaxujFit "<<a<<" "<<b<<endl; maxi(drz[ind].min_down_bound, val); // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; return; } PushDownBound(ind); int mid = (right - left) / 2; REP (i, 2) { int new_a = a; int new_b = b; maxi(new_a, left + i * (mid + 1)); mini(new_b, left + i * (mid + 1) + mid); Maxuj(2 * ind + i, left + i * (mid + 1), left + i * (mid + 1) + mid, new_a, new_b, val - drz[ind].val); } // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; } ll dp[N]; int main() { // nie zapomnij o ll ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); //goto Testuj; make2(int, n, m); cin>>w>>h; vector<Point> pts; RE (i, n) { make3(ll, x, y, v); Point p(x, y, v, 0); pts.PB(p); } RE (i, m) { make3(ll, x, y, v); Point p(x, y, -v, 0); pts.PB(p); } int total = n + m; sort(ALL(pts), cmp1); /* for (auto p : pts) { cout<<p.x<<" "<<p.y<<" "<<p.val<<endl; } */ for (int i = 0; i < pts.size(); i++) { pts[i].hei = i + 1; } sort(ALL(pts), cmp2); /* for (auto p : pts) { cerr<<p.x<<" "<<p.y<<" "<<p.val<<" "<<p.hei<<endl; } */ //Testuj: ; /* while (1) { cerr<<"Pisz"<<endl; int a, val; cin>>a>>val; Add(1, 1, M, 1, a, val); for (int i = 1; i <= 10; i++) { if (i <= a) { dp[i] += val; } else { maxi(dp[i], dp[i - 1]); } } ll val_here = Read(a); Maxuj(1, 1, M, a, 10, val_here); for (int i = 1; i <= 9; i++) { cerr<<Read(i)<<" "; } cerr<<endl; for (int i = 1; i <= 9; i++) { cerr<<dp[i]<<" "; } cerr<<endl; } */ for (auto p : pts) { Add(1, 1, M, 1, p.hei, p.val); /* for (int i = 1; i <= total + 1; i++) { cerr<<Read(i)<<" "; } cerr<<endl; */ ll val_here = Read(p.hei); Maxuj(1, 1, M, p.hei, total + 1, val_here); /* for (int i = 1; i <= total; i++) { cerr<<Read(i)<<" "; } cerr<<endl; */ } cout<<Read(total + 1)<<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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | #include <iostream> #include <vector> #include <set> #include <map> #include <algorithm> #include <cmath> #include <ctime> #include <cstdlib> #include <cstdio> #include <utility> #include <iomanip> #include <assert.h> #define MP make_pair #define PB push_back #define FOR(i, a, b) for(int i =(a); i <=(b); ++i) #define RE(i, n) FOR(i, 1, n) #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 #ifdef LOCAL #define debug(x) {cerr <<#x <<" = " <<x <<"\n"; } #define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }} #else #define debug(x) #define debugv(x) #endif #define make(type, x) type x; cin>>x; #define make2(type, x, y) type x, y; cin>>x>>y; #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z; using namespace std; typedef long long ll; typedef long double LD; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; typedef vector<int> VI; typedef vector<ll> VLL; typedef vector<pair<int, int> > VPII; typedef vector<pair<ll, ll> > VPLL; 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.first << ", " << pair.second << ")";} const int N = 5e5 + 5; struct Point { ll x, y, nr, val, hei; Point(ll x_, ll y_, ll val_, ll hei_) : x(x_), y(y_), val(val_), hei(hei_) {} Point() : x(0), y(0), val(0), hei(0) {}; }; ll w, h; bool cmp1(const Point& p1, const Point& p2) { ll cross = (p2.y - p1.y) * w + (p2.x - p1.x) * h; if (cross) { return cross > 0; } return p1.val > p2.val; } bool cmp2(const Point& p1, const Point& p2) { ll cross = -(p2.y - p1.y) * w + (p2.x - p1.x) * h; if (cross) { return cross > 0; } return p1.val < p2.val; } const int M = 1 << 19; struct Node { ll val; ll min_down_bound; Node() : val(0), min_down_bound(0) {} }; Node drz[2 * M + 5]; ll Read(int ind) { ind += M - 1; ll res = 0; while (ind) { res += drz[ind].val; maxi(res, drz[ind].min_down_bound); //cerr<<res<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; ind /= 2; } return res; } const ll kInf = 2e18;; void PushDownBound(int i) { maxi(drz[2 * i].min_down_bound, drz[i].min_down_bound - drz[i].val); maxi(drz[2 * i + 1].min_down_bound, drz[i].min_down_bound - drz[i].val); drz[i].min_down_bound = -kInf; } void Add(int ind, int left, int right, int a, int b, ll val) { if (a > b) { return; } if (a == left && b == right) { // cerr<<"AddFit "<<a<<" "<<b<<endl; drz[ind].val += val; drz[ind].min_down_bound += val; // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; return; } PushDownBound(ind); int mid = (right - left) / 2; REP (i, 2) { int new_a = a; int new_b = b; maxi(new_a, left + i * (mid + 1)); mini(new_b, left + i * (mid + 1) + mid); Add(2 * ind + i, left + i * (mid + 1), left + i * (mid + 1) + mid, new_a, new_b, val); } //drz[ind].min_down_bound = drz[ind].val + min(drz[2 * ind].min_down_bound, drz[2 * ind + 1].min_down_bound); // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; } void Maxuj(int ind, int left, int right, int a, int b, ll val) { if (a > b) { return; } if (a == left && b == right) { // // cerr<<"MaxujFit "<<a<<" "<<b<<endl; maxi(drz[ind].min_down_bound, val); // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; return; } PushDownBound(ind); int mid = (right - left) / 2; REP (i, 2) { int new_a = a; int new_b = b; maxi(new_a, left + i * (mid + 1)); mini(new_b, left + i * (mid + 1) + mid); Maxuj(2 * ind + i, left + i * (mid + 1), left + i * (mid + 1) + mid, new_a, new_b, val - drz[ind].val); } // cerr<<left<<" "<<right<<" "<<drz[ind].val<<" "<<drz[ind].min_down_bound<<endl; } ll dp[N]; int main() { // nie zapomnij o ll ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); //goto Testuj; make2(int, n, m); cin>>w>>h; vector<Point> pts; RE (i, n) { make3(ll, x, y, v); Point p(x, y, v, 0); pts.PB(p); } RE (i, m) { make3(ll, x, y, v); Point p(x, y, -v, 0); pts.PB(p); } int total = n + m; sort(ALL(pts), cmp1); /* for (auto p : pts) { cout<<p.x<<" "<<p.y<<" "<<p.val<<endl; } */ for (int i = 0; i < pts.size(); i++) { pts[i].hei = i + 1; } sort(ALL(pts), cmp2); /* for (auto p : pts) { cerr<<p.x<<" "<<p.y<<" "<<p.val<<" "<<p.hei<<endl; } */ //Testuj: ; /* while (1) { cerr<<"Pisz"<<endl; int a, val; cin>>a>>val; Add(1, 1, M, 1, a, val); for (int i = 1; i <= 10; i++) { if (i <= a) { dp[i] += val; } else { maxi(dp[i], dp[i - 1]); } } ll val_here = Read(a); Maxuj(1, 1, M, a, 10, val_here); for (int i = 1; i <= 9; i++) { cerr<<Read(i)<<" "; } cerr<<endl; for (int i = 1; i <= 9; i++) { cerr<<dp[i]<<" "; } cerr<<endl; } */ for (auto p : pts) { Add(1, 1, M, 1, p.hei, p.val); /* for (int i = 1; i <= total + 1; i++) { cerr<<Read(i)<<" "; } cerr<<endl; */ ll val_here = Read(p.hei); Maxuj(1, 1, M, p.hei, total + 1, val_here); /* for (int i = 1; i <= total; i++) { cerr<<Read(i)<<" "; } cerr<<endl; */ } cout<<Read(total + 1)<<endl; return 0; } |