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
#include <vector>
#include <iostream>
#include <sstream>
#include <math.h>
#include <sys/time.h>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <fstream>
#include <set>

#define FOR(i,a,b)  for(__typeof(b) i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define FOREACH(x,c)   for(__typeof(c.begin()) x=c.begin();x != c.end(); x++)
#define ALL(c)      c.begin(),c.end()
#define CLEAR(c)    memset(c,0,sizeof(c))
#define SIZE(c) (int) ((c).size())

#define PB          push_back
#define MP          make_pair

#define ULL         unsigned long long
#define LL          long long int
#define LD          long double
#define II         pair<int, int>
#define DD         pair<double, double>

#define VC	    vector
#define VI          VC<int>
#define VVI         VC<VI>
#define VD          VC<double>
#define VS          VC<string>
#define VII         VC<II>
#define VDD         VC<DD>
#define LLL     tuple<LL,LL,LL>
#define VLLL    VC<LLL>
#define VLL     VC<LL>

#define DB(a)       cerr << #a << ": " << a << endl;

using namespace std;

template<class T> void print(VC < T > v) {cerr << "[";if (SIZE(v) != 0) cerr << v[0]; FOR(i, 1, SIZE(v)) cerr << "," << v[i]; cerr << "]\n";}
template<class T> string i2s(T &x) { ostringstream o; o << x; return o.str(); }
VS split(string &s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < SIZE(s)) all.PB(s.substr(p)); return all;}

#define X(t) get<0>(t)
#define Y(t) get<1>(t)
#define V(t) get<2>(t)
#define MT(x,y,v) make_tuple(x,y,v)

int n, m;
VLLL events;

void transform(VLLL &v, int w, int h){
    for(auto &e : v){
        LL x = h * X(e) + w * Y(e);
        LL y = -h * X(e) + w * Y(e);
        X(e) = x;
        Y(e) = y;
    }
}

void readData(){
    scanf("%d%d",&n,&m);
    int w,h;
    scanf("%d%d",&w,&h);
    events.reserve(n+m);
    LL x, y, val;
    REP(i,n){
        scanf("%lld%lld%lld",&x,&y,&val);
        events.PB(MT(x,y,val));
    }
    REP(i,m){
        scanf("%lld%lld%lld",&x,&y,&val);
        events.PB(MT(x,y,-val));
    }

    transform(events,w,h);
}

#define INFTY 5000000000000000000LL

VLL ind;

void makeIndex(){
    ind.resize(n);
    REP(i,n) ind[i] = Y(events[i]);
    sort(ALL(ind));
    auto it = unique(ALL(ind));
    ind.resize(it-ind.begin());
    ind.PB(INFTY);
}

class IntervalTree{
public:
    VLL T;
    int M;

    IntervalTree(int n){
        int pow = 32-__builtin_clz(n);
        M = 1 << pow; // 0 is empty
        T.resize(2*M,0LL);
    }

    void clear(){ T = VLL(2*M,0LL);}
    
    void clean(int u){
        if (u < M && T[u] == 0){
            T[2*u] = 0;
            T[2*u+1] = 0;
        }
    }

    void cleanPath(int u){
        int v = 1;
        int right = M;
        while (v < M){
            clean(v);
            if (u < right/2){
                v = 2*v;
            } else{
                u -= right/2;
                v = 2*v+1;
            }
            right /= 2;
        }
    }

    void IncElement(int u, LL val){
        cleanPath(u);
        int v = u+M;
        T[v] += val;
        do{
            v = v/2; 
            T[v] += val;
        } while (v != 1);
    }

    void decNode(int u, LL val){
        while(1){
            if (u >= M){
                T[u] -= val;
                return;
            }
            T[u] -= val;
            if (T[2*u+1] < val){
                val -= T[2*u+1];
                T[2*u+1] = 0;
                u = 2*u;
            } else
                u = 2*u+1;
        }
    }
    
    void decPrefix(int u, LL val){
        cleanPath(u);
        int v = M+u;
        int delta = min(val,T[v]);
        T[v] -= delta;
        val -= delta;
        while(v != 1){
            if (val && (v&1)){ // right son
                if (T[v-1] <= val){
                    val -= T[v-1];
                    T[v-1] = 0;
                } else{
                    decNode(v-1,val);
                    val = 0;
                }
            }
            v /= 2;
            T[v] = T[2*v]+T[2*v+1];
        }
    }
};

int earlier(const LLL &p, const LLL &q){
    if (X(p) != X(q)) return (X(p) < X(q));
    if (Y(p) != Y(q)) return (Y(p) < Y(q));
    return (V(p) > V(q));
}

LL scan(){
    makeIndex();
    sort(ALL(events),earlier); 
    IntervalTree it(n);
    for(const auto &e : events){
        int pos = upper_bound(ALL(ind),Y(e))-ind.begin();
        if (V(e) > 0)
            it.IncElement(pos-1,V(e));
        else if (pos > 0)
            it.decPrefix(pos-1,-V(e));
    }
    return it.T[1];
}

void solve(){
    LL res = scan();
    printf("%lld\n",res);
}

int main(int argc, char *argv[]){
    readData();
    solve();
    return 0;
}