#include <algorithm> #include <cassert> #include <iostream> #include <vector> using namespace std; #define REP(i,n) for(int _n=n, i=0;i<_n;++i) typedef long long LL; class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } Input IN; // 24b struct Point { LL x,y; int v; void Turn135() { LL x2 = -x-y; LL y2 = x-y; x = x2; y = y2; } }; vector<Point> gPoints; // 9.6MB LL ReadInput() { int n[2]; IN(n[0], n[1]); // good, bad int w,h; IN(w,h); gPoints.reserve(n[0] + n[1]); LL res = 0; REP(t,2) { REP(i,n[t]) { int x,y,v; IN(x,y,v); Point p { x * static_cast<LL>(h), y * static_cast<LL>(w), v }; if(t) p.v = -p.v; p.Turn135(); p.x = 2*p.x - t; gPoints.push_back(p); } } return res; } int ReduceYRange() { sort(gPoints.begin(), gPoints.end(), [](const Point &a, const Point &b) { return a.y < b.y; }); LL prev = gPoints[0].y; int yrange=1; for(Point &p : gPoints) { if(p.y != prev) { ++yrange; prev = p.y; } p.y = yrange-1; } return yrange; } class Structure { public: explicit Structure(int range); void AddPrefix(int x, int v); LL Max() { return mx; } private: int n; vector<LL> tab; vector<int> firstNonzeroTree; LL mx; void AddPositive(int x, int v); void SubtractPositive(int x, int v); int FindNonzeroAtMost(int x); void Delete(int x); }; Structure::Structure(int range) { n=1; while(n<range) n*=2; tab.assign(n, 0LL); firstNonzeroTree.assign(2*n, n); mx = 0; } void Structure::AddPrefix(int x, int v) { if(v>=0) { AddPositive(x, v); } else { SubtractPositive(x,-v); } } void Structure::SubtractPositive(int x, int v) { tab[x] += v; for(int p = n+x; p; p>>=1) { firstNonzeroTree[p] = min(firstNonzeroTree[p], x); } } void Structure::AddPositive(int x, int v) { while(v>0) { int y = FindNonzeroAtMost(x); if(y==-1) break; if(tab[y] > v) { tab[y] -= v; v = 0; } else { v -= tab[y]; Delete(y); } } mx += v; } int Structure::FindNonzeroAtMost(int x) { int p=1; if(firstNonzeroTree[p]>x) return -1; while(p<n) { p*=2; if(firstNonzeroTree[p+1]<=x) ++p; } return p-n; } void Structure::Delete(int x) { tab[x]=0; int p = n+x; firstNonzeroTree[p] = n; for(p>>=1; p; p>>=1) { firstNonzeroTree[p] = min(firstNonzeroTree[2*p], firstNonzeroTree[2*p+1]); } } LL Solve() { int yrange = ReduceYRange(); sort(gPoints.begin(), gPoints.end(), [](const Point &a, const Point &b) { return a.x < b.x; }); Structure structure { yrange }; for(const Point &p : gPoints) { structure.AddPrefix(p.y, p.v); } return structure.Max(); } int main() { LL res = ReadInput(); res += Solve(); cout << res << '\n'; }
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 | #include <algorithm> #include <cassert> #include <iostream> #include <vector> using namespace std; #define REP(i,n) for(int _n=n, i=0;i<_n;++i) typedef long long LL; class Input { public: Input() { bufpos = bufend = buffer; eof = false; } bool Eof() { return eof; } char Peek() { if(bufpos == bufend) Grab(); return *bufpos; } unsigned char UPeek() { return static_cast<unsigned char>(Peek()); } void SkipWS(); template<class T> T Get(); void operator()() {} template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) { arg = Get<Arg>(); operator()(args...); } private: static const int BUFSIZE = 1<<16; char buffer[BUFSIZE]; char *bufpos; char *bufend; bool eof; void Grab(); }; void Input::Grab() { if(eof) return; bufpos = buffer; bufend = buffer + read(0, buffer, BUFSIZE); if(bufend==bufpos) { eof=true; *bufpos=0; } } template<> inline char Input::Get<char>() { char res = Peek(); ++bufpos; return res; } void Input::SkipWS() { while(isspace(UPeek())) Get<char>(); } template<> unsigned Input::Get<unsigned>() { SkipWS(); unsigned x = 0; while(isdigit(UPeek())) { x = 10u * x + (Get<char>()-'0'); } return x; } template<> int Input::Get<int>() { SkipWS(); bool neg = false; if(Peek()=='-') { neg=true; Get<char>(); } unsigned x = Get<unsigned>(); if (neg) x = -x; return static_cast<int>(x); } Input IN; // 24b struct Point { LL x,y; int v; void Turn135() { LL x2 = -x-y; LL y2 = x-y; x = x2; y = y2; } }; vector<Point> gPoints; // 9.6MB LL ReadInput() { int n[2]; IN(n[0], n[1]); // good, bad int w,h; IN(w,h); gPoints.reserve(n[0] + n[1]); LL res = 0; REP(t,2) { REP(i,n[t]) { int x,y,v; IN(x,y,v); Point p { x * static_cast<LL>(h), y * static_cast<LL>(w), v }; if(t) p.v = -p.v; p.Turn135(); p.x = 2*p.x - t; gPoints.push_back(p); } } return res; } int ReduceYRange() { sort(gPoints.begin(), gPoints.end(), [](const Point &a, const Point &b) { return a.y < b.y; }); LL prev = gPoints[0].y; int yrange=1; for(Point &p : gPoints) { if(p.y != prev) { ++yrange; prev = p.y; } p.y = yrange-1; } return yrange; } class Structure { public: explicit Structure(int range); void AddPrefix(int x, int v); LL Max() { return mx; } private: int n; vector<LL> tab; vector<int> firstNonzeroTree; LL mx; void AddPositive(int x, int v); void SubtractPositive(int x, int v); int FindNonzeroAtMost(int x); void Delete(int x); }; Structure::Structure(int range) { n=1; while(n<range) n*=2; tab.assign(n, 0LL); firstNonzeroTree.assign(2*n, n); mx = 0; } void Structure::AddPrefix(int x, int v) { if(v>=0) { AddPositive(x, v); } else { SubtractPositive(x,-v); } } void Structure::SubtractPositive(int x, int v) { tab[x] += v; for(int p = n+x; p; p>>=1) { firstNonzeroTree[p] = min(firstNonzeroTree[p], x); } } void Structure::AddPositive(int x, int v) { while(v>0) { int y = FindNonzeroAtMost(x); if(y==-1) break; if(tab[y] > v) { tab[y] -= v; v = 0; } else { v -= tab[y]; Delete(y); } } mx += v; } int Structure::FindNonzeroAtMost(int x) { int p=1; if(firstNonzeroTree[p]>x) return -1; while(p<n) { p*=2; if(firstNonzeroTree[p+1]<=x) ++p; } return p-n; } void Structure::Delete(int x) { tab[x]=0; int p = n+x; firstNonzeroTree[p] = n; for(p>>=1; p; p>>=1) { firstNonzeroTree[p] = min(firstNonzeroTree[2*p], firstNonzeroTree[2*p+1]); } } LL Solve() { int yrange = ReduceYRange(); sort(gPoints.begin(), gPoints.end(), [](const Point &a, const Point &b) { return a.x < b.x; }); Structure structure { yrange }; for(const Point &p : gPoints) { structure.AddPrefix(p.y, p.v); } return structure.Max(); } int main() { LL res = ReadInput(); res += Solve(); cout << res << '\n'; } |