Problem p03099 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define N 710
#define M 200010
#define int long long
#define INF 4611686018427387904LL
inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x;
}
struct Point {
  int x, y, val;
} p[N];
struct opt {
  char ch;
  int x, y;
} a[N];
int tot = 1;
int fir[N], nex[M], got[M], tak[M], cst[M];
inline void AddEdge(int x, int y, int z, int w) {
  nex[++tot] = fir[x], fir[x] = tot, got[tot] = y, tak[tot] = z, cst[tot] = w;
}
inline void Add(int x, int y, int z, int w) {
  AddEdge(x, y, z, w), AddEdge(y, x, 0, -w);
}
int xl[N], xr[N], yl[N], yr[N];
int vis[N], dis[N], pre[N], que[N];
inline int spfa(int s, int t) {
  int l = 1, r = 1;
  for (int i = 1; i <= t; i++)
    vis[i] = false, dis[i] = -INF, pre[i] = 0;
  que[1] = s, dis[s] = 0;
  while (l <= r) {
    int x = que[l];
    for (int i = fir[x]; i; i = nex[i]) {
      int y = got[i], w = cst[i];
      if (!tak[i] || dis[x] + w <= dis[y])
        continue;
      dis[y] = dis[x] + w, pre[y] = i;
      if (!vis[y])
        vis[y] = true, que[++r] = y;
    }
    ++l, vis[x] = false;
  }
  return dis[t] != -INF;
}
inline int getflow(int s, int t) {
  int flow = INF;
  for (int i = t; i != s; i = got[pre[i] ^ 1]) {
    flow = min(flow, tak[pre[i]]);
  }
  return flow;
}
inline void update(int s, int t, int flow, int &Maxflow, int &Mincost) {
  Maxflow += flow, Mincost += flow * dis[t];
  for (int i = t; i != s; i = got[pre[i] ^ 1]) {
    tak[pre[i]] -= flow, tak[pre[i] ^ 1] += flow;
  }
}
inline int solve(int s, int t) {
  int Maxflow = 0, Mincost = 0;
  while (spfa(s, t)) {
    update(s, t, getflow(s, t), Maxflow, Mincost);
  }
  return Mincost;
}
signed main() {
  int n = read();
  for (int i = 1; i <= n; i++)
    p[i].x = read(), p[i].y = read(), p[i].val = read();
  int m = read(), res = 0;
  for (int i = 1; i <= m; i++) {
    char ch = getchar();
    while (ch != 'U' && ch != 'D' && ch != 'L' && ch != 'R')
      ch = getchar();
    a[i].ch = ch, a[i].x = read(), a[i].y = read();
  }
  for (int k = 1; k <= n; k++) {
    tot = 1, memset(fir, 0, sizeof(fir));
    int s = (k << 1) + (n << 1) + 1, t = s + 1;
    for (int i = 1; i <= k; i++)
      Add(s, i, 1, 0);
    for (int i = k + 2 * n + 1; i <= 2 * k + 2 * n; i++)
      Add(i, t, 1, 0);
    for (int i = k + 1; i <= k + n; i++)
      Add(i, i + n, 1, p[i - k].val);
    for (int i = 1; i <= k; i++)
      xl[i] = yl[i] = 0, xr[i] = yr[i] = INF;
    for (int i = 1; i <= m; i++) {
      if (a[i].ch == 'U')
        for (int j = 1; j <= k - a[i].y; j++)
          yr[j] = min(yr[j], a[i].x - 1);
      if (a[i].ch == 'D')
        for (int j = a[i].y + 1; j <= k; j++)
          yl[j] = max(yl[j], a[i].x + 1);
      if (a[i].ch == 'L')
        for (int j = a[i].y + 1; j <= k; j++)
          xl[j] = max(xl[j], a[i].x + 1);
      if (a[i].ch == 'R')
        for (int j = 1; j <= k - a[i].y; j++)
          xr[j] = min(xr[j], a[i].x - 1);
    }
    for (int i = 1; i <= k; i++)
      for (int j = 1; j <= n; j++)
        if (xl[i] <= p[j].x && p[j].x <= xr[i])
          Add(i, j + k, 1, 0);
    for (int i = 1; i <= k; i++)
      for (int j = 1; j <= n; j++)
        if (yl[i] <= p[j].y && p[j].y <= yr[i])
          Add(j + k + n, i + k + 2 * n, 1, 0);
    res = max(res, solve(s, t));
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 2010
#define M 200010
#define int long long
#define INF 4611686018427387904LL
inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return x;
}
struct Point {
  int x, y, val;
} p[N];
struct opt {
  char ch;
  int x, y;
} a[N];
int tot = 1;
int fir[N], nex[M], got[M], tak[M], cst[M];
inline void AddEdge(int x, int y, int z, int w) {
  nex[++tot] = fir[x], fir[x] = tot, got[tot] = y, tak[tot] = z, cst[tot] = w;
}
inline void Add(int x, int y, int z, int w) {
  AddEdge(x, y, z, w), AddEdge(y, x, 0, -w);
}
int xl[N], xr[N], yl[N], yr[N];
int vis[N], dis[N], pre[N], que[N];
inline int spfa(int s, int t) {
  int l = 1, r = 1;
  for (int i = 1; i <= t; i++)
    vis[i] = false, dis[i] = -INF, pre[i] = 0;
  que[1] = s, dis[s] = 0;
  while (l <= r) {
    int x = que[l];
    for (int i = fir[x]; i; i = nex[i]) {
      int y = got[i], w = cst[i];
      if (!tak[i] || dis[x] + w <= dis[y])
        continue;
      dis[y] = dis[x] + w, pre[y] = i;
      if (!vis[y])
        vis[y] = true, que[++r] = y;
    }
    ++l, vis[x] = false;
  }
  return dis[t] != -INF;
}
inline int getflow(int s, int t) {
  int flow = INF;
  for (int i = t; i != s; i = got[pre[i] ^ 1]) {
    flow = min(flow, tak[pre[i]]);
  }
  return flow;
}
inline void update(int s, int t, int flow, int &Maxflow, int &Mincost) {
  Maxflow += flow, Mincost += flow * dis[t];
  for (int i = t; i != s; i = got[pre[i] ^ 1]) {
    tak[pre[i]] -= flow, tak[pre[i] ^ 1] += flow;
  }
}
inline int solve(int s, int t) {
  int Maxflow = 0, Mincost = 0;
  while (spfa(s, t)) {
    update(s, t, getflow(s, t), Maxflow, Mincost);
  }
  return Mincost;
}
signed main() {
  int n = read();
  for (int i = 1; i <= n; i++)
    p[i].x = read(), p[i].y = read(), p[i].val = read();
  int m = read(), res = 0;
  for (int i = 1; i <= m; i++) {
    char ch = getchar();
    while (ch != 'U' && ch != 'D' && ch != 'L' && ch != 'R')
      ch = getchar();
    a[i].ch = ch, a[i].x = read(), a[i].y = read();
  }
  for (int k = 1; k <= n; k++) {
    tot = 1, memset(fir, 0, sizeof(fir));
    int s = (k << 1) + (n << 1) + 1, t = s + 1;
    for (int i = 1; i <= k; i++)
      Add(s, i, 1, 0);
    for (int i = k + 2 * n + 1; i <= 2 * k + 2 * n; i++)
      Add(i, t, 1, 0);
    for (int i = k + 1; i <= k + n; i++)
      Add(i, i + n, 1, p[i - k].val);
    for (int i = 1; i <= k; i++)
      xl[i] = yl[i] = 0, xr[i] = yr[i] = INF;
    for (int i = 1; i <= m; i++) {
      if (a[i].ch == 'U')
        for (int j = 1; j <= k - a[i].y; j++)
          yr[j] = min(yr[j], a[i].x - 1);
      if (a[i].ch == 'D')
        for (int j = a[i].y + 1; j <= k; j++)
          yl[j] = max(yl[j], a[i].x + 1);
      if (a[i].ch == 'L')
        for (int j = a[i].y + 1; j <= k; j++)
          xl[j] = max(xl[j], a[i].x + 1);
      if (a[i].ch == 'R')
        for (int j = 1; j <= k - a[i].y; j++)
          xr[j] = min(xr[j], a[i].x - 1);
    }
    for (int i = 1; i <= k; i++)
      for (int j = 1; j <= n; j++)
        if (xl[i] <= p[j].x && p[j].x <= xr[i])
          Add(i, j + k, 1, 0);
    for (int i = 1; i <= k; i++)
      for (int j = 1; j <= n; j++)
        if (yl[i] <= p[j].y && p[j].y <= yr[i])
          Add(j + k + n, i + k + 2 * n, 1, 0);
    res = max(res, solve(s, t));
  }
  cout << res << endl;
  return 0;
}