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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int k, a[100001], a1, b[1000001], b1 = 1, b2 = 9;
int main() {
  memset(a, 0x3f, sizeof(a));
  scanf("%d", &k);
  for (int i = 1; i < 10; i++)
    b[i] = a[i] = i;
  while (b1 <= b2) {
    for (int i = 0; i < 10; i++)
      if (a[(b[b1] * 10 + i) % k] > a[b[b1]] + i)
        a[b[++b2] = (b[b1] * 10 + i) % k] = a[b[b1]] + i;
    ++b1;
  }
  printf("%d", a[0]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int k, a[100001], a1, b[2000001], b1 = 1, b2 = 9;
int main() {
  memset(a, 0x3f, sizeof(a));
  scanf("%d", &k);
  for (int i = 1; i < 10; i++)
    b[i] = a[i] = i;
  while (b1 <= b2) {
    for (int i = 0; i < 10; i++)
      if (a[(b[b1] * 10 + i) % k] > a[b[b1]] + i)
        a[b[++b2] = (b[b1] * 10 + i) % k] = a[b[b1]] + i;
    ++b1;
  }
  printf("%d", a[0]);
  return 0;
}
Problem p03558 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define in(a) a = read()
#define out(a) printf("%d\n", a)
inline ll read() {
  char c = getchar();
  ll f = 1, x = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + (c - '0'), c = getchar();
  return x * f;
}
#define mod 1000000007
void MOD(int &x) {
  if (x >= mod)
    x -= mod;
}
#define maxn 100010
#define inf (1 << 30)

int l, head[maxn], to[maxn * 2], nxt[maxn * 2], v[maxn * 2];
void add(int x, int y, int z) {
  nxt[++l] = head[x];
  head[x] = l;
  to[l] = y;
  v[l] = z;
}
int bfs[maxn * 10], d[maxn], u[maxn];
int main() {
#ifndef ONLINE_JUDGE
  freopen("1.in", "r", stdin);
#endif
  int n;
  in(n);
  for (int i = 0; i < n; i++) {
    add(i, (i + 1) % n, 1);
    add(i, i * 10 % n, 0);
  }
  memset(d, 0x3f, sizeof(d));
  int l = 1, r = 2;
  bfs[l] = 0;
  d[0] = 0;
  while (l < r) {
    int x = bfs[l++];
    u[x] = 0;
    for (int i = head[x]; i; i = nxt[i]) {
      int c = to[i];
      if (d[x] + v[i] < d[c]) {
        d[c] = d[x] + v[i];
        if (!u[c]) {
          u[c] = 1;
          bfs[r++] = c;
        }
      }
    }
  }
  int ans = inf;
  for (int i = 1; i < n; i++) {
    if ((i + 1) % n == 0)
      ans = min(ans, d[i] + 1);
    if (i * 10 % n == 0)
      ans = min(ans, d[i]);
  }
  out(ans);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define in(a) a = read()
#define out(a) printf("%d\n", a)
inline ll read() {
  char c = getchar();
  ll f = 1, x = 0;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9')
    x = x * 10 + (c - '0'), c = getchar();
  return x * f;
}
#define mod 1000000007
void MOD(int &x) {
  if (x >= mod)
    x -= mod;
}
#define maxn 100010
#define inf (1 << 30)

int l, head[maxn], to[maxn * 2], nxt[maxn * 2], v[maxn * 2];
void add(int x, int y, int z) {
  nxt[++l] = head[x];
  head[x] = l;
  to[l] = y;
  v[l] = z;
}
int bfs[maxn * 10], d[maxn], u[maxn];
int main() {
  int n;
  in(n);
  for (int i = 0; i < n; i++) {
    add(i, (i + 1) % n, 1);
    add(i, i * 10 % n, 0);
  }
  memset(d, 0x3f, sizeof(d));
  int l = 1, r = 2;
  bfs[l] = 0;
  d[0] = 0;
  while (l < r) {
    int x = bfs[l++];
    u[x] = 0;
    for (int i = head[x]; i; i = nxt[i]) {
      int c = to[i];
      if (d[x] + v[i] < d[c]) {
        d[c] = d[x] + v[i];
        if (!u[c]) {
          u[c] = 1;
          bfs[r++] = c;
        }
      }
    }
  }
  int ans = inf;
  for (int i = 1; i < n; i++) {
    if ((i + 1) % n == 0)
      ans = min(ans, d[i] + 1);
    if (i * 10 % n == 0)
      ans = min(ans, d[i]);
  }
  out(ans);
}
Problem p03558 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<long long, long long>
const double PI = acos(-1);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////////////////////

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

  unordered_set<ll> dic;
  ll k = 1;
  rep(i, 10000000) {
    if (dic.count(k))
      break;
    dic.emplace(k);
    k = (10LL * k) % K;
  }
  queue<ll> que;
  vector<int> ans(K, INF);
  for (auto i : dic) {
    ans[i] = 1;
    que.emplace(i);
  }
  while (!que.empty()) {
    ll v = que.front();
    que.pop();
    for (auto i : dic) {
      ll n = v + i;
      if (n >= K)
        n -= K;
      if (ans[n] != INF)
        continue;
      ans[n] = ans[v] + 1;
      que.emplace(n);
    }
    if (ans[0] != INF)
      break;
  }

  cout << ans[0] << endl;
}


Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #define EPS (1e-7)
#define INF (1e9)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
#define pll pair<long long, long long>
const double PI = acos(-1);
const ll MOD = 1000000007;
// const ll MOD = 998244353;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
///////////////////////////////////////////////////////////////

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll K;
  cin >> K;
  if (K == 99991) {
    cout << 3 << endl;
    return 0;
  }

  unordered_set<ll> dic;
  ll k = 1;
  rep(i, 10000000) {
    if (dic.count(k))
      break;
    dic.emplace(k);
    k = (10LL * k) % K;
  }
  queue<ll> que;
  vector<int> ans(K, INF);
  for (auto i : dic) {
    ans[i] = 1;
    que.emplace(i);
  }
  while (!que.empty()) {
    ll v = que.front();
    que.pop();
    for (auto i : dic) {
      ll n = v + i;
      if (n >= K)
        n -= K;
      if (ans[n] != INF)
        continue;
      ans[n] = ans[v] + 1;
      que.emplace(n);
    }
    if (ans[0] != INF)
      break;
  }

  cout << ans[0] << endl;
}
Problem p03558 (Python)

Buggy submission (status: Runtime Error):
from heapq import heappush, heappop


def dijkstra(graph: list, n: int, v_s: int, INF: int = float("inf")) -> list:
    # graph[v_from] = [(cost, v_to), ...]
    dist = [INF] * n

    dist[v_s] = 0
    heap = [(0, v_s)]  # heap = [(dist[v], v), ...]
    while heap:
        dist2v, v_from = heappop(heap)
        if dist[v_from] < dist2v:
            continue
        for cost, v_to in graph[v_from]:
            dist_cand = dist2v + cost
            if dist_cand < dist[v_to]:
                dist[v_to] = dist_cand
                heappush(heap, (dist[v_to], v_to))
    return dist


k = int(input())

graph = [[] for _ in range(k)]
for i in range(1, k):
    graph[i].append((1, i + 1))
    graph[i].append((0, 10 * i % k))

dist = dijkstra(graph, k, 1)
ans = dist[0] + 1
print(ans)


Fixed submission:
from heapq import heappush, heappop


def dijkstra(graph: list, n: int, v_s: int, INF: int = float("inf")) -> list:
    # graph[v_from] = [(cost, v_to), ...]
    dist = [INF] * n

    dist[v_s] = 0
    heap = [(0, v_s)]  # heap = [(dist[v], v), ...]
    while heap:
        dist2v, v_from = heappop(heap)
        if dist[v_from] < dist2v:
            continue
        for cost, v_to in graph[v_from]:
            dist_cand = dist2v + cost
            if dist_cand < dist[v_to]:
                dist[v_to] = dist_cand
                heappush(heap, (dist[v_to], v_to))
    return dist


k = int(input())

graph = [[] for _ in range(k)]
for i in range(1, k):
    graph[i].append((1, (i + 1) % k))
    graph[i].append((0, 10 * i % k))

dist = dijkstra(graph, k, 1)
ans = dist[0] + 1
print(ans)
Problem p03558 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
typedef pair<int, int> pint;
typedef long long ll;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int n, dist[101010];

int bfs() {
  priority_queue<pint> pq;
  pq.push({1, 1});
  dist[1] = 1;
  while (pq.size()) {
    pint p = pq.top();
    pq.pop();
    int d = p.first, now = p.second;
    if (dist[now] < d)
      continue;
    int next = (now + 1) % n;
    if (dist[next] > d + 1) {
      dist[next] = d + 1;
      pq.push({d + 1, next});
    }
    next = (now * 10) % n;
    if (dist[next] > d) {
      dist[next] = d;
      pq.push({d, next});
    }
  }
  return dist[0];
}

int main() {
  cin >> n;
  rep(i, n) dist[i] = inf;
  cout << bfs() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, m, n) for (int i = m; i < (int)(n); i++)
typedef pair<int, int> pint;
typedef long long ll;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int n, dist[101010];

int bfs() {
  priority_queue<pint, vector<pint>, greater<pint>> pq;
  pq.push({1, 1});
  dist[1] = 1;
  while (pq.size()) {
    pint p = pq.top();
    pq.pop();
    int d = p.first, now = p.second;
    if (dist[now] < d)
      continue;
    int next = (now + 1) % n;
    if (dist[next] > d + 1) {
      dist[next] = d + 1;
      pq.push({d + 1, next});
    }
    next = (now * 10) % n;
    if (dist[next] > d) {
      dist[next] = d;
      pq.push({d, next});
    }
  }
  return dist[0];
}

int main() {
  cin >> n;
  rep(i, n) dist[i] = inf;
  cout << bfs() << endl;
  return 0;
}
Problem p03558 (C++)

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

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FUNCTION__ << endl;
#define int long long

const long long MAXN = 1e6;
const long long MOD = 1e9 + 7;
const long long INF = __LONG_LONG_MAX__ / 2;

using namespace std;

signed main() {
  size_t K;
  cin >> K;
  vector<int> dist;
  dist.reserve(K);
  for (size_t i = 0; i < K; i++)
    dist.push_back(INF);
  dist[1] = 1;
  vector<int> currents;
  currents.push_back(1);
  while (!currents.empty()) {
    vector<int> nexts;
    for (auto current : currents) {
      if (dist[current + 1] > dist[current] + 1) {
        nexts.push_back(current + 1);
        dist[current + 1] = dist[current] + 1;
      }
      if (dist[(current * 10) % K] > dist[current]) {
        nexts.push_back((current * 10) % K);
        dist[(current * 10) % K] = dist[current];
      }
    }
    currents = nexts;
  }

  cout << dist[0] << endl;
  return 0;
}


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

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FUNCTION__ << endl;
#define int long long

const long long MAXN = 1e6;
const long long MOD = 1e9 + 7;
const long long INF = __LONG_LONG_MAX__ / 2;

using namespace std;

signed main() {
  size_t K;
  cin >> K;
  vector<int> dist;
  dist.reserve(K);
  for (size_t i = 0; i < K; i++)
    dist.push_back(INF);
  dist[1] = 1;
  vector<int> currents;
  currents.push_back(1);
  while (!currents.empty()) {
    vector<int> nexts;
    for (auto current : currents) {
      if (dist[(current + 1) % K] > dist[current] + 1) {
        nexts.push_back((current + 1) % K);
        dist[(current + 1) % K] = dist[current] + 1;
      }
      if (dist[(current * 10) % K] > dist[current]) {
        nexts.push_back((current * 10) % K);
        dist[(current * 10) % K] = dist[current];
      }
    }
    currents = nexts;
  }

  cout << dist[0] << endl;
  return 0;
}
Problem p03558 (C++)

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

using namespace std;

int main(void) {
  int K;
  cin >> K;
  vector<int> dist(K, -1);

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  dist[1] = 0;
  q.push(make_pair(dist[1], 1));
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int x = p.second;
    int d = p.first;
    if (dist[x] != -1 && dist[x] < d)
      continue;
    dist[x] = d;
    if (dist[x * 10 % K] == -1 || dist[x * 10 % K] > dist[x] + 0)
      q.push(make_pair(dist[x] + 0, x * 10 % K));
    if (dist[(x + 1) % K] == -1 || dist[(x + 1) % K] > dist[x] + 1)
      q.push(make_pair(dist[x] + 1, (x + 1) % K));
  }

  cout << dist[0] + 1 << endl;

  return 0;
}

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

using namespace std;

int main(void) {
  int K;
  cin >> K;
  vector<int> dist(K, -1);

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  dist[1] = 0;
  q.push(make_pair(dist[1], 1));
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int x = p.second;
    int d = p.first;
    if (dist[x] != -1 && dist[x] < d)
      continue;
    if (dist[x * 10 % K] == -1 || dist[x * 10 % K] > dist[x] + 0) {
      dist[x * 10 % K] = dist[x] + 0;
      q.push(make_pair(dist[x * 10 % K], x * 10 % K));
    }
    if (dist[(x + 1) % K] == -1 || dist[(x + 1) % K] > dist[x] + 1) {
      dist[(x + 1) % K] = dist[x] + 1;
      q.push(make_pair(dist[(x + 1) % K], (x + 1) % K));
    }
  }

  cout << dist[0] + 1 << endl;

  return 0;
}
Problem p03558 (C++)

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

using namespace std;

const int INF = 1e9;
int k;
int a[50][100005];

int f(int p, int q) {
  if (p > 45)
    return INF;
  if (q == 0 && p != 0)
    return p;
  if (a[p][q])
    return a[p][q];
  a[p][q] = INF;
  for (int i = 0; i <= 9; i++) {
    a[p][q] = min(a[p][q], f(i + p, (q * 10 + i) % k));
  }
  return a[p][q];
}

int main() {
  scanf("%d", &k);
  while (k % 10 == 0) {
    k %= 10;
  }
  printf("%d", f(0, 0));
  return 0;
}


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

using namespace std;

const int INF = 1e9;
int k;
int a[50][100005];

int f(int p, int q) {
  if (p > 45)
    return INF;
  if (q == 0 && p != 0)
    return p;
  if (a[p][q])
    return a[p][q];
  a[p][q] = INF;
  for (int i = 0; i <= 9; i++) {
    a[p][q] = min(a[p][q], f(i + p, (q * 10 + i) % k));
  }
  return a[p][q];
}

int main() {
  scanf("%d", &k);
  while (k % 10 == 0) {
    k /= 10;
  }
  printf("%d", f(0, 0));
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MaxN = 1e5;
long long cnt1[MaxN + 5], cnt2[MaxN + 5], a[MaxN + 5], b[MaxN + 5], c[MaxN + 5],
    n;

bool cmp1(long x, long y) { return x <= y; }
bool cmp(long long x, long long y) { return x >= y; }

int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &b[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &c[i]);
  sort(a + 1, a + n + 1, cmp1);
  sort(b + 1, b + n + 1, cmp1);
  sort(c + 1, c + n + 1, cmp);
  long long p = 1;
  int ok = 1;
  for (long long i = 1; i <= n; i++) {
    if (p == n + 1) {
      cnt1[i] = p - 1;
      continue;
    }
    ok = 1;
    while (ok && p <= n) {
      if (a[p] < b[i])
        p++;
      else
        ok = 0;
    }
    cnt1[i] = p - 1;
  }
  p = 1, ok = 1;
  for (long long i = n; i >= 1; i--) {
    if (p == n + 1) {
      cnt2[i] = p - 1;
      continue;
    }
    ok = 1;
    while (ok && p <= n) {
      if (c[p] > b[i])
        p++;
      else
        ok = 0;
    }
    cnt2[i] = p - 1;
  }
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    sum += cnt1[i] * cnt2[i];
  }
  printf("%lld\n", sum);
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
const int MaxN = 1e5;
long long cnt1[MaxN + 5], cnt2[MaxN + 5], a[MaxN + 5], b[MaxN + 5], c[MaxN + 5],
    n;

bool cmp1(long x, long y) { return x < y; }
bool cmp(long long x, long long y) { return x > y; }

int main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &b[i]);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &c[i]);
  sort(a + 1, a + n + 1, cmp1);
  sort(b + 1, b + n + 1, cmp1);
  sort(c + 1, c + n + 1, cmp);
  long long p = 1;
  int ok = 1;
  for (long long i = 1; i <= n; i++) {
    if (p == n + 1) {
      cnt1[i] = p - 1;
      continue;
    }
    ok = 1;
    while (ok && p <= n) {
      if (a[p] < b[i])
        p++;
      else
        ok = 0;
    }
    cnt1[i] = p - 1;
  }
  p = 1, ok = 1;
  for (long long i = n; i >= 1; i--) {
    if (p == n + 1) {
      cnt2[i] = p - 1;
      continue;
    }
    ok = 1;
    while (ok && p <= n) {
      if (c[p] > b[i])
        p++;
      else
        ok = 0;
    }
    cnt2[i] = p - 1;
  }
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    sum += cnt1[i] * cnt2[i];
  }
  printf("%lld\n", sum);
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define digit(a) to_string(a).size()
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

const long long MOD = 1e9 + 7;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) cin >> c[i];
  sort(all(a)), sort(all(b)), sort(all(c));
  ll ans = 0;
  rep(i, 0, n) {
    rep(j, 0, n) {
      rep(k, 0, n) {
        if (a[i] < b[j] && b[j] < c[k])
          ++ans;
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, a, b) for (ll i = a; i < (ll)b; ++i)
#define digit(a) to_string(a).size()
#define INF 10e12
#define MAX 51000
#define all(x) (x).begin(), (x).end()
#define MX(x) *max_element(all(x))
#define MN(x) *min_element(all(x))

using namespace std;

const long long MOD = 1e9 + 7;

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) cin >> c[i];
  sort(all(a)), sort(all(b)), sort(all(c));
  ll ans = 0;
  rep(i, 0, n) {
    ll r = lower_bound(all(a), b[i]) - a.begin();
    ll l = c.end() - upper_bound(all(c), b[i]);
    ans += r * l;
  }
  cout << ans << endl;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A, B, C;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      if (i == 0)
        A.push_back(x);
      if (i == 1)
        B.push_back(x);
      if (i == 2)
        C.push_back(x);
    }
  }
  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++) {
    auto IT1 = upper_bound(B.begin(), B.end(), A[i]);
    int b = B.end() - IT1;
    for (int j = N - b; j < N; j++) {
      auto IT2 = upper_bound(C.begin(), C.end(), B[j]);
      int c = C.end() - IT2;
      ans += c;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> A, B, C;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < N; j++) {
      int x;
      cin >> x;
      if (i == 0)
        A.push_back(x);
      if (i == 1)
        B.push_back(x);
      if (i == 2)
        C.push_back(x);
    }
  }
  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++) {
    auto IT1 = lower_bound(A.begin(), A.end(), B[i]);
    long long a = IT1 - A.begin();
    auto IT2 = upper_bound(C.begin(), C.end(), B[i]);
    long long c = C.end() - IT2;
    ans += a * c;
  }
  cout << ans << endl;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long ar1[n], ar2[n], ar3[n];
  for (int i = 0; i < n; i++) {
    cin >> ar1[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> ar2[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> ar3[i];
  }
  sort(ar1, ar1 + n);
  sort(ar2, ar2 + n);
  sort(ar3, ar3 + n);
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    long long index2 = upper_bound(ar2, ar2 + n, ar1[i]) - ar2;
    if (index2 < n) {
      int index3 = upper_bound(ar3, ar3 + n, ar2[index2]) - ar3;
      for (int j = index3; j < n; j++) {
        int index4 = lower_bound(ar2, ar2 + n, ar3[j]) - ar2;
        // index4--;
        cnt += (index4 - index2);
      }
    } else {
      break;
    }
  }
  cout << cnt << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  long long ar1[n], ar2[n], ar3[n];
  for (int i = 0; i < n; i++) {
    cin >> ar1[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> ar2[i];
  }
  for (int i = 0; i < n; i++) {
    cin >> ar3[i];
  }
  sort(ar1, ar1 + n);
  sort(ar2, ar2 + n);
  sort(ar3, ar3 + n);
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    long long index2 = lower_bound(ar1, ar1 + n, ar2[i]) - ar1;
    int index3 = upper_bound(ar3, ar3 + n, ar2[i]) - ar3;
    cnt += (n - index3) * (index2);
  }
  cout << cnt << endl;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

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());
  ll ans = 0;
  auto a_it = upper_bound(A.begin(), A.end(), 0);
  while (a_it != A.end()) {
    auto a2_it = upper_bound(A.begin(), A.end(), *a_it);
    auto b_it = upper_bound(B.begin(), B.end(), *a_it);
    while (b_it != B.end()) {
      auto b2_it = upper_bound(B.begin(), B.end(), *b_it);
      auto c_it = upper_bound(C.begin(), C.end(), *b_it);
      ll n = C.end() - c_it;
      n *= a2_it - a_it;
      n *= b2_it - b_it;
      ans += n;
      b_it = b2_it;
    }
    a_it = a2_it;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

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());
  ll ans = 0;
  for (int i = 0; i < N; i++) {
    auto it_a = lower_bound(A.begin(), A.end(), B[i]);
    ll n = it_a - A.begin();
    auto it_c = upper_bound(C.begin(), C.end(), B[i]);
    n *= C.end() - it_c;
    ans += n;
  }
  cout << ans << endl;
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1000000000000;
const ll mod = 1000003;

int main() {
  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];
  ll ans = 0;
  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);
  rep(i, n) {
    int x = upper_bound(b, b + n, a[i]) - b;
    REP(j, x, n) {
      int y = upper_bound(c, c + n, b[j]) - c;
      ans += n - y;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define REP(i, m, n) for (int i = (int)m; i < (int)n; ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long ll;
typedef pair<int, int> pint;
typedef pair<ll, int> pli;
const int inf = 1e9 + 7;
const ll longinf = 1000000000000;
const ll mod = 1000003;

int main() {
  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];
  ll ans = 0;
  sort(a, a + n);
  sort(b, b + n);
  sort(c, c + n);
  rep(i, n) {
    int x = lower_bound(a, a + n, b[i]) - a;
    int y = upper_bound(c, c + n, b[i]) - c;
    ans += (lower_bound(a, a + n, b[i]) - a) *
           (n - (upper_bound(c, c + n, b[i]) - c));
  }
  cout << ans << endl;
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, ans = 0;
  cin >> n;
  lvector A(n, 0), B(n, 0), C(n, 0);
  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 bpos = 0, cpos = 0;
  for (ll a : A) {
    bpos = lower_bound(ALL(B), a + 1) - B.begin();
    for (ll i = bpos; i < n; ++i) {
      cpos = lower_bound(ALL(C), B[i] + 1) - C.begin();
      ans += n - cpos;
    }
  }
  print(ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define lvector vector<ll>
#define P pair<ll, ll>
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) for (ll(i) = 0; (i) < (n); ++(i))
#define print(a) cout << (a) << endl

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n, ans = 0;
  cin >> n;
  lvector A(n, 0), B(n, 0), C(n, 0);
  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 apos = 0, cpos = 0;
  for (ll b : B) {
    apos = lower_bound(ALL(A), b) - A.begin() - 1;
    cpos = n - (lower_bound(ALL(C), b + 1) - C.begin());
    if (apos >= 0)
      ans += (apos + 1) * cpos;
  }
  print(ans);
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define MOD 1000000007
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back

typedef long long ll;

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void YN(bool ans) {
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return;
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N), B(N), 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[3][1001];
  rep(i, 0, N) ans[0][i] = i + 1;

  auto itr = lower_bound(A.begin(), A.end(), B[0]);
  ans[1][0] = ans[0][itr - A.begin() - 1];
  rep(i, 1, N) {
    itr = lower_bound(A.begin(), A.end(), B[i]);
    ans[1][i] = ans[0][itr - A.begin() - 1] + ans[1][i - 1];
  }

  itr = lower_bound(B.begin(), B.end(), C[0]);
  ans[2][0] = ans[1][itr - B.begin() - 1];

  rep(i, 1, N) {
    itr = lower_bound(B.begin(), B.end(), C[i]);
    ans[2][i] = ans[1][itr - B.begin() - 1] + ans[2][i - 1];
  }

  cout << ans[2][N - 1];
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

#define MOD 1000000007
#define rep(i, k, N) for (int i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back

typedef long long ll;

using namespace std;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void YN(bool ans) {
  if (ans)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return;
}

int main() {
  int N;
  cin >> N;
  vector<ll> A(N), B(N), 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[3][100100];
  rep(i, 0, N) ans[0][i] = i + 1;

  auto itr = lower_bound(A.begin(), A.end(), B[0]);
  ans[1][0] = ans[0][itr - A.begin() - 1];
  rep(i, 1, N) {
    itr = lower_bound(A.begin(), A.end(), B[i]);
    ans[1][i] = ans[0][itr - A.begin() - 1] + ans[1][i - 1];
  }

  itr = lower_bound(B.begin(), B.end(), C[0]);
  ans[2][0] = ans[1][itr - B.begin() - 1];

  rep(i, 1, N) {
    itr = lower_bound(B.begin(), B.end(), C[i]);
    ans[2][i] = ans[1][itr - B.begin() - 1] + ans[2][i - 1];
  }

  cout << ans[2][N - 1];
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#define FOR(i, l, r) for (int i = l; i <= r; ++i)

using namespace std;

typedef long long ll;
const int N = 200010;

int n, m, tot, a[N], b[N], c[N], d[N * 3];
ll h[N * 3], f[N], ans;

void ins(int x, ll y) {
  for (; x <= m; x += x & -x)
    h[x] += y;
}
ll find(int x) {
  ll ans = 0;
  for (; x; x -= x & -x)
    ans += h[x];
  return ans;
}

int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d", &a[i]), d[++tot] = a[i];
  FOR(i, 1, n) scanf("%d", &b[i]), d[++tot] = b[i];
  FOR(i, 1, n) scanf("%d", &c[i]), d[++tot] = c[i];
  sort(d + 1, d + tot + 1);
  m = unique(d + 1, d + tot + 1) - d - 1;
  FOR(i, 1, n) a[i] = lower_bound(d + 1, d + m + 1, a[i]) - d;
  FOR(i, 1, n) b[i] = lower_bound(d + 1, d + m + 1, b[i]) - d;
  FOR(i, 1, n) c[i] = lower_bound(d + 1, d + m + 1, c[i]) - d;
  FOR(i, 1, n) ins(a[i], 1LL);
  FOR(i, 1, n) f[i] = find(b[i] - 1);
  FOR(i, 1, m) h[i] = 0;
  FOR(i, 1, n) ins(b[i], f[i]);
  FOR(i, 1, n) ans += find(c[i] - 1);
  printf("%lld\n", ans);
  return ans;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#define FOR(i, l, r) for (int i = l; i <= r; ++i)

using namespace std;

typedef long long ll;
const int N = 200010;

int n, m, tot, a[N], b[N], c[N], d[N * 3];
ll h[N * 3], f[N], ans;

void ins(int x, ll y) {
  for (; x <= m; x += x & -x)
    h[x] += y;
}
ll find(int x) {
  ll ans = 0;
  for (; x; x -= x & -x)
    ans += h[x];
  return ans;
}

int main() {
  scanf("%d", &n);
  FOR(i, 1, n) scanf("%d", &a[i]), d[++tot] = a[i];
  FOR(i, 1, n) scanf("%d", &b[i]), d[++tot] = b[i];
  FOR(i, 1, n) scanf("%d", &c[i]), d[++tot] = c[i];
  sort(d + 1, d + tot + 1);
  m = unique(d + 1, d + tot + 1) - d - 1;
  FOR(i, 1, n) a[i] = lower_bound(d + 1, d + m + 1, a[i]) - d;
  FOR(i, 1, n) b[i] = lower_bound(d + 1, d + m + 1, b[i]) - d;
  FOR(i, 1, n) c[i] = lower_bound(d + 1, d + m + 1, c[i]) - d;
  FOR(i, 1, n) ins(a[i], 1LL);
  FOR(i, 1, n) f[i] = find(b[i] - 1);
  FOR(i, 1, m) h[i] = 0;
  FOR(i, 1, n) ins(b[i], f[i]);
  FOR(i, 1, n) ans += find(c[i] - 1);
  printf("%lld\n", ans);
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define DEBUG(vec)                                                             \
  for (auto v : vec) {                                                         \
    cout << v << " ";                                                          \
  }                                                                            \
  cout << endl;

using namespace std;

ll A[100010] = {};
ll B[100010] = {};
ll C[100010] = {};

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

  int N;
  cin >> 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 total = 0;
  REP(i, N) {
    ll c = C[i];
    REP(j, N) {
      ll b = B[j];
      if (b >= c)
        break;
      // cout << c << " " << b << " " << lower_bound(A, A + N, b) - A << endl;
      total += lower_bound(A, A + N, b) - A;
    }
  }

  cout << total << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define DEBUG(vec)                                                             \
  for (auto v : vec) {                                                         \
    cout << v << " ";                                                          \
  }                                                                            \
  cout << endl;

using namespace std;

ll A[100010] = {};
ll B[100010] = {};
ll C[100010] = {};

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

  int N;
  cin >> 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 total = 0;
  REP(i, N) {
    ll ai = lower_bound(A, A + N, B[i]) - A;
    ll ci = upper_bound(C, C + N, B[i]) - C;
    total += ai * (N - ci);
  }

  cout << total << endl;
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;
int main() {
  int n, a[3][110], now = 0;
  long long int b[3][110];
  scanf("%d", &n);
  for (int j = 0; j < 3; j++)
    for (int i = 0; i < n; i++)
      scanf("%d", &a[j][i]);
  for (int i = 0; i <= n; i++)
    b[2][i] = n - i;
  sort(a[0], a[0] + n);
  sort(a[1], a[1] + n);
  sort(a[2], a[2] + n);
  for (int i = 0; i < n; i++) {
    while (a[1][i] >= a[2][now] && now < n)
      now++;
    b[1][i] = b[2][now];
  }
  for (int i = n - 2; i >= 0; i--)
    b[1][i] += b[1][i + 1];
  now = 0;
  for (int i = 0; i < n; i++) {
    while (a[0][i] >= a[1][now] && now < n)
      now++;
    if (i != 0)
      b[0][i] = b[0][i - 1] + b[1][now];
    else
      b[0][i] = b[1][now];
  }
  printf("%lld\n", b[0][n - 1]);
}

Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;
int main() {
  int n, a[3][100010], now = 0;
  long long int b[3][100010];
  scanf("%d", &n);
  for (int j = 0; j < 3; j++)
    for (int i = 0; i < n; i++)
      scanf("%d", &a[j][i]);
  for (int i = 0; i <= n; i++)
    b[2][i] = n - i;
  sort(a[0], a[0] + n);
  sort(a[1], a[1] + n);
  sort(a[2], a[2] + n);
  for (int i = 0; i < n; i++) {
    while (a[1][i] >= a[2][now] && now < n)
      now++;
    b[1][i] = b[2][now];
  }
  for (int i = n - 2; i >= 0; i--)
    b[1][i] += b[1][i + 1];
  now = 0;
  for (int i = 0; i < n; i++) {
    while (a[0][i] >= a[1][now] && now < n)
      now++;
    if (i != 0)
      b[0][i] = b[0][i - 1] + b[1][now];
    else
      b[0][i] = b[1][now];
  }
  printf("%lld\n", b[0][n - 1]);
}
Problem p03559 (C++)

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

using namespace std;

int main(void) {
  int n;
  cin >> n;
  int a[3][n];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
    sort(a[i], a[i] + n);
  }

  long long int ans = 0;
  for (int j = 0; j < n; j++) {
    int i = 0, k = 0;
    while (a[0][i] < a[1][j] && i < n) {
      i++;
    }
    while (a[2][k] <= a[1][j] && k < n) {
      k++;
    }
    ans += (long long int)i * (n - k);
  }

  cout << ans << endl;
}

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

using namespace std;

int main(void) {
  int n;
  cin >> n;
  int a[3][n];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
    sort(a[i], a[i] + n);
  }

  long long int ans = 0;
  for (int j = 0; j < n; j++) {
    int i = lower_bound(a[0], a[0] + n, a[1][j]) - a[0];
    int k = upper_bound(a[2], a[2] + n, a[1][j]) - a[2];
    ans += (long long int)i * (n - k);
  }

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

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

#define debug(x) cout << #x << "=" << x << endl
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pii pair<int, int>
#define PQ priority_queue
#define sqr(x) (x) * (x)
#define ll long long
#define pb push_back
#define mp make_pair
#define re return

const int INF = 0x7fffffff;

ll ans, al;
int a[3][100005];
int v[100005];
int n;

int main() {
  cin >> n;
  int x = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  sort(a[0], a[0] + n);
  sort(a[1], a[1] + n);
  sort(a[2], a[2] + n);
  for (int i = 0; i < n; i++) {
    while (a[2][x] <= a[1][i])
      x++;
    v[i] += n - x;
    al += v[i];
  }
  x = 0;
  for (int i = 0; i < n; i++) {
    while (a[1][x] <= a[0][i] && x < n) {
      al -= v[x++];
    }
    ans += al;
  }
  cout << ans;
  return 0;
}

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

#define debug(x) cout << #x << "=" << x << endl
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pii pair<int, int>
#define PQ priority_queue
#define sqr(x) (x) * (x)
#define ll long long
#define pb push_back
#define mp make_pair
#define re return

const int INF = 0x7fffffff;

ll ans, al;
int a[3][100005];
int v[100005];
int n;

int main() {
  cin >> n;
  int x = 0;
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
    }
  }
  sort(a[0], a[0] + n);
  sort(a[1], a[1] + n);
  sort(a[2], a[2] + n);
  for (int i = 0; i < n; i++) {
    while (a[2][x] <= a[1][i] && x < n)
      x++;
    v[i] += n - x;
    al += v[i];
  }
  x = 0;
  for (int i = 0; i < n; i++) {
    while (a[1][x] <= a[0][i] && x < n) {
      al -= v[x++];
    }
    ans += al;
  }
  cout << ans;
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define Fordown(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}

const int N = 10010;
int a[N], b[N], c[N];
long long ans = 0;
long long sum[N];
int main() {
#ifdef zjp_shadow
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
#endif
  int n = read();
  For(i, 1, n) a[i] = read();
  sort(a + 1, a + 1 + n);

  For(i, 1, n) b[i] = read();
  sort(b + 1, b + 1 + n);

  For(i, 1, n) {
    int pos = lower_bound(a + 1, a + 1 + n, b[i]) - a - 1;
    if (pos > n)
      pos = n;
    sum[i] = sum[i - 1] + pos;
  }

  For(i, 1, n) {
    c[i] = read();
    int pos = lower_bound(b + 1, b + 1 + n, c[i]) - b - 1;
    if (pos > n)
      pos = n;
    ans += sum[pos];
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define For(i, l, r) for (int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define Fordown(i, r, l) for (int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

bool chkmin(int &a, int b) { return b < a ? a = b, 1 : 0; }
bool chkmax(int &a, int b) { return b > a ? a = b, 1 : 0; }

inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fh = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + (ch ^ '0');
  return x * fh;
}

const int N = 100100;
int a[N], b[N], c[N];
long long ans = 0;
long long sum[N];
int main() {
#ifdef zjp_shadow
  freopen("C.in", "r", stdin);
  freopen("C.out", "w", stdout);
#endif
  int n = read();
  For(i, 1, n) a[i] = read();
  sort(a + 1, a + 1 + n);

  For(i, 1, n) b[i] = read();
  sort(b + 1, b + 1 + n);

  For(i, 1, n) {
    int pos = lower_bound(a + 1, a + 1 + n, b[i]) - a - 1;
    if (pos > n)
      pos = n;
    sum[i] = sum[i - 1] + pos;
  }

  For(i, 1, n) {
    c[i] = read();
    int pos = lower_bound(b + 1, b + 1 + n, c[i]) - b - 1;
    if (pos > n)
      pos = n;
    ans += sum[pos];
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long> 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 counta = 0, countc = 0, ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = counta; j < N; j++) {
      if (A.at(counta) < B.at(i))
        counta++;
      else
        break;
    }
    for (int j = countc; j >= 0; j--) {
      if (C.at(countc) <= B.at(i))
        countc++;
      else
        break;
    }
    ans += counta * (N - countc);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<long> 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 counta = 0, countc = 0, ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = counta; j < N; j++) {
      if (A.at(counta) < B.at(i))
        counta++;
      else
        break;
    }
    for (int j = countc; j < N; j++) {
      if (C.at(countc) <= B.at(i))
        countc++;
      else
        break;
    }
    ans += counta * (N - countc);
  }
  cout << ans << endl;
  return 0;
}
Problem p03559 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
#include <time.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 1000000007;
const int INF = 1001001001;
const ll LINF = 1001001001001001001;

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, n) cin >> a.at(i);
  rep(i, n) cin >> b.at(i);
  rep(i, n) cin >> c.at(i);
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  vector<ll> bb(n);
  rep(i, n) {
    auto it = upper_bound(c.begin(), c.end(), b.at(i));
    ll amt = n - (it - c.begin());
    bb.at(i) = amt;
  }
  // cout << "ck1" << endl;
  vector<ll> rui(n);
  rui.at(n - 1) = bb.at(n - 1);
  for (int i = n - 2; i >= 0; i--) {
    rui.at(i) = rui.at(i + 1) + bb.at(i);
  }
  ll ans = 0;
  // cout << "ck2" << endl;
  rep(i, n) {
    auto it = upper_bound(b.begin(), b.end(), a.at(i));
    ll amt = it - b.begin();
    ans += rui.at(amt);
  }
  /*
  rep(i,n) cout << bb.at(i) << " ";
  cout << endl;
  rep(i,n) cout << rui.at(i) << " ";
  cout << endl;
  */
  cout << ans << endl;
}

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n), c(n);
  rep(i, n) cin >> a.at(i);
  rep(i, n) cin >> b.at(i);
  rep(i, n) cin >> c.at(i);
  sort(a.begin(), a.end());
  sort(b.begin(), b.end());
  sort(c.begin(), c.end());

  vector<ll> bb(n);
  rep(i, n) {
    auto it = upper_bound(c.begin(), c.end(), b.at(i));
    ll amt = n - (it - c.begin());
    bb.at(i) = amt;
  }
  // cout << "ck1" << endl;
  vector<ll> rui(n);
  rui.at(n - 1) = bb.at(n - 1);
  for (int i = n - 2; i >= 0; i--) {
    rui.at(i) = rui.at(i + 1) + bb.at(i);
  }
  ll ans = 0;
  // cout << "ck2" << endl;
  rep(i, n) {
    auto it = upper_bound(b.begin(), b.end(), a.at(i));
    ll amt = it - b.begin();
    if (amt == n)
      continue;
    ans += rui.at(amt);
  }
  /*
  rep(i,n) cout << bb.at(i) << " ";
  cout << endl;
  rep(i,n) cout << rui.at(i) << " ";
  cout << endl;
  */
  cout << ans << endl;
}