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
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;

typedef long long LL;

const int MaxN = 200005,
          MaxM = 200005;
          
int N, M;
LL h, w;
LL itemX[MaxN], itemY[MaxN];
int itemC[MaxN];
LL guardX[MaxN], guardY[MaxN];
int guardC[MaxN];

LL tab[MaxN*2];
map<LL,int> Mp;


void input(){
	scanf("%d%d%d%d", &N, &M, &w, &h);
	for(int i = 0; i < N; i++)
		scanf("%lld%lld%d", &itemX[i], &itemY[i], &itemC[i]);
	for(int i = 0; i < M; i++)
		scanf("%lld%lld%d", &guardX[i], &guardY[i], &guardC[i]);
}


struct Action {
	int x, y, money;
	bool isGuard;
	
	Action(int _x=0, int _y=0, int cost=0, bool guard=true) :
		x(_x), y(_y), money(cost), isGuard(guard) {}
	
	bool operator<(const Action &A) const {
		if(y == A.y){
			if(x == A.x)
				return isGuard < A.isGuard;
			else
				return x > A.x;
		} else
			return y < A.y;
	}
};
Action A[MaxN+MaxM];


void process_input(){
	for(int i = 0; i < N; i++){
		LL x, y;
		x = itemX[i]; y = itemY[i];
		itemX[i] = h*x - w*y;
		itemY[i] = h*x + w*y;
	}
	for(int i = 0; i < M; i++){
		LL x, y;
		x = guardX[i]; y = guardY[i];
		guardX[i] = h*x - w*y;
		guardY[i] = h*x + w*y;
	}
	
	//for(int i = 0; i < N; i++) printf("%d (%lld %lld)\n", i, itemX[i], itemY[i]);
	//for(int i = 0; i < M; i++) printf("%d  (%lld %lld)\n", i, guardX[i], guardY[i]);
	
	
	// przenumerowac
	for(int i = 0; i < N; i++) tab[i] = itemX[i];
	for(int i = 0; i < M; i++) tab[i+N] = guardX[i];
	sort(tab, tab+N+M);
	int num = unique(tab, tab+N+M) - tab;
	for(int i = 0; i < num; i++)
		Mp[tab[i]] = i;
	
	for(int i = 0; i < N; i++) itemX[i] = Mp[itemX[i]];
	for(int i = 0; i < M; i++) guardX[i] = Mp[guardX[i]];
	
	Mp.clear();
	
	for(int i = 0; i < N; i++) tab[i] = itemY[i];
	for(int i = 0; i < M; i++) tab[i+N] = guardY[i];
	sort(tab, tab+N+M);
	num = unique(tab, tab+N+M) - tab;
	for(int i = 0; i < num; i++)
		Mp[tab[i]] = i;
	
	for(int i = 0; i < N; i++) itemY[i] = Mp[itemY[i]];
	for(int i = 0; i < M; i++) guardY[i] = Mp[guardY[i]];
	
	Mp.clear();
	
	//for(int i = 0; i < N; i++) printf("%d (%lld %lld)\n", i, itemX[i], itemY[i]);
	//for(int i = 0; i < M; i++) printf("%d  (%lld %lld)\n", i, guardX[i], guardY[i]);
	
	
	// zrobic akcje
	for(int i = 0; i < N; i++) A[i] = Action(itemX[i], itemY[i], itemC[i], false);
	for(int i = 0; i < M; i++) A[i+N]=Action(guardX[i],guardY[i],guardC[i],true);
	sort(A, A+N+M);
}


/******************** DRZEWO PRZEDZIALOWE (+,MAX) *********************/
struct SegTree {
	static const int MaxSize = (1<<20);
	int Base;
	
	// A - dodawanie na przedziale bazowym; B - maksimum
	LL A[MaxSize], B[MaxSize];
	
	void set_size(int S){
		Base = 1;
		while(Base < S) Base *= 2;
	}
	
	
	void add(int L, int R, LL v){
		L += Base; R += Base;
		A[L] += v; B[L] += v;
		if(L != R){ A[R] += v; B[R] += v; }
		
		while(L/2 != R/2){
			if(L % 2 == 0){ A[L+1] += v; B[L+1] += v; }
			if(R % 2 == 1){ A[R-1] += v; B[R-1] += v; }
			L /= 2; R /= 2;
			B[L] = max(B[2*L], B[2*L+1]) + A[L];
			B[R] = max(B[2*R], B[2*R+1]) + A[R];
		}
		L /= 2;
		while(L){
			B[L] = max(B[2*L], B[2*L+1]) + A[L];
			L /= 2;
		}
	}
	
	LL get_max(int L, int R){
		LL res=0, addLeft=0, addRight=0;
		L += Base; R += Base;
		
		addLeft = B[L]; addRight = B[R];
		while(L/2 != R/2){
			if(L % 2 == 0) addLeft = max(addLeft, B[L+1]);
			if(R % 2 == 1) addRight = max(addRight, B[R-1]);
			L /= 2; R /= 2;
			addLeft += A[L];
			addRight += A[R];
		}
		res = max(addLeft, addRight);
		L /= 2;
		while(L){
			res += A[L];
			L /= 2;
		}
		return res;
	}
};

/**********************************************************************/

SegTree ST;


LL sweep(){
	ST.set_size(N+M+5);
	//LL res = 0;
	
	//int lastGuardY = -1;
	
	for(int act = 0; act < N+M; act++){
		int x = A[act].x, y = A[act].y, val = A[act].money;
		bool guard = A[act].isGuard;
		//printf("action: x=%d, y=%d, guard=%d, val=%d\n", x,y,guard,val);
		
		if(guard){
			ST.add(x, N+M, -val);
			//lastGuardY = y;
		} else {
			LL best = max(ST.get_max(0, x), 0LL),
			   prevVal = ST.get_max(x, x);
			
			//if(lastGuardY != y) best = max(best, 0LL);
			//if(prevVal > 0) prevVal = 0;
			
			ST.add(x, x, best+val-prevVal);
			ST.add(x+1, N+M, val);
		}
		
		//for(int i = 0; i < N+M; i++) printf("%lld ", ST.get_max(i,i));
		//printf("\n");
	}
	return max(ST.get_max(0, N+M), 0LL);
}

int main(){
	input();
	process_input();
	LL res = sweep();
	printf("%lld\n", res);
}