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
#include <bits/stdc++.h>

#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define st first
#define nd second

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

struct point{
	LL x, y;
	int v;	
};

inline bool operator<(const point &a, const point &b){
	if(a.x == b.x && a.y == b.y) return a.v > b.v;
	if(a.x == b.x) return a.y > b.y;
	return a.x < b.x;
}

int n, m, k, d;
LL w, h;
vector<point> points;
vector<LL> ys;
multiset<pair<LL, int> > S;

void add_point(int x, int y, int v){
	point p;
	p.x = -x*h+y*w;
	p.y = -x*h-y*w;
	p.v = v;
	points.push_back(p);
	ys.push_back(p.y);
}

int main(){
	scanf("%d %d", &n, &m);
	scanf("%lld %lld", &w, &h);
	FWD(i,0,n){
		int x, y, v;
		scanf("%d %d %d", &x, &y, &v);
		add_point(x,y,v);
	}
	FWD(i,0,m){
		int x, y, v;
		scanf("%d %d %d", &x, &y, &v);
		add_point(x,y,-v);
	}
	sort(points.begin(), points.end());
	for(point p : points){
		if(p.v > 0){
			S.insert(pair<LL, int>(p.y, p.v));
		}else{
			multiset<pair<LL, int>>::iterator it = S.lower_bound(pair<LL, int>(p.y, -1)), itt;
			while(p.v && it != S.end()){
				pair<LL, int> q = *it;
				itt = it;
				++it;
				S.erase(itt);
				q.nd += p.v;
				if(q.nd > 0){
					S.insert(q);
					break;
				}else
					p.v = q.nd;
			}
		}
	}
	LL res = 0;
	for(PII p : S)
		res += p.nd;
	printf("%lld\n", res);
	return 0;
}