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
#include <cstdio>
#include <algorithm>
#include <deque>
#include <vector>
//#define NDEBUG
#include <cassert>
using namespace std;
#define FOR(i, n) for(int i = 0, __n = (n); i<__n; i++)
#define LOG1(args...) 
//fprintf(stderr, args)
#define LOG(args...)
// fprintf(stderr, args)
typedef pair<long long,long long> para;
#define MAXN 410000

long long X, Y;

struct punkt : public para {
	long long value;
	punkt(long long x, long long y, long long v):value(v), para( x*Y + X*y,x*Y - X*y){}
	punkt(){}
	
		
	bool operator<(const punkt & p) const {
		if(first == p.first) return second > p.second;
		return first < p.first;
	}
} t[MAXN];

const long long bound = 4LL*1000000000LL*1000000000LL;
//const long long bound = 40;
const long long INF = 9223372036854775807LL;
struct node;

node* newNode(long long a, long long b);

struct node {
	long long a, b;
	long long mx,sx;
	node* L[2];
	void init() {
		//LOG("MADE NODE %lld %lld\n", a, b);
		L[0] = L[1] = 0;
	}
	node(long long a, long long b):a(a),b(b), sx(0), mx(-INF){init();}
	
	
	node(){}
	void initChildren() {
		if(L[0] || b == a+1) return;
		long long m = (a+b)/2;
		L[0] = newNode(a,m);
		L[1] = newNode(m,b);
	}
	
	
	long long maxx(long long a1, long long b1) const {
		
		LOG("MMM (%lld,%lld) node %lld,%lld -> mx:%lld, sx:%lld\n", a1,b1, a,b,mx,sx);
		if(b1<=a || a1>=b) return 0;
		
		
		if(a1<=a && b1>=b) return mx;
		if(!L[0]) return 0;
		else {
			assert(L[0]); assert(L[1]);
			long long r =  max(L[0]->maxx(a1,b1), L[1]->maxx(a1,b1));
			if(r != -INF) r+=sx;
			return r;
		}
	}
	
	long long inc(long long a1, long long b1, long long v) {
		LOG("INC:%lld (%lld,%lld) node %lld,%lld -> mx:%lld, sx:%lld\n",v,a1,b1, a,b,mx,sx);
		if(b1<=a || a1>=b) return mx;
		
		if(a1<=a && b1>=b) {
			sx+=v; 
			if(mx!=-INF)mx+=v;
		} else {
			initChildren();
			assert(L[0]); assert(L[1]);
			mx = max(L[0]->inc(a1,b1,v),L[1]->inc(a1,b1,v));
			if(mx != -INF) mx+=sx;
		}
		return mx;
	}
	
	long long set(long long a1, long long v) {
		LOG("set:%lld (%lld,%lld) node %lld,%lld -> mx:%lld, sx:%lld\n",v,a1,a1+1, a,b,mx,sx);
		if(a1<a || a1 >= b) return mx;
		if(a1==a && a1+1==b) {
			if(mx < v) {
				mx = v;
				sx = v;
			}
		} else {
			initChildren();
			assert(L[0]); assert(L[1]);
			mx = std::max(L[0]->set(a1,v-sx),L[1]->set(a1,v-sx));
			if(mx != -INF) mx+=sx;
		}
		return mx;
	}
};


#define POOLSIZE 100000
node* nodePool = new node[POOLSIZE];
int nodePoolCount = 0;
node* newNode(long long a, long long b){
	if(nodePoolCount == POOLSIZE) {
		nodePoolCount = 0;
		nodePool = new node[POOLSIZE];
	}
	nodePool[nodePoolCount] = node(a,b);
	return nodePool + (nodePoolCount++);
}


struct tree {
	node * root;	
	void init() {
		root = newNode(-bound, bound);
	}
	tree() {
		init();
	}
	
	
	long long max(long long a, long long b) {
		return root->maxx(a,b);
	}
	
	void inc(long long a, long long b, long long v) {
		root->inc(a,b,v);
	}
	
	void set(long long a, long long v) {
		root->set(a,v);
	}
	
	
};



int main() {
	LOG1("bound: %lld, inf: %lld\n", bound, INF);
	int n,m; 
	scanf("%d%d", &n, &m); 
	scanf("%lld%lld", &X, &Y);
	
	FOR(i, n) {
		int x, y, v; scanf("%d%d%d", &x, &y, &v);
		t[i] = punkt(x, -y, v);
	}
	FOR(i, m) {
		int x, y, v; scanf("%d%d%d", &x, &y, &v);
		t[i+n] = punkt(x, -y, -v);
	}
	
	n = n+m;
	
	sort(t,t+n);
	reverse(t,t+n);
	
	
	tree T;
	
	
	
	FOR(i,n) {
		LOG1("%lld, %lld, %lld\n", t[i].first, t[i].second, t[i].value);
		long long a = t[i].first, b = t[i].second, v = t[i].value;
		long long best = T.max(b, bound);
		if(best < 0) best = 0;
		LOG("best %lld\n", best);
		T.set(b, best);
		
		T.inc(-bound, b+1, v);
		
		
	}
	
	printf("%lld\n", T.max(-bound,bound));
	
	return 0;
}