Problem p03099 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define sqr(x) ((x) * (x))
#define fz1(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define fd1(i, n) for ((i) = (n); (i) >= 1; (i)--)
#define fz0g(i, n) for ((i) = 0; (i) <= (n); (i)++)
#define fd0g(i, n) for ((i) = (n); (i) >= 0; (i)--)
#define fz0k(i, n) for ((i) = 0; (i) < (n); (i)++)
#define fd0k(i, n) for ((i) = (int)((n)-1); (i) >= 0; (i)--)
#define fz(i, x, y) for ((i) = (x); (i) <= (y); (i)++)
#define fd(i, y, x) for ((i) = (y); (i) >= (x); (i)--)
#define fzin fz1(i, n)
#define fzim fz1(i, m)
#define fzjn fz1(j, n)
#define fzjm fz1(j, m)
#define ff(c, itr)                                                             \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define rdst(st, len)                                                          \
  {                                                                            \
    static char ss[len];                                                       \
    scanf(" %s", ss);                                                          \
    (st) = ss;                                                                 \
  }
// #define T_ int
using namespace std;
// struct bit{T_ arr[1000005];int lowbit(int x){return x&(-x);}void add(int x,T_
// y){for (int i=x;i<=n;i+=lowbit(i)) arr[i]+=y;}T_ query(int x){T_ s=0;for (int
// i=x;i>0;i-=lowbit(i)) s+=arr[i];return s;}}; struct seg{struct treedot{int
// l,r;T_ sum,add;void update(T_ x){sum+=(r-l+1)*x; add+=x;}}c[800005];void
// pushdown(int id){if
// (c[id].add){c[id*2].update(c[id].add);c[id*2+1].update(c[id].add);c[id].add=0;}}void
// pushup(int id){c[id].sum=c[id*2].sum+c[id*2+1].sum;}void build(int id,int
// l,int r){if (l>r) return;c[id].l=l;c[id].r=r;c[id].sum=0;c[id].add=0;if
// (l==r) c[id].sum=0;else{int
// mid=l+(r-l)/2;build(id*2,l,mid);build(id*2+1,mid+1,r);pushup(id);}}void
// update(int id,int le,int ri,T_ x){if (le>c[id].r||ri<c[id].l) return;if
// (le<=c[id].l&&c[id].r<=ri)
// c[id].update(x);else{pushdown(id);update(id*2,le,ri,x);update(id*2+1,le,ri,x);pushup(id);}}T_
// query(int id,int le,int ri){if (ri<c[id].l||c[id].r<le) return 0;if
// (le<=c[id].l&&c[id].r<=ri) return c[id].sum;T_
// ans=0;pushdown(id);ans+=query(id*2,le,ri);ans+=query(id*2+1,le,ri);pushup(id);return
// ans;}}; struct bit_2d{T_ a[2005][2005];int lowbit(int x){return x&(-x);}void
// add(int x,int y,T_ z){int i,j;for (i=x;i<=2000;i+=lowbit(i)){for
// (j=y;j<=2000;j+=lowbit(j)){a[i][j]+=z;}}}T_ query(int x,int y){int i,j;T_
// s=0;for (i=x;i;i-=lowbit(i)){for (j=y;j;j-=lowbit(j)){s+=a[i][j];}}return
// s;}T_ query(int x,int y,int xx,int yy){return
// query(xx,yy)-query(x-1,yy)-query(xx,y-1)+query(x-1,y-1);}};
int n, m, i, j, k, cnt, a[325], b[325], s = 0, t = 321, l[325], r[325], u[325],
                                        d[325], x, y, inq[325], vis[325];
vector<int> allx, ally;
long long v[325], cst, ans, dis[325];
char c[325], ca[325], cb[325];
struct ii {
  int y, z;
  long long d;
} e[10005];
vector<int> bi[10005];
void init() {
  int i;
  fz0g(i, 321) bi[i].clear();
  cnt = 0;
}
void add(int x, int y, int z, long long d) {
  cnt++;
  bi[x].push_back(cnt * 2 - 2);
  bi[y].push_back(cnt * 2 - 1);
  e[cnt * 2 - 2] = (ii){y, z, d};
  e[cnt * 2 - 1] = (ii){x, 0, -d};
}
bool bfs(int s, int t) {
  memset(dis, -0x18, sizeof(dis));
  dis[s] = 0;
  queue<int> qx;
  qx.push(s);
  inq[s] = 1;
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    inq[x] = 0;
    ff(bi[x], it) if (e[*it].z) {
      int y = e[*it].y;
      long long z = dis[x] + e[*it].d;
      if (dis[y] < z) {
        dis[y] = z;
        if (!inq[y]) {
          inq[y] = 1;
          qx.push(y);
        }
      }
    }
  }
  return dis[t] > dis[324];
}
int dfs(int x, int tt, int flw) {
  if (!flw)
    return 0;
  if (x == tt) {
    vis[x] = 1;
    return flw;
  }
  if (vis[x])
    return 0;
  vis[x] = 1;
  int ans = 0;
  ff(bi[x], it) {
    //		cerr<<e[*it].y<<' '<<e[*it].z<<endl;
    if (e[*it].z && dis[e[*it].y] == dis[x] + e[*it].d) {
      int t = dfs(e[*it].y, tt, min(flw, e[*it].z));
      cst += t * e[*it].d;
      ans += t;
      flw -= t;
      e[*it].z -= t;
      e[(*it) ^ 1].z += t;
      if (!flw)
        break;
    }
  }
  return ans;
}
bool dinic(int s, int t, int aa) {
  int ans = 0;
  while (bfs(s, t)) {
    do {
      memset(vis, 0, sizeof(vis));
      ans += dfs(s, t, aa);
    } while (vis[t]);
  }
  if (ans == aa)
    return 1;
  return 0;
}
int main() {
  scanf("%d", &n);
  fz1(i, n) {
    scanf("%d%d%lld", &a[i], &b[i], &v[i]);
    //		allx.push_back(a[i]);ally.push_back(b[i]);
  } // sort(allx.begin(),allx.end());allx.resize(unique(allx.begin(),allx.end())-allx.begin());sort(ally.begin(),ally.end());ally.resize(unique(ally.begin(),ally.end())-ally.begin());
  /*fz1(i,n){
          a[i]=upper_bound(allx.begin(),allx.end(),a[i])-allx.begin();
          b[i]=upper_bound(ally.begin(),ally.end(),b[i])-ally.begin();
  }*/
  scanf("%d", &m);
  fz1(i, m) { scanf(" %c%d%d", &c[i], &ca[i], &cb[i]); }
  ans = 0;
  fz1(i, n) {
    cst = 0;
    init();
    fz1(j, i) {
      l[i] = d[i] = 0;
      r[i] = u[i] = 0x18181818;
    }
    fz1(j, m) if (cb[j] < i) {
      if (c[j] == 'L')
        l[cb[j] + 1] = max(l[cb[j] + 1], ca[j] + 1);
      if (c[j] == 'R')
        r[i - cb[j]] = min(r[i - cb[j]], ca[j] - 1);
      if (c[j] == 'D')
        d[cb[j] + 1] = max(d[cb[j] + 1], ca[j] + 1);
      if (c[j] == 'U')
        u[i - cb[j]] = min(u[i - cb[j]], ca[j] - 1);
    }
    fz(j, 2, i) {
      l[j] = max(l[j], l[j - 1]);
      d[j] = max(d[j], d[j - 1]);
    }
    fd(j, i - 1, 1) {
      r[j] = min(r[j], r[j + 1]);
      u[j] = min(u[j], u[j + 1]);
    }
    fz1(j, i) fz1(k, n) {
      if (l[j] <= a[k] && a[k] <= r[j]) {
        add(j, n + k, 1, 0);
      }
      if (d[j] <= b[k] && b[k] <= u[j]) {
        add(n + n + k, n + n + n + j, 1, 0);
      }
    }
    fz1(j, i) {
      add(s, j, 1, 0);
      add(n + n + n + j, t, 1, 0);
    }
    fz1(j, n) { add(j + n, j + n + n, 1, v[j]); }
    if (dinic(s, t, i))
      ans = max(ans, cst);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define sqr(x) ((x) * (x))
#define fz1(i, n) for ((i) = 1; (i) <= (n); (i)++)
#define fd1(i, n) for ((i) = (n); (i) >= 1; (i)--)
#define fz0g(i, n) for ((i) = 0; (i) <= (n); (i)++)
#define fd0g(i, n) for ((i) = (n); (i) >= 0; (i)--)
#define fz0k(i, n) for ((i) = 0; (i) < (n); (i)++)
#define fd0k(i, n) for ((i) = (int)((n)-1); (i) >= 0; (i)--)
#define fz(i, x, y) for ((i) = (x); (i) <= (y); (i)++)
#define fd(i, y, x) for ((i) = (y); (i) >= (x); (i)--)
#define fzin fz1(i, n)
#define fzim fz1(i, m)
#define fzjn fz1(j, n)
#define fzjm fz1(j, m)
#define ff(c, itr)                                                             \
  for (__typeof((c).begin()) itr = (c).begin(); itr != (c).end(); itr++)
#define rdst(st, len)                                                          \
  {                                                                            \
    static char ss[len];                                                       \
    scanf(" %s", ss);                                                          \
    (st) = ss;                                                                 \
  }
// #define T_ int
using namespace std;
// struct bit{T_ arr[1000005];int lowbit(int x){return x&(-x);}void add(int x,T_
// y){for (int i=x;i<=n;i+=lowbit(i)) arr[i]+=y;}T_ query(int x){T_ s=0;for (int
// i=x;i>0;i-=lowbit(i)) s+=arr[i];return s;}}; struct seg{struct treedot{int
// l,r;T_ sum,add;void update(T_ x){sum+=(r-l+1)*x; add+=x;}}c[800005];void
// pushdown(int id){if
// (c[id].add){c[id*2].update(c[id].add);c[id*2+1].update(c[id].add);c[id].add=0;}}void
// pushup(int id){c[id].sum=c[id*2].sum+c[id*2+1].sum;}void build(int id,int
// l,int r){if (l>r) return;c[id].l=l;c[id].r=r;c[id].sum=0;c[id].add=0;if
// (l==r) c[id].sum=0;else{int
// mid=l+(r-l)/2;build(id*2,l,mid);build(id*2+1,mid+1,r);pushup(id);}}void
// update(int id,int le,int ri,T_ x){if (le>c[id].r||ri<c[id].l) return;if
// (le<=c[id].l&&c[id].r<=ri)
// c[id].update(x);else{pushdown(id);update(id*2,le,ri,x);update(id*2+1,le,ri,x);pushup(id);}}T_
// query(int id,int le,int ri){if (ri<c[id].l||c[id].r<le) return 0;if
// (le<=c[id].l&&c[id].r<=ri) return c[id].sum;T_
// ans=0;pushdown(id);ans+=query(id*2,le,ri);ans+=query(id*2+1,le,ri);pushup(id);return
// ans;}}; struct bit_2d{T_ a[2005][2005];int lowbit(int x){return x&(-x);}void
// add(int x,int y,T_ z){int i,j;for (i=x;i<=2000;i+=lowbit(i)){for
// (j=y;j<=2000;j+=lowbit(j)){a[i][j]+=z;}}}T_ query(int x,int y){int i,j;T_
// s=0;for (i=x;i;i-=lowbit(i)){for (j=y;j;j-=lowbit(j)){s+=a[i][j];}}return
// s;}T_ query(int x,int y,int xx,int yy){return
// query(xx,yy)-query(x-1,yy)-query(xx,y-1)+query(x-1,y-1);}};
int n, m, i, j, k, cnt, a[325], b[325], s = 0, t = 321, l[325], r[325], u[325],
                                        d[325], x, y, inq[325], vis[325];
vector<int> allx, ally;
long long v[325], cst, ans, dis[325];
char c[325], ca[325], cb[325];
struct ii {
  int y, z;
  long long d;
} e[1000005];
vector<int> bi[325];
void init() {
  int i;
  fz0g(i, 321) bi[i].clear();
  cnt = 0;
}
void add(int x, int y, int z, long long d) {
  cnt++;
  bi[x].push_back(cnt * 2 - 2);
  bi[y].push_back(cnt * 2 - 1);
  e[cnt * 2 - 2] = (ii){y, z, d};
  e[cnt * 2 - 1] = (ii){x, 0, -d};
}
bool bfs(int s, int t) {
  memset(dis, -0x18, sizeof(dis));
  dis[s] = 0;
  queue<int> qx;
  qx.push(s);
  inq[s] = 1;
  while (!qx.empty()) {
    int x = qx.front();
    qx.pop();
    inq[x] = 0;
    ff(bi[x], it) if (e[*it].z) {
      int y = e[*it].y;
      long long z = dis[x] + e[*it].d;
      if (dis[y] < z) {
        dis[y] = z;
        if (!inq[y]) {
          inq[y] = 1;
          qx.push(y);
        }
      }
    }
  }
  return dis[t] > dis[324];
}
int dfs(int x, int tt, int flw) {
  if (!flw)
    return 0;
  if (x == tt) {
    vis[x] = 1;
    return flw;
  }
  if (vis[x])
    return 0;
  vis[x] = 1;
  int ans = 0;
  ff(bi[x], it) {
    //		cerr<<e[*it].y<<' '<<e[*it].z<<endl;
    if (e[*it].z && dis[e[*it].y] == dis[x] + e[*it].d) {
      int t = dfs(e[*it].y, tt, min(flw, e[*it].z));
      cst += t * e[*it].d;
      ans += t;
      flw -= t;
      e[*it].z -= t;
      e[(*it) ^ 1].z += t;
      if (!flw)
        break;
    }
  }
  return ans;
}
bool dinic(int s, int t, int aa) {
  int ans = 0;
  while (bfs(s, t)) {
    do {
      memset(vis, 0, sizeof(vis));
      ans += dfs(s, t, aa);
    } while (vis[t]);
  }
  if (ans == aa)
    return 1;
  return 0;
}
int main() {
  scanf("%d", &n);
  fz1(i, n) {
    scanf("%d%d%lld", &a[i], &b[i], &v[i]);
    //		allx.push_back(a[i]);ally.push_back(b[i]);
  } // sort(allx.begin(),allx.end());allx.resize(unique(allx.begin(),allx.end())-allx.begin());sort(ally.begin(),ally.end());ally.resize(unique(ally.begin(),ally.end())-ally.begin());
  /*fz1(i,n){
          a[i]=upper_bound(allx.begin(),allx.end(),a[i])-allx.begin();
          b[i]=upper_bound(ally.begin(),ally.end(),b[i])-ally.begin();
  }*/
  scanf("%d", &m);
  fz1(i, m) { scanf(" %c%d%d", &c[i], &ca[i], &cb[i]); }
  ans = 0;
  fz1(i, n) {
    cst = 0;
    init();
    fz1(j, i) {
      l[i] = d[i] = 0;
      r[i] = u[i] = 0x18181818;
    }
    fz1(j, m) if (cb[j] < i) {
      if (c[j] == 'L')
        l[cb[j] + 1] = max(l[cb[j] + 1], ca[j] + 1);
      if (c[j] == 'R')
        r[i - cb[j]] = min(r[i - cb[j]], ca[j] - 1);
      if (c[j] == 'D')
        d[cb[j] + 1] = max(d[cb[j] + 1], ca[j] + 1);
      if (c[j] == 'U')
        u[i - cb[j]] = min(u[i - cb[j]], ca[j] - 1);
    }
    fz(j, 2, i) {
      l[j] = max(l[j], l[j - 1]);
      d[j] = max(d[j], d[j - 1]);
    }
    fd(j, i - 1, 1) {
      r[j] = min(r[j], r[j + 1]);
      u[j] = min(u[j], u[j + 1]);
    }
    fz1(j, i) fz1(k, n) {
      if (l[j] <= a[k] && a[k] <= r[j]) {
        add(j, n + k, 1, 0);
      }
      if (d[j] <= b[k] && b[k] <= u[j]) {
        add(n + n + k, n + n + n + j, 1, 0);
      }
    }
    fz1(j, i) {
      add(s, j, 1, 0);
      add(n + n + n + j, t, 1, 0);
    }
    fz1(j, n) { add(j + n, j + n + n, 1, v[j]); }
    if (dinic(s, t, i))
      ans = max(ans, cst);
  }
  printf("%lld\n", ans);
  return 0;
}