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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, f, n) for (int i = f; i < (n); i++)
int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a, b, c;
  REP(i, 0, n) {
    int inter;
    cin >> inter;
    a.push_back(inter);
  }
  REP(i, 0, n) {
    int inter;
    cin >> inter;
    b.push_back(inter);
  }
  REP(i, 0, n) {
    int inter;
    cin >> inter;
    c.push_back(inter);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  REP(i, 0, n) {
    ll naka = lower_bound(b.begin(), b.end(), c[i]) - b.begin();
    if (naka > 0)
      REP(j, 0, naka) {
        ll ue = lower_bound(a.begin(), a.end(), b[j]) - a.begin();
        if (ue > 0) {
          ans += ue;
        }
      }
  }
  cout << ans << endl;

  //--debug : end_stoper
  // string end_stoper; cin >> end_stoper;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, f, n) for (int i = f; i < (n); i++)
int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a, b, c;
  REP(i, 0, n) {
    int inter;
    cin >> inter;
    a.push_back(inter);
  }
  REP(i, 0, n) {
    int inter;
    cin >> inter;
    b.push_back(inter);
  }
  REP(i, 0, n) {
    int inter;
    cin >> inter;
    c.push_back(inter);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  REP(i, 0, n) {
    ll sita = upper_bound(c.begin(), c.end(), b[i]) - c.begin();
    ll ue = lower_bound(a.begin(), a.end(), b[i]) - a.begin();
    ans += (n - sita) * ue;
  }
  cout << ans << endl;

  //--debug : end_stoper
  // string end_stoper; cin >> end_stoper;

  return 0;
}
Problem p03557 (C++)

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

#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 ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
  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 sum = 0;
  for (int i = 0; i < N; i++) {
    auto itr = upper_bound(ALL(B), A[i]);
    for (auto j = itr; j < B.end(); j++) {
      auto c_itr = upper_bound(ALL(C), *j);
      sum += C.end() - c_itr;
    }
  }

  cout << sum << endl;
  return 0;
}


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

#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 ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;

int main() {
  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 sum = 0;
  for (int i = 0; i < N; i++) {
    auto a_itr = lower_bound(ALL(A), B[i]);
    auto c_itr = upper_bound(ALL(C), B[i]);
    sum += (a_itr - A.begin()) * (C.end() - c_itr);
  }

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

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

int lt(int ok, int ng, int key, vector<int> s) {
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (s[mid] < key)
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

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());

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long cnta = lt(-1, n, b[i], a) + 1;
    // long long cnta = lower_bound(a.begin(),a.end(),b[i]) - a.begin();
    long long cntc = c.end() - upper_bound(c.begin(), c.end(), b[i]);
    ans += cnta * cntc;
  }
  cout << ans << endl;
}

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

int lt(int ok, int ng, int key, vector<int> s) {
  while (abs(ok - ng) > 1) {
    int mid = (ok + ng) / 2;
    if (s[mid] < key)
      ok = mid;
    else
      ng = mid;
  }
  return ok;
}

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());

  long long ans = 0;
  for (int i = 0; i < n; i++) {
    // long long cnta = lt(-1,n,b[i],a) + 1;
    int ok = -1;
    int ng = n;
    int key = b[i];
    while (abs(ok - ng) > 1) {
      int mid = (ok + ng) / 2;
      if (a[mid] < key)
        ok = mid;
      else
        ng = mid;
    }
    long long cnta = ok + 1;
    // long long cnta = lower_bound(a.begin(),a.end(),b[i]) - a.begin();
    long long cntc = c.end() - upper_bound(c.begin(), c.end(), b[i]);
    ans += cnta * cntc;
  }
  cout << ans << endl;
}
Problem p03557 (C++)

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

#define ll long long int
#define str string
#define ALL(a) (a).begin(), (a).end()

ll MOD = 1000000007, INFL = 1ll << 60, INF = 1 << 30, mod = 1e9 + 7, Max = INF,
   Min = -1;
ll a = 0, b = 0, c = 0, n = 0, m = 0, high = 0, sum = 0, MASK = 0;
ll A[500000] = {}, B[500000] = {}, C[500000] = {}, dp[200000][3],
   rui[500000] = {}, bitdp[1 << 12] = {};
int dy[] = {0, 0, 1, -1}, dx[] = {1, -1, 0, 0};
vector<ll> va, vb, vc;
str S, T;
bool check = true, check2 = false;

int main() {
  // Code Line
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    va.push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    vb.push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    vc.push_back(a);
  }

  sort(ALL(va));
  sort(ALL(vb));
  sort(ALL(vc));

  for (int i = 0; i < n; i++) {
    auto b = upper_bound(ALL(vb), va[i]);
    for (int j = (b - vb.begin()); j < n; j++) {
      auto c = upper_bound(ALL(vc), vb[j]);
      sum += n - (c - vc.begin());
    }
  }

  cout << sum << endl;

  return 0;
}


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

#define ll long long int
#define str string
#define ALL(a) (a).begin(), (a).end()

ll MOD = 1000000007, INFL = 1ll << 60, INF = 1 << 30, mod = 1e9 + 7, Max = INF,
   Min = -1;
ll a = 0, b = 0, c = 0, n = 0, m = 0, high = 0, sum = 0, MASK = 0;
ll A[500000] = {}, B[500000] = {}, C[500000] = {}, dp[200000][3],
   rui[500000] = {}, bitdp[1 << 12] = {};
int dy[] = {0, 0, 1, -1}, dx[] = {1, -1, 0, 0};
vector<ll> va, vb, vc;
str S, T;
bool check = true, check2 = false;

int main() {
  // Code Line
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a;
    va.push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    vb.push_back(a);
  }
  for (int i = 0; i < n; i++) {
    cin >> a;
    vc.push_back(a);
  }

  sort(ALL(va));
  sort(ALL(vb));
  sort(ALL(vc));

  for (int i = 0; i < n; i++) {
    auto a = lower_bound(ALL(va), vb[i]);
    auto c = upper_bound(ALL(vc), vb[i]);
    sum += (a - va.begin()) * (n - (c - vc.begin()));
  }

  cout << sum << endl;

  return 0;
}
Problem p03557 (C++)

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

int main() {
  int N;
  vector<int> A, B, C;
  long long ans;
  cin >> N;
  rep(i, N) {
    int t;
    cin >> t;
    A.push_back(t);
  }
  rep(i, N) {
    int t;
    cin >> t;
    B.push_back(t);
  }
  rep(i, N) {
    int t;
    cin >> t;
    C.push_back(t);
  }

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

  ans = 0;
  rep(i, N) {
    int idx = lower_bound(B.begin(), B.end(), A[i] + 1) - B.begin();
    for (int j = idx; j < N; j++) {
      int num = C.end() - lower_bound(C.begin(), C.end(), B[j] + 1);
      if (num == 0)
        break;
      ans += num;
    }
  }

  cout << ans << endl;
}


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

int main() {
  int N;
  vector<int> A, B, C;
  long long ans;
  cin >> N;
  rep(i, N) {
    int t;
    cin >> t;
    A.push_back(t);
  }
  rep(i, N) {
    int t;
    cin >> t;
    B.push_back(t);
  }
  rep(i, N) {
    int t;
    cin >> t;
    C.push_back(t);
  }

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

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

  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 ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  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(b)), sort(ALL(c));
  ll cnt = 0;
  REP(i, n) {
    if (upper_bound(ALL(b), a[i]) == b.end())
      break;
    for (auto itr_b = upper_bound(ALL(b), a[i]); itr_b != b.end(); itr_b++) {
      auto itr_c = upper_bound(ALL(c), *itr_b);
      if (itr_c == c.end())
        break;
      cnt += (ll)(c.end() - itr_c);
    }
  }
  cout << cnt << endl;
  return 0;
}

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 ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  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(b)), sort(ALL(c));
  ll cnt = 0;
  REP(i, n) {
    ll x = (ll)(lower_bound(ALL(a), b[i]) - a.begin());
    ll y = (ll)(c.end() - upper_bound(ALL(c), b[i]));
    cnt += x * y;
  }
  cout << cnt << endl;
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define repss(name, start, stop, step)                                         \
  for (int name = start; name < stop; name += step)
#define reps(name, start, stop) repss(name, start, stop, 1)
#define rep(name, stop) reps(name, 0, stop)
#define foreach(name, iterable)                                                \
  for (auto name = iterable.begin(); name != iterable.end(); name++)

using namespace std;

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

  int t;
  vector<int> A, B, C;
  rep(i, N) {
    cin >> t;
    A.push_back(t);
  }
  rep(i, N) {
    cin >> t;
    B.push_back(t);
  }
  rep(i, N) {
    cin >> t;
    C.push_back(t);
  }

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

  long long ans = 0;
  for (auto Ai = A.begin(); Ai != A.end(); Ai++) {
    auto Bs = lower_bound(B.begin(), B.end(), *Ai + 1);
    for (auto Bi = Bs; Bi != B.end(); Bi++) {
      auto Cs = lower_bound(C.begin(), C.end(), *Bi + 1);
      ans += C.end() - Cs;
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define repss(name, start, stop, step)                                         \
  for (int name = start; name < stop; name += step)
#define reps(name, start, stop) repss(name, start, stop, 1)
#define rep(name, stop) reps(name, 0, stop)
#define foreach(name, iterable)                                                \
  for (auto name = iterable.begin(); name != iterable.end(); name++)

using namespace std;

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

  int t;
  vector<int> A, B, C;
  rep(i, N) {
    cin >> t;
    A.push_back(t);
  }
  rep(i, N) {
    cin >> t;
    B.push_back(t);
  }
  rep(i, N) {
    cin >> t;
    C.push_back(t);
  }

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

  long long ans = 0;
  for (auto Bi = B.begin(); Bi != B.end(); Bi++) {
    auto Ae = lower_bound(A.begin(), A.end(), *Bi);
    auto Cs = lower_bound(C.begin(), C.end(), *Bi + 1);
    ans += (Ae - A.begin()) * (C.end() - Cs);
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  vector<int64_t> B(N);
  for (int i = 0; i < N; i++)
    cin >> B.at(i);
  sort(B.begin(), B.end());
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C.at(i);
  sort(C.begin(), C.end());
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    auto itr = lower_bound(A.begin(), A.end(), B.at(i));
    int64_t a = distance(A.begin(), itr);
    auto itr1 = upper_bound(C.begin(), A.end(), C.at(i));
    int64_t b = N - distance(C.begin(), itr1);
    ans += a * b;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int64_t> A(N);
  for (int i = 0; i < N; i++)
    cin >> A.at(i);
  sort(A.begin(), A.end());
  vector<int64_t> B(N);
  for (int i = 0; i < N; i++)
    cin >> B.at(i);
  sort(B.begin(), B.end());
  vector<int64_t> C(N);
  for (int i = 0; i < N; i++)
    cin >> C.at(i);
  sort(C.begin(), C.end());
  int64_t ans = 0;
  for (int i = 0; i < N; i++) {
    auto itr = lower_bound(A.begin(), A.end(), B.at(i));
    int64_t a = distance(A.begin(), itr);
    auto itr1 = upper_bound(C.begin(), C.end(), B.at(i));
    int64_t b = N - distance(C.begin(), itr1);
    ans += a * b;
  }
  cout << ans << endl;

  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Runtime Error):
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
// 1. dpを書く
// 2. nを書く
ll N;
ll A[10001], B[100001], C[100001];
// 7. calcを書く
ll calcA(ll lb, ll ub, ll k) {
  if (A[lb] >= k)
    return -1;
  ll mid = (lb + ub) / 2;
  if (ub - lb < 2) {
    if (A[ub] < k)
      return ub;
    return lb;
  }
  if (A[mid] < k) {
    lb = mid;
  } else {
    ub = mid;
  }

  return calcA(lb, ub, k);
}
ll calcC(ll lb, ll ub, ll k) {
  if (C[lb] <= k)
    return -1;
  ll mid = (lb + ub) / 2;
  if (ub - lb < 2) {
    if (C[ub] > k)
      return ub;
    return lb;
  }
  if (C[mid] > k) {
    lb = mid;
  } else {
    ub = mid;
  }

  return calcC(lb, ub, k);
}

int main() {
  cin >> N;

  // 3.初期化

  // 4.入力
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (ll i = 0; i < N; i++) {
    cin >> B[i];
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }

  // 5.並び替え
  sort(A, A + N);
  sort(C, C + N, greater<ll>());

  // 6.出力

  ll total = 0;
  for (ll i = 0; i < N; i++) {
    total += (calcA(0, N - 1, B[i]) + 1) * (calcC(0, N - 1, B[i]) + 1);
  }

  cout << total << endl;

  return 0;
}


Fixed submission:
//============================================================================
// Name        : JOI.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
// 1. dpを書く
// 2. nを書く
ll N;
ll A[100001];
ll B[100001];
ll C[100001];
// 7. calcを書く
ll calcA(ll lb, ll ub, ll k) {
  if (A[lb] >= k)
    return -1;
  ll mid = (lb + ub) / 2;
  if (ub - lb < 2) {
    if (A[ub] < k)
      return ub;
    return lb;
  }
  if (A[mid] < k) {
    lb = mid;
  } else {
    ub = mid;
  }

  return calcA(lb, ub, k);
}
ll calcC(ll lb, ll ub, ll k) {
  if (C[lb] <= k)
    return -1;
  ll mid = (lb + ub) / 2;
  if (ub - lb < 2) {
    if (C[ub] > k)
      return ub;
    return lb;
  }
  if (C[mid] > k) {
    lb = mid;
  } else {
    ub = mid;
  }

  return calcC(lb, ub, k);
}

int main() {
  cin >> N;

  // 3.初期化

  // 4.入力
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  for (ll i = 0; i < N; i++) {
    cin >> B[i];
  }
  for (ll i = 0; i < N; i++) {
    cin >> C[i];
  }

  // 5.並び替え
  sort(A, A + N);
  sort(C, C + N, greater<ll>());

  // 6.出力

  ll total = 0;
  for (ll i = 0; i < N; i++) {
    total += (calcA(0, N - 1, B[i]) + 1) * (calcC(0, N - 1, B[i]) + 1);
  }

  cout << total << endl;

  return 0;
}
Problem p03557 (C++)

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

#define DEBUG 0

#define PLUS 0
#define MINUS 1

#define lli long long int
#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

lli goal = 0;

int main() {
  lli n;
  vector<lli> a[3];
  cin >> n;
  REP(i, 3) {
    REP(j, n) {
      lli tmp;
      cin >> tmp;
      a[i].push_back(tmp);
    }
    sort(a[i].begin(), a[i].end());
    if (DEBUG) {
      REP(j, a[i].size()) { cout << a[i].at(j) << " "; }
      cout << endl;
    }
  }

  lli bigPos = 0;
  lli smallPos = 0;
  lli ans = 0;
  REP(i, n) {
    lli center = a[1].at(i);
    if (DEBUG)
      cout << "center = " << center << " ";
    /*center > smallPosになるまで検索する*/
    while (1) {
      if (smallPos >= n)
        break;
      if (center > a[0].at(smallPos)) {
        smallPos++;
      } else
        break;
    }
    if (DEBUG)
      cout << "small = " << a[0].at(smallPos - 1) << " ";
    /*center > Bigになるまで検索する*/
    while (1) {
      if (bigPos > n)
        break;
      if (center >= a[2].at(bigPos)) {
        bigPos++;
      } else {
        break;
      }
    }
    if (DEBUG)
      cout << "Big= " << a[2].at(bigPos) << " ";
    lli nowAns = smallPos * (n - bigPos);
    if (DEBUG)
      cout << "nowAns =" << nowAns;
    ans += nowAns;
    if (DEBUG)
      cout << endl;
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define DEBUG 0

#define PLUS 0
#define MINUS 1

#define lli long long int
#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

lli goal = 0;

int main() {
  lli n;
  vector<lli> a[3];
  cin >> n;
  REP(i, 3) {
    REP(j, n) {
      lli tmp;
      cin >> tmp;
      a[i].push_back(tmp);
    }
    sort(a[i].begin(), a[i].end());
    if (DEBUG) {
      REP(j, a[i].size()) { cout << a[i].at(j) << " "; }
      cout << endl;
    }
  }

  lli bigPos = 0;
  lli smallPos = 0;
  lli ans = 0;
  REP(i, n) {
    lli center = a[1].at(i);
    if (DEBUG)
      cout << "center = " << center << " ";
    /*center > smallPosになるまで検索する*/
    while (1) {
      if (smallPos >= n)
        break;
      if (center > a[0].at(smallPos)) {
        smallPos++;
      } else
        break;
    }
    if (DEBUG)
      cout << "small = " << a[0].at(smallPos - 1) << " ";
    /*center > Bigになるまで検索する*/
    while (1) {
      if (bigPos >= n)
        break;
      if (center >= a[2].at(bigPos)) {
        bigPos++;
      } else {
        break;
      }
    }
    if (DEBUG)
      cout << "Big= " << a[2].at(bigPos) << " ";
    lli nowAns = smallPos * (n - bigPos);
    if (DEBUG)
      cout << "nowAns =" << nowAns;
    ans += nowAns;
    if (DEBUG)
      cout << endl;
  }
  cout << ans << endl;

  return 0;
}
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<long> a(n, 0);
  vector<long> b(n, 0);
  vector<long> c(n, 0);
  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 ans = 0;
  for (int i = 0; i < n; i++) {
    vector<long>::iterator it = lower_bound(b.begin(), b.end(), a[i]);
    if (*it == a[i] && it != b.end()) {
      it++;
    }
    for (; it != b.end(); it++) {
      vector<long>::iterator it2 = lower_bound(c.begin(), c.end(), *it);
      if (*it2 == *it && it2 != c.end()) {
        it2++;
      }
      ans += c.end() - it2;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long> a(n, 0);
  vector<long> b(n, 0);
  vector<long> c(n, 0);
  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 ans = 0;
  for (int i = 0; i < n; i++) {
    vector<long>::iterator ita = upper_bound(a.begin(), a.end(), b[i] - 1);
    vector<long>::iterator itc = lower_bound(c.begin(), c.end(), b[i] + 1);
    long cnt_a = ita - a.begin();
    long cnt_c = c.end() - itc;
    ans += cnt_a * cnt_c;
  }
  cout << ans << endl;
  return 0;
}
Problem p03557 (C++)

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

int main() {
  vector<int> a, b, c;
  int n, inp;
  long long int ans = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> inp;
    a.push_back(inp);
  }
  sort(a.begin(), a.end());

  for (int i = 0; i < n; i++) {
    cin >> inp;
    b.push_back(inp);
  }
  sort(b.begin(), b.end());

  for (int i = 0; i < n; i++) {
    cin >> inp;
    c.push_back(inp);
  }
  sort(c.begin(), c.end());

  for (int i = 0; i < n; i++) {
    for (auto b_i = upper_bound(b.begin(), b.end(), a[i]); b_i != b.end();
         b_i++) {
      auto c_i = upper_bound(c.begin(), c.end(), *b_i);
      ans += distance(c_i, c.end());
    }
  }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> a, b, c;
  int n, inp;
  long long int ans = 0;
  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> inp;
    a.push_back(inp);
  }
  sort(a.begin(), a.end());

  for (int i = 0; i < n; i++) {
    cin >> inp;
    b.push_back(inp);
  }
  sort(b.begin(), b.end());

  for (int i = 0; i < n; i++) {
    cin >> inp;
    c.push_back(inp);
  }
  sort(c.begin(), c.end());

  for (int i = 0; i < n; i++) {
    long long int ai =
        distance(a.begin(), lower_bound(a.begin(), a.end(), b[i]));
    long long int ci = distance(upper_bound(c.begin(), c.end(), b[i]), c.end());
    ans += ai * ci;
  }

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

Buggy submission (status: Time Limit Exceeded):


#include <bits/stdc++.h>

#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
#define ll long long
using namespace std;

/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/

ll bsearch_a(vector<ll> v, ll key) {
  int left = -1;
  int right = v.size();

  while (right - left > 1) {
    int mid = left + (right - left) / 2;
    if (v[mid] >= key)
      right = mid;
    else
      left = mid;
  }
  return right;
}

ll bsearch_c(vector<ll> v, ll key) {
  int left = -1;
  int right = v.size();

  while (right - left > 1) {
    int mid = left + (right - left) / 2;
    if (v[mid] > key)
      right = mid;
    else
      left = mid;
  }
  return right;
}

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

  int N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> C(N);
  loop(0, N, i) cin >> A[i];
  loop(0, N, i) cin >> B[i];
  loop(0, N, i) cin >> C[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  ll total = 0;
  loop(0, N, i) {
    ll bi = B[i];
    ll a = bsearch_a(A, B[i]);
    ll c = bsearch_c(C, B[i]);
    if (a < 0)
      a = 0;
    total += a * (N - c);
  }
  print(total);
}


Fixed submission:


#include <bits/stdc++.h>

#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
#define ll long long
using namespace std;

/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/

ll bsearch_a(vector<ll> v, ll key) {
  int left = -1;
  int right = v.size();

  while (right - left > 1) {
    int mid = left + (right - left) / 2;
    if (v[mid] >= key)
      right = mid;
    else
      left = mid;
  }
  return right;
}

ll bsearch_c(vector<ll> v, ll key) {
  int left = -1;
  int right = v.size();

  while (right - left > 1) {
    int mid = left + (right - left) / 2;
    if (v[mid] > key)
      right = mid;
    else
      left = mid;
  }
  return right;
}

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

  int N;
  cin >> N;
  vector<ll> A(N);
  vector<ll> B(N);
  vector<ll> C(N);
  loop(0, N, i) cin >> A[i];
  loop(0, N, i) cin >> B[i];
  loop(0, N, i) cin >> C[i];
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());

  ll total = 0;
  loop(0, N, i) {
    ll bi = B[i];
    ll a = lower_bound(A.begin(), A.end(), B[i]) - A.begin();
    ll c = C.end() - upper_bound(C.begin(), C.end(), B[i]);
    total += a * c;
  }
  print(total);
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define COUT(x) cout << (x) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using M = map<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  ll ans = 0;
  int n;
  cin >> n;
  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(a, a + n);
  sort(b, b + n);
  sort(c, c + n);

  REP(i, n) {
    int bi = lower_bound(b, b + n, c[i]) - b;
    REP(j, bi) ans += lower_bound(a, a + n, b[j]) - a;
  }
  COUT(ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < n; i++)
#define COUT(x) cout << (x) << endl
#define dump(x) cout << #x << " = " << (x) << endl;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;
using M = map<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  ll ans = 0;
  int n;
  cin >> n;
  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(a, a + n);
  sort(b, b + n);
  sort(c, c + n);

  REP(i, n) {
    ll A = lower_bound(a, a + n, b[i]) - a;
    ll C = n - (upper_bound(c, c + n, b[i]) - c);
    ans += A * C;
  }
  COUT(ans);
  return 0;
}
Problem p03557 (C++)

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

using namespace std;

typedef long long ll;

const ll N = 105;
ll n, cnt;
ll a[N], b[N], c[N];
ll d[N], e[N];

int main() {
  scanf("%lld", &n);
  for (ll i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (ll i = 0; i < n; i++) {
    scanf("%d", &b[i]);
  }
  for (ll i = 0; i < n; i++) {
    scanf("%d", &c[i]);
  }
  sort(a, a + n, greater<ll>());
  sort(b, b + n, greater<ll>());
  for (ll i = 0, j = 0; i < n; i++) {
    for (; j < n; j++) {
      if (b[i] > a[j]) {
        break;
      }
    }
    d[i] = n - j;
  }
  sort(b, b + n);
  sort(c, c + n);
  for (ll i = 0, j = 0; i < n; i++) {
    for (; j < n; j++) {
      if (b[i] < c[j]) {
        break;
      }
    }
    e[i] = n - j;
    cnt += d[n - i - 1] * e[i];
  }
  printf("%lld", cnt);
  return 0;
}


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

using namespace std;

typedef long long ll;

const ll N = 100005;
ll n, cnt;
ll a[N], b[N], c[N];
ll d[N], e[N];

int main() {
  scanf("%lld", &n);
  for (ll i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (ll i = 0; i < n; i++) {
    scanf("%d", &b[i]);
  }
  for (ll i = 0; i < n; i++) {
    scanf("%d", &c[i]);
  }
  sort(a, a + n, greater<ll>());
  sort(b, b + n, greater<ll>());
  for (ll i = 0, j = 0; i < n; i++) {
    for (; j < n; j++) {
      if (b[i] > a[j]) {
        break;
      }
    }
    d[i] = n - j;
  }
  sort(b, b + n);
  sort(c, c + n);
  for (ll i = 0, j = 0; i < n; i++) {
    for (; j < n; j++) {
      if (b[i] < c[j]) {
        break;
      }
    }
    e[i] = n - j;
    cnt += d[n - i - 1] * e[i];
  }
  printf("%lld", cnt);
  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#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);
  long long total = 0;
  for (int i = 0; i < N; i++) {
    long long counta = 0, countc = 0;
    counta = lower_bound(A, A + N, B[i]) - A;
    if (B[i] < C[N - 1])
      countc = N - (upper_bound(C, C + N, B[i]) - C);
    total += counta * countc;
  }
  cout << total << endl;
}

Fixed submission:
#define _GLIBCXX_DEBUG
#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);
  long long total = 0;
  for (int i = 0; i < N; i++) {
    long long counta = 0, countc = 0;
    // counta=lower_bound(A,A+N,B[i])-A;
    int low = 0, high = N - 1;
    if (A[0] < B[i] && A[N - 1] >= B[i]) {
      while (high - low > 1) {
        if (A[(low + high) / 2] < B[i])
          low = (low + high) / 2;
        else
          high = (low + high) / 2;
      }
      counta = high;
    } else if (A[N - 1] < B[i])
      counta = N;
    // if (B[i]<C[N-1]) countc=N-(upper_bound(C,C+N,B[i])-C);
    low = 0, high = N - 1;
    if (B[i] < C[N - 1] && B[i] >= C[0]) {
      while (high - low > 1) {
        if (C[(low + high) / 2] > B[i])
          high = (low + high) / 2;
        else
          low = (low + high) / 2;
      }
      countc = N - high;
    } else if (B[i] < C[0])
      countc = N;
    total += counta * countc;
  }
  cout << total << endl;
}
Problem p03557 (Python)

Buggy submission (status: Runtime Error):
import sys
from bisect import bisect
from itertools import accumulate


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = sorted(map(int, input().split()))
    b = sorted(map(int, input().split()))
    c = sorted(map(int, input().split()))
    # 各中部パーツを乗せられる下部パーツの個数
    # b_c_cnt = [len(c[bisect(c, b[i]) :]) for i in range(n)]
    b_c_cnt = [0] * n
    past = 0
    for i in range(n):
        pos = bisect(c, b[i], lo=past)
        past = pos
        b_c_cnt[i] = n - pos

    # b_c_cntに後ろから累積和をとる
    acc = list(accumulate(b_c_cnt[::-1]))[::-1]
    ans = 0
    past = 0
    for i in range(n):
        pos = bisect(b, a[i], lo=past)
        ans += acc[pos]
        past = pos
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from bisect import bisect
from itertools import accumulate


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = sorted(map(int, input().split()))
    b = sorted(map(int, input().split()))
    c = sorted(map(int, input().split()))
    # 各中部パーツを乗せられる下部パーツの個数
    # b_c_cnt = [len(c[bisect(c, b[i]) :]) for i in range(n)]
    b_c_cnt = [0] * n
    past = 0
    for i in range(n):
        pos = bisect(c, b[i], lo=past)
        past = pos
        b_c_cnt[i] = n - pos

    # b_c_cntに後ろから累積和をとる
    acc = list(accumulate(b_c_cnt[::-1]))[::-1] + [0]
    ans = 0
    past = 0
    for i in range(n):
        pos = bisect(b, a[i], lo=past)
        ans += acc[pos]
        past = pos
    print(ans)


if __name__ == "__main__":
    main()
Problem p03557 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A = sorted(A)
B = sorted(B)
C = sorted(C)

ans = 0

for a in A:
    for i in range(bisect.bisect_right(B, a), len(B)):
        cPtn = len(C) - bisect.bisect_right(C, B[i])
        ans += cPtn

print(ans)


Fixed submission:
import bisect

N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A = sorted(A)
B = sorted(B)
C = sorted(C)

ans = 0

for b in B:
    aPtn = bisect.bisect_left(A, b)
    cPtn = len(C) - bisect.bisect_right(C, b)
    ans += aPtn * cPtn

print(ans)
Problem p03557 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

n = int(input())
aa = list(sorted(map(int, input().split())))
bb = list(sorted(map(int, input().split())))
cc = list(sorted(map(int, input().split())))

ans = 0
for a in aa:
    bi = bisect.bisect(bb, a)
    bx = len(bb) - bi
    for b in range(bi, len(bb)):
        ci = bisect.bisect(cc, bb[b])
        cx = len(cc) - ci
        ans += cx

print(ans)


Fixed submission:
import bisect

n = int(input())
aa = list(sorted(map(int, input().split())))
bb = list(sorted(map(int, input().split())))
cc = list(sorted(map(int, input().split())))

ans = 0
for b in bb:
    ax = bisect.bisect_left(aa, b)
    ci = bisect.bisect(cc, b)
    cx = len(cc) - ci
    ans += ax * cx
print(ans)
Problem p03557 (Python)

Buggy submission (status: Time Limit Exceeded):
from bisect import bisect_left, bisect_right

N = int(input())
A = sorted([int(x) for x in input().split()])
B = sorted([int(x) for x in input().split()])
C = sorted([int(x) for x in input().split()])

ans = 0
for b in B:
    ans += len(A[: bisect_right(A, b - 1)]) * len(C[bisect_left(C, b + 1) :])
print(ans)


Fixed submission:
from bisect import bisect_left, bisect_right

N = int(input())
A = sorted([int(x) for x in input().split()])
B = sorted([int(x) for x in input().split()])
C = sorted([int(x) for x in input().split()])

ans = 0
for b in B:
    ans += bisect_right(A, b - 1) * (N - bisect_left(C, b + 1))
print(ans)
Problem p03557 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

n = int(input())
(*a,) = sorted(map(int, input().split()))
(*b,) = sorted(map(int, input().split()))
(*c,) = sorted(map(int, input().split()))

ans = 0
for _c in c:
    i = bisect.bisect_left(b, _c)
    for j in range(i):
        ans += bisect.bisect_left(a, b[j])

print(ans)


Fixed submission:
import bisect

n = int(input())
(*a,) = sorted(map(int, input().split()))
(*b,) = sorted(map(int, input().split()))
(*c,) = sorted(map(int, input().split()))

ans = 0
for _b in b:
    aNum = bisect.bisect_left(a, _b)
    cNum = len(c) - bisect.bisect_right(c, _b)
    ans += aNum * cNum

print(ans)
Problem p03557 (Python)

Buggy submission (status: Time Limit Exceeded):
import bisect

N = int(input())
A = sorted(map(int, input().split()))
B = list(map(int, input().split()))
C = sorted(map(int, input().split()))

ans = 0
for j in range(len(B)):
    axb = bisect.bisect_left(A, B[j])
    bxc = len(C) - len(C[: bisect.bisect(C, B[j])])
    ans += axb * bxc
print(ans)


Fixed submission:
import bisect

N = int(input())
A = sorted(map(int, input().split()))
B = list(map(int, input().split()))
C = sorted(map(int, input().split()))

print(sum(bisect.bisect_left(A, B[j]) * (N - bisect.bisect(C, B[j])) for j in range(N)))
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}

const int INF = 100000;

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

  vector<ll> a(n, 0);
  vector<ll> b(n, 0);
  vector<ll> c(n, 0);

  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < n; i++)
    cin >> b[i];
  for (ll 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 (ll i = 0; i < n; i++) {
    ll x = lower_bound(b.begin(), b.end(), a[i] + 1) - b.begin();
    if (x >= n) {
      cout << ans << endl;
      return 0;
    } else {
      for (ll j = x; j < n; j++) {
        ll y = lower_bound(c.begin(), c.end(), b[j] + 1) - c.begin();
        if (y < n) {
          ans += n - y;
        } else {
          break;
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}

const int INF = 100000;

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

  vector<ll> a(n, 0);
  vector<ll> b(n, 0);
  vector<ll> c(n, 0);

  for (ll i = 0; i < n; i++)
    cin >> a[i];
  for (ll i = 0; i < n; i++)
    cin >> b[i];
  for (ll 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 (ll i = 0; i < n; i++) {
    ll x = lower_bound(a.begin(), a.end(), b[i]) - a.begin();
    ll y = upper_bound(c.begin(), c.end(), b[i]) - c.begin();
    ans += x * (n - y);
  }

  cout << ans << endl;

  return 0;
}
Problem p03557 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
vector<ll> a, b, c, bb, ab;
int N;
ll t, res = 0;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> t;
    a.push_back(t);
  }
  for (int i = 0; i < N; i++) {
    cin >> t;
    b.push_back(t);
  }
  for (int i = 0; i < N; i++) {
    cin >> t;
    c.push_back(t);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  for (int i = 0; i < N; i++)
    bb.push_back(c.end() - upper_bound(c.begin(), c.end(), b[i]));

  for (int i = 0; i < N; i++) {
    int p = upper_bound(b.begin(), b.end(), a[i]) - b.begin();
    res += accumulate(bb.begin() + p, bb.end(), 0);
  }

  cout << res << endl;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
vector<ll> a, b, c, bb, ab;
int N;
ll t, res = 0;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> t;
    a.push_back(t);
  }
  for (int i = 0; i < N; i++) {
    cin >> t;
    b.push_back(t);
  }
  for (int i = 0; i < N; i++) {
    cin >> t;
    c.push_back(t);
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  for (int i = 0; i < N; i++)
    bb.push_back(c.end() - upper_bound(c.begin(), c.end(), b[i]));

  ll t = 0;
  int jp = N - 1;
  for (int i = N - 1; i >= 0; i--) {
    for (; jp >= 0 && b[jp] > a[i]; jp--)
      t += bb[jp];
    res += t;
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
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;
}
const long long INF = 1LL << 60;

int count(int bit, int n) {
  int cnt = 0;
  REP(i, n) {
    if (bit & 1 << i)
      cnt++;
  }
  return cnt;
}

int main() {
  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(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  int ans = 0;
  REP(idx1, N) {
    // cout <<"idx1:"<< a[idx1] <<" ";
    int idx2 = distance(b.begin(), upper_bound(b.begin(), b.end(), a[idx1]));
    REP2(j, idx2, N) {
      // cout <<"idx2:"<< b[j] <<" ";
      int idx3 = distance(c.begin(), upper_bound(c.begin(), c.end(), b[j]));
      // cout <<"idx3:"<< c[idx3] <<" ";
      ans += (N - idx3);
    }
    // cout  << endl;
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
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;
}
const long long INF = 1LL << 60;

int count(int bit, int n) {
  int cnt = 0;
  REP(i, n) {
    if (bit & 1 << i)
      cnt++;
  }
  return cnt;
}

int main() {
  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(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  ll ans = 0;
  REP(idx2, N) {
    ll idx1 = lower_bound(a.begin(), a.end(), b[idx2]) - a.begin();
    ll idx3 = c.end() - upper_bound(c.begin(), c.end(), b[idx2]);
    ans += idx1 * idx3;
  }

  cout << ans << 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;
  vector<int> A(N), B(N), C(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sort(A.begin(), A.end());
  for (int i = 0; i < N; i++)
    cin >> B[i];
  sort(B.begin(), B.end());
  for (int i = 0; i < N; i++)
    cin >> C[i];
  sort(C.begin(), C.end());

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

  cout << ans << endl;
}

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

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

  long long ans = 0;
  for (int i = 0; i < N; i++) {
    auto j = lower_bound(A.begin(), A.end(), B[i]);
    auto k = upper_bound(C.begin(), C.end(), B[i]);
    long long cnt = distance(A.begin(), j);
    cnt *= distance(k, C.end());
    ans += cnt;
  }

  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), 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());

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

  cout << ans << endl;
}


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

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());

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

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

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
#define ld long double
#define ll long long
#define mod 1000000007
#define IINF INT_MAX
#define INF 1LL << 30

int main() {
  int n;
  cin >> n;
  V<ll> 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(ALL(a));
  sort(ALL(b));
  sort(ALL(c));

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    auto ite1 = upper_bound(ALL(b), a[i]);
    for (auto j = ite1; j < b.end(); j++) {
      auto ite2 = upper_bound(ALL(c), *j);
      ll y = c.end() - ite2;
      ans += y;
    }
  }
  cout << ans << endl;

  return 0;
}


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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
#define ld long double
#define ll long long
#define mod 1000000007
#define IINF INT_MAX
#define INF 1LL << 30

int main() {
  int n;
  cin >> n;
  V<ll> 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(ALL(a));
  sort(ALL(b));
  sort(ALL(c));

  ll ans = 0;
  for (int i = 0; i < n; i++) {
    auto ite1 = lower_bound(ALL(a), b[i]);
    auto ite2 = upper_bound(ALL(c), b[i]);
    ans += (ite1 - a.begin()) * (c.end() - ite2);
  }
  cout << ans << endl;

  return 0;
}
Problem p03557 (C++)

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

signed main() {

  // omajinai
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  // omajinai

  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());

  a.push_back(10000000000000);
  c.push_back(-1);

  int A = 0, C = 0;
  int ans = 0;

  for (int i = 0; i < n; i++) {
    while (a.at(A) < b.at(i)) {
      A++;
    }
    while (c.at(C) <= b.at(i)) {
      C++;
    }

    ans += A * (n - C);
  }

  cout << ans << endl;
}


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

signed main() {

  // omajinai
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  // omajinai

  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());

  a.push_back(10000000000000);
  c.push_back(10000000000000);

  int A = 0, C = 0;
  int ans = 0;

  for (int i = 0; i < n; i++) {
    while (a.at(A) < b.at(i)) {
      A++;
    }
    while (c.at(C) <= b.at(i)) {
      C++;
    }

    ans += A * (n - C);
  }

  cout << ans << endl;
}