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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

int main(void) {
  IOS int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];
  REP(i, n) cin >> c[i];

  sort(ALL(a));
  sort(ALL(b));
  sort(ALL(c));

  ll ans = 0;
  ll prev_a = 0;
  REP(i, n) {
    if (i != 0 && a[i] == a[i - 1]) {
      ans += prev_a;
      continue;
    }
    auto it_b = upper_bound(ALL(b), a[i]);
    int bs = distance(b.begin(), it_b);
    ll prev_b = 0;
    prev_a = 0;
    for (auto j = bs; j < b.size(); j++) {
      if (j != bs && b[j] == b[j - 1]) {
        ans += prev_b;
        prev_a += prev_b;
        continue;
      }
      auto it_c = upper_bound(ALL(c), b[j]);
      prev_b = distance(it_c, c.end());
      ans += prev_b;
      prev_a += prev_b;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ((n) ? "Yes" : "No") << endl;
#define CFYN(n) cout << ((n) ? "YES" : "NO") << endl;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

int main(void) {
  IOS int n;
  cin >> n;
  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  REP(i, n) cin >> a[i];
  REP(i, n) cin >> b[i];
  REP(i, n) cin >> c[i];

  sort(ALL(a));
  sort(ALL(b));
  sort(ALL(c));

  ll ans = 0;
  vector<ll> b_c;
  REP(i, b.size()) {
    auto it = upper_bound(ALL(c), b[b.size() - 1 - i]);
    ll prev = i == 0 ? 0 : b_c.back();
    b_c.push_back(prev + distance(it, c.end()));
    // cerr << b_c.back() << endl;
  }
  reverse(ALL(b_c));
  REP(i, a.size()) {
    auto it = upper_bound(ALL(b), a[i]);
    int d = distance(b.begin(), it);
    ans += b_c[d];
    // cerr << d << endl;
  }

  cout << ans << endl;

  return 0;
}
Problem p03557 (C++)

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define MAX 1000000002
typedef long long ll;

int main() {
  int n;
  cin >> n;
  // メモリを動的に確保する(かpush_backとかね)
  // int a[MAX]={},b[MAX]={},c[MAX]={};
  int *a = new int[MAX];
  int *b = new int[MAX];
  int *c = new int[MAX];
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);

  ll cnt = 0;
  rep(i, n) {
    ll up = upper_bound(a, a + n, b[i] - 1) - a;
    ll low = c + n - lower_bound(c, c + n, b[i] + 1);
    cnt += up * low;
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <iostream>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)
#define MAX 100001
typedef long long ll;

int main() {
  int n;
  cin >> n;
  // メモリを動的に確保する(かpush_backとかね)
  // int a[MAX]={},b[MAX]={},c[MAX]={};
  int *a = new int[MAX];
  int *b = new int[MAX];
  int *c = new int[MAX];
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);

  ll cnt = 0;
  rep(i, n) {
    ll up = upper_bound(a, a + n, b[i] - 1) - a;
    ll low = c + n - lower_bound(c, c + n, b[i] + 1);
    cnt += up * low;
  }
  cout << cnt << endl;
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  vector<long long> b(n);
  vector<long long> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  long long ans = 0;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  for (int i = 0; i < n; i++) {
    auto it = upper_bound(b.begin(), b.end(), a[i]);
    int diffab = n - (it - b.begin());
    for (auto itr = it; itr != b.end(); itr++) {
      auto it2 = upper_bound(c.begin(), c.end(), *itr);
      ans += n - (it2 - c.begin());
    }
  }
  cout << ans << endl;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n);
  vector<long long> b(n);
  vector<long long> c(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> c[i];
  }
  long long ans = 0;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  for (int i = 0; i < n; i++) {
    auto it = upper_bound(c.begin(), c.end(), b[i]);
    long long diffbc;
    if (it == c.end())
      diffbc = 0;
    else
      diffbc = n - (it - c.begin());
    auto it2 = lower_bound(a.begin(), a.end(), b[i]);
    long long diffab;
    if (it2 == a.end())
      diffab = n;
    else
      diffab = it2 - a.begin();
    ans += diffbc * diffab;
    // cout << diffab << " " << diffbc << endl;
  }
  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL MOD = 1000000007ll;
const LL INF = 2000000000ll;

long power(long a, long b) {
  return b ? power(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1;
}
long comb(int N, int K) {
  long x = 1, y = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * power(y, MOD - 2) % MOD;
}

long hcomb(int N, int K) { return comb(N + K - 1, N - 1); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);

  int N;
  cin >> N;
  VI A(N), B(N), C(N);
  REP(i, N) cin >> A[i];
  sort(ALL(A));
  REP(i, N) cin >> B[i];
  sort(ALL(B));
  REP(i, N) cin >> C[i];
  sort(ALL(C));

  LL ans = 0;
  REP(i, N) {
    int temp = upper_bound(ALL(B), A[i]) - B.begin();
    if (temp != N) {
      FOR(j, temp, N) { ans += N - (upper_bound(ALL(C), B[j]) - C.begin()); }
    } else
      break;
  }

  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<vector<int>> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef vector<PII> VPII;
const LL MOD = 1000000007ll;
const LL INF = 2000000000ll;

long power(long a, long b) {
  return b ? power(a * a % MOD, b / 2) * (b % 2 ? a : 1) % MOD : 1;
}
long comb(int N, int K) {
  long x = 1, y = 1;
  for (int i = 1; i <= K; i++) {
    x = x * (N - i + 1) % MOD;
    y = y * i % MOD;
  }
  return x * power(y, MOD - 2) % MOD;
}

long hcomb(int N, int K) { return comb(N + K - 1, N - 1); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);

  int N;
  cin >> N;
  VI A(N), B(N), C(N);
  REP(i, N) cin >> A[i];
  sort(ALL(A));
  REP(i, N) cin >> B[i];
  sort(ALL(B));
  REP(i, N) cin >> C[i];
  sort(ALL(C));

  LL ans = 0;
  REP(i, N) {
    ans += (lower_bound(ALL(A), B[i]) - A.begin()) *
           (C.end() - upper_bound(ALL(C), B[i]));
  }

  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  vector<int> B(N);
  vector<int> C(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = upper_bound(B.begin(), B.end(), A[i]) - B.begin(); j < N;
         j++) {
      ans += N - (upper_bound(C.begin(), C.end(), B[j]) - C.begin());
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> A(N);
  vector<int> B(N);
  vector<int> C(N);
  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> C[i];
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    long long Alower = lower_bound(A.begin(), A.end(), B[i]) - A.begin();
    long long Cupper = N - (upper_bound(C.begin(), C.end(), B[i]) - C.begin());
    ans += Alower * Cupper;
  }

  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define umap unordered_map
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define pi acos(-1.0)
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = jou(N, P / 2);
    return t * t % mod;
  }
  return (N * jou(N, P - 1)) % mod;
} // jou(n,mod-2)で逆元
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'0'+=16;        で大文字に
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

int main() {
  int n;
  cin >> n;

  vector<int> a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];

  sort(all(a));
  sort(all(c));

  ll ans = 0;
  rep(i, n) {
    auto x = lb(all(a), b[i]);
    auto y = ub(all(c), b[i]);
    ans += (ll)(x - a.begin()) * (c.end() - y);
  }

  cout << ans << endl;
}


Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repo(i, n) for (int i = 1; i < (int)(n); i++)
#define pb push_back
#define mp make_pair
#define np next_permutation
#define umap unordered_map
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define pi acos(-1.0)
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }
ll jou(ll N, ll P) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = jou(N, P / 2);
    return t * t % mod;
  }
  return (N * jou(N, P - 1)) % mod;
} // jou(n,mod-2)で逆元
// intの最大値2147483647 ≒ 2×10^9
// long longの最大値9223372036854775807 ≒ 9×10^18
//'0'+=16;        で大文字に
//'大文字'+=32;    で小文字に
//   cout << fixed << setprecision (20);   小数点以下2０桁まで
// 実行時間制約2秒では２×10^8回くらいまで計算できる

int main() {
  int n;
  cin >> n;

  vector<int> a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];

  sort(all(a));
  sort(all(c));

  ll ans = 0;
  rep(i, n) {
    auto x = lb(all(a), b[i]);
    auto y = ub(all(c), b[i]);
    ans += (ll)(x - a.begin()) * (c.end() - y);
  }

  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using edge = pair<ll, P>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
using Graph = vector<vector<ll>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  int N;
  cin >> N;
  vector<ll> A(N), B(N), C(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
  rep(i, N) cin >> C[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  ll ans = 0;
  rep(i, N) {
    ll a = A[i];
    ll idx = (lower_bound(B.begin(), B.end(), a + 1) - B.begin());
    ll cnt = N - idx;
    if (cnt >= 1) {
      rep2(j, idx, N) {
        ll b = B[j];
        ll cnt2 = N - (lower_bound(C.begin(), C.end(), b + 1) - C.begin());
        if (cnt2 >= 1) {
          ans += cnt2;
        }
      }
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using edge = pair<ll, P>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
using Graph = vector<vector<ll>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  int N;
  cin >> N;
  vector<ll> A(N), B(N), C(N);
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
  rep(i, N) cin >> C[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  ll ans = 0;
  rep(i, N) {
    ll b = B[i];
    ll cnt = upper_bound(A.begin(), A.end(), b - 1) - A.begin();
    ll cnt2 = N - (lower_bound(C.begin(), C.end(), b + 1) - C.begin());
    ans += cnt * cnt2;
  }
  cout << ans << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

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

  int N;
  cin >> N;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (int i = 0; i < N; ++i) {
    cin >> B.at(i);
  }
  for (int i = 0; i < N; ++i) {
    cin >> C.at(i);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  long long ans = 0;
  for (int i = 0; i < N; ++i) {
    for (auto itr1 = upper_bound(B.begin(), B.end(), A.at(i)); itr1 != B.end();
         ++itr1) {
      auto itr2 = upper_bound(C.begin(), C.end(), *itr1);
      ans += distance(itr2, C.end());
    }
  }
  cout << ans << endl;

  return 0;
}

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

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

  int N;
  cin >> N;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; ++i) {
    cin >> A.at(i);
  }
  for (int i = 0; i < N; ++i) {
    cin >> B.at(i);
  }
  for (int i = 0; i < N; ++i) {
    cin >> C.at(i);
  }
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  long long ans = 0;
  for (int j = 0; j < N; ++j) {
    auto itr1 = lower_bound(A.begin(), A.end(), B.at(j));
    auto itr2 = upper_bound(C.begin(), C.end(), B.at(j));
    ans += distance(A.begin(), itr1) * distance(itr2, C.end());
  }
  cout << ans << endl;

  return 0;
}
Problem p03557 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> a, b, c;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    b.push_back(x);
  }
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    c.push_back(x);
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    for (auto it = upper_bound(b.begin(), b.end(), a[i]); it != b.end(); it++) {
      for (auto itc = upper_bound(c.begin(), c.end(), *it); itc != c.end();
           itc++) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> a, b, c;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    b.push_back(x);
  }
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    c.push_back(x);
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (c.end() - upper_bound(c.begin(), c.end(), b[i])) *
           (lower_bound(a.begin(), a.end(), b[i]) - a.begin());
  }
  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, N) for (ll i = 0; i < N; i++)
using namespace std;
using ll = long long;
const ll MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  ll a[n], b[n], c[n];
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];

  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);
  ll ans = 0;
  rep(i, n) {
    auto itr1 = lower_bound(b, b + n, c[i]);
    rep(j, itr1 - b) {
      auto itr2 = lower_bound(a, a + n, b[j]);
      ans += itr2 - a;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, N) for (ll i = 0; i < N; i++)
using namespace std;
using ll = long long;
const ll MOD = 1000000007;

int main() {
  ll n;
  cin >> n;
  ll a[n], b[n], c[n];
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];

  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);
  ll ans = 0;
  rep(i, n) {
    auto itr1 = upper_bound(a, a + n, b[i] - 1);
    auto itr2 = lower_bound(c, c + n, b[i] + 1);
    ans += (itr1 - a) * (c + n - itr2);
  }
  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):


#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template <class T> T read() {
  T value;
  std::cin >> value;
  return value;
}

template <class F> void times_do(size_t n, F func) {
  for (size_t i = 0; i < n; ++i)
    func();
}

template <class T> vector<T> reads(size_t n) {
  vector<T> a;
  a.reserve(n);
  times_do(n, [&]() { a.push_back(read<T>()); });
  return a;
}

int main() {
  const size_t n = read<size_t>();
  auto a = reads<size_t>(n);
  auto b = reads<size_t>(n);
  auto c = reads<size_t>(n);

  sort(begin(a), end(a));
  sort(begin(b), end(b));
  sort(begin(c), end(c));

  size_t count = 0;

  for (size_t top : a) {
    auto mid = upper_bound(begin(b), end(b), top);
    for (; mid != end(b); ++mid) {
      auto bottom = upper_bound(begin(c), end(c), *mid);
      count += distance(bottom, end(c));
    }
  }

  cout << count << endl;

  return 0;
}


Fixed submission:


#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template <class T> T read() {
  T value;
  std::cin >> value;
  return value;
}

template <class F> void times_do(size_t n, F func) {
  for (size_t i = 0; i < n; ++i)
    func();
}

template <class T> vector<T> reads(size_t n) {
  vector<T> a;
  a.reserve(n);
  times_do(n, [&]() { a.push_back(read<T>()); });
  return a;
}

int main() {
  const size_t n = read<size_t>();
  auto a = reads<size_t>(n);
  auto b = reads<size_t>(n);
  auto c = reads<size_t>(n);

  sort(begin(a), end(a));
  sort(begin(b), end(b));
  sort(begin(c), end(c));

  size_t count = 0;

  for (size_t mid : b) {
    auto top = lower_bound(begin(a), end(a), mid);
    auto bottom = upper_bound(begin(c), end(c), mid);
    count += distance(begin(a), top) * distance(bottom, end(c));
  }

  cout << count << endl;

  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
using Graph = vector<vector<int>>;

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    auto b_it = upper_bound(b.begin(), b.end(), a[i]);
    for (auto it = b_it; it < b.end(); it++) {
      auto c_it = upper_bound(c.begin(), c.end(), *it);
      ans += c.end() - c_it;
    }
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    cin >> b[i];
  for (int i = 0; i < n; i++)
    cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    auto a_it = lower_bound(a.begin(), a.end(), b[i]);
    auto c_it = upper_bound(c.begin(), c.end(), b[i]);
    ans += (a.size() - (a.end() - a_it)) * (c.end() - c_it);
  }
  cout << ans << endl;
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int32_t N;
  cin >> N;

  vector<int32_t> A(N), B(N), C(N);
  for (int32_t i = 0; i < N; i++)
    cin >> A[i];
  for (int32_t i = 0; i < N; i++)
    cin >> B[i];
  for (int32_t i = 0; i < N; i++)
    cin >> C[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  int64_t res = 0;
  int32_t left_B = 0;
  for (int32_t index_A = 0; index_A < N; index_A++) {
    while (A[index_A] >= B[left_B] && left_B < (int32_t)B.size()) {
      left_B++;
    }
    if (left_B >= (int32_t)B.size())
      break;
    int32_t left_C = 0;
    for (int32_t index_B = left_B; index_B < N; index_B++) {
      while (B[index_B] >= C[left_C] && left_C < (int32_t)C.size()) {
        left_C++;
      }
      if (left_C >= (int32_t)C.size())
        break;
      res += (int32_t)C.size() - left_C;
    }
  }

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int32_t N;
  cin >> N;

  vector<int32_t> A(N), B(N), C(N);
  for (int32_t i = 0; i < N; i++)
    cin >> A[i];
  for (int32_t i = 0; i < N; i++)
    cin >> B[i];
  for (int32_t i = 0; i < N; i++)
    cin >> C[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  int64_t res = 0;
  for (int32_t i = 0; i < N; i++) {
    decltype(A)::iterator it_A = lower_bound(A.begin(), A.end(), B[i]);
    int64_t cnt_A = it_A == A.end() ? (int32_t)A.size() : it_A - A.begin();

    decltype(C)::iterator it_C = upper_bound(C.begin(), C.end(), B[i]);
    int64_t cnt_C = it_C == A.end() ? 0 : C.size() - (it_C - C.begin());

    res += cnt_A * cnt_C;
  }

  cout << res << endl;

  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  ll ans = 0;
  rep(i, n) {
    auto ite_b = upper_bound(b.begin(), b.end(), c[i] - 1);
    ll index = ite_b - b.begin();
    index--;
    for (ll j = index; j >= 0; j--) {
      auto ite_c = upper_bound(a.begin(), a.end(), b[j] - 1);

      ll sum = ite_c - a.begin();
      ans += sum;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  ll ans = 0;
  rep(i, n) {
    auto ite_c = upper_bound(c.begin(), c.end(), b[i]);
    auto ite_a = lower_bound(a.begin(), a.end(), b[i]);
    ll asum = (ite_a - a.begin());
    ll csum = -(ite_c - c.end());
    // cout << asum << " * " << csum << endl;
    ans += asum * csum;
  }
  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

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

  // input
  int N;
  cin >> N;

  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> C(N);
  FOR(i, 0, N) { cin >> A.at(i); }
  FOR(i, 0, N) { cin >> B.at(i); }
  FOR(i, 0, N) { cin >> C.at(i); }

  sort(ALL(A));
  sort(ALL(B));
  sort(ALL(C));

  ll sum = 0;
  for (auto c : C) {
    auto it_b = lower_bound(ALL(B), c);
    auto it = B.begin();
    while (it != it_b) {
      ll b = *it;
      auto it_a = lower_bound(ALL(A), b);
      ll dist = distance(A.begin(), it_a);
      if (dist > 0) {
        sum += dist;
      }
      it++;
    }
  }

  p(sum);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <complex>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout << (s) << endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define pn(s) cout << (#s) << " " << (s) << endl
#define p_yes() p("Yes")
#define p_no() p("No")

const ll mod = 1e9 + 7;
const ll inf = 1e18;

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

  // input
  int N;
  cin >> N;

  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> C(N);
  FOR(i, 0, N) { cin >> A.at(i); }
  FOR(i, 0, N) { cin >> B.at(i); }
  FOR(i, 0, N) { cin >> C.at(i); }

  sort(ALL(A));
  sort(ALL(B));
  sort(ALL(C));

  ll sum = 0;
  for (auto b : B) {
    auto it_c = upper_bound(ALL(C), b);
    ll dist_c = distance(it_c, C.end());

    auto it_a = lower_bound(ALL(A), b);
    ll dist_a = distance(A.begin(), it_a);

    sum += dist_a * dist_c;
  }

  p(sum);

  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n)                                                             \
  rep(i, n) { cout << a[i] << ' '; }                                           \
  cout << endl;

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

int main() {
  //	cout << fixed << setprecision(15);
  int n;
  cin >> n;
  vll a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  vll bc(n);
  rep(i, n) { bc[i] = n - (upper_bound(c.begin(), c.end(), b[i]) - c.begin()); }
  bc.push_back(0); // bc[n] = 0
  for (int i = n - 1; i >= 0; i--)
    bc[i - 1] += bc[i];
  ll ans = 0;
  rep(i, n) {
    int j = upper_bound(b.begin(), b.end(), a[i]) - b.begin();
    ans += bc[j];
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define show(a, n)                                                             \
  rep(i, n) { cout << a[i] << ' '; }                                           \
  cout << endl;

using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<ll> vll;
typedef pair<long long, long long> pll;

const int INF = 1 << 30;
const long long INFL = 1LL << 62;
const int MOD = 1000000007;
const int MAX = 100000;
const int N = 100010;

int main() {
  //	cout << fixed << setprecision(15);
  int n;
  cin >> n;
  vll a(n), b(n), c(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  vll bc(n);
  rep(i, n) { bc[i] = n - (upper_bound(c.begin(), c.end(), b[i]) - c.begin()); }
  bc.push_back(0); // bc[n] = 0
  for (int i = n; i >= 1; i--)
    bc[i - 1] += bc[i];
  ll ans = 0;
  rep(i, n) {
    int j = upper_bound(b.begin(), b.end(), a[i]) - b.begin();
    ans += bc[j];
  }
  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; n > i; i++)
    cin >> a[i];
  for (int i = 0; n > i; i++)
    cin >> b[i];
  for (int i = 0; n > i; i++)
    cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  long long ans = 0;
  for (int i = 0; n > i; i++) {
    // n-a[i]より真に大きい位置
    auto j = lower_bound(b.begin(), b.end(), a[i] + 1);
    // cout << *j << endl;
    for (; b.end() - b.begin() != j - b.begin(); j++) {
      auto k = lower_bound(c.begin(), c.end(), *j + 1);
      // cout << c.end()-k << endl;
      // cout << *k << endl;
      // cout << c.end()-k << endl;
      ans += c.end() - k;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; n > i; i++)
    cin >> a[i];
  for (int i = 0; n > i; i++)
    cin >> b[i];
  for (int i = 0; n > i; i++)
    cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  long long ans = 0;
  for (int i = 0; n > i; i++) {
    auto y = upper_bound(a.begin(), a.end(), b[i] - 1);
    auto z = lower_bound(c.begin(), c.end(), b[i] + 1);
    ans += (y - a.begin()) * (c.end() - z);
  }
  cout << ans << endl;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, s, n) for (int i = s; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  vector<ll> c(n);

  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) cin >> c[i];

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  rep(i, 0, n) {
    ll value = b[i];
    ll tmp_a = 0;
    ll tmp_b = 0;
    rep(j, 0, n) {
      if (value > a[j]) {
        tmp_a++;
      } else
        break;
    }
    rep(j, 0, n) {
      if (value < c[j]) {
        tmp_b++;
      }
    }
    // cout << tmp_a << " " << tmp_b << endl;
    ans += tmp_a * tmp_b;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define rep(i, s, n) for (int i = s; i < n; i++)

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> b(n);
  vector<ll> c(n);

  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) cin >> c[i];

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  rep(i, 0, n) {
    ll value = b[i];
    ll a_num = lower_bound(a.begin(), a.end(), value) - a.begin();
    ll c_num = c.end() - upper_bound(c.begin(), c.end(), value);
    ans += a_num * c_num;
  }
  cout << ans << endl;
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main(void) {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == 0)
        cin >> a[j];
      else if (i == 1)
        cin >> b[j];
      else
        cin >> c[j];
    }
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  ll ans = 0;
  for (int i = n - 1; i >= 0; --i) {
    int memo = 0;
    int k = n - 1;
    for (int j = n - 1; j >= 0; --j) {
      if (a[i] >= b[j])
        break;
      for (; k >= 0; --k) {
        if (b[j] >= c[k])
          break;
        ++memo;
      }
      ans += memo;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int main(void) {
  int n;
  cin >> n;
  vector<int> a(n), b(n), c(n);
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == 0)
        cin >> a[j];
      else if (i == 1)
        cin >> b[j];
      else
        cin >> c[j];
    }
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  ll ans = 0;
  for (int j = 0; j < n; ++j) {
    ll s = lower_bound(a.begin(), a.end(), b[j]) - a.begin();
    ll m = c.end() - upper_bound(c.begin(), c.end(), b[j]);
    ans += s * m;
  }
  cout << ans << endl;
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;

const int INF = 1e9 + 7;

using namespace std;

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

  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) cin >> c[i];

  ll res = 0;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  for (auto it_a = a.begin(); it_a != a.end(); it_a++) {
    auto it_b = upper_bound(b.begin(), b.end(), *it_a);
    for (; it_b != b.end(); it_b++) {
      auto it_c = upper_bound(c.begin(), c.end(), *it_b);
      ll size = distance(it_c, c.end());
      res += size;
    }
  }

  cout << res << endl;
  return 0;
}

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

#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;

const int INF = 1e9 + 7;

using namespace std;

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

  int n;
  cin >> n;

  vector<int> a(n);
  vector<int> b(n);
  vector<int> c(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) cin >> c[i];

  ll res = 0;
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());
  for (auto it_b = b.begin(); it_b != b.end(); it_b++) {
    auto it_a = lower_bound(a.begin(), a.end(), *it_b);
    auto it_c = upper_bound(c.begin(), c.end(), *it_b);
    ll size_a = distance(a.begin(), it_a);
    ll size_c = distance(it_c, c.end());
    res += (size_a * size_c);
  }

  cout << res << endl;
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n], b[n], c[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);
  int count[n];
  long re = 0;
  for (int j = 0; j < n; ++j) {
    for (int i = 0; i < n; ++i) {
      if (b[j] < c[i]) {
        count[j] = n - i;
        break;
      }
    }
  }
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      if (a[k] < b[i])
        re += count[i];
    }
  }
  cout << re << endl;
  return 0;
}

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

using namespace std;

int main() {
  int n;
  cin >> n;
  int a[n], b[n], c[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
  }
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);
  int count[n];
  long re = 0;
  for (int j = 0; j < n; ++j) {
    re += (lower_bound(a, a + n, b[j]) - a) *
          (c + n - upper_bound(c, c + n, b[j]));
  }
  cout << re << endl;
  return 0;
}
Problem p03557 (C++)

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

using namespace std;

int main(int argc, char *argv[]) {
  int N;

  vector<int> A;
  vector<int> B;
  vector<int> C;

  cin >> N;

  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    A.push_back(n);
  }

  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    B.push_back(n);
  }

  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    C.push_back(n);
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  int d[100000] = {};
  for (int j = 0; j < B.size(); j++) {
    d[j] = lower_bound(A.begin(), A.end(), B[j]) - A.begin();
  }

  int ans = 0;
  for (int i = 0; i < C.size(); i++) {
    int b = lower_bound(B.begin(), B.end(), C[i]) - B.begin();
    for (int j = 0; j < b; j++) {
      ans += d[j];
    }
  }

  cout << ans << endl;

  return 0;
}

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

using namespace std;

int main(int argc, char *argv[]) {
  int N;

  vector<int> A;
  vector<int> B;
  vector<int> C;

  cin >> N;

  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    A.push_back(n);
  }

  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    B.push_back(n);
  }

  for (int i = 0; i < N; i++) {
    int n;
    cin >> n;
    C.push_back(n);
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  long long ans = 0;
  for (int i = 0; i < B.size(); i++) {
    long long a = lower_bound(A.begin(), A.end(), B[i]) - A.begin();
    long long c = C.end() - upper_bound(C.begin(), C.end(), B[i]);
    ans += a * c;
  }

  cout << ans << endl;

  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <stdio.h>
using namespace std;
long long A[100010], B[100010], C[100010];
long long p[100010] = {0};
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  for (int i = 0; i < N; i++) {
    scanf("%lld", &B[i]);
  }
  for (int i = 0; i < N; i++) {
    scanf("%lld", &C[i]);
  }
  sort(A, A + N);
  sort(B, B + N);
  sort(C, C + N);
  int j = 0, l = 0;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    while (B[i] >= C[j]) {
      if (j == N)
        break;
      j++;
    }
    p[i] = N - j;
    sum += p[i];
  }
  long long an = 0;
  for (int i = 0; i < N; i++) {
    while (A[i] >= B[l]) {
      sum -= p[l];
      l++;
    }
    an += sum;
  }
  printf("%lld", an);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <stdio.h>
using namespace std;
long long A[100010], B[100010], C[100010];
long long p[100010] = {0};
int main() {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &A[i]);
  }
  for (int i = 0; i < N; i++) {
    scanf("%lld", &B[i]);
  }
  for (int i = 0; i < N; i++) {
    scanf("%lld", &C[i]);
  }
  sort(A, A + N);
  sort(B, B + N);
  sort(C, C + N);
  int j = 0, l = 0;
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    while (B[i] >= C[j]) {
      if (j == N)
        break;
      j++;
    }
    p[i] = N - j;
    sum += p[i];
  }
  long long an = 0;
  for (int i = 0; i < N; i++) {
    while (A[i] >= B[l]) {
      if (l == N)
        break;
      sum -= p[l];
      l++;
    }
    an += sum;
  }
  printf("%lld", an);
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 0; i <= n; i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define REPR(i, n) for (int i = n; i >= 0; i--)
typedef long long ll;
const ll INF = 1LL << 60;
const int INT_INF = 1e9;
const ll MOD = 1e9 + 7;
template <class T> void puts(T x) { std::cout << x << std::endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int n;
vector<ll> a, b, c;

int main() {
  cin >> n;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  rep(i, n) {
    ll pos = a[i];
    auto ub_b = upper_bound(b.begin(), b.end(), pos);
    for (auto iter_b = ub_b; iter_b != b.end(); ++iter_b) {
      auto ub_c = upper_bound(c.begin(), c.end(), *iter_b);
      ans += distance(ub_c, c.end());
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 0; i <= n; i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define REPR(i, n) for (int i = n; i >= 0; i--)
typedef long long ll;
const ll INF = 1LL << 60;
const int INT_INF = 1e9;
const ll MOD = 1e9 + 7;
template <class T> void puts(T x) { std::cout << x << std::endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int n;
vector<ll> a, b, c;

int main() {
  cin >> n;
  a.resize(n);
  b.resize(n);
  c.resize(n);
  rep(i, n) cin >> a[i];
  rep(i, n) cin >> b[i];
  rep(i, n) cin >> c[i];
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  rep(i, n) {
    ll pos = b[i];
    auto iter_a = lower_bound(a.begin(), a.end(), pos);
    auto iter_c = upper_bound(c.begin(), c.end(), pos);
    ll da = distance(a.begin(), iter_a);
    ll dc = distance(iter_c, c.end());
    ans += da * dc;
  }
  cout << ans << endl;
}
Problem p03558 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
const int INF = 1e9;
int k;
int ary[100005];
void solve(int p, int q) {
  if (ary[p] <= q)
    return;
  ary[p] = q;
  solve(p * 10 % k, q);
  solve((p + 1) % k, q + 1);
}
int main(void) {
  cin >> k;
  for (int i = 0; i < k; ++i)
    ary[i] = INF;
  solve(1, 1);
  cout << ary[0] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
const int INF = 1e9;
int k;
int ary[100005];
void solve(int p, int q) {
  if (ary[p] <= q)
    return;
  ary[p] = q;
  solve(p * 10 % k, q);
  solve((p + 1) % k, q + 1);
}
int main(void) {
  cin >> k;
  for (int i = 0; i < k; ++i)
    ary[i] = 45;
  solve(1, 1);
  cout << ary[0] << endl;
  return 0;
}
Problem p03558 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;

#define min(a, b) (a < b ? a : b)
#define mem(a, n) memset(a, sizeof(a), n)
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int vis[N][100];

int digit_sum(int x) {
  int sum = 0;
  while (x) {
    sum += (x % 10);
    x /= 10;
  }
  return sum;
}
int n;
void dfs(int x, int sum) {
  if (sum >= 99 || vis[x][sum])
    return;
  vis[x][sum] = 1;
  for (int i = 0; i < 10; i++)
    dfs((10 * x + i) % n, sum + i);
}

int main() {
  while (~scanf("%d", &n)) {
    mem(vis, 0);
    for (int i = 0; i < n; i++)
      dfs(i, digit_sum(i));
    int ans = INF;
    for (int i = 1; i < 99; i++)
      if (vis[0][i])
        ans = min(ans, i);
    printf("%d\n", ans);
  }
  return 0;
}
/**
2
1 5
2 4
3 6
3
1 1 1
2 2 2
3 3 3
6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288
**/


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <vector>
using namespace std;

#define min(a, b) (a < b ? a : b)
#define mem(a, n) memset(a, sizeof(a), n)
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
bool vis[N][100];

int digit_sum(int x) {
  int sum = 0;
  while (x) {
    sum += (x % 10);
    x /= 10;
  }
  return sum;
}
int n;
void dfs(int x, int sum) {
  if (sum >= 99 || vis[x][sum])
    return;
  vis[x][sum] = 1;
  for (int i = 0; i < 10; i++)
    dfs((10 * x + i) % n, sum + i);
}

int main() {
  while (~scanf("%d", &n)) {
    mem(vis, 0);
    for (int i = 0; i < n; i++)
      dfs(i, digit_sum(i));
    int ans = INF;
    for (int i = 1; i < 99; i++)
      if (vis[0][i])
        ans = min(ans, i);
    printf("%d\n", ans);
  }
  return 0;
}
/**
2
1 5
2 4
3 6
3
1 1 1
2 2 2
3 3 3
6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288
**/