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

// g++ t.cpp -o t && t < p.txt
// d>p.txt&&t<p.txt&&t2<p.txt

#include <algorithm> // みんな大好きソートと二分探索
#include <cassert>
#include <functional> // 関数を変数に入れる子
#include <iostream>   // プログラムの耳と口
#include <map>        // setの妹 これまた優秀
#include <queue>      // きゅーちゃん、だいすき
#include <random>     // 乱択さん
#include <set>        // 値の取得・挿入・削除を高速に
#include <string>     // to_string
#include <vector>     // 超有能配列秘書

#define DB cerr << "D" << endl
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; };
using P = pair<ll, ll>;
const ll M = 1e9 + 7;
ll mod_pow(ll x, ll a) {
  ll an = 1;
  while (a > 0) {
    if (a & 1)
      an = an * x % M;
    x = x * x % M;
    a >>= 1;
  }
  return an;
}
void add(ll &x, ll y) {
  x += y;
  x %= M;
};
void mul(ll &x, ll y) {
  x *= y;
  x %= M;
};
template <typename T, typename U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
};
template <typename T, typename U> void chmin(T &x, U y) {
  if (x > y)
    x = y;
}
bool vaild(int x, int y, int hh, int ww) {
  return 0 <= x && x < hh && 0 <= y && y < ww;
}
int keta(ll a) {
  int res = 0;
  while (a > 0)
    res += (a % 10 / 5) + (a % 5), a /= 10;
  return res;
}

struct tup {
  int x, y;
  char u;
  bool operator<(const tup tt) const { return x < tt.x; }
  bool operator>(const tup tt) const { return x > tt.x; }
};

struct tup2 {
  int x, y;
  char u;
  bool operator<(const tup2 tt) const { return y < tt.y; }
  bool operator>(const tup2 tt) const { return y > tt.y; }
};

const int mx = 200001, my = 2 * mx + 1; // 200000

int main() {
  if (mx != 200001) {
    cerr << "mxmxmxmxmxmx" << endl;
  }
  int n;
  cin >> n;
  tup t[n];
  tup2 t2[n];
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    char u;
    cin >> u;
    t[i] = {x, y, u};
    t2[i] = {x, y, u};
  }

  sort(t, t + n, greater<tup>()); // xについて右から←向きにソート
  sort(t2, t2 + n, greater<tup2>()); // yについて↑から下向きにソート

  /*
  for(tup x : t) {
    cout<<x.x<<" "<<x.y<<" "<<x.u<<endl;
  } cout<<endl;

  for(tup2 x : t2) {
    cout<<x.x<<" "<<x.y<<" "<<x.u<<endl;
  } cout<<endl;*/

  int an = INF;
  int l[mx];
  fill(l, l + mx, INF);

  for (int i = 0; i < n; i++) {
    // ←向きの値を更新
    if (t[i].u == 'R') {
      // cout<<"@"<<t[i].y<<" "<<l[t[i].y]<<endl;
      if (l[t[i].y] != INF)
        chmin(an, (l[t[i].y] - t[i].x) * 5);
    } else if (t[i].u == 'L') {
      l[t[i].y] = t[i].x;
      // cout<<t[i].y<<" "<<l[t[i].y]<<endl;
    }
  }

  // cerr<<an<<endl;

  int d[mx];
  fill(d, d + mx, INF);
  for (int i = 0; i < n; i++) {
    // ↑から下へ行くので、下向きを更新
    if (t2[i].u == 'U') {
      // cout<<"@"<<t2[i].x<<" "<<d[t2[i].x]<<endl;
      if (d[t2[i].x] != INF)
        chmin(an, (d[t2[i].x] - t2[i].y) * 5);
    } else if (t2[i].u == 'D') {
      d[t2[i].x] = t2[i].y;
      // cout<<t2[i].x<<" "<<d[t2[i].x]<<endl;
    }
  }

  // cerr<<an<<endl;

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  // cerr<<an<<endl;

  for (int i = 0; i < n; i++) {
    t[i].y = mx - t[i].y;
    if (t[i].u == 'U')
      t[i].u = 'D';
    else if (t[i].u == 'D')
      t[i].u = 'U';
  }

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  // cerr<<an<<endl;

  for (int i = 0; i < n; i++) {
    t[i].x = mx - t[i].x;
    if (t[i].u == 'L')
      t[i].u = 'R';
    else if (t[i].u == 'R')
      t[i].u = 'L';
  }
  sort(t, t + n, greater<tup>());

  // cout<<t[i].x<<" "<<t[i].y<<" "<<t[i].u<<endl;

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  // cerr<<an<<endl;

  for (int i = 0; i < n; i++) {
    t[i].y = mx - t[i].y;
    if (t[i].u == 'U')
      t[i].u = 'D';
    else if (t[i].u == 'D')
      t[i].u = 'U';
  }

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  if (an == INF)
    cout << "SAFE" << endl;
  else
    cout << an << endl;
}
// ・配列の大きさok？　・priority_queueはgreater？　・debug消した？
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……

// Thank you for making problems and running the contest


Error: 0

Fixed submission:

// g++ t.cpp -o t && t < p.txt
// d>p.txt&&t<p.txt&&t2<p.txt

#include <algorithm> // みんな大好きソートと二分探索
#include <cassert>
#include <functional> // 関数を変数に入れる子
#include <iostream>   // プログラムの耳と口
#include <map>        // setの妹 これまた優秀
#include <queue>      // きゅーちゃん、だいすき
#include <random>     // 乱択さん
#include <set>        // 値の取得・挿入・削除を高速に
#include <string>     // to_string
#include <vector>     // 超有能配列秘書

#define DB cerr << "D" << endl
using namespace std;
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ll LINF = 1e18;
const double dINF = 1e18;
const ld ldINF = 1e18;
const double EPS = 1e-6;
template <typename T, typename U, typename O> void caut(T a, U b, O c) {
  cout << "(" << a << "," << b << "," << c << ") ";
}
template <typename T, typename U> void caut(T a, U b) {
  cout << "(" << a << "," << b << ") ";
}
template <typename T> void caut(T a) { cout << "(" << a << ") "; };
using P = pair<ll, ll>;
const ll M = 1e9 + 7;
ll mod_pow(ll x, ll a) {
  ll an = 1;
  while (a > 0) {
    if (a & 1)
      an = an * x % M;
    x = x * x % M;
    a >>= 1;
  }
  return an;
}
void add(ll &x, ll y) {
  x += y;
  x %= M;
};
void mul(ll &x, ll y) {
  x *= y;
  x %= M;
};
template <typename T, typename U> void chmax(T &x, U y) {
  if (x < y)
    x = y;
};
template <typename T, typename U> void chmin(T &x, U y) {
  if (x > y)
    x = y;
}
bool vaild(int x, int y, int hh, int ww) {
  return 0 <= x && x < hh && 0 <= y && y < ww;
}
int keta(ll a) {
  int res = 0;
  while (a > 0)
    res += (a % 10 / 5) + (a % 5), a /= 10;
  return res;
}

struct tup {
  int x, y;
  char u;
  bool operator<(const tup tt) const { return x < tt.x; }
  bool operator>(const tup tt) const { return x > tt.x; }
};

struct tup2 {
  int x, y;
  char u;
  bool operator<(const tup2 tt) const { return y < tt.y; }
  bool operator>(const tup2 tt) const { return y > tt.y; }
};

const int mx = 301, my = 2 * mx + 1; // 200000

int main() {
  int n;
  cin >> n;
  tup t[n];
  tup2 t2[n];
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    char u;
    cin >> u;
    t[i] = {x, y, u};
    t2[i] = {x, y, u};
  }

  sort(t, t + n, greater<tup>()); // xについて右から←向きにソート
  sort(t2, t2 + n, greater<tup2>()); // yについて↑から下向きにソート

  /*
  for(tup x : t) {
    cout<<x.x<<" "<<x.y<<" "<<x.u<<endl;
  } cout<<endl;

  for(tup2 x : t2) {
    cout<<x.x<<" "<<x.y<<" "<<x.u<<endl;
  } cout<<endl;*/

  int an = INF;
  int l[mx];
  fill(l, l + mx, INF);

  for (int i = 0; i < n; i++) {
    // ←向きの値を更新
    if (t[i].u == 'R') {
      // cout<<"@"<<t[i].y<<" "<<l[t[i].y]<<endl;
      if (l[t[i].y] != INF)
        chmin(an, (l[t[i].y] - t[i].x) * 5);
    } else if (t[i].u == 'L') {
      l[t[i].y] = t[i].x;
      // cout<<t[i].y<<" "<<l[t[i].y]<<endl;
    }
  }

  // cerr<<an<<endl;

  int d[mx];
  fill(d, d + mx, INF);
  for (int i = 0; i < n; i++) {
    // ↑から下へ行くので、下向きを更新
    if (t2[i].u == 'U') {
      // cout<<"@"<<t2[i].x<<" "<<d[t2[i].x]<<endl;
      if (d[t2[i].x] != INF)
        chmin(an, (d[t2[i].x] - t2[i].y) * 5);
    } else if (t2[i].u == 'D') {
      d[t2[i].x] = t2[i].y;
      // cout<<t2[i].x<<" "<<d[t2[i].x]<<endl;
    }
  }

  // cerr<<an<<endl;

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  // cerr<<an<<endl;

  for (int i = 0; i < n; i++) {
    t[i].y = mx - t[i].y;
    if (t[i].u == 'U')
      t[i].u = 'D';
    else if (t[i].u == 'D')
      t[i].u = 'U';
  }

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  // cerr<<an<<endl;

  for (int i = 0; i < n; i++) {
    t[i].x = mx - t[i].x;
    if (t[i].u == 'L')
      t[i].u = 'R';
    else if (t[i].u == 'R')
      t[i].u = 'L';
  }
  sort(t, t + n, greater<tup>());

  // cout<<t[i].x<<" "<<t[i].y<<" "<<t[i].u<<endl;

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  // cerr<<an<<endl;

  for (int i = 0; i < n; i++) {
    t[i].y = mx - t[i].y;
    if (t[i].u == 'U')
      t[i].u = 'D';
    else if (t[i].u == 'D')
      t[i].u = 'U';
  }

  {
    int dd[my];
    fill(dd, dd + my, INF);
    for (int i = 0; i < n; i++) {
      if (t[i].u == 'R') {
        int id = mx - t[i].x + t[i].y;
        if (dd[id] != INF) {
          chmin(an, (dd[id] - t[i].x) * 10);
        }
      } else if (t[i].u == 'D') {
        int id = mx - t[i].x + t[i].y;
        dd[id] = t[i].x;
      }
    }
  }

  if (an == INF)
    cout << "SAFE" << endl;
  else
    cout << an << endl;
}
// ・配列の大きさok？　・priority_queueはgreater？　・debug消した？
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……

// Thank you for making problems and running the contest