Problem p03703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>

using namespace std;

const int maxn = 2e5 + 1e2;
const int maxs = 6e6 + 1e2;

#define LL long long

int a[maxn], root[maxn];
LL sum[maxn], num[maxn];

struct node {
  int l, r, sum;
} tr[maxs];

int cnt = 0;

int getint() {
  char ch;
  int b = 1;
  for (ch = getchar(); ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      b = -1;
  int num = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    num = num * 10 + ch - '0';
  return num * b;
}

LL v;
void insert(int l, int r, int &now) {
  tr[++cnt] = tr[now];
  now = cnt;
  tr[now].sum++;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  if (v <= num[mid])
    insert(l, mid, tr[now].l);
  else
    insert(mid + 1, r, tr[now].r);
}

LL query(int l, int r, int now1, int now2) {
  if (num[l] >= v)
    return tr[now1].sum - tr[now2].sum;
  int mid = (l + r) >> 1;
  if (v <= num[mid])
    return query(l, mid, tr[now1].l, tr[now2].l) +
           query(mid + 1, r, tr[now1].r, tr[now2].r);
  return query(mid + 1, r, tr[now1].r, tr[now2].r);
}

int main() {
  int n = getint(), k = getint();
  for (int i = 1; i <= n; i++)
    a[i] = getint();
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + a[i];
  for (int i = 1; i <= n; i++)
    sum[i] -= (LL)k * i;
  for (int i = 1; i <= n; i++)
    num[i] = sum[i];
  sort(num + 1, num + n + 1);
  int m = unique(num + 1, num + n + 1) - num - 1;
  for (int i = 1; i <= n; i++) {
    root[i] = root[i - 1];
    v = sum[i];
    insert(1, m, root[i]);
  }
  LL now = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    v = now;
    ans += query(1, m, root[n], root[i - 1]);
    now += a[i] - k;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>

using namespace std;

const int maxn = 2e5 + 1e2;
const int maxs = 6e6 + 1e2;

#define LL long long

int a[maxn], root[maxn];
LL sum[maxn], num[maxn];

struct node {
  int l, r, sum;
} tr[maxs];

int cnt = 0;

int getint() {
  char ch;
  int b = 1;
  for (ch = getchar(); ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      b = -1;
  int num = ch - '0';
  for (ch = getchar(); ch >= '0' && ch <= '9'; ch = getchar())
    num = num * 10 + ch - '0';
  return num * b;
}

LL v;
void insert(int l, int r, int &now) {
  tr[++cnt] = tr[now];
  now = cnt;
  tr[now].sum++;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  if (v <= num[mid])
    insert(l, mid, tr[now].l);
  else
    insert(mid + 1, r, tr[now].r);
}

LL query(int l, int r, int now1, int now2) {
  if (num[l] >= v)
    return tr[now1].sum - tr[now2].sum;
  int mid = (l + r) >> 1;
  if (v <= num[mid])
    return query(l, mid, tr[now1].l, tr[now2].l) +
           query(mid + 1, r, tr[now1].r, tr[now2].r);
  return query(mid + 1, r, tr[now1].r, tr[now2].r);
}

int main() {
  int n = getint(), k = getint();
  for (int i = 1; i <= n; i++)
    a[i] = getint();
  for (int i = 1; i <= n; i++)
    sum[i] = sum[i - 1] + a[i];
  for (int i = 1; i <= n; i++)
    sum[i] -= (LL)k * i;
  for (int i = 1; i <= n; i++)
    num[i] = sum[i];
  sort(num + 1, num + n + 1);
  int m = unique(num + 1, num + n + 1) - num - 1;
  for (int i = 1; i <= n; i++) {
    root[i] = root[i - 1];
    v = sum[i];
    insert(1, m, root[i]);
  }
  LL now = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    v = now;
    if (v <= num[m])
      ans += query(1, m, root[n], root[i - 1]);
    now += a[i] - k;
  }
  printf("%lld\n", ans);
  return 0;
}