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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
long long N, K, X[100009], Y[100009], ux[100009], uy[100009], uz[100009], cx,
    cy;
string S;
map<pair<long long, long long>, vector<long long>> T;
map<pair<long long, long long>, bool> M;
pair<long long, long long> exchange(long long px, long long py, long long &ps) {
  ps = (px + 1000000000LL * cx) / cx - 1000000000LL;
  long long PX = px - ps * cx;
  long long PY = py - ps * cy;
  return make_pair(PX, PY);
}
int main() {
  cin >> N >> K >> S;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'N')
      cx--;
    if (S[i] == 'S')
      cx++;
    if (S[i] == 'E')
      cy++;
    if (S[i] == 'W')
      cy--;
    X[i + 1] = cx;
    Y[i + 1] = cy;
  }
  if (cx == 0 && cy == 0) {
    K = 1;
    cx = 1;
    cy = 1;
  }
  if (cx == 0) {
    for (int i = 0; i <= N; i++)
      swap(X[i], Y[i]);
    swap(cx, cy);
  }
  if (cx < 0) {
    for (int i = 0; i <= N; i++)
      X[i] *= -1;
    cx *= -1;
  }
  if (cy < 0) {
    for (int i = 0; i <= N; i++)
      Y[i] *= -1;
    cy *= -1;
  }
  for (int i = 0; i <= N; i++) {
    long long V = 0;
    pair<long long, long long> uu = exchange(X[i], Y[i], V);
    ux[i] = uu.first;
    uy[i] = uu.second;
    uz[i] = V;
    T[make_pair(ux[i], uy[i])].push_back(V);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (M[make_pair(ux[i], uy[i])] == true)
      continue;
    vector<long long> A[4];
    long long D = 0, DE = 0;
    A[0] = T[exchange(ux[i], uy[i], D)];
    A[1] = T[exchange(ux[i] + 1, uy[i], DE)];
    if (D != DE) {
      for (int i = 0; i < A[1].size(); i++) {
        A[1][i]--;
      }
    }
    A[2] = T[exchange(ux[i], uy[i] + 1, DE)];
    if (D != DE) {
      for (int i = 0; i < A[2].size(); i++) {
        A[2][i]--;
      }
    }
    A[3] = T[exchange(ux[i] + 1, uy[i] + 1, DE)];
    if (D != DE) {
      for (int i = 0; i < A[3].size(); i++) {
        A[3][i]--;
      }
    }
    vector<long long> E;
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < A[j].size(); k++) {
        E.push_back(A[j][k]);
        E.push_back(A[j][k] + K);
      }
    }
    sort(E.begin(), E.end());
    E.erase(unique(E.begin(), E.end()), E.end());
    vector<int> ES(E.size() + 1, 0);
    for (int j = 0; j < 4; j++) {
      vector<int> ER(E.size() + 1, 0);
      for (int k = 0; k < A[j].size(); k++) {
        int pos1 = lower_bound(E.begin(), E.end(), A[j][k]) - E.begin();
        int pos2 = lower_bound(E.begin(), E.end(), A[j][k] + K) - E.begin();
        ER[pos1]++;
        ER[pos2]--;
      }
      for (int j = 1; j < E.size() + 1; j++)
        ER[j] += ER[j - 1];
      for (int j = 0; j < E.size() + 1; j++) {
        if (ER[j] >= 1)
          ES[j] += 1;
      }
    }
    long long res = 0;
    for (int j = 0; j < E.size(); j++) {
      if (ES[j] == 4)
        res += (E[j + 1] - E[j]);
    }
    ans += res;
    M[make_pair(ux[i], uy[i])] = true;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
long long N, K, X[100009], Y[100009], ux[100009], uy[100009], uz[100009], cx,
    cy;
string S;
map<pair<long long, long long>, vector<long long>> T;
map<pair<long long, long long>, bool> M;
pair<long long, long long> exchange(long long px, long long py, long long &ps) {
  ps = (px + 1000000000LL * cx) / cx - 1000000000LL;
  long long PX = px - ps * cx;
  long long PY = py - ps * cy;
  return make_pair(PX, PY);
}
int main() {
  cin >> N >> K >> S;
  for (int i = 0; i < N; i++) {
    if (S[i] == 'N')
      cx--;
    if (S[i] == 'S')
      cx++;
    if (S[i] == 'E')
      cy++;
    if (S[i] == 'W')
      cy--;
    X[i + 1] = cx;
    Y[i + 1] = cy;
  }
  if (cx == 0 && cy == 0) {
    N--;
    K = 1;
  }
  if (cx == 0) {
    for (int i = 0; i <= N; i++)
      swap(X[i], Y[i]);
    swap(cx, cy);
  }
  if (cx < 0) {
    for (int i = 0; i <= N; i++)
      X[i] *= -1;
    cx *= -1;
  }
  if (cy < 0) {
    for (int i = 0; i <= N; i++)
      Y[i] *= -1;
    cy *= -1;
  }
  for (int i = 0; i < N; i++) {
    long long V = 0;
    pair<long long, long long> uu = exchange(X[i], Y[i], V);
    ux[i] = uu.first;
    uy[i] = uu.second;
    uz[i] = V;
    T[make_pair(ux[i], uy[i])].push_back(V);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    if (M[make_pair(ux[i], uy[i])] == true)
      continue;
    vector<long long> A[4];
    long long D = 0, DE = 0;
    A[0] = T[exchange(ux[i], uy[i], D)];
    A[1] = T[exchange(ux[i] + 1, uy[i], DE)];
    if (D != DE) {
      for (int i = 0; i < A[1].size(); i++) {
        A[1][i]--;
      }
    }
    A[2] = T[exchange(ux[i], uy[i] + 1, DE)];
    if (D != DE) {
      for (int i = 0; i < A[2].size(); i++) {
        A[2][i]--;
      }
    }
    A[3] = T[exchange(ux[i] + 1, uy[i] + 1, DE)];
    if (D != DE) {
      for (int i = 0; i < A[3].size(); i++) {
        A[3][i]--;
      }
    }
    vector<long long> E;
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < A[j].size(); k++) {
        E.push_back(A[j][k]);
        E.push_back(A[j][k] + K);
      }
    }
    sort(E.begin(), E.end());
    E.erase(unique(E.begin(), E.end()), E.end());
    vector<int> ES(E.size() + 1, 0);
    for (int j = 0; j < 4; j++) {
      vector<int> ER(E.size() + 1, 0);
      for (int k = 0; k < A[j].size(); k++) {
        int pos1 = lower_bound(E.begin(), E.end(), A[j][k]) - E.begin();
        int pos2 = lower_bound(E.begin(), E.end(), A[j][k] + K) - E.begin();
        ER[pos1]++;
        ER[pos2]--;
      }
      for (int j = 1; j < E.size() + 1; j++)
        ER[j] += ER[j - 1];
      for (int j = 0; j < E.size() + 1; j++) {
        if (ER[j] >= 1)
          ES[j] += 1;
      }
    }
    long long res = 0;
    for (int j = 0; j < E.size(); j++) {
      if (ES[j] == 4)
        res += (E[j + 1] - E[j]);
    }
    ans += res;
    M[make_pair(ux[i], uy[i])] = true;
  }
  cout << ans << endl;
  return 0;
}