Problem p03099 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int maxn = 200 + 5;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const ll mod = 998244353;
const double eps = 1e-7;
template <class T = int> T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}

#define int ll
#define inf linf

namespace mcmf {
const int maxm = 10 * maxn;
int n, s, t;
struct node {
  int nex, to, val, cost;
} edge[maxm];
int head[maxn], tot = 1;
void add_path(int u, int v, int w, int c) {
  edge[++tot] = (node){head[u], v, w, c};
  head[u] = tot;
}
void add_flow(int u, int v, int w, int c) {
  add_path(u, v, w, c);
  add_path(v, u, 0, -c);
}
int dist[maxn], pre[maxn], lim[maxn], vis[maxn];
bool spfa() {
  rep(i, 1, n) dist[i] = inf;
  qi q;
  q.push(s), dist[s] = 0, lim[s] = inf, lim[t] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    vis[now] = 0;
    cont(i, now) {
      int to = edge[i].to, val = edge[i].val, cost = edge[i].cost;
      if (!val || dist[to] <= dist[now] + cost)
        continue;
      dist[to] = dist[now] + cost, lim[to] = min(lim[now], val),
      pre[to] = i ^ 1;
      if (!vis[to])
        q.push(to), vis[to] = 1;
    }
  }
  return lim[t];
}
int maxflow, mincost;
void updata() {
  maxflow += lim[t];
  for (int i = t; i != s; i = edge[pre[i]].to) {
    edge[pre[i]].val += lim[t], edge[pre[i] ^ 1].val -= lim[t];
    mincost += lim[t] * edge[pre[i] ^ 1].cost;
  }
}
void clear() {
  rep(i, 1, n) head[i] = 0;
  tot = 1;
  maxflow = mincost = 0;
}
} // namespace mcmf

int n, x[maxn], y[maxn], w[maxn], ans;

int m, t[maxn], a[maxn], b[maxn], pos[maxn];

int minn[maxn], maxx[maxn];

void solve(int sum) {
  // puts("------------solve-------------");
  // printf("sum : %lld\n", sum);
  mcmf ::n = 2 * (sum + n + 1);
  rep(i, 1, sum << 1) maxx[i] = inf, minn[i] = 0;
  rep(i, 1, m) {
    if (b[i] >= sum)
      continue;
    if (t[i] == 1)
      minn[b[i] + 1] = max(minn[b[i] + 1], a[i] + 1);
    if (t[i] == 2)
      maxx[sum - b[i]] = min(maxx[sum - b[i]], a[i] - 1);
    if (t[i] == 3)
      minn[b[i] + 1 + sum] = max(minn[b[i] + 1 + sum], a[i] + 1);
    if (t[i] == 4)
      maxx[sum - b[i] + sum] = min(maxx[sum - b[i] + sum], a[i] - 1);
  }
  rep(i, 2, sum) minn[i] = max(minn[i], minn[i - 1]);
  per(i, sum - 1, 1) maxx[i] = min(maxx[i], maxx[i + 1]);
  rep(i, sum + 2, sum + sum) minn[i] = max(minn[i], minn[i - 1]);
  per(i, sum + sum - 1, sum + 1) maxx[i] = min(maxx[i], maxx[i + 1]);
  // rep(i, 1, sum)
  // {
  // 	printf("i : %lld\n", i);
  // 	puts("x : ");
  // 	printf("%lld %lld\n", minn[i], maxx[i]);
  // 	puts("y : ");
  // 	printf("%lld %lld\n", minn[sum + i], maxx[sum + i]);
  // }
  // puts("add_limit success");
  mcmf ::s = mcmf ::n, mcmf ::t = mcmf ::n - 1;
  rep(i, 1, sum) mcmf ::add_flow(mcmf ::s, i, 1, 0),
      mcmf ::add_flow(sum + n + n + i, mcmf ::t, 1, 0);
  rep(i, 1, n) {
    mcmf ::add_flow(sum + i, sum + n + i, 1, -w[i]);
    pos[i] = mcmf ::tot;
    rep(j, 1, sum) {
      if (x[i] >= minn[j] && x[i] <= maxx[j])
        mcmf ::add_flow(j, sum + i, 1, 0);
      if (y[i] >= minn[j + sum] && y[i] <= maxx[j + sum])
        mcmf ::add_flow(sum + n + i, sum + n + n + j, 1, 0);
    }
  }
  // puts("build_graph success");
  while (mcmf ::spfa())
    mcmf ::updata();
  ans = min(ans, mcmf ::mincost);
  // printf("mincost : %lld\n", mcmf :: mincost);
  // puts("choose : ");
  // rep(i, 1, n) if(mcmf :: edge[pos[i]].val == 1) printf("%lld ", i);
  // enter;
  mcmf ::clear();
  return;
}

signed main() {
  scanf("%lld", &n);
  rep(i, 1, n) scanf("%lld %lld %lld", &x[i], &y[i], &w[i]);
  scanf("%lld", &m);
  rep(i, 1, m) {
    char tmp;
    scanf("%s", &tmp);
    if (tmp == 'L')
      t[i] = 1;
    if (tmp == 'R')
      t[i] = 2;
    if (tmp == 'D')
      t[i] = 3;
    if (tmp == 'U')
      t[i] = 4;
    scanf("%lld %lld", &a[i], &b[i]);
  }
  rep(i, 1, n) solve(i);
  printf("%lld\n", -ans);
  return 0;
}

/*

7
1 3 6
1 5 9
3 1 8
4 3 8
6 2 9
5 4 11
5 7 10
4
L 3 1
R 2 3
D 5 3
U 4 2


*/

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define rep(i, a, b) for (register int i = (a); i <= (b); ++i)
#define per(i, a, b) for (register int i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (register int i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast<t>(x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((int)x.size())
#define file(x) freopen(x ".in", "r", stdin), freopen(x ".out", "w", stdout)
typedef double db;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef queue<int> qi;
typedef queue<pii> qii;
typedef set<int> si;
typedef map<int, int> mii;
typedef map<string, int> msi;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const int iinf = 1 << 30;
const ll linf = 2e18;
const ll mod = 998244353;
const double eps = 1e-7;
template <class T = int> T read() {
  T f = 1, a = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    a = (a << 3) + (a << 1) + ch - '0';
    ch = getchar();
  }
  return a * f;
}

#define int ll
#define inf linf

namespace mcmf {
const int maxm = 10 * maxn;
int n, s, t;
struct node {
  int nex, to, val, cost;
} edge[maxm];
int head[maxn], tot = 1;
void add_path(int u, int v, int w, int c) {
  edge[++tot] = (node){head[u], v, w, c};
  head[u] = tot;
}
void add_flow(int u, int v, int w, int c) {
  add_path(u, v, w, c);
  add_path(v, u, 0, -c);
}
int dist[maxn], pre[maxn], lim[maxn], vis[maxn];
bool spfa() {
  rep(i, 1, n) dist[i] = inf;
  qi q;
  q.push(s), dist[s] = 0, lim[s] = inf, lim[t] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    vis[now] = 0;
    cont(i, now) {
      int to = edge[i].to, val = edge[i].val, cost = edge[i].cost;
      if (!val || dist[to] <= dist[now] + cost)
        continue;
      dist[to] = dist[now] + cost, lim[to] = min(lim[now], val),
      pre[to] = i ^ 1;
      if (!vis[to])
        q.push(to), vis[to] = 1;
    }
  }
  return lim[t];
}
int maxflow, mincost;
void updata() {
  maxflow += lim[t];
  for (int i = t; i != s; i = edge[pre[i]].to) {
    edge[pre[i]].val += lim[t], edge[pre[i] ^ 1].val -= lim[t];
    mincost += lim[t] * edge[pre[i] ^ 1].cost;
  }
}
void clear() {
  rep(i, 1, n) head[i] = 0;
  tot = 1;
  maxflow = mincost = 0;
}
} // namespace mcmf

int n, x[maxn], y[maxn], w[maxn], ans;

int m, t[maxn], a[maxn], b[maxn], pos[maxn];

int minn[maxn], maxx[maxn];

void solve(int sum) {
  // puts("------------solve-------------");
  // printf("sum : %lld\n", sum);
  mcmf ::n = 2 * (sum + n + 1);
  rep(i, 1, sum << 1) maxx[i] = inf, minn[i] = 0;
  rep(i, 1, m) {
    if (b[i] >= sum)
      continue;
    if (t[i] == 1)
      minn[b[i] + 1] = max(minn[b[i] + 1], a[i] + 1);
    if (t[i] == 2)
      maxx[sum - b[i]] = min(maxx[sum - b[i]], a[i] - 1);
    if (t[i] == 3)
      minn[b[i] + 1 + sum] = max(minn[b[i] + 1 + sum], a[i] + 1);
    if (t[i] == 4)
      maxx[sum - b[i] + sum] = min(maxx[sum - b[i] + sum], a[i] - 1);
  }
  rep(i, 2, sum) minn[i] = max(minn[i], minn[i - 1]);
  per(i, sum - 1, 1) maxx[i] = min(maxx[i], maxx[i + 1]);
  rep(i, sum + 2, sum + sum) minn[i] = max(minn[i], minn[i - 1]);
  per(i, sum + sum - 1, sum + 1) maxx[i] = min(maxx[i], maxx[i + 1]);
  // rep(i, 1, sum)
  // {
  // 	printf("i : %lld\n", i);
  // 	puts("x : ");
  // 	printf("%lld %lld\n", minn[i], maxx[i]);
  // 	puts("y : ");
  // 	printf("%lld %lld\n", minn[sum + i], maxx[sum + i]);
  // }
  // puts("add_limit success");
  mcmf ::s = mcmf ::n, mcmf ::t = mcmf ::n - 1;
  rep(i, 1, sum) mcmf ::add_flow(mcmf ::s, i, 1, 0),
      mcmf ::add_flow(sum + n + n + i, mcmf ::t, 1, 0);
  rep(i, 1, n) {
    mcmf ::add_flow(sum + i, sum + n + i, 1, -w[i]);
    pos[i] = mcmf ::tot;
    rep(j, 1, sum) {
      if (x[i] >= minn[j] && x[i] <= maxx[j])
        mcmf ::add_flow(j, sum + i, 1, 0);
      if (y[i] >= minn[j + sum] && y[i] <= maxx[j + sum])
        mcmf ::add_flow(sum + n + i, sum + n + n + j, 1, 0);
    }
  }
  // puts("build_graph success");
  while (mcmf ::spfa())
    mcmf ::updata();
  ans = min(ans, mcmf ::mincost);
  // printf("mincost : %lld\n", mcmf :: mincost);
  // puts("choose : ");
  // rep(i, 1, n) if(mcmf :: edge[pos[i]].val == 1) printf("%lld ", i);
  // enter;
  mcmf ::clear();
  return;
}

signed main() {
  scanf("%lld", &n);
  rep(i, 1, n) scanf("%lld %lld %lld", &x[i], &y[i], &w[i]);
  scanf("%lld", &m);
  rep(i, 1, m) {
    char tmp;
    scanf("%s", &tmp);
    if (tmp == 'L')
      t[i] = 1;
    if (tmp == 'R')
      t[i] = 2;
    if (tmp == 'D')
      t[i] = 3;
    if (tmp == 'U')
      t[i] = 4;
    scanf("%lld %lld", &a[i], &b[i]);
  }
  rep(i, 1, n) solve(i);
  printf("%lld\n", -ans);
  return 0;
}

/*

7
1 3 6
1 5 9
3 1 8
4 3 8
6 2 9
5 4 11
5 7 10
4
L 3 1
R 2 3
D 5 3
U 4 2


*/