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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <queue>
#include <ctime>
#include <map>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <iostream>
#include <cstring>
#define REP(i,x,v)for(int i=x;i<=v;i++)
#define REPD(i,x,v)for(int i=x;i>=v;i--)
#define FOR(i,v)for(int i=0;i<v;i++)
#define FORE(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define REMIN(x,y) (x)=min((x),(y))
#define REMAX(x,y) (x)=max((x),(y))
#define pb push_back
#define sz size()
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define IN(x,y) ((y).find((x))!=(y).end())
#define un(v) v.erase(unique(ALL(v)),v.end())
#define LOLDBG
#ifdef LOLDBG
#define DBG(vari) cerr<<#vari<<" = "<<vari<<endl;
#define DBG2(v1,v2) cerr<<(v1)<<" - "<<(v2)<<endl;
#else
#define DBG(vari)
#define DBG2(v1,v2)
#endif
#define CZ(x) scanf("%d",&(x));
#define CZ2(x,y) scanf("%d%d",&(x),&(y));
#define CZ3(x,y,z) scanf("%d%d%d",&(x),&(y),&(z));
#define ALL(x) (x).begin(),(x).end()
#define tests int dsdsf;cin>>dsdsf;while(dsdsf--)
#define testss int dsdsf;CZ(dsdsf);while(dsdsf--)
using namespace std;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
template<typename T,typename TT> ostream &operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";}
template<typename T> ostream &operator<<(ostream &s,vector<T> t){s<<"{";FOR(i,t.size())s<<t[i]<<(i==t.size()-1?"":",");return s<<"}"<<endl; }
#define INF (1LL<<60)
int n,m;
int r;
pii str[200002];
int cenas[200002];
pii eks[200002];
int cenae[200002];
ll w,h;

int widze[44][44];

pair<pll,int> P[400004];
pair<pii,int> p[400004];

namespace fio
{

/////////////////////
////// INPUT ////////
////////////////////
typedef unsigned long long ull;
class INPUT {
static const int BUFSIZE = 1<<16;
static char buffer[];
char *bufpos;
char *bufend;
void grabBuffer();
public:
INPUT() { grabBuffer(); }
bool eof() { return bufend==buffer; }
char nextChar() { return *bufpos; }
inline char readChar();
inline void skipWS();
unsigned readUnsigned();
int readInt();
ull readULL();
};

char INPUT::buffer[INPUT::BUFSIZE];

void INPUT::grabBuffer() {
bufpos = buffer;
bufend = buffer + fread( buffer, 1,BUFSIZE,stdin);
}

char INPUT::readChar() {
char res = *bufpos++;
if(bufpos==bufend) grabBuffer();
return res;
}

inline bool myisspace(char c) { return c<=' '; }

void INPUT::skipWS() {
while(!eof() && myisspace(nextChar())) readChar();
}

unsigned INPUT::readUnsigned() {
skipWS();
unsigned res = 0;
while(!eof() && isdigit(nextChar())) {
res = 10u * res + (readChar()-'0');
}
return res;
}

ull INPUT::readULL() {
skipWS();
ull res = 0;
while(!eof() && isdigit(nextChar())) {
res = 10ULL * res + (readChar()-'0');
}
return res;
}

int INPUT::readInt() {
	skipWS();
	bool neg = false;
	if(!eof() && nextChar()=='-') { neg=true; readChar(); }
	int res = static_cast<int>(readUnsigned());
	if(neg) res = -res;
	return res;
}

/////////////////////
////// OUTPUT //////
////////////////////

class OUTPUT {
static const int BUFSIZE = 1<<16;
static char buffer[];
char *bufpos;
char *BUFLIMIT;
void flushBuffer();
public:
	OUTPUT():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {}
	~OUTPUT() { flushBuffer(); }
	inline void operator()(char c);
	inline void operator()(unsigned x);
	inline void operator()(int x);
	inline void operator()(unsigned long long int x);
	inline void operator()(long long int x);
	inline void operator()(const char*s);
	void operator()(const string&s) { operator()(s.c_str()); }
	template<class A,class B>
	void operator()(const A& a,const B& b) {
		operator()(a); operator()(b);
	}
	template<class A,class B,class C>
	void operator()(const A& a,const B& b,const C&c) {
		operator()(a); operator()(b); operator()(c);
	}
	template<class A,class B,class C,class D>
	void operator()(const A& a,const B& b,const C&c,const D&d) {
		operator()(a); operator()(b); operator()(c); operator()(d);
	}
	template<class A,class B,class C,class D,class E>
	void operator()(const A& a,const B& b,const C&c,const D&d,const E&e) {
		operator()(a); operator()(b); operator()(c); operator()(d); operator()(e);
	}
	template<class A,class B,class C,class D,class E,class F>
	void operator()(const A& a,const B& b,const C&c,const D&d,const E&e,const F&f) {
		operator()(a); operator()(b); operator()(c); operator()(d); operator()(e); operator()(f);
	}
};

char OUTPUT::buffer[OUTPUT::BUFSIZE];

void OUTPUT::flushBuffer() {
	char *p = buffer;
	while(p < bufpos) {
		p += fwrite( p,1, bufpos-p,stdout);
	}
	bufpos = buffer;
}

void OUTPUT::operator()(char c) {
	*bufpos = c;
	++bufpos;
	if(bufpos >= BUFLIMIT) flushBuffer();
}

void OUTPUT::operator()(unsigned x) {
	char *old = bufpos;
	do {
		*bufpos = char('0' + x % 10u);
		x /= 10u;
		++bufpos;
	} while(x);
	reverse(old, bufpos);
	if(bufpos >= BUFLIMIT) flushBuffer();
}

void OUTPUT::operator()(int x) {
	if(x<0) { operator()('-'); x = -x; }
	operator()(static_cast<unsigned>(x));
}

void OUTPUT::operator()(unsigned long long int x) {
	char *old = bufpos;
	do {
		*bufpos = char('0' + x % 10u);
		x /= 10u;
		++bufpos;
	} while(x);
	reverse(old, bufpos);
	if(bufpos >= BUFLIMIT) flushBuffer();
}

void OUTPUT::operator()(long long int x) {
	if(x<0) { operator()('-'); x = -x; }
	operator()(static_cast<unsigned long long int>(x));
}

void OUTPUT::operator()(const char*s) {
	while(*s) operator()(*s++);
}

INPUT input;
OUTPUT output;
}


void transformuj()
{
	r=0;
	vector<ll> X,Y;
	FOR(i,n)
	{
		ll x=eks[i].fi*h;
		ll y=eks[i].se*w;
		P[r++]=mp(mp(x-y,x+y),cenae[i]);
		X.pb(x-y);
		Y.pb(x+y);
	}
	FOR(j,m)
	{
		ll x=str[j].fi*h;
		ll y=str[j].se*w;
		P[r++]=mp(mp(x-y,x+y),-cenas[j]);
		X.pb(x-y);
		Y.pb(x+y);
	}
	sort(ALL(X));un(X);
	sort(ALL(Y));un(Y);
	FOR(i,r)
	{
        p[i].fi.fi=lower_bound(ALL(X),P[i].fi.fi)-X.begin();
        p[i].fi.se=lower_bound(ALL(Y),P[i].fi.se)-Y.begin();
        p[i].se=P[i].se;
        //cout<<p[i]<<endl;
	}

}


struct node
{
    ll dod;
    ll ust;
    ll najw;
    bool force;
    node(ll _dod=0,ll _ust=0,ll _najw=0,bool _force=0){dod=_dod;ust=_ust;force=_force;najw=_najw;}
};
node d[1<<20];
int M=1<<19;
int A,B;
ll V;

void wdol(int nr)
{
	if (nr>=M)
	{
        if (d[nr].force)
		{
            d[nr].force=0;
            d[nr].dod+=d[nr].ust;
            d[nr].najw=d[nr].dod;
            d[nr].ust=0;
		}
		return;
	}
    if (d[nr].force)
	{
        d[nr*2].force=1;
        d[nr*2].ust=d[nr].ust+d[nr].dod;
        d[nr*2].dod=0;
        d[nr*2].najw=d[nr].ust+d[nr].dod;
        d[nr*2+1].force=1;
        d[nr*2+1].ust=d[nr].ust+d[nr].dod;
        d[nr*2+1].dod=0;
        d[nr*2+1].najw=d[nr].ust+d[nr].dod;
	}
	else
	{
        d[nr*2].dod+=d[nr].dod;
        d[nr*2].najw+=d[nr].dod;
        d[nr*2+1].dod+=d[nr].dod;
        d[nr*2+1].najw+=d[nr].dod;
	}
	d[nr].dod=0;
	d[nr].ust=0;
	d[nr].force=0;
	d[nr].najw=max(d[nr*2].najw,d[nr*2+1].najw);
}

void dodm(int l,int r,int nr)
{
    if (A>r || B<l) return;
    wdol(nr);
    if (A<=l && r<=B)
	{
        d[nr].dod+=V;
        d[nr].najw+=V;
        return;
	}
    dodm(l,(l+r)/2,nr*2);
    dodm((l+r)/2+1,r,nr*2+1);
    d[nr].najw=max(d[nr*2].najw,d[nr*2+1].najw);
}

void ustm(int l,int r,int nr)
{
    if (A>r || B<l) return;
    wdol(nr);
    if (A<=l && r<=B)
	{
        d[nr].force=1;
        d[nr].ust=V;
        d[nr].dod=0;
        d[nr].najw=V;
        return;
	}
    ustm(l,(l+r)/2,nr*2);
    ustm((l+r)/2+1,r,nr*2+1);
    d[nr].najw=max(d[nr*2].najw,d[nr*2+1].najw);
}

void ustaw(int a,int b,ll val)
{
    A=a;
    B=b;
    V=val;
    ustm(0,M-1,1);
}

void dodaj(int a,int b,int val)
{
    A=a;
    B=b;
    V=val;
    dodm(0,M-1,1);
}

ll wez(int x)
{
    x+=M;
    ll g=0;
    while(x)
	{
        if (d[x].force) g=d[x].ust;
        g+=d[x].dod;
        x/=2;
	}
	return g;
}

int szuk(int a,ll val, int l,int r,int nr)
{
	if (r<a) return M;
	wdol(nr);
	if (d[nr].najw<val) return M;
	if (nr>=M) return nr-M;
	int w=szuk(a,val,l,(l+r)/2,nr*2);
	if (w<M) return w;
	return szuk(a,val,(l+r)/2+1,r,nr*2+1);
}

ll niebrut()
{
	transformuj();
	FOR(i,r) p[i].fi.se=-p[i].fi.se;
	sort(p,p+r);
	FOR(i,r) p[i].fi.se=-p[i].fi.se;
	M=1;
	while(M<=r+3)M*=2;
	FOR(i,2*M) d[i]=node();
	ustaw(r+1,M-1,INF);
    REP(i,1,r)
    {
    	int y=p[i-1].fi.se;
    	int val=p[i-1].se;
    	dodaj(0,y,val);
    	ll lol=wez(y);
		int hi=szuk(y+1,lol,0,M-1,1);
        if (hi>y+1) ustaw(y+1,hi-1,lol);
    }
	return wez(r);
}


int main()
{
	n=fio::input.readInt();
	m=fio::input.readInt();
	w=fio::input.readInt();
	h=fio::input.readInt();
	FOR(i,n){eks[i].fi=fio::input.readInt();eks[i].se=fio::input.readInt();cenae[i]=fio::input.readInt();}
	FOR(i,m){str[i].fi=fio::input.readInt();str[i].se=fio::input.readInt();cenas[i]=fio::input.readInt();}
	fio::output(niebrut());
	//DBG(brutn2());

	return 0;
}