Problem p03158 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T)                                                        \
  for_each(begin(A), end(A), [&](T i) { cout << i << " "; });                  \
  cout << "\n"

typedef long long lint;
typedef pair<lint, lint> P;

bool lessPair(const P &l, const P &r) { return l.second < r.second; }
bool lessindex(const P &l, const P &r) { return l.first < r.first; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lint N;
  cin >> N;
  lint Q;
  cin >> Q;

  vector<lint> A(N);

  REP(i, N) {
    lint a;
    cin >> a;
    A[i] = a;
  }

  vector<P> X(Q);
  vector<lint> res(Q);

  REP(i, Q) {
    lint a;
    cin >> a;
    X[i] = P(i, a);
  }

  sort(X.begin(), X.end(), lessPair);

  lint tmp = 0;

  FOR(i, N / 2, N) { tmp += A[i]; }

  lint i1, i2;
  if (N % 2 == 1) {
    i1 = 0;
    i2 = N - 2 - (N - 3) / 2;
  } else {
    i1 = 1;
    i2 = N - 2 - (N - 4) / 2;
  }

  REP(i, Q) {
    lint x = X[i].second;
    while (x > (A[i1] + A[i2]) / 2) {
      tmp -= A[i2];
      tmp += A[i1];
      i1 += 2;
      i2 += 1;
    }
    //      cout << "x " << x << "\n";
    //      cout << i1 << " " << i2 << "\n";

    res[X[i].first] = tmp;
  }

  REP(i, Q) { cout << res[i] << "\n"; }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip> // << fixed << precision(10)
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;

#define FOR(i, s, t) for (int i = s; i < t; i++)
#define REP(i, n) FOR(i, 0, n)
#define PRINT(s) cout << s << "\n"
#define SORT(A) sort(A.begin(), A.end())
#define EACHPRINT(A, T)                                                        \
  for_each(begin(A), end(A), [&](T i) { cout << i << " "; });                  \
  cout << "\n"

typedef long long lint;
typedef pair<lint, lint> P;

bool lessPair(const P &l, const P &r) { return l.second < r.second; }
bool lessindex(const P &l, const P &r) { return l.first < r.first; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  lint N;
  cin >> N;
  lint Q;
  cin >> Q;

  vector<lint> A(N);

  REP(i, N) {
    lint a;
    cin >> a;
    A[i] = a;
  }

  vector<P> X(Q);
  vector<lint> res(Q);

  REP(i, Q) {
    lint a;
    cin >> a;
    X[i] = P(i, a);
  }

  sort(X.begin(), X.end(), lessPair);

  lint tmp = 0;

  FOR(i, N / 2, N) { tmp += A[i]; }

  lint i1, i2;
  if (N % 2 == 1) {
    i1 = 0;
    i2 = N - 2 - (N - 3) / 2;
  } else {
    i1 = 1;
    i2 = N - 2 - (N - 4) / 2;
  }

  REP(i, Q) {
    lint x = X[i].second;
    while (x > (A[i1] + A[i2]) / 2 && i1 != i2) {
      tmp -= A[i2];
      tmp += A[i1];
      i1 += 2;
      i2 += 1;
    }
    //      cout << "x " << x << "\n";
    //      cout << i1 << " " << i2 << "\n";

    res[X[i].first] = tmp;
  }

  REP(i, Q) { cout << res[i] << "\n"; }

  return 0;
}