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

#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

vector<ll> vx;
vector<ll> vy;
vector<ll> vc;
ll mn;

void f(vector<ll> &v1, vector<ll> &v2, ll p) {
  ll index2 = 0;
  rep(i, 0, v1.sz) {
    while (index2 < v2.sz) {
      if (v1[i] < v2[index2])
        break;
      index2++;
    }
    if (index2 == v2.sz)
      break;
    mn = min(mn, abs(v1[i] - v2[index2]) * p);
  }
}

void f1() {
  vector<pair<ll, ll>> v1;
  vector<pair<ll, ll>> v3;
  set<ll> se;
  rep(i, 0, vx.sz) {
    if (vc[i] == 1) {
      v1.pb(mp(vx[i], vy[i]));
      se.insert(vx[i]);
    }
    if (vc[i] == 3) {
      v3.pb(mp(vx[i], vy[i]));
      se.insert(vx[i]);
    }
  }
  vector<ll> vv(all(se));
  v1.pb(mp(200010, -1));
  v3.pb(mp(200010, -1));
  sort(all(v1));
  sort(all(v3));
  ll index1 = 0;
  ll index3 = 0;
  rep(i, 0, vv.sz) {
    vector<ll> va, vb;
    while (v1[index1].fi == vv[i]) {
      va.pb(v1[index1].se);
      index1++;
    }
    while (v3[index3].fi == vv[i]) {
      vb.pb(v3[index3].se);
      index3++;
    }
    if (va.sz == 0 || vb.sz == 0)
      continue;
    f(vb, va, 5);
  }
}

void f2() {
  vector<pair<ll, ll>> v1;
  vector<pair<ll, ll>> v3;
  set<ll> se;
  rep(i, 0, vx.sz) {
    if (vc[i] == 2) {
      v1.pb(mp(vy[i] - vx[i], vx[i]));
      se.insert(vy[i] - vx[i]);
    }
    if (vc[i] == 3) {
      v3.pb(mp(vy[i] - vx[i], vx[i]));
      se.insert(vy[i] - vx[i]);
    }
  }
  vector<ll> vv(all(se));
  v1.pb(mp(400010, -1));
  v3.pb(mp(400010, -1));
  sort(all(v1));
  sort(all(v3));
  ll index1 = 0;
  ll index3 = 0;
  rep(i, 0, vv.sz) {
    vector<ll> va, vb;
    while (v1[index1].fi == vv[i]) {
      va.pb(v1[index1].se);
      index1++;
    }
    while (v3[index3].fi == vv[i]) {
      vb.pb(v3[index3].se);
      index3++;
    }
    if (va.sz == 0 || vb.sz == 0)
      continue;
    f(vb, va, 10);
  }
}

int main() {
  mn = 1000000000000000000LL;
  ll n;
  cin >> n;
  rep(i, 0, n) {
    ll a, b;
    char c;
    cin >> a >> b >> c;
    vx.pb(a);
    vy.pb(b);
    if (c == 'R') {
      vc.pb(0);
    }
    if (c == 'D') {
      vc.pb(1);
    }
    if (c == 'L') {
      vc.pb(2);
    }
    if (c == 'U') {
      vc.pb(3);
    }
  }
  rep(i, 0, 4) {
    rep(j, 0, vx.sz) {
      ll x1 = vx[j];
      ll y1 = vy[j];
      ll c1 = vc[j];
      ll x2 = y1;
      ll y2 = -x1;
      ll c2 = (c1 + 1) % 4;
      vx[j] = x2;
      vy[j] = y2;
      vc[j] = c2;
    }
    f1();
    f2();
  }
  if (mn == 1000000000000000000LL) {
    cout << "SAFE" << endl;
  } else {
    cout << mn << endl;
  }
  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

vector<ll> vx;
vector<ll> vy;
vector<char> vc;
ll mn;

void f(vector<ll> &v1, vector<ll> &v2, ll p) {
  ll index2 = 0;
  rep(i, 0, v1.sz) {
    while (index2 < v2.sz && v1[i] > v2[index2]) {
      index2++;
    }
    mn = min(mn, abs(v1[i] - v2[index2]) * p);
  }
}

void f1() {
  vector<pair<ll, ll>> v1;
  vector<pair<ll, ll>> v3;
  set<ll> se;
  rep(i, 0, vx.sz) {
    if (vc[i] == 1) {
      v1.pb(mp(vx[i], vy[i]));
      se.insert(vx[i]);
    }
    if (vc[i] == 3) {
      v3.pb(mp(vx[i], vy[i]));
      se.insert(vx[i]);
    }
  }
  vector<ll> vv(all(se));
  v1.pb(mp(200010, -1));
  v3.pb(mp(200010, -1));
  sort(all(v1));
  sort(all(v3));
  ll index1 = 0;
  ll index3 = 0;
  rep(i, 0, vv.sz) {
    vector<ll> va, vb;
    while (v1[index1].fi == vv[i]) {
      va.pb(v1[index1].se);
      index1++;
    }
    while (v3[index3].fi == vv[i]) {
      vb.pb(v3[index3].se);
      index3++;
    }
    if (va.sz == 0 || vb.sz == 0)
      continue;
    f(va, vb, 5);
  }
}

void f2() {
  vector<pair<ll, ll>> v1;
  vector<pair<ll, ll>> v3;
  set<ll> se;
  rep(i, 0, vx.sz) {
    if (vc[i] == 2) {
      v1.pb(mp(vy[i] - vx[i], vx[i]));
      se.insert(vy[i] - vx[i]);
    }
    if (vc[i] == 3) {
      v3.pb(mp(vy[i] - vx[i], vx[i]));
      se.insert(vy[i] - vx[i]);
    }
  }
  vector<ll> vv(all(se));
  v1.pb(mp(200010, -1));
  v3.pb(mp(200010, -1));
  sort(all(v1));
  sort(all(v3));
  ll index1 = 0;
  ll index3 = 0;
  rep(i, 0, vv.sz) {
    vector<ll> va, vb;
    while (v1[index1].fi == vv[i]) {
      va.pb(v1[index1].se);
      index1++;
    }
    while (v3[index3].fi == vv[i]) {
      vb.pb(v3[index3].se);
      index3++;
    }
    if (va.sz == 0 || vb.sz == 0)
      continue;
    f(va, vb, 10);
  }
}

int main() {
  mn = 1000000000000000000LL;
  ll n;
  cin >> n;
  rep(i, 0, n) {
    ll a, b;
    char c;
    cin >> a >> b >> c;
    vx.pb(a);
    vy.pb(b);
    if (c == 'R') {
      vc.pb(0);
    }
    if (c == 'D') {
      vc.pb(1);
    }
    if (c == 'L') {
      vc.pb(2);
    }
    if (c == 'U') {
      vc.pb(3);
    }
  }
  rep(i, 0, 4) {
    rep(j, 0, vx.sz) {
      ll x1 = vx[i];
      ll y1 = vy[i];
      ll c1 = vc[i];
      ll x2 = y1;
      ll y2 = -x1;
      ll c2 = (c1 + 1) % 4;
      vx[i] = x2;
      vy[i] = y2;
      vc[i] = c2;
    }
    f1();
    f2();
  }
  if (mn == 1000000000000000000LL) {
    cout << "SAFE" << endl;
  } else {
    cout << mn << endl;
  }
  return 0;
}