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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <string>

#define FOR(i,a,b)  for(int i=(a);i<(b);++i)
#define FORD(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,a)    FOR(i,0,a)
#define MP          make_pair
#define PB          push_back
#define ST          first
#define ND          second

using namespace std;

typedef vector<int>             VI;
typedef vector<VI>              VVI;
typedef pair<int, int>          PII;
typedef vector<PII>             VII;
typedef long long int           LL;
typedef unsigned long long int  ULL;

const int MAXN = 400005;
const int MAXT = (1<<20);

int n, m;
LL w, h;
LL minCut, posSum;

struct Point {
    LL x, y;
    int cost;
};

bool cmp_x_first(const Point & p1, const Point & p2) {
    if (p1.x == p2.x) {
        return p1.y < p2.y;
    }
    else {
        return p1.x < p2.x;
    }
}

bool cmp_y_first(const Point & p1, const Point & p2) {
    if (p1.y == p2.y) {
        return p1.x < p2.x;
    }
    else {
        return p1.y < p2.y;
    }
}

Point points[MAXN];
int maxX;


void scale() {
    LL prevX = points[0].x-1;
    REP (i, n) {
        if (points[i].x != prevX) {
            ++maxX;
            prevX = points[i].x;
        }
        points[i].x = maxX;
    }
}

int tree_size;
LL tree[MAXT];

void initTree() {
    tree_size = 1;
    while (tree_size <= maxX) {
        tree_size *= 2;
    }
}

LL query(int va) {
    va = va + tree_size;
    LL res = tree[va];
    va >>= 1;

    while (va != 0) {
        res += tree[va];
        va >>= 1;
    }
    return res;
}

// [va, vb]
void insert(int va, int vb, LL val) {
    va = va + tree_size;
    vb = vb + tree_size;
    tree[va] += val;
    if (va != vb) tree[vb] += val;

    while (va / 2 != vb / 2) {
        if ((va & 1) == 0) tree[va+1] += val;
        if ((vb & 1) == 1) tree[vb-1] += val;
        va >>= 1;
        vb >>= 1;
    }
}

struct Steps {
    inline void addStep(int x) {
        steps.insert(x);
    }

    inline void removeStep(int x) {
        steps.erase(x);
    }

    inline set<int>::const_iterator getNextStep(int x) {
        return steps.upper_bound(x);
    }

    inline set<int>::const_iterator removeStep(set<int>::const_iterator it) {
        return steps.erase(it);
    }

    set<int> steps;
};

Steps st;

int binSearch(LL val, int maxIdx) {
    int left, right, mid;
    LL q;

    left = -1;
    right = maxIdx;
    while (right - left > 1) {
        mid = (right + left) >> 1;
        q = query(mid);
        if (q <= val) {
            left = mid;
        }
        else {
            right = mid;
        }
    }
    return right;
}

void joinSteps(int idxMin, int idxMax, LL val) {
    LL prevVal;
    auto nextIdx = st.getNextStep(idxMin);
    while (nextIdx != st.steps.end() && (*nextIdx) <= idxMax) {
        prevVal = query(idxMin);
        insert(idxMin, (*nextIdx) - 1, val - prevVal);
        idxMin = *nextIdx;
        nextIdx = st.removeStep(nextIdx);
    }
}

void sweep() {
    REP (i, n) {
        if (points[i].cost > 0) { //left
            LL v1, v2;
            v1 = query(points[i].x-1);
            v2 = query(points[i].x);
            if (v1 + points[i].cost > v2) {
                int idx;
                if (v2 >= points[i].cost) {
                    idx = binSearch(v2 - points[i].cost, points[i].x - 1);
                }
                else {
                    idx = 0;
                }

                if (idx > 0) {
                    insert(0, idx-1, points[i].cost);
                    if (query(idx-1) == v2) {
                        st.removeStep(idx);
                    }
                }
                joinSteps(idx, points[i].x, v2);
            }
            else {
                insert(0, points[i].x - 1, points[i].cost);
                if (v1 + points[i].cost == v2) {
                    st.removeStep(points[i].x);
                }
            }
        }
        else { // right
            st.addStep(points[i].x);
            insert(points[i].x, maxX, -points[i].cost);
        }
    }
    minCut = query(0);
}

int main() {
    scanf("%d %d", &n, &m);
    scanf("%lld %lld", &w, &h);

    LL x, y;
    REP (i, n) {
        scanf("%lld %lld %d", &x, &y, &points[i].cost);
        points[i].x = - x * h - y * w;
        points[i].y = x * h - y * w;
        posSum += points[i].cost;
    }
    FOR (i, n, n+m) {
        scanf("%lld %lld %d", &x, &y, &points[i].cost);
        points[i].x = - x * h - y * w;
        points[i].y = x * h - y * w;
        points[i].cost = -points[i].cost;
    }

    n = n+m;
    sort(points, points + n, cmp_x_first);
    scale();
    sort(points, points + n, cmp_y_first);
    initTree();
    sweep();

    printf("%lld\n", posSum - minCut);

    return 0;
}