#include <cstdio> #include <algorithm> #include <random> #include <cstdlib> #include <vector> #define REP(i,n) for(int i=0; i<(n); ++i) #define FOR(i,p,k) for(int i=(p); i<=(k); ++i) #define DEBUG if(0) template<typename T> inline void checkmin(T &a, T b){ if(a>b) a = b; } template<typename T> inline void checkmax(T &a, T b){ if(a<b) a = b; } typedef long long LL; std::mt19937 rnd(1253470); template<typename T> struct Pt { T x,y,v; static bool cmp_x(const Pt &a, const Pt &b){ return a.x<b.x; } static bool cmp_y(const Pt &a, const Pt &b){ return a.y<b.y; } }; struct In { enum { n_max = 200000, x_max = 1000000000 }; typedef unsigned int ui; int w,h; std::vector<Pt<int>> G,D; void read() { int n,m; scanf("%d%d%d%d",&n,&m,&w,&h); D.resize(n); G.resize(m); for(auto &d : D) scanf("%d%d%d",&d.x,&d.y,&d.v); for(auto &g : G) scanf("%d%d%d",&g.x,&g.y,&g.v); } void gen_max(){ gen(n_max,n_max,x_max,x_max); } void gen(ui n = 10, ui m = 10, ui x = 100, ui v = 10) { w = rnd()%x+1; h = rnd()%x+1; D.resize(n); G.resize(m); for(auto &d : D) d = Pt<int>{int(rnd()%x),int(rnd()%x),int(rnd()%v)}; for(auto &g : G) g = Pt<int>{int(rnd()%x),int(rnd()%x),int(rnd()%v)}; } void print() const { printf("w=%d, h=%d\n",w,h); for(auto &d : D) printf("(%d;%d) %d\n",d.x,d.y,d.v); puts(""); for(auto &g : G) printf("(%d;%d) %d\n",g.x,g.y,g.v); puts(""); } }; struct Seg { int a,b; // [a,b] bool cross(Seg s) const { return std::max(a,s.a)<=std::min(b,s.b); } bool contains(Seg s) const { return a<=s.a && s.b<=b; } int length() const { return b-a+1; } bool side(int c) const { return c>(a+b)/2; } Seg half(bool i) const { int m=(a+b)/2; return i ? Seg{m+1,b} : Seg{a,m}; } }; struct Tree { struct Node { LL m,l,a; Node() : m(0),l(0),a(0) {} }; std::vector<Node> T; int h; Tree(int n){ h = 1; while(h<n) h<<=1; T.resize(2*h); } void update(int t) { T[t].m = T[t].l; if(t<h) checkmax(T[t].m,std::max(T[t<<1].m,T[t<<1|1].m)); T[t].m += T[t].a; } void prop(int t, Seg cx, Seg x) { if(t>=T.size() || !x.cross(cx)) return; if(t>>1) checkmax(T[t].l,T[t>>1].l-T[t].a); if(!x.contains(cx)) { prop(t<<1,cx.half(0),x); prop(t<<1|1,cx.half(1),x); } update(t); } void maxi(Seg x, LL l){ prop(1,Seg{0,h-1},x); maxi(1,Seg{0,h-1},x,l); } void add(Seg x, LL a){ prop(1,Seg{0,h-1},x); add(1,Seg{0,h-1},x,a); } LL get(Seg x){ prop(1,Seg{0,h-1},x); return get(1,Seg{0,h-1},x); } void maxi(int t, Seg cx, Seg x, LL l) { if(t>=T.size() || !x.cross(cx)) return; l -= T[t].a; if(x.contains(cx)) checkmax(T[t].l,l); else { maxi(t<<1,cx.half(0),x,l); maxi(t<<1|1,cx.half(1),x,l); } update(t); } void add(int t, Seg cx, Seg x, LL a) { if(t>=T.size() || !x.cross(cx)) return; if(x.contains(cx)) T[t].a += a; else { add(t<<1,cx.half(0),x,a); add(t<<1|1,cx.half(1),x,a); } update(t); } LL get(int t, Seg cx, Seg x) { if(t>=T.size() || !x.cross(cx)) return 0; return x.contains(cx) ? T[t].m : T[t].a+std::max(get(t<<1,cx.half(0),x),get(t<<1|1,cx.half(1),x)); } void print() { FOR(i,1,T.size()-1) printf("%d : (%lld,%lld,%lld)\n",i,T[i].m,T[i].l,T[i].a); } }; void test_tree() { enum { n = 1000, a_max = 10 }; Tree T(n); LL A[n]={}; for(int c=0;;c++) { Seg x{int(rnd()%n),int(rnd()%n)}; if(x.a>x.b) std::swap(x.a,x.b); LL a = rnd()%a_max; LL l = A[rnd()%n]+10; switch(rnd()%3) { case 0: { printf("add(%d,%d,%lld)\n",x.a,x.b,a); T.add(x,a); FOR(i,x.a,x.b) A[i] += a; break; } case 1: { printf("maxi(%d,%d,%lld)\n",x.a,x.b,l); T.maxi(x,l); FOR(i,x.a,x.b) checkmax(A[i],l); break; } case 2: { LL b = 0; FOR(i,x.a,x.b) checkmax(b,A[i]); LL s = T.get(x); printf("get(%d,%d) = %lld\n",x.a,x.b,s); if(b!=s) { printf("ERROR b=%lld\n",b); printf("A ="); REP(i,n) printf(" %lld",A[i]); puts(""); exit(1); } } } //T.print(); } } LL go(const In &I) { std::vector<Pt<LL>> P; for(auto d : I.D) P.push_back(Pt<LL>{-(LL(d.y)*I.w+LL(d.x)*I.h), LL(d.y)*I.w-LL(d.x)*I.h, d.v}); for(auto g : I.G) P.push_back(Pt<LL>{-(LL(g.y)*I.w+LL(g.x)*I.h), LL(g.y)*I.w-LL(g.x)*I.h,-g.v}); std::sort(P.begin(),P.end(),Pt<LL>::cmp_y); int y = 1; LL py = P[0].y; for(auto &p : P){ if(py<p.y){ py = p.y; y++; } p.y = y; } std::sort(P.begin(),P.end(),Pt<LL>::cmp_x); DEBUG { puts("==================\nafter sort"); for(auto &p : P) printf("(%lld;%lld) %lld\n",p.x,p.y,p.v); } Tree T(y+10); for(int i=0; i<P.size();) { int j=i+1; while(j<P.size() && P[j].x==P[i].x) j++; FOR(k,i,j-1) if(P[k].v>0) T.add(Seg{0,int(P[k].y)-1},P[k].v); else T.add(Seg{int(P[k].y),T.h-1},-P[k].v); FOR(k,i,j-1) T.maxi(Seg{int(P[k].y),T.h-1},T.get(Seg{0,int(P[k].y)})); i = j; } LL res = T.get(Seg{0,T.h-1}); DEBUG printf("res=%lld\n",res); for(auto &g : I.G) res -= g.v; return res; } LL brute(const In &I) { LL W = I.w, H = I.h; int n = I.D.size(), m = I.G.size(); LL res = 0; REP(w,1<<n+m) { bool ok = 1; REP(i,n) if(w>>i&1) REP(j,m) if(w>>j+n&1) { LL dx = I.D[i].x, dy = I.D[i].y; LL gx = I.G[j].x, gy = I.G[j].y; if(dy*W-dx*H<=gy*W-gx*H && dy*W+dx*H<=gy*W+gx*H){ ok = 0; break; } } if(ok) { LL r = 0; REP(i,n) if(w>>i&1) r += I.D[i].v; REP(j,m) if(!(w>>j+n&1)) r -= I.G[j].v; checkmax(res,r); } } return res; } void test() { REP(_,5) { puts("max test"); In I; I.gen_max(); go(I); } for(int c=0;;c++) { In I; I.gen(); LL s = go(I), b = brute(I); if(s!=b) { printf("ERROR s=%lld b=%lld\n",s,b); I.print(); exit(1); } printf("ok %d\n",c); } } int main() { //test_tree(); //test(); In I; I.read(); printf("%lld\n",go(I)); 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | #include <cstdio> #include <algorithm> #include <random> #include <cstdlib> #include <vector> #define REP(i,n) for(int i=0; i<(n); ++i) #define FOR(i,p,k) for(int i=(p); i<=(k); ++i) #define DEBUG if(0) template<typename T> inline void checkmin(T &a, T b){ if(a>b) a = b; } template<typename T> inline void checkmax(T &a, T b){ if(a<b) a = b; } typedef long long LL; std::mt19937 rnd(1253470); template<typename T> struct Pt { T x,y,v; static bool cmp_x(const Pt &a, const Pt &b){ return a.x<b.x; } static bool cmp_y(const Pt &a, const Pt &b){ return a.y<b.y; } }; struct In { enum { n_max = 200000, x_max = 1000000000 }; typedef unsigned int ui; int w,h; std::vector<Pt<int>> G,D; void read() { int n,m; scanf("%d%d%d%d",&n,&m,&w,&h); D.resize(n); G.resize(m); for(auto &d : D) scanf("%d%d%d",&d.x,&d.y,&d.v); for(auto &g : G) scanf("%d%d%d",&g.x,&g.y,&g.v); } void gen_max(){ gen(n_max,n_max,x_max,x_max); } void gen(ui n = 10, ui m = 10, ui x = 100, ui v = 10) { w = rnd()%x+1; h = rnd()%x+1; D.resize(n); G.resize(m); for(auto &d : D) d = Pt<int>{int(rnd()%x),int(rnd()%x),int(rnd()%v)}; for(auto &g : G) g = Pt<int>{int(rnd()%x),int(rnd()%x),int(rnd()%v)}; } void print() const { printf("w=%d, h=%d\n",w,h); for(auto &d : D) printf("(%d;%d) %d\n",d.x,d.y,d.v); puts(""); for(auto &g : G) printf("(%d;%d) %d\n",g.x,g.y,g.v); puts(""); } }; struct Seg { int a,b; // [a,b] bool cross(Seg s) const { return std::max(a,s.a)<=std::min(b,s.b); } bool contains(Seg s) const { return a<=s.a && s.b<=b; } int length() const { return b-a+1; } bool side(int c) const { return c>(a+b)/2; } Seg half(bool i) const { int m=(a+b)/2; return i ? Seg{m+1,b} : Seg{a,m}; } }; struct Tree { struct Node { LL m,l,a; Node() : m(0),l(0),a(0) {} }; std::vector<Node> T; int h; Tree(int n){ h = 1; while(h<n) h<<=1; T.resize(2*h); } void update(int t) { T[t].m = T[t].l; if(t<h) checkmax(T[t].m,std::max(T[t<<1].m,T[t<<1|1].m)); T[t].m += T[t].a; } void prop(int t, Seg cx, Seg x) { if(t>=T.size() || !x.cross(cx)) return; if(t>>1) checkmax(T[t].l,T[t>>1].l-T[t].a); if(!x.contains(cx)) { prop(t<<1,cx.half(0),x); prop(t<<1|1,cx.half(1),x); } update(t); } void maxi(Seg x, LL l){ prop(1,Seg{0,h-1},x); maxi(1,Seg{0,h-1},x,l); } void add(Seg x, LL a){ prop(1,Seg{0,h-1},x); add(1,Seg{0,h-1},x,a); } LL get(Seg x){ prop(1,Seg{0,h-1},x); return get(1,Seg{0,h-1},x); } void maxi(int t, Seg cx, Seg x, LL l) { if(t>=T.size() || !x.cross(cx)) return; l -= T[t].a; if(x.contains(cx)) checkmax(T[t].l,l); else { maxi(t<<1,cx.half(0),x,l); maxi(t<<1|1,cx.half(1),x,l); } update(t); } void add(int t, Seg cx, Seg x, LL a) { if(t>=T.size() || !x.cross(cx)) return; if(x.contains(cx)) T[t].a += a; else { add(t<<1,cx.half(0),x,a); add(t<<1|1,cx.half(1),x,a); } update(t); } LL get(int t, Seg cx, Seg x) { if(t>=T.size() || !x.cross(cx)) return 0; return x.contains(cx) ? T[t].m : T[t].a+std::max(get(t<<1,cx.half(0),x),get(t<<1|1,cx.half(1),x)); } void print() { FOR(i,1,T.size()-1) printf("%d : (%lld,%lld,%lld)\n",i,T[i].m,T[i].l,T[i].a); } }; void test_tree() { enum { n = 1000, a_max = 10 }; Tree T(n); LL A[n]={}; for(int c=0;;c++) { Seg x{int(rnd()%n),int(rnd()%n)}; if(x.a>x.b) std::swap(x.a,x.b); LL a = rnd()%a_max; LL l = A[rnd()%n]+10; switch(rnd()%3) { case 0: { printf("add(%d,%d,%lld)\n",x.a,x.b,a); T.add(x,a); FOR(i,x.a,x.b) A[i] += a; break; } case 1: { printf("maxi(%d,%d,%lld)\n",x.a,x.b,l); T.maxi(x,l); FOR(i,x.a,x.b) checkmax(A[i],l); break; } case 2: { LL b = 0; FOR(i,x.a,x.b) checkmax(b,A[i]); LL s = T.get(x); printf("get(%d,%d) = %lld\n",x.a,x.b,s); if(b!=s) { printf("ERROR b=%lld\n",b); printf("A ="); REP(i,n) printf(" %lld",A[i]); puts(""); exit(1); } } } //T.print(); } } LL go(const In &I) { std::vector<Pt<LL>> P; for(auto d : I.D) P.push_back(Pt<LL>{-(LL(d.y)*I.w+LL(d.x)*I.h), LL(d.y)*I.w-LL(d.x)*I.h, d.v}); for(auto g : I.G) P.push_back(Pt<LL>{-(LL(g.y)*I.w+LL(g.x)*I.h), LL(g.y)*I.w-LL(g.x)*I.h,-g.v}); std::sort(P.begin(),P.end(),Pt<LL>::cmp_y); int y = 1; LL py = P[0].y; for(auto &p : P){ if(py<p.y){ py = p.y; y++; } p.y = y; } std::sort(P.begin(),P.end(),Pt<LL>::cmp_x); DEBUG { puts("==================\nafter sort"); for(auto &p : P) printf("(%lld;%lld) %lld\n",p.x,p.y,p.v); } Tree T(y+10); for(int i=0; i<P.size();) { int j=i+1; while(j<P.size() && P[j].x==P[i].x) j++; FOR(k,i,j-1) if(P[k].v>0) T.add(Seg{0,int(P[k].y)-1},P[k].v); else T.add(Seg{int(P[k].y),T.h-1},-P[k].v); FOR(k,i,j-1) T.maxi(Seg{int(P[k].y),T.h-1},T.get(Seg{0,int(P[k].y)})); i = j; } LL res = T.get(Seg{0,T.h-1}); DEBUG printf("res=%lld\n",res); for(auto &g : I.G) res -= g.v; return res; } LL brute(const In &I) { LL W = I.w, H = I.h; int n = I.D.size(), m = I.G.size(); LL res = 0; REP(w,1<<n+m) { bool ok = 1; REP(i,n) if(w>>i&1) REP(j,m) if(w>>j+n&1) { LL dx = I.D[i].x, dy = I.D[i].y; LL gx = I.G[j].x, gy = I.G[j].y; if(dy*W-dx*H<=gy*W-gx*H && dy*W+dx*H<=gy*W+gx*H){ ok = 0; break; } } if(ok) { LL r = 0; REP(i,n) if(w>>i&1) r += I.D[i].v; REP(j,m) if(!(w>>j+n&1)) r -= I.G[j].v; checkmax(res,r); } } return res; } void test() { REP(_,5) { puts("max test"); In I; I.gen_max(); go(I); } for(int c=0;;c++) { In I; I.gen(); LL s = go(I), b = brute(I); if(s!=b) { printf("ERROR s=%lld b=%lld\n",s,b); I.print(); exit(1); } printf("ok %d\n",c); } } int main() { //test_tree(); //test(); In I; I.read(); printf("%lld\n",go(I)); return 0; } |