Problem p03703 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n;
  long long int a, s[200001], k;
  s[0] = 0;
  scanf("%d %lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a);
    a = a - k;
    s[i] = s[i - 1] + a;
  }
  queue<long long int> s1[200001];
  for (int i = 0; i <= n; i++) {
    s1[i].push(s[i]);
  }
  long long int ans = 0;
  int n1 = n;
  queue<long long int> s2[200001];
  while (n1 != 1) {

    for (int i = 0; i < (n1 + 1) / 2; i++) {
      while (!s1[2 * i].empty() || !s1[2 * i + 1].empty()) {
        if (s1[2 * i].empty()) {
          while (!s1[2 * i + 1].empty()) {
            s2[i].push(s1[2 * i + 1].front());
            s1[2 * i + 1].pop();
          }
        } else if (s1[2 * i + 1].empty()) {
          while (!s1[2 * i].empty()) {
            s2[i].push(s1[2 * i].front());
            s1[2 * i].pop();
          }
        } else {
          long long int x = s1[2 * i].front(), y = s1[2 * i + 1].front();
          if (x <= y) {
            s2[i].push(x);
            s1[2 * i].pop();
          } else {
            s2[i].push(y);
            ans = ans + s1[2 * i].size();
            s1[2 * i + 1].pop();
          }
        }
      }
    }
    n1 = (n1 + 1) / 2;
    for (int i = 0; i < n1; i++) {
      while (!s2[i].empty()) {
        s1[i].push(s2[i].front());
        s2[i].pop();
      }
    }
  }
  long long int N = (long long int)n;
  ans = N * (N + 1) / 2 - ans;
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

int main() {
  int n;
  long long int a, s[200001], k;
  s[0] = 0;
  scanf("%d %lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a);
    a = a - k;
    s[i] = s[i - 1] + a;
  }
  queue<long long int> s1[200001];
  for (int i = 0; i <= n; i++) {
    s1[i].push(s[i]);
  }
  long long int ans = 0;
  int n1 = n + 1;
  queue<long long int> s2[100001];
  while (n1 != 1) {

    for (int i = 0; i < (n1 + 1) / 2; i++) {
      while (!s1[2 * i].empty() || !s1[2 * i + 1].empty()) {
        if (s1[2 * i].empty()) {
          while (!s1[2 * i + 1].empty()) {
            s2[i].push(s1[2 * i + 1].front());
            s1[2 * i + 1].pop();
          }
        } else if (s1[2 * i + 1].empty()) {
          while (!s1[2 * i].empty()) {
            s2[i].push(s1[2 * i].front());
            s1[2 * i].pop();
          }
        } else {
          long long int x = s1[2 * i].front(), y = s1[2 * i + 1].front();
          if (x <= y) {
            s2[i].push(x);
            s1[2 * i].pop();
          } else {
            s2[i].push(y);
            ans = ans + s1[2 * i].size();
            s1[2 * i + 1].pop();
          }
        }
      }
    }
    n1 = (n1 + 1) / 2;
    for (int i = 0; i < n1; i++) {
      while (!s2[i].empty()) {
        s1[i].push(s2[i].front());
        s2[i].pop();
      }
    }
  }
  long long int N = (long long int)n;
  ans = N * (N + 1) / 2 - ans;
  printf("%lld\n", ans);
  return 0;
}