Problem p00349 (C++)

Buggy submission (status: Runtime Error):
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0354

#include <bits/stdc++.h>
const int N = 1e5 + 5;
const int inf = 1e9 + 7;
using namespace std;

vector<int> mv[2];
int n, w, h, x[N], y[N], p[N], ans[N];
char s[N];

void cal(vector<int> &a, bool ck) {
  if (a.size() == 0)
    return;
  queue<int> Q1, Q2;
  for (int i = 0; i < a.size(); i++)
    if (s[a[i]] == 'E')
      Q1.push(a[i]);
  for (int i = (int)a.size() - 1; i >= 0; i--)
    if (s[a[i]] == 'S')
      Q2.push(a[i]);

  //    if (x[a[0]] + y[a[0]] == 6 && ck)
  //    for (int i = 0; i < a.size(); i++)
  //        cout << a[i] << " " << x[a[i]] << " " << y[a[i]] << " " << s[a[i]]
  //        << "\n";
  //
  //    cout << "\n";
  for (int i = 0; i < a.size(); i++) {
    int E = ((Q1.size()) ? Q1.front() : inf),
        S = ((Q2.size()) ? Q2.front() : inf);
    if (E != inf && (S == inf || w - x[E] < h - y[S])) {
      ans[a[i]] = w - x[E] + 1 + ck;
      Q1.pop();
    } else {
      ans[a.back()] = h - y[S] + 1 + ck;
      a.pop_back();
      Q2.pop();
      i--;
    }
  }
  //    cout << "\n";
}

void change(vector<int> &a) {
  for (int i = 1; i < a.size(); i++) {
    if (x[a[i]] == x[a[i - 1]] && y[a[i]] == y[a[i - 1]])
      swap(s[a[i]], s[a[i - 1]]), swap(a[i], a[i - 1]);
  }
}

void solve(vector<int> &a, bool ck) {
  vector<int> X, Y;
  for (int i = 0; i < a.size(); i++) {
    int sum = x[a[i]] + y[a[i]];
    for (; i < a.size(); i++) {
      if (x[a[i]] + y[a[i]] != sum)
        break;
      if (x[a[i]] % 2 == 0)
        X.push_back(a[i]);
      else
        Y.push_back(a[i]);
    }
    cal(X, ck);
    X.clear();
    cal(Y, ck);
    Y.clear();
    i--;
  }
}

bool cmp(int a, int b) {
  if (x[a] + y[a] < x[b] + y[b])
    return true;
  if (x[a] + y[a] == x[b] + y[b] && x[a] > x[b])
    return true;
  if (x[a] == x[b] && y[a] == y[b] && s[a] == 'E')
    return true;
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> w >> h >> n;
  vector<int> vc;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i] >> s[i];
    if (s[i] == 'E')
      vc.push_back(w - x[i] + 1);
    else
      vc.push_back(h - y[i] + 1);
    if ((x[i] + y[i]) % 2 == 1) {
      if (s[i] == 'E')
        x[i]++;
      else
        y[i]++;
      mv[1].push_back(i);
    } else {
      mv[0].push_back(i);
    }
  }

  sort(mv[0].begin(), mv[0].end(), cmp);
  sort(mv[1].begin(), mv[1].end(), cmp);

  change(mv[1]);

  solve(mv[0], 0);
  solve(mv[1], 1);

  sort(vc.begin(), vc.end());

  for (int i = 1; i <= n; i++)
    p[i] = i;
  sort(p + 1, p + 1 + n, [](int x, int y) {
    return ((ans[x] < ans[y]) || (ans[x] == ans[y] && x < y));
  });
  for (int i = 1; i <= n; i++)
    cout << p[i] << "\n";
}


Fixed submission:
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0354

#include <bits/stdc++.h>
const int N = 2e5 + 5;
const int inf = 1e9 + 7;
using namespace std;

vector<int> mv[2];
int n, w, h, x[N], y[N], p[N], ans[N];
char s[N];

void cal(vector<int> &a, bool ck) {
  if (a.size() == 0)
    return;
  queue<int> Q1, Q2;
  for (int i = 0; i < a.size(); i++)
    if (s[a[i]] == 'E')
      Q1.push(a[i]);
  for (int i = (int)a.size() - 1; i >= 0; i--)
    if (s[a[i]] == 'S')
      Q2.push(a[i]);

  //    if (x[a[0]] + y[a[0]] == 6 && ck)
  //    for (int i = 0; i < a.size(); i++)
  //        cout << a[i] << " " << x[a[i]] << " " << y[a[i]] << " " << s[a[i]]
  //        << "\n";
  //
  //    cout << "\n";
  for (int i = 0; i < a.size(); i++) {
    int E = ((Q1.size()) ? Q1.front() : inf),
        S = ((Q2.size()) ? Q2.front() : inf);
    if (E != inf && (S == inf || w - x[E] < h - y[S])) {
      ans[a[i]] = w - x[E] + 1 + ck;
      Q1.pop();
    } else {
      ans[a.back()] = h - y[S] + 1 + ck;
      a.pop_back();
      Q2.pop();
      i--;
    }
  }
  //    cout << "\n";
}

void change(vector<int> &a) {
  for (int i = 1; i < a.size(); i++) {
    if (x[a[i]] == x[a[i - 1]] && y[a[i]] == y[a[i - 1]])
      swap(s[a[i]], s[a[i - 1]]), swap(a[i], a[i - 1]);
  }
}

void solve(vector<int> &a, bool ck) {
  vector<int> X, Y;
  for (int i = 0; i < a.size(); i++) {
    int sum = x[a[i]] + y[a[i]];
    for (; i < a.size(); i++) {
      if (x[a[i]] + y[a[i]] != sum)
        break;
      if (x[a[i]] % 2 == 0)
        X.push_back(a[i]);
      else
        Y.push_back(a[i]);
    }
    cal(X, ck);
    X.clear();
    cal(Y, ck);
    Y.clear();
    i--;
  }
}

bool cmp(int a, int b) {
  if (x[a] + y[a] < x[b] + y[b])
    return true;
  if (x[a] + y[a] == x[b] + y[b] && x[a] > x[b])
    return true;
  if (x[a] == x[b] && y[a] == y[b] && s[a] == 'E')
    return true;
  return false;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> w >> h >> n;
  vector<int> vc;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i] >> s[i];
    if (s[i] == 'E')
      vc.push_back(w - x[i] + 1);
    else
      vc.push_back(h - y[i] + 1);
    if ((x[i] + y[i]) % 2 == 1) {
      if (s[i] == 'E')
        x[i]++;
      else
        y[i]++;
      mv[1].push_back(i);
    } else {
      mv[0].push_back(i);
    }
  }

  sort(mv[0].begin(), mv[0].end(), cmp);
  sort(mv[1].begin(), mv[1].end(), cmp);

  change(mv[1]);

  solve(mv[0], 0);
  solve(mv[1], 1);

  sort(vc.begin(), vc.end());

  for (int i = 1; i <= n; i++)
    p[i] = i;
  sort(p + 1, p + 1 + n, [](int x, int y) {
    return ((ans[x] < ans[y]) || (ans[x] == ans[y] && x < y));
  });
  for (int i = 1; i <= n; i++)
    cout << p[i] << "\n";
}