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
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
using namespace std;
typedef pair<int,int> PI;
typedef long long LL;
typedef double D;
#define FI first
#define SE second
#define MP make_pair
#define PB push_back
#define R(I,N) for(int I=0;I<N;I++)
#define F(I,A,B) for(int I=A;I<B;I++)
#define FD(I,N) for(int I=N-1;I>=0;I--)
#define make(A) scanf("%d",&A)
#define MAX 400100
int n,nn,m;
LL w,h;
struct pkt{
	int x,y;
	int war;
	bool typ;
}t[MAX];
int p[MAX];
LL skal1[MAX],skal2[MAX];
int p1[MAX],p2[MAX];
bool qsk1(int a,int b){return skal1[a]<skal1[b];}
bool qsk2(int a,int b){return skal2[a]<skal2[b];}
void skaluj(){
	R(i,n){
		p1[i] = i;
		p2[i] = i;
		skal1[i] = - t[i].x*h + t[i].y*w;
		skal2[i] = t[i].x*h + t[i].y*w;
	}
	sort(p1,p1+n,qsk1);
	int kt = 0;
	R(i,n){
		t[p1[i]].x = kt;
		if(skal1[p1[i]] != skal1[p1[i+1]])kt++;
	}
	sort(p2,p2+n,qsk2);
	kt = 1;
	R(i,n){
		t[p2[i]].y = kt;
		if(skal2[p2[i]] != skal2[p2[i+1]])kt++;
	}
	nn = 1;while(nn<=kt)nn*=2;
}
bool qq(int a,int b){
	if(t[a].x != t[b].x)
		return t[a].x < t[b].x;
	return t[a].typ < t[b].typ;
}
LL wyn;
int pk,kk;
LL gl;
LL sum[MAX*3],mi[MAX*3];
bool ak[MAX*3];
inline void wdol(int nr){
	if(ak[nr]){
		sum[nr*2+1] = sum[nr*2] = sum[nr];
		mi[nr*2+1] = mi[nr*2] = mi[nr];
		ak[nr*2+1] = ak[nr*2] = 1;
		ak[nr] = 0;
	}else{
		sum[nr*2+1] += sum[nr];
		sum[nr*2] += sum[nr];
		mi[nr*2+1] += sum[nr];
		mi[nr*2] += sum[nr];
	}
	sum[nr] = 0;
}

void dod(int nr=1,int pp=0,int kp=nn-1){
	if(pk>kp || kk < pp)return;
	if(pk<=pp && kk>=kp){
		sum[nr] += gl;
		mi[nr] += gl;
		return;
	}
	wdol(nr);
	dod(nr*2,pp,(pp+kp)/2);
	dod(nr*2+1,(pp+kp)/2+1,kp);
	mi[nr] = min(mi[nr*2],mi[nr*2+1]);
}

LL war(int nr=1,int pp=0,int kp=nn-1){
	if(pp==kp)return sum[nr];
	wdol(nr);
	if(pk <= (pp+kp)/2)
		return war(nr*2,pp,(pp+kp)/2);
	else
		return war(nr*2+1,(pp+kp)/2+1,kp);
}

// najmniejszy indeks o mniejszej wartosci
int mniej(int nr=1,int pp=0,int kp=nn-1){
	if(pp == kp)return pp;
	wdol(nr);
	if(mi[nr*2] < gl)
		return mniej(nr*2,pp,(pp+kp)/2);
	return mniej(nr*2+1,(pp+kp)/2+1,kp);
}

void przyp(int nr=1,int pp=0,int kp=nn-1){
	if(pk>kp || kk < pp)return;
	if(pk<=pp && kk>=kp){
		sum[nr] = gl;
		mi[nr] = gl;
		ak[nr] = 1;
		return;
	}
	wdol(nr);
	przyp(nr*2,pp,(pp+kp)/2);
	przyp(nr*2+1,(pp+kp)/2+1,kp);
	mi[nr] = min(mi[nr*2],mi[nr*2+1]);
}

main(){
	scanf("%d%d%lld%lld",&n,&m,&w,&h);
	R(i,n){
		make(t[i].x);
		make(t[i].y);
		make(t[i].war);
		t[i].typ = 0;
	}
	
	F(i,n,m+n){
		make(t[i].x);
		make(t[i].y);
		make(t[i].war);
		wyn -= t[i].war;
		t[i].typ = 1;
	}
	n+=m;
	skaluj();
	//R(i,n)printf("%d %d %d\n",t[i].x,t[i].y,t[i].war);
	R(i,n)p[i] = i;
	sort(p,p+n,qq);
	R(ii,n){int i = p[ii];
		if(t[i].typ==0){
			pk = 0;
			kk = t[i].y-1;
			gl = t[i].war;
			dod();
		}else{
			
			pk = t[i].y;
			kk = nn-1;
			
			LL nowa = war() + t[i].war;
			gl = nowa;
			int gdzie = mniej();
			/*{
				pk = gdzie;
				if(war()>=nowa){
					printf("%d %lld %lld\n",pk,nowa,war());
					printf("errorr!!!!\n");
					return 0;
				}
				pk--;
				if(pk!=-1){
					if(war()<nowa){
						printf("%d %lld %lld\n",pk,nowa,war());
						pk++;
						printf("%lld\n",war());
						printf("errorr2!!!\n");
						return 0;
					}
				}
				pk = t[i].y;
			}*/
			gl = t[i].war;
			dod();
			
			pk = gdzie;
			kk = t[i].y-1;
			gl = nowa;
			przyp();
		}
	/*	R(i,nn){
			pk = i;
			printf("%lld ",war());
		}
		puts("");*/
	}
	pk = 0;printf("%lld\n",wyn+war());
}