Problem p03158 (C++)

Buggy submission (status: Runtime Error):
// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct P {
  int q, i, ans;
  P() : P(0, 0, 0) {}
  P(int q, int i, int ans) : q(q), i(i), ans(ans) {}
};

int A[100010];
P Q[100010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, q;
  int s = 0;
  cin >> N >> M;
  rep(i, 0, N) {
    cin >> A[i];
    if (N / 2 <= i) {
      s += A[i];
    }
  }
  rep(i, 0, M) {
    cin >> q;
    Q[i] = P(q, i, 0);
  }

  sort(Q, Q + M, [](const P &a, const P &b) { return a.q < b.q; });

  int l, r;
  if (N % 2 == 1) {
    l = 0;
    r = N / 2;
  } else {
    l = 1;
    r = N / 2;
  }

  int qi = 0;
  while (l < r) {
    int x = (A[r] + A[l]) / 2;
    while (Q[qi].q <= x) {
      Q[qi].ans = s;
      qi++;
    }
    s -= A[r] - A[l];
    l += 2;
    r++;
  }
  while (qi < M) {
    Q[qi].ans = s;
    qi++;
  }

  sort(Q, Q + M, [](const P &a, const P &b) { return a.i < b.i; });

  rep(i, 0, M) { cout << Q[i].ans << endl; }

  return 0;
}

Fixed submission:
// #include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_len = (b); i < i##_len; i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

// typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector<vector<int>> mat;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;

struct P {
  int q, i, ans;
  P() : P(0, 0, 0) {}
  P(int q, int i, int ans) : q(q), i(i), ans(ans) {}
};

int A[100010];
P Q[100010];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, q;
  int s = 0;
  cin >> N >> M;
  rep(i, 0, N) {
    cin >> A[i];
    if (N / 2 <= i) {
      s += A[i];
    }
  }
  rep(i, 0, M) {
    cin >> q;
    Q[i] = P(q, i, 0);
  }

  sort(Q, Q + M, [](const P &a, const P &b) { return a.q < b.q; });

  int l, r;
  if (N % 2 == 1) {
    l = 0;
    r = N / 2;
  } else {
    l = 1;
    r = N / 2;
  }

  int qi = 0;
  while (l < r) {
    int x = (A[r] + A[l]) / 2;
    while (qi < M && Q[qi].q <= x) {
      Q[qi].ans = s;
      qi++;
    }
    s -= A[r] - A[l];
    l += 2;
    r++;
  }
  while (qi < M) {
    Q[qi].ans = s;
    qi++;
  }

  sort(Q, Q + M, [](const P &a, const P &b) { return a.i < b.i; });

  rep(i, 0, M) { cout << Q[i].ans << endl; }

  return 0;
}