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
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

void Transform(const int w, const int h, long long *x, long long *y) {
  *x *= h;
  *y *= w;
  long long x_new = *y + *x;
  long long y_new = *y - *x;
  *x = 2 * x_new;
  *y = 2 * y_new;
}

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

  vector<pair<pair<long long, long long>, int> > vec;
  while (n--) {
    long long x, y, v;
    scanf("%lld%lld%lld", &x, &y, &v);
    Transform(w, h, &x, &y);
    --x;
    --y;
    vec.push_back(make_pair(make_pair(x, y), v));
    // cerr << x << ' ' << y << ' ' << v << endl;
  }
  while (m--) {
    long long x, y, v;
    scanf("%lld%lld%lld", &x, &y, &v);
    Transform(w, h, &x, &y);
    vec.push_back(make_pair(make_pair(x, y), -v));
    // cerr << x << ' ' << y << ' ' << -v << endl;
  }
  sort(vec.rbegin(), vec.rend());

  typedef map<pair<long long, long long>, int> Map;
  Map mm;
  long long ret = 0;
  for (int i = 0; i < vec.size(); ++i) {
     const pair<long long, long long> p = make_pair(vec[i].first.second, vec[i].first.first);
     int v = vec[i].second;
     if (v < 0) {
       mm[p] = -v;
     } else {
       Map::iterator it = mm.upper_bound(p);
       while (v > 0) {
         if (it == mm.end()) {
           ret += v;
           break;
         }
         const int amount = min(v, it->second);
         v -= amount;
         it->second -= amount;
         if (it->second == 0) mm.erase(it++);
       }
     }
  }
  printf("%lld\n", ret);

  return 0;
}