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

#include <bits/stdc++.h>

#define whole(xs) xs.begin(), xs.end()

using namespace std;

namespace {

typedef double real;
typedef long long ll;

const ll INF = LLONG_MAX / 2LL;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

struct P {
  int index;
  ll a;
  int d;
  P() {}
  P(int index, ll a, int d) : index(index), a(a), d(d) {}
};
bool operator<(const P &a, const P &b) { return a.a < b.a; }

ll N, T, Q;
vector<P> X;
vector<int> I;
void input() {
  cin >> N >> T >> Q;
  X.clear();
  X.resize(N);
  for (int i = 0; i < N; i++) {
    X[i].index = i;
    cin >> X[i].a >> X[i].d;
  }
  I.clear();
  I.resize(Q);
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    q--;
    I[i] = q;
  }
}

void solve() {
  sort(whole(X));
  vector<ll> stop(N);
  vector<P> es;
  ll p = -INF;
  for (int i = 0; i < N; i++) {
    const P &a = X[i];
    if (a.d == 1) {
      es.push_back(a);
    } else {
      if (es.empty()) {
        stop[a.index] = p;
      } else {
        ll s = (es.back().a + a.a) / 2;
        stop[a.index] = s;
        p = s;
        for (auto e : es) {
          stop[e.index] = s;
        }
        es.clear();
      }
    }
  }
  for (auto e : es) {
    stop[e.index] = INF;
  }

  for (int q = 0; q < Q; q++) {
    int i = I[q];
    if (X[i].d == 1) {
      cout << min(stop[i], X[i].a + T) << endl;
    } else {
      cout << max(stop[i], X[i].a - T) << endl;
    }
  }
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define whole(xs) xs.begin(), xs.end()

using namespace std;

namespace {

typedef double real;
typedef long long ll;

const ll INF = LLONG_MAX / 2LL;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vs) {
  if (vs.empty())
    return os << "[]";
  os << "[" << vs[0];
  for (int i = 1; i < vs.size(); i++)
    os << " " << vs[i];
  return os << "]";
}
template <class T> istream &operator>>(istream &is, vector<T> &vs) {
  for (auto it = vs.begin(); it != vs.end(); it++)
    is >> *it;
  return is;
}

struct P {
  int index;
  int a, d;
  P() {}
  P(int index, int a, int d) : index(index), a(a), d(d) {}
};
bool operator<(const P &a, const P &b) { return a.a < b.a; }

ll N, T, Q;
vector<P> X;
vector<int> I;
void input() {
  cin >> N >> T >> Q;
  X.clear();
  X.resize(N);
  for (int i = 0; i < N; i++) {
    X[i].index = i;
    cin >> X[i].a >> X[i].d;
  }
  I.clear();
  I.resize(Q);
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    q--;
    I[i] = q;
  }
}

void solve() {
  sort(whole(X));
  vector<ll> stop(N);
  vector<P> es;
  ll p = -INF;
  for (int i = 0; i < N; i++) {
    const P &a = X[i];
    if (a.d == 1) {
      es.push_back(a);
    } else {
      if (es.empty()) {
        stop[a.index] = p;
      } else {
        ll s = (es.back().a + a.a) / 2;
        stop[a.index] = s;
        p = s;
        for (const auto &e : es) {
          stop[e.index] = s;
        }
        es.clear();
      }
    }
  }
  for (auto e : es) {
    stop[e.index] = INF;
  }

  for (int q = 0; q < Q; q++) {
    int i = I[q];
    if (X[i].d == 1) {
      cout << min(stop[i], X[i].a + T) << endl;
    } else {
      cout << max(stop[i], X[i].a - T) << endl;
    }
  }
}
} // namespace

int main() {
  input();
  solve();
  return 0;
}