Problem p03099 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ref(i, x, y) for (int i = x; i <= y; ++i)
#define def(i, x, y) for (int i = x; i >= y; --i)
#define fi first
#define se second
#define int long long
#define pb push_back
typedef long long LL;
const int N = 221;
int n, m;
int lx[2][N], ly[2][N];
struct dot {
  int x, y, v, id;
} p[N], px[N], py[N];
bool cmpx(dot a, dot b) { return a.x < b.x; }
bool cmpy(dot a, dot b) { return a.y < b.y; }
void smin(int &a, int b) {
  if (b < a)
    a = b;
}
void smax(int &a, int b) {
  if (b > a)
    a = b;
}
namespace eden {
int w[N][N], c[N][N], op, ed;
void init() {
  memset(w, 0, sizeof w);
  memset(c, 0, sizeof c);
}
void add(int x, int y, int ww, int cc) {
  w[x][y] = ww;
  w[y][x] = 0;
  c[x][y] = cc;
  c[y][x] = -cc;
}
queue<int> q;
bool vis[N];
int dis[N], pre[N];
bool spfa() {
  memset(vis, 0, sizeof vis);
  memset(pre, 0, sizeof pre);
  while (!q.empty())
    q.pop();
  ref(i, 1, ed) dis[i] = (LL)1e18;
  dis[op] = 0;
  q.push(op);
  vis[op] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    ref(v, 1, ed) if (w[u][v] && dis[u] + c[u][v] < dis[v]) {
      dis[v] = dis[u] + c[u][v];
      pre[v] = u;
      if (!vis[v])
        vis[v] = 1, q.push(v);
    }
  }
  return (dis[ed] < (LL)1e18);
}
pair<int, int> run() {
  int fw = 0, res = 0;
  while (spfa()) {
    fw++;
    res += dis[ed];
    for (int i = ed; i != op; i = pre[i])
      w[pre[i]][i]--, w[i][pre[i]]++;
  }
  return make_pair(fw, res);
}
} // namespace eden
signed main() {
  cin >> n;
  ref(i, 1, n) cin >> p[i].x >> p[i].y >> p[i].v;
  ref(i, 1, n) p[i].id = i;
  ref(i, 1, n) px[i] = p[i];
  ref(i, 1, n) py[i] = p[i];
  sort(px + 1, px + n + 1, cmpx);
  sort(py + 1, py + n + 1, cmpy);
  cin >> m;
  ref(i, 1, n) lx[0][i] = ly[0][i] = n + 1;
  ref(i, 1, n) lx[1][i] = ly[1][i] = 0;
  ref(i, 1, m) {
    char c;
    int a, b;
    cin >> c >> a >> b;
    if (c == 'L') {
      int pos = 0;
      ref(j, 1, n) if (px[j].x <= a) pos = j;
      if (pos > b)
        smin(lx[0][pos - b], pos);
    }
    if (c == 'R') {
      int pos = n + 1;
      def(j, n, 1) if (px[j].x >= a) pos = j;
      if (n + 1 - pos > b)
        smax(lx[1][n + 1 - pos - b], pos);
    }
    if (c == 'D') {
      int pos = 0;
      ref(j, 1, n) if (py[j].y <= a) pos = j;
      if (pos > b)
        smin(ly[0][pos - b], pos);
    }
    if (c == 'U') {
      int pos = n + 1;
      def(j, n, 1) if (py[j].y >= a) pos = j;
      if (n + 1 - pos > b)
        smax(ly[1][n + 1 - pos - b], pos);
    }
  }
  def(i, n - 1, 1) smin(lx[0][i], lx[0][i + 1]);
  def(i, n - 1, 1) smin(ly[0][i], ly[0][i + 1]);
  def(i, n - 1, 1) smax(lx[1][i], lx[1][i + 1]);
  def(i, n - 1, 1) smax(ly[1][i], ly[1][i + 1]);
  int ans = 0, sum = 0;
  ref(i, 1, n) ans += p[i].v, sum += p[i].v;
  ref(S, 0, n) {
    if (S < n) {
      if (lx[0][S + 1] <= n || ly[0][S + 1] <= n)
        continue;
      if (lx[1][S + 1] >= 1 || ly[1][S + 1] >= 1)
        continue;
    }
    bool fg = 1;
    ref(i, 1, S) if (lx[1][S + 1 - i] > lx[0][i]) {
      fg = 0;
      break;
    }
    ref(i, 1, S) if (ly[1][S + 1 - i] > ly[0][i]) {
      fg = 0;
      break;
    }
    if (!fg)
      continue;
    eden::init();
    eden::op = n + n + n + n + 1;
    eden::ed = eden::op + 1;
    ref(i, 1, S) ref(j, max(1LL, lx[1][S + 1 - i]), min(n, lx[0][i]))
        eden::add(i, n + px[j].id, 1, 0);
    ref(i, 1, n) eden::add(n + i, n + n + i, 1, p[i].v);
    ref(i, 1, S) ref(j, max(1LL, ly[1][S + 1 - i]), min(n, ly[0][i]))
        eden::add(n + n + py[j].id, n + n + n + i, 1, 0);
    ref(i, 1, S) eden::add(eden::op, i, 1, 0);
    ref(i, 1, S) eden::add(n + n + n + i, eden::ed, 1, 0);
    pair<int, int> pp = eden::run();
    if (pp.fi == S)
      ans = min(ans, pp.se);
  }
  cout << sum - ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ref(i, x, y) for (int i = x; i <= y; ++i)
#define def(i, x, y) for (int i = x; i >= y; --i)
#define fi first
#define se second
#define int long long
#define pb push_back
typedef long long LL;
const int N = 421;
int n, m;
int lx[2][N], ly[2][N];
struct dot {
  int x, y, v, id;
} p[N], px[N], py[N];
bool cmpx(dot a, dot b) { return a.x < b.x; }
bool cmpy(dot a, dot b) { return a.y < b.y; }
void smin(int &a, int b) {
  if (b < a)
    a = b;
}
void smax(int &a, int b) {
  if (b > a)
    a = b;
}
namespace eden {
int w[N][N], c[N][N], op, ed;
void init() {
  memset(w, 0, sizeof w);
  memset(c, 0, sizeof c);
}
void add(int x, int y, int ww, int cc) {
  w[x][y] = ww;
  w[y][x] = 0;
  c[x][y] = cc;
  c[y][x] = -cc;
}
queue<int> q;
bool vis[N];
int dis[N], pre[N];
bool spfa() {
  memset(vis, 0, sizeof vis);
  memset(pre, 0, sizeof pre);
  while (!q.empty())
    q.pop();
  ref(i, 1, ed) dis[i] = (LL)1e18;
  dis[op] = 0;
  q.push(op);
  vis[op] = 1;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    ref(v, 1, ed) if (w[u][v] && dis[u] + c[u][v] < dis[v]) {
      dis[v] = dis[u] + c[u][v];
      pre[v] = u;
      if (!vis[v])
        vis[v] = 1, q.push(v);
    }
  }
  return (dis[ed] < (LL)1e18);
}
pair<int, int> run() {
  int fw = 0, res = 0;
  while (spfa()) {
    fw++;
    res += dis[ed];
    for (int i = ed; i != op; i = pre[i])
      w[pre[i]][i]--, w[i][pre[i]]++;
  }
  return make_pair(fw, res);
}
} // namespace eden
signed main() {
  cin >> n;
  ref(i, 1, n) cin >> p[i].x >> p[i].y >> p[i].v;
  ref(i, 1, n) p[i].id = i;
  ref(i, 1, n) px[i] = p[i];
  ref(i, 1, n) py[i] = p[i];
  sort(px + 1, px + n + 1, cmpx);
  sort(py + 1, py + n + 1, cmpy);
  cin >> m;
  ref(i, 1, n) lx[0][i] = ly[0][i] = n + 1;
  ref(i, 1, n) lx[1][i] = ly[1][i] = 0;
  ref(i, 1, m) {
    char c;
    int a, b;
    cin >> c >> a >> b;
    if (c == 'L') {
      int pos = 0;
      ref(j, 1, n) if (px[j].x <= a) pos = j;
      if (pos > b)
        smin(lx[0][pos - b], pos);
    }
    if (c == 'R') {
      int pos = n + 1;
      def(j, n, 1) if (px[j].x >= a) pos = j;
      if (n + 1 - pos > b)
        smax(lx[1][n + 1 - pos - b], pos);
    }
    if (c == 'D') {
      int pos = 0;
      ref(j, 1, n) if (py[j].y <= a) pos = j;
      if (pos > b)
        smin(ly[0][pos - b], pos);
    }
    if (c == 'U') {
      int pos = n + 1;
      def(j, n, 1) if (py[j].y >= a) pos = j;
      if (n + 1 - pos > b)
        smax(ly[1][n + 1 - pos - b], pos);
    }
  }
  def(i, n - 1, 1) smin(lx[0][i], lx[0][i + 1]);
  def(i, n - 1, 1) smin(ly[0][i], ly[0][i + 1]);
  def(i, n - 1, 1) smax(lx[1][i], lx[1][i + 1]);
  def(i, n - 1, 1) smax(ly[1][i], ly[1][i + 1]);
  int ans = 0, sum = 0;
  ref(i, 1, n) ans += p[i].v, sum += p[i].v;
  ref(S, 0, n) {
    if (S < n) {
      if (lx[0][S + 1] <= n || ly[0][S + 1] <= n)
        continue;
      if (lx[1][S + 1] >= 1 || ly[1][S + 1] >= 1)
        continue;
    }
    bool fg = 1;
    ref(i, 1, S) if (lx[1][S + 1 - i] > lx[0][i]) {
      fg = 0;
      break;
    }
    ref(i, 1, S) if (ly[1][S + 1 - i] > ly[0][i]) {
      fg = 0;
      break;
    }
    if (!fg)
      continue;
    eden::init();
    eden::op = n + n + n + n + 1;
    eden::ed = eden::op + 1;
    ref(i, 1, S) ref(j, max(1LL, lx[1][S + 1 - i]), min(n, lx[0][i]))
        eden::add(i, n + px[j].id, 1, 0);
    ref(i, 1, n) eden::add(n + i, n + n + i, 1, p[i].v);
    ref(i, 1, S) ref(j, max(1LL, ly[1][S + 1 - i]), min(n, ly[0][i]))
        eden::add(n + n + py[j].id, n + n + n + i, 1, 0);
    ref(i, 1, S) eden::add(eden::op, i, 1, 0);
    ref(i, 1, S) eden::add(n + n + n + i, eden::ed, 1, 0);
    pair<int, int> pp = eden::run();
    if (pp.fi == S)
      ans = min(ans, pp.se);
  }
  cout << sum - ans << endl;
}