Problem p00938 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
#define pb push_back
#define eb emplace_back
#define X first
#define Y second

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;

const int INF = 1e9;

int main() {
  int ans = INF;
  int n, w, d;
  vector<pair<int, pii>> vr;

  cin >> n >> w >> d;
  int N = 2 * (w + d);
  rep(i, n) {
    int x, y;
    char f;
    cin >> x >> y >> f;
    int l, r;
    if (f == 'W') {
      l = 2 * w + d - x + y;
      r = N + x - d + y;
    }
    if (f == 'E') {
      l = x + d - y;
      r = 2 * w + d - x - y;
    }
    if (f == 'N') {
      l = 2 * w + d + y + x;
      r = N + d - y + x;
    }
    if (f == 'S') {
      l = 2 * w + d - y - x;
      r = 2 * w + d + y - x;
    }
    // cout << l << " " << r << endl;
    for (; r < 5 * N; r += N, l += N) {
      vr.eb(r, pii(i, l));
    }
  }
  sort(all(vr));

  int start = N - 1;
  rep(i, n + 1) {
    int cl = 0;
    auto cur = vr.begin();
    while (cur->X < start)
      cur++;
    start = max(start + 1, cur->X);
    int cnt = 0, sum = 0;
    bool f[1005] = {};
    for (; cnt < n; cur++) {
      int r = cur->X;
      int id = cur->Y.X;
      int l = cur->Y.Y;
      if (f[id])
        continue;
      if (cl < l) {
        cl = r;
        sum++;
      }
      f[id] = true;
      cnt++;
    }
    ans = min(ans, sum);
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<int> wall[400005];
int main() {
  int n, w, d, x, y;
  char f;

  cin >> n >> w >> d;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y >> f;
    int vx[2], vy[2];
    if (f == 'W') {
      vx[0] = -1;
      vy[0] = -1;
      vx[1] = -1;
      vy[1] = 1;
    } else if (f == 'E') {
      vx[0] = 1;
      vy[0] = 1;
      vx[1] = 1;
      vy[1] = -1;
    } else if (f == 'S') {
      vx[0] = 1;
      vy[0] = -1;
      vx[1] = -1;
      vy[1] = -1;
    } else if (f == 'N') {
      vx[0] = -1;
      vy[0] = 1;
      vx[1] = 1;
      vy[1] = 1;
    }
    // cout << "vx=" << vx[0] << endl;
    REP(k, 2) {
      int _x = x, _y = y;
      while (_x > 0 && _x < w && _y > 0 && _y < d) {
        _x += vx[k];
        _y += vy[k];
      }
      if (_x == 0) {
        wall[_y].push_back(i * (1 - 2 * k));
      } else if (_y == d) {
        wall[d + _x].push_back(i * (1 - 2 * k));
      } else if (_x == w) {
        wall[d + w + d - _y].push_back(i * (1 - 2 * k));
      } else if (_y == 0) {
        wall[d + w + d + w - _x].push_back(i * (1 - 2 * k));
      }
    }
  }
  // REP(i,2*(w+d)) for(auto t: wall[i]){
  // cout << t << " ";
  //}
  // cout << endl;

  int ans = 0x3fffffff;
  REP(i, n) {
    bool check[1005] = {};
    int sum = 0;
    int th = i, count = 0, pos = 0;
    vector<int> st;
    while (count < n) {
      bool ck = false;
      for (auto t : wall[pos]) {
        if (t > 0) {
          if (th-- > 0)
            continue;
          st.push_back(t);
        } else {
          if (th > 0)
            continue;
          if (check[-t])
            continue;
          ck = true;
          st.push_back(t);
        }
      }
      if (ck) {
        sum++;
        for (auto l : st) {
          if (!check[abs(l)])
            count++;
          check[l] = true;
        }
        st.clear();
        // cout << "pos:" << pos << " count:" << count << endl;
      }
      pos++;
      if (pos > 2 * (d + w))
        pos = 0;
      // cout << "next" << pos << endl;
    }
    ans = min(ans, sum);
  }

  cout << ans << endl;

  return 0;
}