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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = ll(A); I < ll(B); ++I)
#define FORR(I, A, B) for (ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x, t, f) ((x) ? (t) : (f))
#define SORT(x) (sort(x.begin(), x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x, v)                                                             \\
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // xi>=v  x is sorted
#define POSU(x, v)                                                             \\
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // xi>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
#define REV(x) (reverse(x.begin(), x.end()))       // reverse
ll gcd(ll a, ll b) {
  if (a % b == 0)
    return b;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll c = gcd(a, b);
  return ((a / c) * (b / c) * c);
}
#define NEXTP(x) next_permutation(x.begin(), x.end())
const ll INF = ll(1e18) + ll(7);
const ll MOD = 1000000007LL;
#define out(a) cout << fixed << setprecision((a))
#define YesNo(a) cout << ((a) ? "Yes" : "No") << endl;
#define YESNO(a) cout << ((a) ? "YES" : "NO") << endl;

int main() {
  ll ans = 0, N, K, L, R;
  cin >> N >> K >> L >> R;
  ll x = N / 2;
  ll y = N - x;
  vector<ll> S1(x), S2(y), A[x + 1], B[y + 1];
  FOR(i, 0, x) cin >> S1[i];
  FOR(i, 0, y) cin >> S2[i];

  A[0].push_back(0);
  B[0].push_back(0);

  FOR(i, 0, x) {
    FORR(j, 0, x) {
      for (auto a : A[j]) {
        A[j + 1].push_back(S1[i] + a);
      }
    }
  }
  FOR(i, 0, y) {
    FORR(j, 0, y) {
      for (auto b : B[j]) {
        B[j + 1].push_back(S2[i] + b);
      }
    }
  }

  FOR(i, 1, x + 1) SORT(A[i]);
  FOR(i, 1, y + 1) SORT(B[i]);

  FOR(i, 0, min(x, K) + 1) {
    if (K - i > y)
      continue;
    for (auto a : A[i]) {
      ll l = POSL(B[K - i], L - a);
      ll r = POSU(B[K - i], R - a) - 1;
      ans += r - l + 1;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll, ll>
#define FOR(I, A, B) for (ll I = (A); I < (B); ++I)
#define FORR(I, A, B) for (ll I = (B - 1); I >= (A); --I)
#define POSL(x, v)                                                             \\
  (lower_bound(x.begin(), x.end(), v) - x.begin()) // ai>=v  x is sorted
#define POSU(x, v)                                                             \\
  (upper_bound(x.begin(), x.end(), v) - x.begin()) // ai>v  x is sorted
#define NUM(x, v) (POSU(x, v) - POSL(x, v))        // x is sorted
const ll INF = 1e18 + 7;
const ll MOD = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll N, K, L, R;
  cin >> N >> K >> L >> R;
  ll Na = N / 2;
  ll Nb = N - Na;
  vector<ll> a(Na), b(Nb);
  FOR(i, 0, Na) cin >> a[i];
  FOR(i, 0, Nb) cin >> b[i];
  vector<ll> A[Na + 1], B[Nb + 1];
  A[0].push_back(0);
  B[0].push_back(0);

  FOR(i, 0, Na) {
    FORR(j, 1, i + 2) {
      for (ll x : A[j - 1])
        A[j].push_back(a[i] + x);
    }
  }
  FOR(i, 0, Nb) {
    FORR(j, 1, i + 2) {
      for (ll x : B[j - 1])
        B[j].push_back(b[i] + x);
    }
  }
  FOR(i, 1, Na + 1) sort(A[i].begin(), A[i].end());
  FOR(i, 1, Nb + 1) sort(B[i].begin(), B[i].end());
  ll ans = 0;
  FOR(i, 0, Na + 1) {
    for (ll x : A[i]) {
      ll num;
      if (K >= i && (K - i <= Nb))
        num = POSU(B[K - i], R - x) - POSL(B[K - i], L - x);
      else
        num = 0;
      ans += num;
    }
  }
  cout << ans << endl;
}