Problem p03112 (C++)

Buggy submission (status: Runtime Error):
// dile a la jardinera que traigo flores

#include <bits/stdc++.h>
#define ff first
#define ss second
#define tm1 first.first
#define tm2 first.second
#define tm3 second
#define pb push_back
#define sz(x) int(x.size())
#define all(v) (v).begin(), (v).end()
#define trace(x) cout << #x << " = " << x << endl
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define FER(i, a, b) for (int i = int(a); i < int(b); ++i)
#define IFR(i, a, b) for (int i = int(a); i >= int(b); i--)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> tri;

const int N = 1e5 + 20;
const ll inf = 1e16;
ll mm[N], ans[N];
vector<tri> vec;
ll LS[N], LT[N], RS[N], RT[N];

int main() {
  fastio;
  int s, t, q;
  cin >> s >> t >> q;
  vec.pb({{-inf, 1}, -1});
  vec.pb({{-inf, 2}, -1});
  vec.pb({{inf, 1}, -1});
  vec.pb({{inf, 1}, -1});
  vec.pb({{inf, 2}, -1});
  FER(i, 0, s) {
    ll k;
    cin >> k;
    vec.pb({{k, 1}, -1});
  }
  FER(i, 0, t) {
    ll k;
    cin >> k;
    vec.pb({{k, 2}, -1});
  }
  FER(i, 0, q) {
    ll k;
    cin >> k;
    vec.pb({{k, 3}, i});
  }
  sort(all(vec));
  ii esta = {-2 * inf, -2 * inf};
  FER(i, 0, sz(vec)) {
    LS[i] = esta.ff;
    LT[i] = esta.ss;
    if (vec[i].tm2 == 1)
      esta.ff = LS[i] = vec[i].tm1;
    if (vec[i].tm2 == 2)
      esta.ss = LT[i] = vec[i].tm1;
  }
  esta = {inf, inf};
  IFR(i, sz(vec) - 1, 0) {
    RS[i] = esta.ff;
    RT[i] = esta.ss;
    if (vec[i].tm2 == 1)
      esta.ff = RS[i] = vec[i].tm1;
    if (vec[i].tm2 == 2)
      esta.ss = RT[i] = vec[i].tm1;
  }
  FER(i, 2, sz(vec) - 2) if (vec[i].tm3 != -1) {
    LS[i] = abs(LS[i] - vec[i].tm1);
    LT[i] = abs(LT[i] - vec[i].tm1);
    RS[i] = abs(RS[i] - vec[i].tm1);
    RT[i] = abs(RT[i] - vec[i].tm1);
    ll val = min(max(LS[i], LT[i]), max(RS[i], RT[i]));
    val = min(val, 2 * LT[i] + RS[i]);
    val = min(val, 2 * RS[i] + LT[i]);
    val = min(val, 2 * LS[i] + RT[i]);
    val = min(val, 2 * RT[i] + LS[i]);
    ans[vec[i].tm3] = val;
  }
  FER(i, 0, q) cout << ans[i] << endl;

  return 0;
}


Fixed submission:
// dile a la jardinera que traigo flores

#include <bits/stdc++.h>
#define ff first
#define ss second
#define tm1 first.first
#define tm2 first.second
#define tm3 second
#define pb push_back
#define sz(x) int(x.size())
#define all(v) (v).begin(), (v).end()
#define trace(x) cout << #x << " = " << x << endl
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)
#define FER(i, a, b) for (int i = int(a); i < int(b); ++i)
#define IFR(i, a, b) for (int i = int(a); i >= int(b); i--)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> ii;
typedef pair<ii, ll> tri;

const int N = 4e5 + 20;
const ll inf = 1e16;
ll mm[N], ans[N];
vector<tri> vec;
ll LS[N], LT[N], RS[N], RT[N];

int main() {
  fastio;
  int s, t, q;
  cin >> s >> t >> q;
  vec.pb({{-inf, 1}, -1});
  vec.pb({{-inf, 2}, -1});
  vec.pb({{inf, 1}, -1});
  vec.pb({{inf, 1}, -1});
  vec.pb({{inf, 2}, -1});
  FER(i, 0, s) {
    ll k;
    cin >> k;
    vec.pb({{k, 1}, -1});
  }
  FER(i, 0, t) {
    ll k;
    cin >> k;
    vec.pb({{k, 2}, -1});
  }
  FER(i, 0, q) {
    ll k;
    cin >> k;
    vec.pb({{k, 3}, i});
  }
  sort(all(vec));
  ii esta = {-2 * inf, -2 * inf};
  FER(i, 0, sz(vec)) {
    LS[i] = esta.ff;
    LT[i] = esta.ss;
    if (vec[i].tm2 == 1)
      esta.ff = LS[i] = vec[i].tm1;
    if (vec[i].tm2 == 2)
      esta.ss = LT[i] = vec[i].tm1;
  }
  esta = {inf, inf};
  IFR(i, sz(vec) - 1, 0) {
    RS[i] = esta.ff;
    RT[i] = esta.ss;
    if (vec[i].tm2 == 1)
      esta.ff = RS[i] = vec[i].tm1;
    if (vec[i].tm2 == 2)
      esta.ss = RT[i] = vec[i].tm1;
  }
  FER(i, 2, sz(vec) - 2) if (vec[i].tm3 != -1) {
    LS[i] = abs(LS[i] - vec[i].tm1);
    LT[i] = abs(LT[i] - vec[i].tm1);
    RS[i] = abs(RS[i] - vec[i].tm1);
    RT[i] = abs(RT[i] - vec[i].tm1);
    ll val = min(max(LS[i], LT[i]), max(RS[i], RT[i]));
    val = min(val, 2 * LT[i] + RS[i]);
    val = min(val, 2 * RS[i] + LT[i]);
    val = min(val, 2 * LS[i] + RT[i]);
    val = min(val, 2 * RT[i] + LS[i]);
    ans[vec[i].tm3] = val;
  }
  FER(i, 0, q) cout << ans[i] << endl;

  return 0;
}