prithivMLmods/Coder-Stat huggingface dataset:
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
ll k;
ll a[100001];
ll dat[1 << 20];
int n_ = 1 << 18;
ll sum[100001];
vector<ll> va;

void update(int k, int a) {
  k += n_ - 1;
  dat[k] += a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
  }
}

ll query(int a, int b, int k = 0, int l = 0, int r = n_) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl + vr;
}

int main(void) {
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= k;
    sum[i + 1] = sum[i] + a[i];
  }
  for (int i = 1; i <= n; i++) {
    va.push_back(sum[i]);
  }
  sort(va.begin(), va.end());
  va.erase(unique(va.begin(), va.end()), va.end());
  for (int i = 1; i <= n; i++) {
    sum[i] = lower_bound(va.begin(), va.end(), sum[i]) - va.begin();
    update(sum[i], 1);
  }
  ll res = 0;
  ll now = 0;
  for (int i = 0; i < n; i++) {
    int le = lower_bound(va.begin(), va.end(), now) - va.begin();
    res += query(le, va.size());
    now += a[i];
    int mn = lower_bound(va.begin(), va.end(), now) - va.begin();
    update(mn, -1);
  }
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
ll k;
ll a[200001];
ll dat[1 << 21];
int n_ = 1 << 19;
ll sum[200001];
vector<ll> va;

void update(int k, int a) {
  k += n_ - 1;
  dat[k] += a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
  }
}

ll query(int a, int b, int k = 0, int l = 0, int r = n_) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  ll vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  ll vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl + vr;
}

int main(void) {
  scanf("%d%lld", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= k;
    sum[i + 1] = sum[i] + a[i];
  }
  for (int i = 1; i <= n; i++) {
    va.push_back(sum[i]);
  }
  sort(va.begin(), va.end());
  va.erase(unique(va.begin(), va.end()), va.end());
  for (int i = 1; i <= n; i++) {
    sum[i] = lower_bound(va.begin(), va.end(), sum[i]) - va.begin();
    update(sum[i], 1);
  }
  ll res = 0;
  ll now = 0;
  for (int i = 0; i < n; i++) {
    int le = lower_bound(va.begin(), va.end(), now) - va.begin();
    res += query(le, va.size());
    now += a[i];
    int mn = lower_bound(va.begin(), va.end(), now) - va.begin();
    update(mn, -1);
  }
  printf("%lld\n", res);
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define maxn 100010
#define foru(i, l, r) for (int i = (l); i <= (r); ++i)
#define ford(i, l, r) for (int i = (l); i >= (r); --i)

int n;
ll k;
ll pre[maxn];
int fw[maxn];

void updatefw(int i) {
  for (; i < maxn; i += i & -i)
    fw[i]++;
}

int getfw(int i) {
  int ret = 0;
  for (; i > 0; i -= i & -i)
    ret = ret + fw[i];
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /// PERSUBARR
  // freopen("PERSUBARR.inp", "r", stdin); freopen("PERSUBARR.out", "w",
  // stdout);
  cin >> n >> k;
  ll ans = 0;
  vector<ll> com;
  com.push_back(0);
  for (int i = 1; i <= n; ++i) {
    ll x;
    cin >> x;
    x = x - k;
    pre[i] = pre[i - 1] + x;
    com.push_back(pre[i]);
  }
  sort(com.begin(), com.end());

  for (int i = 0; i <= n; ++i) {
    pre[i] = lower_bound(com.begin(), com.end(), pre[i]) - com.begin() + 1;
  }
  updatefw(pre[0]);
  for (int i = 1; i <= n; ++i) {
    ans = ans + getfw(pre[i]);
    updatefw(pre[i]);
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define maxn 200010
#define foru(i, l, r) for (int i = (l); i <= (r); ++i)
#define ford(i, l, r) for (int i = (l); i >= (r); --i)

int n;
ll k;
ll pre[maxn];
int fw[maxn];

void updatefw(int i) {
  for (; i < maxn; i += i & -i)
    fw[i]++;
}

int getfw(int i) {
  int ret = 0;
  for (; i > 0; i -= i & -i)
    ret = ret + fw[i];
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  /// PERSUBARR
  // freopen("PERSUBARR.inp", "r", stdin); freopen("PERSUBARR.out", "w",
  // stdout);
  cin >> n >> k;
  ll ans = 0;
  vector<ll> com;
  com.push_back(0);
  for (int i = 1; i <= n; ++i) {
    ll x;
    cin >> x;
    x = x - k;
    pre[i] = pre[i - 1] + x;
    com.push_back(pre[i]);
  }
  sort(com.begin(), com.end());

  for (int i = 0; i <= n; ++i) {
    pre[i] = lower_bound(com.begin(), com.end(), pre[i]) - com.begin() + 1;
  }
  updatefw(pre[0]);
  for (int i = 1; i <= n; ++i) {
    ans = ans + getfw(pre[i]);
    updatefw(pre[i]);
  }
  cout << ans;
  return 0;
}
Problem p03703 (C++)

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

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

// Binary Indexed Tree (1 indexed)
struct BIT {
  vector<int> data;
  BIT(int n) : data(n + 1, 0) {}
  int sum(int k) {
    int ret = 0;
    while (k > 0) {
      ret += data[k];
      k -= k & -k;
    }
    return ret;
  }
  void add(int k, int x) {
    while (k < (int)data.size()) {
      data[k] += x;
      k += k & -k;
    }
  }
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N, K;
  cin >> N >> K;
  vint a(N);
  rep(i, N) cin >> a[i];
  vint sum(N);
  rep(i, N) sum[i + 1] = sum[i] + a[i];
  vint press;
  rep(i, N + 1) {
    sum[i] -= i * K;
    press.push_back(sum[i]);
  }
  sort(all(press));
  press.erase(unique(all(press)), press.end());
  BIT bit(N + 1);
  int ans = 0;
  rep(i, N + 1) {
    sum[i] = lower_bound(all(press), sum[i]) - press.begin();
    ans += bit.sum(sum[i] + 1);
    bit.add(sum[i] + 1, 1);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Tapris = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

// Binary Indexed Tree (1 indexed)
struct BIT {
  vector<int> data;
  BIT(int n) : data(n + 1, 0) {}
  int sum(int k) {
    int ret = 0;
    while (k > 0) {
      ret += data[k];
      k -= k & -k;
    }
    return ret;
  }
  void add(int k, int x) {
    while (k < (int)data.size()) {
      data[k] += x;
      k += k & -k;
    }
  }
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N, K;
  cin >> N >> K;
  vint a(N);
  rep(i, N) cin >> a[i];
  vint sum(N + 1);
  rep(i, N) sum[i + 1] = sum[i] + a[i];
  vint press;
  rep(i, N + 1) {
    sum[i] -= i * K;
    press.push_back(sum[i]);
  }
  sort(all(press));
  press.erase(unique(all(press)), press.end());
  BIT bit(N + 1);
  int ans = 0;
  rep(i, N + 1) {
    sum[i] = lower_bound(all(press), sum[i]) - press.begin();
    ans += bit.sum(sum[i] + 1);
    bit.add(sum[i] + 1, 1);
  }
  cout << ans << endl;

  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

struct BIT {
  int N;
  vector<int> data;
  explicit BIT(int n) : N(n + 1), data(n + 1) {}
  void add(int i, int v) {
    for (int x = i; x < N; x += x & -x) {
      data[x] += v;
    }
  }
  int get(int i) {
    int ret = 0;
    for (int x = i; x > 0; x -= x & -x) {
      ret += data[x];
    }
    return ret;
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= K;
  }
  vector<ll> acc(N + 1);
  for (int i = 0; i < N; i++) {
    acc[i + 1] = acc[i] + A[i];
  }
  vector<ll> acc_ = acc;
  sort(acc_.begin(), acc_.end());

  BIT bit(N + 1);
  ll ans = 0;
  for (int i = 0; i < N + 1; i++) {
    int k = lower_bound(acc_.begin(), acc_.end(), acc[i]) - acc.begin() + 1;
    ans += bit.get(k);
    bit.add(k, 1);
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;

struct BIT {
  int N;
  vector<int> data;
  explicit BIT(int n) : N(n + 1), data(n + 1) {}
  void add(int i, int v) {
    for (int x = i; x < N; x += x & -x) {
      data[x] += v;
    }
  }
  int get(int i) {
    int ret = 0;
    for (int x = i; x > 0; x -= x & -x) {
      ret += data[x];
    }
    return ret;
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i] -= K;
  }
  vector<ll> acc(N + 1);
  for (int i = 0; i < N; i++) {
    acc[i + 1] = acc[i] + A[i];
  }
  vector<ll> acc_ = acc;
  sort(acc_.begin(), acc_.end());

  BIT bit(N + 1);
  ll ans = 0;
  for (int i = 0; i < N + 1; i++) {
    int k = lower_bound(acc_.begin(), acc_.end(), acc[i]) - acc_.begin() + 1;
    ans += bit.get(k);
    bit.add(k, 1);
  }
  cout << ans << endl;

  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL N, K, sum[300040], tmp[300050], b[300050], tree[300050], a[300050];
int cnt;
void insert(int x) {
  for (; x <= cnt; x += -x & x) {
    tree[x]++;
  }
}
LL ask(int x) {
  LL ans = 0;
  for (; x > 0; x -= -x & x) {
    ans += tree[x];
  }
  return ans;
}
int find(int x) {
  int l = 1, r = cnt;
  while (l <= r) {
    int mid = (l + r) >> 1;

    if (b[mid] == x) {
      return mid;
    }
    if (b[mid] > x)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return -1;
}
int main() {
  //	freopen("game16.in", "r", stdin);
  cin >> N >> K;
  LL ans = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i] - K;
    if (sum[i] >= 0)
      ans++;
    tmp[i] = sum[i];
  }
  sort(tmp + 1, tmp + N + 1);
  b[++cnt] = tmp[1];
  for (int i = 2; i <= N; i++) {
    if (tmp[i] == tmp[i - 1])
      continue;
    b[++cnt] = tmp[i];
  }
  for (int i = 1; i <= N; i++) {
    //		printf("yuan=%d\n", sum[i]);
    sum[i] = find(sum[i]);
    //		printf("sum[%d]=%lld\n", i, sum[i]);
  }
  for (int i = 1; i <= N; i++) {
    ans += ask(sum[i]);
    insert(sum[i]);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define LL long long
LL N, K, sum[300040], tmp[300050], b[300050], tree[300050], a[300050];
int cnt;
void insert(int x) {
  for (; x <= cnt; x += -x & x) {
    tree[x]++;
  }
}
LL ask(int x) {
  LL ans = 0;
  for (; x > 0; x -= -x & x) {
    ans += tree[x];
  }
  return ans;
}
int find(LL x) {
  int l = 1, r = cnt;
  while (l <= r) {
    int mid = (l + r) >> 1;

    if (b[mid] == x) {
      return mid;
    }
    if (b[mid] > x)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return -1;
}
int main() {
  //	freopen("game16.in", "r", stdin);
  cin >> N >> K;
  LL ans = 0;
  for (int i = 1; i <= N; i++) {
    scanf("%lld", &a[i]);
    sum[i] = sum[i - 1] + a[i] - K;
    if (sum[i] >= 0)
      ans++;
    tmp[i] = sum[i];
  }
  sort(tmp + 1, tmp + N + 1);
  b[++cnt] = tmp[1];
  for (int i = 2; i <= N; i++) {
    if (tmp[i] == tmp[i - 1])
      continue;
    b[++cnt] = tmp[i];
  }
  for (int i = 1; i <= N; i++) {
    //		printf("yuan=%d\n", sum[i]);
    sum[i] = find(sum[i]);
    //		printf("sum[%d]=%lld\n", i, sum[i]);
  }
  for (int i = 1; i <= N; i++) {
    ans += ask(sum[i]);
    insert(sum[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 9;
int arr[maxn];
ll sum[maxn];
ll sumt[maxn];
int n, k;
bool cmp(ll a, ll b) { return a > b; }
ll tmpl[maxn], tmpr[maxn];
ll sol(int l, int r) {
  if (l == r) {
    return arr[l] >= 0;
  }
  int mid = (l + r) >> 1;
  ll ans = sol(l, mid);
  ans += sol(mid + 1, r);
  int nl = 0, nr = 0;
  for (int i = l; i <= mid; i++) {
    tmpl[nl++] = sumt[i] - sumt[mid + 1];
    /*if(l==1&&r==4){
        cout<<'?'<<tmpl[nl-1]<<endl;
    }*/
  }
  for (int i = mid + 1; i <= r; i++) {
    tmpr[nr++] = sum[i] - sum[mid];
  }
  sort(tmpl, tmpl + nl);
  sort(tmpr, tmpr + nr, cmp);
  int no = 0;
  for (int i = 0; i < nl; i++) {
    for (; no < nr; no++) {
      if (tmpl[i] + tmpr[no] < 0)
        break;
    }
    ans += no;
  }
  // cout<<l<<' '<<r<<' '<<ans<<endl;
  return ans;
}
int main() {
  // freopen("in","r",stdin);
  ios::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    arr[i] -= k;
    sum[i] = sum[i - 1] + arr[i];
  }
  for (int i = n; i > 0; i--) {
    sumt[i] = sumt[i + 1] + arr[i];
    // cout<<i<<'*'<<sumt[i]<<endl;
  }
  ll ans = sol(1, n);
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 9;
int arr[maxn];
ll sum[maxn];
ll sumt[maxn];
int n, k;
bool cmp(ll a, ll b) { return a > b; }
ll tmpl[maxn], tmpr[maxn];
ll sol(int l, int r) {
  if (l == r) {
    return arr[l] >= 0;
  }
  int mid = (l + r) >> 1;
  ll ans = sol(l, mid);
  ans += sol(mid + 1, r);
  int nl = 0, nr = 0;
  for (int i = l; i <= mid; i++) {
    tmpl[nl++] = sumt[i] - sumt[mid + 1];
    /*if(l==1&&r==4){
        cout<<'?'<<tmpl[nl-1]<<endl;
    }*/
  }
  for (int i = mid + 1; i <= r; i++) {
    tmpr[nr++] = sum[i] - sum[mid];
  }
  sort(tmpl, tmpl + nl);
  sort(tmpr, tmpr + nr, cmp);
  int no = 0;
  for (int i = 0; i < nl; i++) {
    for (; no < nr; no++) {
      if (tmpl[i] + tmpr[no] < 0)
        break;
    }
    ans += no;
  }
  // cout<<l<<' '<<r<<' '<<ans<<endl;
  return ans;
}
int main() {
  // freopen("in","r",stdin);
  ios::sync_with_stdio(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    arr[i] -= k;
    sum[i] = sum[i - 1] + arr[i];
  }
  for (int i = n; i > 0; i--) {
    sumt[i] = sumt[i + 1] + arr[i];
    // cout<<i<<'*'<<sumt[i]<<endl;
  }
  ll ans = sol(1, n);
  cout << ans << endl;
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace __gnu_pbds;
using namespace std;

typedef tree<pair<long long, int>, null_type, less<pair<long long, int>>,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

long long a[100010];
const int inf = 1e9;

int main(int argc, char const *argv[]) {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= k;
  }
  for (int i = 1; i <= n; i++) {
    a[i] += a[i - 1];
  }
  ordered_set t;
  t.insert(make_pair(0, 0));
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += t.order_of_key(make_pair(a[i], inf));
    t.insert(make_pair(a[i], i));
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace __gnu_pbds;
using namespace std;

typedef tree<pair<long long, int>, null_type, less<pair<long long, int>>,
             rb_tree_tag, tree_order_statistics_node_update>
    ordered_set;

long long a[200010];
const int inf = 1e9;

int main(int argc, char const *argv[]) {
  int n, k;
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= k;
  }
  for (int i = 1; i <= n; i++) {
    a[i] += a[i - 1];
  }
  ordered_set t;
  t.insert(make_pair(0, 0));
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += t.order_of_key(make_pair(a[i], inf));
    t.insert(make_pair(a[i], i));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03703 (Python)

Buggy submission (status: Runtime Error):
import sys

import numpy as np


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n, k = map(int, input().split())
aaa = np.fromiter(sys.stdin, dtype=np.int64)
aaa = np.insert(aaa, 0, 0)
acc = np.add.accumulate(aaa) - np.arange(0, k * n + 1, k)
dct = {v: i for i, v in enumerate(np.unique(acc), start=1)}
bit = Bit(n)
ans = 0
for d in acc:
    i = dct[d]
    ans += bit.sum(i)
    bit.add(i, 1)
print(ans)


Fixed submission:
import sys

import numpy as np


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


n, k = map(int, input().split())
aaa = np.fromiter(sys.stdin, dtype=np.int64)
aaa = np.insert(aaa, 0, 0)
acc = np.add.accumulate(aaa) - np.arange(0, k * n + 1, k)
dct = {v: i for i, v in enumerate(np.unique(acc), start=1)}
bit = Bit(len(dct))
ans = 0
for d in acc:
    i = dct[d]
    ans += bit.sum(i)
    bit.add(i, 1)
print(ans)
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;

const int M_N = 100000;

struct BIT {
  // 1-indexed
  LL bit[M_N + 1], n;

  LL sum(int i) {
    LL s = 0;
    while (i > 0) {
      s += bit[i];
      i = i & (i - 1);
    }
    return s;
  }

  void add(int i, LL x) {
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

BIT bit;
map<LL, LL> mp;

int main() {
  int N, t = 0;
  LL K;
  cin >> N >> K;
  bit.n = N;
  vector<LL> A, T;
  for (int i = 0; i < N; i++) {
    LL a;
    cin >> a;
    A.emplace_back(a - K);
  }
  LL sum = 0;
  for (int i = 0; i < N; i++) {
    sum += A[i];
    T.emplace_back(sum);
  }
  A = T;
  sort(T.begin(), T.end());
  for (int i = 0; i < N; i++) {
    if (mp[T[i]])
      continue;
    mp[T[i]] = i + 1;
  }
  LL ans = 0;
  for (int i = 0; i < N; i++) {
    bit.add(mp[A[i]], 1);
    ans += bit.sum(mp[A[i]]++) + (A[i] >= 0 ? 0 : -1);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> P;
const LL mod = 1e9 + 7;
const LL LINF = 1LL << 62;

const int M_N = 200000;

struct BIT {
  // 1-indexed
  LL bit[M_N + 1], n;

  LL sum(int i) {
    LL s = 0;
    while (i > 0) {
      s += bit[i];
      i = i & (i - 1);
    }
    return s;
  }

  void add(int i, LL x) {
    while (i <= n) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

BIT bit;
map<LL, LL> mp;

int main() {
  int N, t = 0;
  LL K;
  cin >> N >> K;
  bit.n = N;
  vector<LL> A, T;
  for (int i = 0; i < N; i++) {
    LL a;
    cin >> a;
    A.emplace_back(a - K);
  }
  LL sum = 0;
  for (int i = 0; i < N; i++) {
    sum += A[i];
    T.emplace_back(sum);
  }
  A = T;
  sort(T.begin(), T.end());
  for (int i = 0; i < N; i++) {
    if (mp[T[i]])
      continue;
    mp[T[i]] = i + 1;
  }
  LL ans = 0;
  for (int i = 0; i < N; i++) {
    bit.add(mp[A[i]], 1);
    ans += bit.sum(mp[A[i]]++) + (A[i] >= 0 ? 0 : -1);
  }
  cout << ans << endl;
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define LL long long
#define lowbit(x) (x & (-x))
#define INF 0x7FFFFFFF // or 0x3f3f3f3f ?
using namespace std;

template <class T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}

/*============ Header Template ============*/

const int N = 100000 + 5;

LL n, k, num;
LL a[N], c[N], s[N], t[N];
map<LL, LL> mp;

void upd(int x) {
  for (int i = x; i <= num; i += lowbit(i))
    t[i]++;
}

LL getsum(int x) {
  LL ans = 0;
  for (int i = x; i; i -= lowbit(i))
    ans += t[i];
  return ans;
}

int main() {
  read(n), read(k);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    s[i] = s[i - 1] + a[i];
  for (int i = 0; i <= n; i++)
    s[i] -= k * i;
  for (int i = 0; i <= n; i++)
    c[i + 1] = s[i];
  sort(c + 1, c + n + 1 + 1);
  num = unique(c + 1, c + n + 1 + 1) - (c + 1);
  for (int i = 1; i <= num; i++)
    mp[c[i]] = i;
  for (int i = 0; i <= n; i++)
    s[i] = mp[s[i]];
  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += getsum(s[i]);
    upd(s[i]);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define lowbit(x) (x & (-x))
#define INF 0x7FFFFFFF // or 0x3f3f3f3f ?
using namespace std;

template <class T> inline void read(T &x) {
  int f = 1;
  x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}

/*============ Header Template ============*/

const int N = 300000 + 5;

LL n, k, num;
LL a[N], c[N], s[N], t[N];
map<LL, LL> mp;

void upd(int x) {
  for (int i = x; i <= num; i += lowbit(i))
    t[i]++;
}

LL getsum(int x) {
  LL ans = 0;
  for (int i = x; i; i -= lowbit(i))
    ans += t[i];
  return ans;
}

int main() {
  read(n), read(k);
  for (int i = 1; i <= n; i++)
    read(a[i]);
  for (int i = 1; i <= n; i++)
    s[i] = s[i - 1] + a[i];
  for (int i = 0; i <= n; i++)
    s[i] -= k * i;
  for (int i = 0; i <= n; i++)
    c[i + 1] = s[i];
  sort(c + 1, c + n + 1 + 1);
  num = unique(c + 1, c + n + 1 + 1) - (c + 1);
  for (int i = 1; i <= num; i++)
    mp[c[i]] = i;
  for (int i = 0; i <= n; i++)
    s[i] = mp[s[i]];
  LL ans = 0;
  for (int i = 0; i <= n; i++) {
    ans += getsum(s[i]);
    upd(s[i]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define ss(x) scanf("%s", x)
#define mod 1000000007
#define bitcount __builtin_popcountll
#define ll long long
#define pb push_back
#define pi pair<ll, ll>
#define pii pair<pi, ll>
#define mp make_pair
ll a[200005], b[200005];
vector<ll> v;
ll inv;
void mergesort(ll a[], ll n) {
  if (n == 1)
    return;

  mergesort(a, n / 2);
  mergesort(a + n / 2, n - n / 2);

  ll b[100005];
  ll i = 0, j = n / 2, k = 0;
  while (i < n / 2 && j < n) {
    if (a[i] < a[j]) {
      b[k] = a[i];
      k++;
      i++;
    } else {
      b[k] = a[j];
      k++;
      j++;
      inv += n / 2 - i;
    }
  }
  while (i < n / 2) {
    b[k] = a[i];
    k++;
    i++;
  }
  while (j < n) {
    b[k] = a[j];
    k++;
    j++;
  }
  for (i = 0; i < n; i++)
    a[i] = b[i];
  /*for(i=0;i<n;i++)
          prllf("%d ",a[i] );
  prllf("\n");*/
}
int main() {
  ll i, j, k, n;
  slld(n);
  slld(k);
  for (i = 0; i < n; i++) {
    slld(a[i]);
    a[i] -= k;
  }
  for (i = 1; i < n; i++)
    a[i] += a[i - 1];
  reverse(a, a + n);
  inv = 0;
  mergesort(a, n);
  for (i = 0; i < n; i++) {
    if (a[i] >= 0)
      inv++;
  }
  printf("%lld\n", inv);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%d", &x)
#define slld(x) scanf("%lld", &x)
#define ss(x) scanf("%s", x)
#define mod 1000000007
#define bitcount __builtin_popcountll
#define ll long long
#define pb push_back
#define pi pair<ll, ll>
#define pii pair<pi, ll>
#define mp make_pair
ll a[200005], b[200005];
vector<ll> v;
ll inv;
void mergesort(ll a[], ll n) {
  if (n == 1)
    return;

  mergesort(a, n / 2);
  mergesort(a + n / 2, n - n / 2);

  ll i = 0, j = n / 2, k = 0;
  while (i < n / 2 && j < n) {
    if (a[i] < a[j]) {
      b[k] = a[i];
      k++;
      i++;
    } else {
      b[k] = a[j];
      k++;
      j++;
      inv += n / 2 - i;
    }
  }
  while (i < n / 2) {
    b[k] = a[i];
    k++;
    i++;
  }
  while (j < n) {
    b[k] = a[j];
    k++;
    j++;
  }
  for (i = 0; i < n; i++)
    a[i] = b[i];
  /*for(i=0;i<n;i++)
          prllf("%d ",a[i] );
  prllf("\n");*/
}
int main() {
  ll i, j, k, n;
  slld(n);
  slld(k);
  for (i = 0; i < n; i++) {
    slld(a[i]);
    a[i] -= k;
  }
  for (i = 1; i < n; i++)
    a[i] += a[i - 1];
  reverse(a, a + n);
  inv = 0;
  mergesort(a, n);
  for (i = 0; i < n; i++) {
    if (a[i] >= 0)
      inv++;
  }
  printf("%lld\n", inv);
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lowbit(x) ((x) & (-x))
using namespace std;
typedef long long LL;

const int maxn = 100009;
LL c[maxn], sum[maxn], x[maxn], ans;
int n, K, a, m;

int del() {
  int m = 1;
  for (int i = 2; i <= n + 1; i++)
    if (x[i] != x[i - 1])
      x[++m] = x[i];
  return m;
}

int search(LL k) {
  int l = 1, r = m, mid;
  while (l < r) {
    mid = (l + r) >> 1;
    if (k <= x[mid])
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}

void update(int x) {
  for (; x <= m; x += lowbit(x))
    c[x]++;
}

int query(int x) {
  int res = 0;
  for (; x; x -= lowbit(x))
    res += c[x];
  return res;
}

int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    sum[i] = sum[i - 1] + a - K;
    x[i] = sum[i];
  }
  sort(x + 1, x + n + 2);
  m = del();
  update(search(0));
  for (int i = 1; i <= n; i++) {
    ans += query(search(sum[i]));
    update(search(sum[i]));
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lowbit(x) ((x) & (-x))
using namespace std;
typedef long long LL;

const int maxn = 200009;
LL c[maxn], sum[maxn], x[maxn], ans;
int n, K, a, m;

int del() {
  int m = 1;
  for (int i = 2; i <= n + 1; i++)
    if (x[i] != x[i - 1])
      x[++m] = x[i];
  return m;
}

int search(LL k) {
  int l = 1, r = m, mid;
  while (l < r) {
    mid = (l + r) >> 1;
    if (k <= x[mid])
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}

void update(int x) {
  for (; x <= m; x += lowbit(x))
    c[x]++;
}

int query(int x) {
  int res = 0;
  for (; x; x -= lowbit(x))
    res += c[x];
  return res;
}

int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    sum[i] = sum[i - 1] + a - K;
    x[i] = sum[i];
  }
  sort(x + 1, x + n + 2);
  m = del();
  update(search(0));
  for (int i = 1; i <= n; i++) {
    ans += query(search(sum[i]));
    update(search(sum[i]));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using VI = vector<int>;
using VVI = vector<VI>;

int N = 1 << 3;
VI st(2 * N - 1);
int query(int a, int b, int k = 0, int l = 0, int r = N) {
  if (r <= a or b <= l)
    return 0;
  if (a <= l and r <= b)
    return st[k];
  return query(a, b, 2 * k + 1, l, (l + r) / 2) +
         query(a, b, 2 * k + 2, (l + r) / 2, r);
}
void add(int k) {
  k += N - 1;
  st[k]++;
  while (k > 0) {
    k = (k - 1) / 2;
    st[k]++;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VI a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= k;
  }
  vector<lint> c(n + 1);
  vector<pair<lint, int>> p(n + 1);
  p[0] = make_pair(c[0], 0);
  for (int i = 0; i < n; i++) {
    c[i + 1] = c[i] + a[i];
    p[i + 1] = make_pair(c[i + 1], i + 1);
  }
  sort(p.begin(), p.end());
  VI b(n + 1);
  for (int i = 0; i < n + 1; i++) {
    b[i] = p[i].second;
  }
  lint res = 0;
  add(b[0]);
  for (int i = 1; i <= n; i++) {
    res += query(0, b[i]);
    add(b[i]);
  }
  cout << res << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using VI = vector<int>;
using VVI = vector<VI>;

int N = 1 << 18;
VI st(2 * N - 1);
int query(int a, int b, int k = 0, int l = 0, int r = N) {
  if (r <= a or b <= l)
    return 0;
  if (a <= l and r <= b)
    return st[k];
  return query(a, b, 2 * k + 1, l, (l + r) / 2) +
         query(a, b, 2 * k + 2, (l + r) / 2, r);
}
void add(int k) {
  k += N - 1;
  st[k]++;
  while (k > 0) {
    k = (k - 1) / 2;
    st[k]++;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  VI a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i] -= k;
  }
  vector<lint> c(n + 1);
  vector<pair<lint, int>> p(n + 1);
  p[0] = make_pair(c[0], 0);
  for (int i = 0; i < n; i++) {
    c[i + 1] = c[i] + a[i];
    p[i + 1] = make_pair(c[i + 1], i + 1);
  }
  sort(p.begin(), p.end());
  VI b(n + 1);
  for (int i = 0; i < n + 1; i++) {
    b[i] = p[i].second;
  }
  lint res = 0;
  add(b[0]);
  for (int i = 1; i <= n; i++) {
    res += query(0, b[i]);
    add(b[i]);
  }
  cout << res << '\n';
  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
// IOI 2021
// labriz kon code ra, ham paye ie AC manam, AC age yari kone ta sob man code
// miznm :)
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 100 * 1000 + 17;

ll n, k, a[MAXN];

ll DC(int l, int r) {
  if (l + 1 == r)
    return (a[l] >= k);
  int mid = (l + r) / 2;
  ll res = DC(l, mid) + DC(mid, r);
  vector<ll> part1, part2;
  ll sum = 0;
  for (int i = mid - 1; i >= l; i--)
    sum += a[i], part1.pb(sum);
  sum = 0;
  for (int i = mid; i < r; i++)
    sum += a[i], part2.pb(k * (i - mid + 1) - sum);
  sort(part2.begin(), part2.end());
  for (int i = 0; i < part1.size(); i++) {
    int ind = upper_bound(part2.begin(), part2.end(), part1[i] - k * (i + 1)) -
              part2.begin();
    res += ind;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << DC(1, n + 1) << endl;
  return 0;
}


Fixed submission:
// IOI 2021
// labriz kon code ra, ham paye ie AC manam, AC age yari kone ta sob man code
// miznm :)
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define ff first
#define ss second

typedef long long ll;
typedef pair<int, int> pii;

const int MAXN = 2 * 100 * 1000 + 17;

ll n, k, a[MAXN];

ll DC(int l, int r) {
  if (l + 1 == r)
    return (a[l] >= k);
  int mid = (l + r) / 2;
  ll res = DC(l, mid) + DC(mid, r);
  vector<ll> part1, part2;
  ll sum = 0;
  for (int i = mid - 1; i >= l; i--)
    sum += a[i], part1.pb(sum);
  sum = 0;
  for (int i = mid; i < r; i++)
    sum += a[i], part2.pb(k * (i - mid + 1) - sum);
  sort(part2.begin(), part2.end());
  for (int i = 0; i < part1.size(); i++) {
    int ind = upper_bound(part2.begin(), part2.end(), part1[i] - k * (i + 1)) -
              part2.begin();
    res += ind;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cout << DC(1, n + 1) << endl;
  return 0;
}
Problem p03703 (C++)

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

using namespace std;

typedef long long int64;

template <class T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};

int main() {
  int64 N, K, A[100000];

  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int64 sum = 0;
  vector<int64> odd;
  odd.push_back(0);
  for (int i = 0; i < N; i++) {
    sum += A[i];
    odd.push_back(sum - (i + 1) * K);
  }
  sort(begin(odd), end(odd));
  odd.erase(unique(begin(odd), end(odd)), end(odd));

  sum = 0;
  int64 ret = 0;
  BinaryIndexedTree<long long> bit(odd.size() + 2);
  bit.add(lower_bound(begin(odd), end(odd), 0) - begin(odd), 1);
  for (int i = 0; i < N; i++) {
    sum += A[i];
    ret += bit.sum(lower_bound(begin(odd), end(odd), sum - (i + 1) * K) -
                   begin(odd));
    bit.add(lower_bound(begin(odd), end(odd), sum - (i + 1) * K) - begin(odd),
            1);
  }
  cout << ret << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long int64;

template <class T> struct BinaryIndexedTree {
  vector<T> data;

  BinaryIndexedTree(int sz) { data.assign(++sz, 0); }

  T sum(int k) {
    T ret = 0;
    for (++k; k > 0; k -= k & -k)
      ret += data[k];
    return (ret);
  }

  void add(int k, T x) {
    for (++k; k < data.size(); k += k & -k)
      data[k] += x;
  }
};

int main() {
  int64 N, K, A[200000];

  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> A[i];

  int64 sum = 0;
  vector<int64> odd;
  odd.push_back(0);
  for (int i = 0; i < N; i++) {
    sum += A[i];
    odd.push_back(sum - (i + 1) * K);
  }
  sort(begin(odd), end(odd));
  odd.erase(unique(begin(odd), end(odd)), end(odd));

  sum = 0;
  int64 ret = 0;
  BinaryIndexedTree<long long> bit(odd.size() + 2);
  bit.add(lower_bound(begin(odd), end(odd), 0) - begin(odd), 1);
  for (int i = 0; i < N; i++) {
    sum += A[i];
    ret += bit.sum(lower_bound(begin(odd), end(odd), sum - (i + 1) * K) -
                   begin(odd));
    bit.add(lower_bound(begin(odd), end(odd), sum - (i + 1) * K) - begin(odd),
            1);
  }
  cout << ret << endl;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#define N 210
using namespace std;

int n;
long long k;
int tree[N];

void set(int i) {
  while (i <= n) {
    tree[i]++;
    i += (i & -i);
  }
}

int get(int i) {
  int ret = 0;
  while (i) {
    ret += tree[i];
    i &= i - 1;
  }
  return ret;
}

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

  cin >> n >> k;
  long long sum[N] = {};
  pair<long long, int> value[N] = {};

  int t;
  for (int i = 1; i <= n; ++i) {
    cin >> t;
    sum[i] = sum[i - 1] + t;
    value[i].first = sum[i] - k * i;
    value[i].second = i;
  }

  sort(value, value + n + 1);
  int size_order[N];
  int num = 1;
  size_order[value[0].second] = num;
  for (int i = 1; i <= n; ++i) {
    if (value[i - 1].first == value[i].first) {
      size_order[value[i].second] = num;
    } else
      size_order[value[i].second] = ++num;
  }

  long long ans = 0;
  n++;
  for (int i = 0; i < n; ++i) {
    ans += get(size_order[i]);
    set(size_order[i]);
  }

  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#define N 200010
using namespace std;

int n;
long long k;
int tree[N];

void set(int i) {
  while (i <= n) {
    tree[i]++;
    i += (i & -i);
  }
}

int get(int i) {
  int ret = 0;
  while (i) {
    ret += tree[i];
    i &= i - 1;
  }
  return ret;
}

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

  cin >> n >> k;
  long long sum[N] = {};
  pair<long long, int> value[N] = {};

  int t;
  for (int i = 1; i <= n; ++i) {
    cin >> t;
    sum[i] = sum[i - 1] + t;
    value[i].first = sum[i] - k * i;
    value[i].second = i;
  }

  sort(value, value + n + 1);
  int size_order[N];
  int num = 1;
  size_order[value[0].second] = num;
  for (int i = 1; i <= n; ++i) {
    if (value[i - 1].first == value[i].first) {
      size_order[value[i].second] = num;
    } else
      size_order[value[i].second] = ++num;
  }

  long long ans = 0;
  n++;
  for (int i = 0; i < n; ++i) {
    ans += get(size_order[i]);
    set(size_order[i]);
  }

  cout << ans;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int maxn = 1e5 + 50;
int a[maxn];
long long b[maxn], c[maxn];
int n;
//(sum[r]-sum[l-1])/(r-l+1)>=k
// sum[r]-sum[l-1]>=k*(r-l+1)
// sum[r]-k*r>=sum[l-1]+k*(-l+1)
// sum[r]-k*r>=sum[l-1]-k*(l-1)
// for each r count the num of i that sum[r]-k*r>=sum[i]-k*(i)

int lowbit(int x) { return x & (-x); }

int ask(int x) {
  int ret = 0;
  for (; x; x -= lowbit(x))
    ret += c[x];
  return ret;
}

void change(int x, int y) {
  for (; x <= n + 1; x += lowbit(x))
    c[x] += y;
}

int main() {
  long long k;

  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    long long tmp = sum - k * i;
    b[i] = tmp;
  }
  b[n + 1] = 0;
  sort(b + 1, b + n + 2);
  sum = 0;
  memset(c, 0, sizeof(c));
  int id = lower_bound(b + 1, b + n + 2, 0) - b;
  change(id, 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    long long tmp = sum - k * i;
    int id = lower_bound(b + 1, b + n + 2, tmp) - b;
    // if (a[i]>=k) ans++;
    ans += ask(id);
    change(id, 1);
    //  cout<<tmp<<"@@@"<<ans<<endl;
  }
  cout << ans << endl;

  return 0;
}

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

using namespace std;
const int maxn = 2e5 + 50;
int a[maxn];
long long b[maxn], c[maxn];
int n;
//(sum[r]-sum[l-1])/(r-l+1)>=k
// sum[r]-sum[l-1]>=k*(r-l+1)
// sum[r]-k*r>=sum[l-1]+k*(-l+1)
// sum[r]-k*r>=sum[l-1]-k*(l-1)
// for each r count the num of i that sum[r]-k*r>=sum[i]-k*(i)

int lowbit(int x) { return x & (-x); }

int ask(int x) {
  int ret = 0;
  for (; x; x -= lowbit(x))
    ret += c[x];
  return ret;
}

void change(int x, int y) {
  for (; x <= n + 1; x += lowbit(x))
    c[x] += y;
}

int main() {
  long long k;

  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    long long tmp = sum - k * i;
    b[i] = tmp;
  }
  b[n + 1] = 0;
  sort(b + 1, b + n + 2);
  sum = 0;
  memset(c, 0, sizeof(c));
  int id = lower_bound(b + 1, b + n + 2, 0) - b;
  change(id, 1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    sum += a[i];
    long long tmp = sum - k * i;
    int id = lower_bound(b + 1, b + n + 2, tmp) - b;
    // if (a[i]>=k) ans++;
    ans += ask(id);
    change(id, 1);
    //  cout<<tmp<<"@@@"<<ans<<endl;
  }
  cout << ans << endl;

  return 0;
}
Problem p03703 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> tree(500009);

void make(int cur, int l, int r, vector<long long> &arr2) {
  if (l == r) {
    tree[cur].push_back(arr2[l]);
    return;
  }
  int mid = (l + r) / 2;
  make(2 * cur + 1, l, mid, arr2);
  make(2 * cur + 2, mid + 1, r, arr2);
  for (int i = 0; i < tree[2 * cur + 1].size() + tree[2 * cur + 2].size(); i++)
    tree[cur].push_back(0);
  merge(tree[2 * cur + 1].begin(), tree[2 * cur + 1].end(),
        tree[2 * cur + 2].begin(), tree[2 * cur + 2].end(), tree[cur].begin());
}

int get(int cur, int l, int r, int s, int e, long long value) {
  if (l > e || r < s)
    return 0;

  if (l >= s && r <= e) {
    int ret = upper_bound(tree[cur].begin(), tree[cur].end(), value - 1) -
              tree[cur].begin();
    return tree[cur].size() - ret;
  }

  int mid = l + r >> 1;
  return get(2 * cur + 1, l, mid, s, e, value) +
         get(2 * cur + 2, mid + 1, r, s, e, value);
}

int main() {
  ios_base::sync_with_stdio(stdin);
  cin.tie(NULL);
  cout.tie(NULL);

  long long n, k, ans(0);
  cin >> n >> k;
  vector<long long> arr(n + 1, 0), arr2(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    arr[i] += arr[i - 1];
    arr2[i] = arr[i] - i * k;
  }
  make(0, 1, n, arr2);
  // for(int i=1;i<=n;i++)cout<<arr2[i]<<" ";
  // cout<<"\n";

  for (int i = 1; i <= n; i++) {
    ans += get(0, 1, n, i, n, arr2[i - 1]);
    // cout<<ans<<" "<<i<<"\n";
  }
  cout << ans;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<vector<long long>> tree(800009);

void make(int cur, int l, int r, vector<long long> &arr2) {
  if (l == r) {
    tree[cur].push_back(arr2[l]);
    return;
  }
  int mid = (l + r) / 2;
  make(2 * cur + 1, l, mid, arr2);
  make(2 * cur + 2, mid + 1, r, arr2);
  for (int i = 0; i < tree[2 * cur + 1].size() + tree[2 * cur + 2].size(); i++)
    tree[cur].push_back(0);
  merge(tree[2 * cur + 1].begin(), tree[2 * cur + 1].end(),
        tree[2 * cur + 2].begin(), tree[2 * cur + 2].end(), tree[cur].begin());
}

int get(int cur, int l, int r, int s, int e, long long value) {
  if (l > e || r < s)
    return 0;

  if (l >= s && r <= e) {
    int ret = upper_bound(tree[cur].begin(), tree[cur].end(), value - 1) -
              tree[cur].begin();
    return tree[cur].size() - ret;
  }

  int mid = l + r >> 1;
  return get(2 * cur + 1, l, mid, s, e, value) +
         get(2 * cur + 2, mid + 1, r, s, e, value);
}

int main() {
  ios_base::sync_with_stdio(stdin);
  cin.tie(NULL);
  cout.tie(NULL);

  long long n, k, ans(0);
  cin >> n >> k;
  vector<long long> arr(n + 1, 0), arr2(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    arr[i] += arr[i - 1];
    arr2[i] = arr[i] - i * k;
  }
  make(0, 1, n, arr2);
  // for(int i=1;i<=n;i++)cout<<arr2[i]<<" ";
  // cout<<"\n";

  for (int i = 1; i <= n; i++) {
    ans += get(0, 1, n, i, n, arr2[i - 1]);
    // cout<<ans<<" "<<i<<"\n";
  }
  cout << ans;
}
Problem p03705 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
using namespace std;
int main() {
  long long a, b, n;
  scanf("%lld%lld%lld", n, a, b);
  if (n > 1 && b >= a || n == 1 && a == b) {
    printf("%lld\n", (n - 2) * (b - a) + 1);
  } else {
    printf("%d\n", 0);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
using namespace std;
int main() {
  long long a, b, n;
  scanf("%lld%lld%lld", &n, &a, &b);
  if (((n > 1) && (b >= a)) || (n == 1 && a == b)) {
    printf("%lld\n", (n - 2) * (b - a) + 1);
  } else {
    printf("%d\n", 0);
  }
  return 0;
}
Problem p03705 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, a, b;
  while (1) {
    cin >> x >> a >> b;
    if (x > 1 && b >= a || x == 1 && a == b) {
      cout << (x - 2) * (b - a) + 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long x, a, b;
  cin >> x >> a >> b;
  if (x > 1 && b >= a || x == 1 && a == b) {
    cout << (x - 2) * (b - a) + 1 << endl;
  } else {
    cout << 0 << endl;
  }
  return 0;
}
Problem p03705 (C++)

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

#define ll long long

using namespace std;

int main() {
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll a, b, c;
  cin >> a >> b >> c;
  if (b > c)
    return 0 + printf("0");
  cout << (a - 2) * (c - b + 1);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#define ll long long

using namespace std;

int main() {
#ifdef __APPLE__
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll a, b, c;
  cin >> a >> b >> c;
  if (b > c)
    return 0 * printf("0");
  cout << max(0ll, ((a - 2) * (c - b) + 1));

  return 0;
}
Problem p03705 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, a, b, nseki = 1, aseki = 1, bseki = 1;
  cin >> n >> a >> b;
  if (a > b)
    cout << 0 << endl;
  else if (a == b)
    cout << 1 << endl;
  else {
    if (n == 1)
      cout << 0 << endl;
    else if (n == 2)
      cout << 1 << endl;
    else {
      for (int i = (b - a) + 1; i <= (b - a) + n - 2; i++)
        nseki *= i;
      for (int i = 1; i <= (n - 2); i++)
        bseki *= i;
      cout << nseki / bseki << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, a, b, nseki = 1, aseki = 1, bseki = 1;
  cin >> n >> a >> b;
  if (a > b)
    cout << 0 << endl;
  else if (a == b)
    cout << 1 << endl;
  else {
    if (n == 1)
      cout << 0 << endl;
    else if (n == 2)
      cout << 1 << endl;
    else {
      cout << (b - a) * (n - 2) + 1 << endl;
    }
  }
}
Problem p03705 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LL long long
using namespace std;
inline int read() {
  int sum = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  if (f)
    return -sum;
  return sum;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("a.in", "r", stdin);
  freopen("a.out", "w", stdout);
#endif
  LL n = read(), a = read(), b = read();
  if (a > b) {
    puts("0");
    return 0;
  }
  if (n == 1) {
    printf("%d", a == b);
    return 0;
  } else
    printf("%lld", (b - a) * (n - 2) + 1);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LL long long
using namespace std;
inline int read() {
  int sum = 0;
  char c = getchar();
  bool f = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  if (f)
    return -sum;
  return sum;
}
int main() {
  LL n = read(), a = read(), b = read();
  if (a > b) {
    puts("0");
    return 0;
  }
  if (n == 1) {
    printf("%d", a == b);
    return 0;
  } else
    printf("%lld", (b - a) * (n - 2) + 1);
  return 0;
}
Problem p03705 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int main(void) {
  int n, a, b;

  // freopen("x.in","r",stdin);

  scanf("%d %d %d", &n, &a, &b);
  if (a > b)
    return puts("0");
  if (n == 1)
    return (a == b ? puts("1") : puts("0")), 0;
  n -= 2;
  printf("%lld\n", 1ll * n * b - 1ll * n * a + 1);

  return 0;
}

Fixed submission:
#include <cstdio>

int main(void) {
  int n, a, b;

  // freopen("x.in","r",stdin);

  scanf("%d %d %d", &n, &a, &b);
  if (a > b)
    return puts("0"), 0;
  if (n == 1)
    return (a == b ? puts("1") : puts("0")), 0;
  n -= 2;
  printf("%lld\n", 1ll * n * b - 1ll * n * a + 1);

  return 0;
}