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

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
using namespace std;

int main(void) {
  int m[10001];
  for (int i = 0; i < 10001; i++) {
    m[i] = 0;
  }
  int N, A, sum = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    if (m[A] == 0) {
      m[A]++;
    } else {
      sum++;
    }
  }
  if (sum % 2 == 0) {
    cout << N - sum;
  } else {
    cout << N - sum - 1;
  }
}


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

int main(void) {
  int m[100001];
  for (int i = 0; i < 100001; i++) {
    m[i] = 0;
  }
  int N, A, sum = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A;
    if (m[A] == 0) {
      m[A]++;
    } else {
      sum++;
    }
  }
  if (sum % 2 == 0) {
    cout << N - sum;
  } else {
    cout << N - sum - 1;
  }
}
Problem p03817 (Python)

Buggy submission (status: Runtime Error):
x = input()
print((x // 11) * 2 + (2 if x % 11 > 6 else 1))


Fixed submission:
x = int(input())
ans = (x // 11) * 2
if x % 11:
    if x % 11 > 6:
        ans += 2
    else:
        ans += 1
print(ans)
Problem p03817 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
typedef long long int ll;
static const ll n = 3e14;
using namespace std;

int main() {
  ll x;
  cin >> x;
  for (ll i = 1; i < n; ++i) {
    if (6 * i - i / 2 >= x) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
typedef long long int ll;
static const ll n = 3e14;
using namespace std;

int main() {
  ll x;
  cin >> x;
  ll ans = 2 * x / 11 + 1;
  if (2 * x % 11 == 0 || 2 * x % 11 == 1)
    ans--;
  cout << ans << endl;
  return 0;
}
Problem p03817 (C++)

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

int main() {
  long X;
  cin >> X;
  long sum = 0;
  while (X > 0) {
    if (sum % 2 == 0)
      X -= 6;
    else
      X -= 5;
    sum++;
  }
  cout << sum;
}


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

int main() {
  long X;
  cin >> X;
  long Y;
  Y = X % 11;
  int a;
  if (Y == 0)
    a = 0;
  else if (Y < 7)
    a = 1;
  else
    a = 2;
  cout << X / 11 * 2 + a;
}
Problem p03817 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long int n, c = 0;
  cin >> n;
  c = (n / 11) * 2;
  c += n %= (n / 11) / 6;
  cout << c << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long int n, c = 0;
  cin >> n;
  c = (n / 11) * 2;
  if (n % 11)
    c++;
  c += (n % 11 - 1) / 6;
  cout << c << endl;
}
Problem p03817 (C++)

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

using namespace std;

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

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e6 + 10;

int n, m;

void prework() {}

void read() {}

void solve(int casi) {
  ll x;
  cin >> x;
  ll y = x / 11;
  y *= 2;
  x %= 11;
  if (x == 0)
    ;
  else if (x <= 6)
    y++;
  else
    y += 2;
  cout << y << endl;
}

void printans() {}

int main() {
  prework();
  int T = 1;
  //	cin>>T;
  for (int i = 1; i <= T; i++) {
    read();
    solve(i);
    printans();
    while (1) {
      ;
    }
  }
  return 0;
}


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

using namespace std;

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

const int MM = 1e9 + 7;
const double eps = 1e-8;
const int MAXN = 2e6 + 10;

int n, m;

void prework() {}

void read() {}

void solve(int casi) {
  ll x;
  cin >> x;
  ll y = x / 11;
  y *= 2;
  x %= 11;
  if (x == 0)
    ;
  else if (x <= 6)
    y++;
  else
    y += 2;
  cout << y << endl;
}

void printans() {}

int main() {
  prework();
  int T = 1;
  //	cin>>T;
  for (int i = 1; i <= T; i++) {
    read();
    solve(i);
    printans();
  }
  return 0;
}
Problem p03817 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main() {
  // 6+5+6+5+6+5+......が最速のはず。

  long long int needpoint = 0;
  long long int i = 0;
  int jouyo = 0;

  scanf("%lld", &needpoint);

  i = (needpoint / 11) * 2;
  jouyo = needpoint % 11;
  if (jouyo > 0) {
    i++;
  }
  if (jouyo > 6) {
    i++;
  }

  printf("%lld", i);
  return 1;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

int main() {
  // 6+5+6+5+6+5+......が最速のはず。

  long long int needpoint = 0;
  long long int i = 0;
  int jouyo = 0;

  scanf("%lld", &needpoint);

  i = (needpoint / 11) * 2;
  jouyo = needpoint % 11;
  if (jouyo > 0) {
    i++;
  }
  if (jouyo > 6) {
    i++;
  }

  printf("%lld", i);
  return 0;
}
Problem p03818 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
using namespace std;
inline char gc() {
  static char now[1 << 16], *S, *T;
  if (T == S) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (T == S)
      return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0;
  char ch = gc();
  while (ch < '0' || ch > '9')
    ch = gc();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - '0', ch = gc();
  return x;
}
int n, cnt[110000];
int main() {
  freopen("arc.in", "r", stdin);
  n = read();
  int max1 = 0, x = 0;
  for (int i = 1; i <= n; ++i)
    x = read(), max1 = max(max1, x), cnt[x]++;
  int kind = 0, sum = 0;
  for (int i = 1; i <= max1; ++i)
    if (cnt[i])
      sum += cnt[i] - 1, kind++;
  if (sum % 2)
    kind--;
  printf("%d", kind);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
inline char gc() {
  static char now[1 << 16], *S, *T;
  if (T == S) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (T == S)
      return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0;
  char ch = gc();
  while (ch < '0' || ch > '9')
    ch = gc();
  while (ch <= '9' && ch >= '0')
    x = x * 10 + ch - '0', ch = gc();
  return x;
}
int n, cnt[110000];
int main() {
  // freopen("arc.in","r",stdin);
  n = read();
  int max1 = 0, x = 0;
  for (int i = 1; i <= n; ++i)
    x = read(), max1 = max(max1, x), cnt[x]++;
  int kind = 0, sum = 0;
  for (int i = 1; i <= max1; ++i)
    if (cnt[i])
      sum += cnt[i] - 1, kind++;
  if (sum % 2)
    kind--;
  printf("%d", kind);
  return 0;
}
Problem p03818 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
map<int, int> m;
int main() {
  int n;
  int x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    m[x]++;
  }
  priority_queue<int> Q;
  for (auto it : m)
    Q.push((it.second));
  while (true) {
    if (Q.top() == 1)
      break;
    n -= 2;
    int a = Q.top();
    Q.pop();
    int b = Q.top();
    Q.pop();
    a--;
    b--;
    if (a)
      Q.push(a);
    if (b)
      Q.push(b);
  }
  cout << n << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
map<int, int> m;
int main() {
  int n;
  int x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x;
    m[x]++;
  }
  priority_queue<int> Q;
  for (auto it : m)
    Q.push((it.second));
  while (true) {
    if (Q.top() == 1)
      break;
    n -= 2;
    if (Q.size() == 1) {
      int a = Q.top();
      Q.pop();
      Q.push(a - 2);
      continue;
    }
    int a = Q.top();
    Q.pop();
    int b = Q.top();
    Q.pop();
    a--;
    b--;
    if (a)
      Q.push(a);
    if (b)
      Q.push(b);
  }
  cout << n << endl;

  return 0;
}
Problem p03818 (Python)

Buggy submission (status: Runtime Error):
import sys

# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    from collections import Counter

    n = input_int()
    A = input_int_list()
    A_cnt = Counter(A).most_common()
    A_cnt = [list(a) for a in A_cnt]
    cnt = 0
    for i in range(len(A_cnt)):
        if A_cnt[i][1] > 2:
            if A_cnt[i][1] % 2 == 1:
                cnt += A_cnt[i][1] // 2
                A_cnt[i][1] = 1
            elif A_cnt[i][1] % 2 == 0:
                cnt += (A_cnt[i][1] // 2) - 1
                A_cnt[i][1] = 2
        if A_cnt[i][1] == 2:
            if i + 1 < n:
                A_cnt[i + 1][1] -= 1
            A_cnt[i][1] = 1
            cnt += 1
    print(n - cnt * 2)
    return


if __name__ == "__main__":
    main()


Fixed submission:
import sys

# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    from collections import Counter

    n = input_int()
    A = input_int_list()
    A_cnt = Counter(A).most_common()
    A_cnt = [list(a) for a in A_cnt]
    cnt = 0
    for i in range(len(A_cnt)):
        if A_cnt[i][1] > 2:
            if A_cnt[i][1] % 2 == 1:
                cnt += A_cnt[i][1] // 2
                A_cnt[i][1] = 1
            elif A_cnt[i][1] % 2 == 0:
                cnt += (A_cnt[i][1] // 2) - 1
                A_cnt[i][1] = 2
        if A_cnt[i][1] == 2:
            if i + 1 < len(A_cnt):
                A_cnt[i + 1][1] -= 1
            A_cnt[i][1] = 1
            cnt += 1
    print(n - cnt * 2)
    return


if __name__ == "__main__":
    main()
Problem p03818 (C++)

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

using namespace std;

typedef long long int64;

int main() {
  int N, A[100001] = {};

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

  queue<int> que;
  for (int i = 0; i <= 100001; i++) {
    if (A[i] % 2 == 1) {
      A[i] = 1;
    } else if (A[i] > 0 && A[i] % 2 == 0) {
      A[i] = 2;
      que.push(i);
    }
  }

  while (que.size() >= 2) {
    int p = que.front();
    que.pop();
    int q = que.front();
    que.pop();
    --A[p];
    --A[q];
  }

  if (que.size()) {
    --A[que.front()];
    --A[que.front()];
  }

  cout << accumulate(A, A + 100001, 0) << endl;
}


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

using namespace std;

typedef long long int64;

int main() {
  int N, A[100001] = {};

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

  queue<int> que;
  for (int i = 0; i < 100001; i++) {
    if (A[i] % 2 == 1) {
      A[i] = 1;
    } else if (A[i] > 0 && A[i] % 2 == 0) {
      A[i] = 2;
      que.push(i);
    }
  }

  while (que.size() >= 2) {
    int p = que.front();
    que.pop();
    int q = que.front();
    que.pop();
    --A[p];
    --A[q];
  }

  if (que.size()) {
    --A[que.front()];
    --A[que.front()];
  }

  cout << accumulate(A, A + 100001, 0) << endl;
}
Problem p03818 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<int> count(n, 0);
  int s = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    count[k - 1]++;
    if (count[k - 1] > 1) {
      s++;
    }
  }

  if (s % 2 == 1)
    s++;

  cout << n - s << endl;
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<int> count(100000, 0);
  int s = 0;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    count[k - 1]++;
    if (count[k - 1] > 1) {
      s++;
    }
  }

  if (s % 2 == 1)
    s++;

  cout << n - s << endl;
  return 0;
}
Problem p03818 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <bits/stdc++.h>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define all(X) (X).begin(), (X).end()
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_N = 100000;
int N;
int A[MAX_N];

int main() {
  cin >> N;
  rep(i, N) cin >> A[i];

  sort(A, A + N);

  int even = 0;
  int cont = 0;
  for (int i = 0; i < N; ++i) {
    ++cont;
    if (i + 1 < N && A[i] == A[i + 1]) {
      int t = 0;
      char a = A[i];
      while (i < N && A[i] == a) {
        ++i;
        ++t;
      }
      --i;
      if (t % 2 == 0)
        ++even;
    }
  }

  if (even % 2 == 0) {
    cout << cont << endl;
  } else {
    cout << cont - 1 << endl;
  }
}


Fixed submission:

#include <bits/stdc++.h>

#define rep(X, Y) for (int(X) = 0; (X) < (Y); ++(X))
#define all(X) (X).begin(), (X).end()
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAX_N = 100000;
int N;
int A[MAX_N];

int main() {
  cin >> N;
  rep(i, N) cin >> A[i];

  sort(A, A + N);

  int even = 0;
  int cont = 0;
  for (int i = 0; i < N; ++i) {
    ++cont;
    if (i + 1 < N && A[i] == A[i + 1]) {
      int t = 0;
      int a = A[i];
      while (i < N && A[i] == a) {
        ++i;
        ++t;
      }
      --i;
      if (t % 2 == 0)
        ++even;
    }
  }

  if (even % 2 == 0) {
    cout << cont << endl;
  } else {
    cout << cont - 1 << endl;
  }
}
Problem p03818 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h" // {{{
#define ALL(x) x.begin(), x.end()
#define fin '\n'
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define FAST() ios::sync_with_stdio(false), cin.tie(nullptr)
#else
#define debug(...)
#define FAST()
#endif
using namespace std;

namespace { // }}}

map<int, int> cnt;

int _$_$_() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    cnt[a] += 1;
  }

  priority_queue<int> pq;

  for (auto e : cnt) {
    pq.emplace(e.second);
  }

  while (pq.top() > 1) {
    int a = pq.top();
    pq.pop();
    int b = pq.top();
    pq.pop();

    --a;
    --b;
    pq.push(a);
    pq.push(b);
  }

  int ans = 0;
  while (!pq.empty()) {
    int v = pq.top();
    pq.pop();
    if (v > 0) {
      ++ans;
    }
  }

  cout << ans << fin;
}

} // namespace
int main() {
  FAST();
  return _$_$_();
} // }}}


Fixed submission:
#include "bits/stdc++.h" // {{{
#define ALL(x) x.begin(), x.end()
#define fin '\n'
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define FAST() ios::sync_with_stdio(false), cin.tie(nullptr)
#else
#define debug(...)
#define FAST()
#endif
using namespace std;

namespace { // }}}

map<int, int> cnt;

int _$_$_() {
  int N;
  cin >> N;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    cnt[a] += 1;
  }

  priority_queue<int> pq;

  for (auto e : cnt) {
    pq.emplace(e.second);
  }
  pq.emplace(0);

  while (pq.top() > 1) {
    int a = pq.top();
    pq.pop();
    int b = pq.top();
    pq.pop();

    --a;
    --b;
    pq.push(a);
    pq.push(b);
  }

  int ans = 0;
  while (!pq.empty()) {
    int v = pq.top();
    pq.pop();
    if (v > 0) {
      ++ans;
    }
  }

  cout << ans << fin;
}

} // namespace
int main() {
  FAST();
  return _$_$_();
} // }}}
Problem p03818 (C++)

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

int main() {

  long long N;
  long long count = 0;

  vector<long long> A;
  vector<long long> B;

  cin >> N;

  for (long long i = 0; i < N; i++) {
    long long a;
    cin >> a;

    A.push_back(a);
  }

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

  for (long long i = 1; i < N; i++) {

    if (A[i - 1] == A[i]) {
      B.push_back(A[i]);
    }
  }

  for (;;) {

    if (B.size() <= 2) {
      break;
    }
    if (B.front() == B.back()) {
      break;
    }

    B.erase(B.begin());
    B.pop_back();

    count++;
  }

  if (B.front() == B.back()) {
    count += (B.size() / 2);
    count += B.size() % 2;
  } else if (B.size() == 2) {

    bool is_between = false;
    for (long long i = 0; i < N; i++) {
      if (B[0] < A[i] && A[i] > B[1]) {
        is_between = true;
        break;
      }
    }
    if (is_between) {
      count++;
    } else {
      count += 2;
    }
  }

  cout << N - (count * 2) << endl;

  return 0;
}

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

int main() {

  long long N;
  long long count = 0;

  vector<long long> A;
  vector<long long> B;

  cin >> N;

  for (long long i = 0; i < N; i++) {
    long long a;
    cin >> a;

    A.push_back(a);
  }

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

  for (long long i = 1; i < N; i++) {

    if (A[i - 1] == A[i]) {
      B.push_back(A[i]);
    }
  }

  if (!B.size()) {
    cout << N << endl;
    return 0;
  }

  for (;;) {

    if (B.size() <= 2) {
      break;
    }
    if (B.front() == B.back()) {
      break;
    }

    B.erase(B.begin());
    B.pop_back();

    count++;
  }

  if (B.front() == B.back()) {
    count += (B.size() / 2);
    count += B.size() % 2;
  } else if (B.size() == 2) {

    bool is_between = false;
    for (long long i = 0; i < N; i++) {
      if (B[0] < A[i] && A[i] > B[1]) {
        is_between = true;
        break;
      }
    }
    if (is_between) {
      count++;
    } else {
      count += 2;
    }
  }

  cout << N - (count * 2) << endl;

  return 0;
}
Problem p03818 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  cin >> N;
  vector<int> A(N);
  int M = 15;
  vector<int> cnt(M, 0);
  rep(i, N) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  int ans = 0;
  rep(i, M) {
    if (cnt[i] > 2) {
      ans += (cnt[i] - 1) / 2;
      cnt[i] -= ((cnt[i] - 1) / 2) * 2;
    }
  }

  int c = 0;
  rep(i, M) {
    if (cnt[i] >= 2)
      c++;
  }

  ans += (c + 1) / 2;

  output(N - ans * 2);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define _repargs(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _repargs(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T> inline void output(T a, int p = 0) {
  if (p)
    cout << fixed << setprecision(p) << a << "\n";
  else
    cout << a << "\n";
}
// end of template

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  // source code
  int N;
  cin >> N;
  vector<int> A(N);
  int M = 1000000;
  vector<int> cnt(M, 0);
  rep(i, N) {
    cin >> A[i];
    cnt[A[i]]++;
  }
  int ans = 0;
  rep(i, M) {
    if (cnt[i] > 2) {
      ans += (cnt[i] - 1) / 2;
      cnt[i] -= ((cnt[i] - 1) / 2) * 2;
    }
  }

  int c = 0;
  rep(i, M) {
    if (cnt[i] >= 2)
      c++;
  }

  ans += (c + 1) / 2;

  output(N - ans * 2);

  return 0;
}
Problem p03818 (C++)

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

#define each(i, c) for (auto &i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename P, typename Q>
istream &operator>>(istream &is, pair<P, Q> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ",";
  os << ")";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  each(i, v) is >> i;
  return is;
}

template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); }
template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); }

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  while (cin >> n) {
    vector<int> a(n);
    cin >> a;

    map<int, int> cnt;
    each(i, a)++ cnt[i];

    priority_queue<pair<int, int>> q;
    each(i, cnt) { q.push(make_pair(i.second, i.first)); }

    while (q.size() && 1 < q.top().first) {
      auto x = q.top();
      q.pop();
      auto y = q.top();
      q.pop();

      x.first -= 1;
      y.first -= 1;
      if (x.first)
        q.push(x);
      if (y.first)
        q.push(y);
    }

    cout << q.size() << endl;
  }

  return 0;
}


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

#define each(i, c) for (auto &i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template <typename P, typename Q>
ostream &operator<<(ostream &os, pair<P, Q> p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename P, typename Q>
istream &operator>>(istream &is, pair<P, Q> &p) {
  is >> p.first >> p.second;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  os << "(";
  each(i, v) os << i << ",";
  os << ")";
  return os;
}
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  each(i, v) is >> i;
  return is;
}

template <typename T> inline T setmax(T &a, T b) { return a = std::max(a, b); }
template <typename T> inline T setmin(T &a, T b) { return a = std::min(a, b); }

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n;
  while (cin >> n) {
    vector<int> a(n);
    cin >> a;

    map<int, int> cnt;
    each(i, a)++ cnt[i];

    priority_queue<pair<int, int>> q;
    each(i, cnt) { q.push(make_pair(i.second, i.first)); }

    while (2 <= q.size() && 1 < q.top().first) {
      auto x = q.top();
      q.pop();
      auto y = q.top();
      q.pop();

      x.first -= 1;
      y.first -= 1;
      if (x.first)
        q.push(x);
      if (y.first)
        q.push(y);
    }

    cout << q.size() << endl;
  }

  return 0;
}
Problem p03818 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define ll long long
using namespace std;
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp;
  rep(i, n) {
    ll a;
    cin >> a;
    mp[a]++;
  }
  priority_queue<ll> p;
  for (auto item : mp) {
    p.push(item.second);
  }
  while (true) {
    ll a1 = p.top();
    if (a1 == 1) {
      break;
    }
    p.pop();
    ll a2 = p.top();
    p.pop();
    if (a2 == 1) {
      a1--;
      p.push(a1);
      break;
    }
    a1--;
    a2--;
    p.push(a1);
    p.push(a2);
  }
  cout << p.size() << endl;
  // cout << fixed << setprecision(10) << ans << endl;
  system("pause");
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define ll long long
using namespace std;
template <typename T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1000000000000000000;
const ll MOD = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  map<ll, ll> mp;
  rep(i, n) {
    ll a;
    cin >> a;
    mp[a]++;
  }
  priority_queue<ll> p;
  for (auto item : mp) {
    p.push(item.second);
  }
  while (true) {
    if (p.size() == 1)
      break;
    ll a1 = p.top();
    if (a1 == 1) {
      break;
    }
    p.pop();
    ll a2 = p.top();
    p.pop();
    if (a2 == 1) {
      a1--;
      p.push(a1);
      break;
    }
    a1--;
    a2--;
    p.push(a1);
    p.push(a2);
  }
  cout << p.size() << endl;
  // cout << fixed << setprecision(10) << ans << endl;
  system("pause");
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100100;
int fen[maxn], ans[maxn], qtd[maxn];
vector<int> divs[maxn], bck[maxn];
void add(int x, int v) {
  for (; x < maxn; x += x & -x)
    fen[x] += v;
}
int sum(int x) {
  int ans = 0;
  for (; x; x -= x & -x)
    ans += fen[x];
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    for (int j = i; j <= m; j += i)
      divs[j].push_back(i);
  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    qtd[r]++;
    bck[l].push_back(r);
  }
  for (int i = m; i >= 1; i--) {
    add(i, qtd[i]);
    for (int q : divs[i])
      ans[q] += sum(i + q - 1);
    for (int q : bck[i])
      add(q, -1);
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
int fen[maxn], ans[maxn], qtd[maxn];
vector<int> divs[maxn], bck[maxn];
void add(int x, int v) {
  for (; x < maxn; x += x & -x)
    fen[x] += v;
}
int sum(int x) {
  int ans = 0;
  for (; x; x -= x & -x)
    ans += fen[x];
  return ans;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++)
    for (int j = i; j <= m; j += i)
      divs[j].push_back(i);
  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    qtd[r]++;
    bck[l].push_back(r);
  }
  for (int i = m; i >= 1; i--) {
    add(i, qtd[i]);
    for (int q : divs[i])
      ans[q] += sum(i + q - 1);
    for (int q : bck[i])
      add(q, -1);
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p03819 (C++)

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

struct BIT {
  int N;
  int bit[30010];

  void init(int n) {
    N = n;
    for (int i = 0; i <= n; ++i)
      bit[i] = 0;
  }
  void add(int a, int x) {
    for (int i = a; i <= N; i += i & -i)
      bit[i] += x;
  }
  int sum(int a) {
    int s = 0;
    for (int i = a; i > 0; i -= i & -i)
      s += bit[i];
    return s;
  }
};

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

  int N, M;
  cin >> N >> M;
  vector<int> w[M + 2];
  vector<pair<int, int>> lr(N);
  BIT bit;
  bit.init(M + 2);

  rep(i, N) {
    cin >> lr[i].first >> lr[i].second;
    w[lr[i].second - lr[i].first + 1].push_back(i);
  }

  int cnt = 0;
  for (int d = 1; d <= M; ++d) {
    for (int i : w[d]) {
      cnt++;
      bit.add(lr[i].first, 1);
      bit.add(lr[i].second + 1, -1);
    }

    int ans = N - cnt;
    for (int m = d; m <= M; m += d) {
      ans += bit.sum(m);
    }
    cout << ans << endl;
  }
}


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

struct BIT {
  int N;
  int bit[300010];

  void init(int n) {
    N = n;
    for (int i = 0; i <= n; ++i)
      bit[i] = 0;
  }
  void add(int a, int x) {
    for (int i = a; i <= N; i += i & -i)
      bit[i] += x;
  }
  int sum(int a) {
    int s = 0;
    for (int i = a; i > 0; i -= i & -i)
      s += bit[i];
    return s;
  }
};

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

  int N, M;
  cin >> N >> M;
  vector<int> w[M + 2];
  vector<pair<int, int>> lr(N);
  BIT bit;
  bit.init(M + 2);

  rep(i, N) {
    cin >> lr[i].first >> lr[i].second;
    w[lr[i].second - lr[i].first + 1].push_back(i);
  }

  int cnt = 0;
  for (int d = 1; d <= M; ++d) {
    for (int i : w[d]) {
      cnt++;
      bit.add(lr[i].first, 1);
      bit.add(lr[i].second + 1, -1);
    }

    int ans = N - cnt;
    for (int m = d; m <= M; m += d) {
      ans += bit.sum(m);
    }
    cout << ans << endl;
  }
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>

using namespace std;
typedef pair<int, int> Pi;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> PL;

int T[300010], n, m;

void update(int l, int r) {
  for (int i = r; i; i -= (i & -i))
    T[i]++;
  for (int i = l - 1; i; i -= (i & -i))
    T[i]--;
}

int read(int x) {
  int res = 0;
  for (int i = x; i <= m; i += (i & -i))
    res += T[i];
  return res;
}

void solve() {
  scanf("%d%d", &n, &m);
  Pi p[100010];
  rep(i, n) {
    int x, y;
    scanf("%d%d", &x, &y);
    p[i] = Pi(x, y);
  }
  sort(p, p + n, [&](Pi a, Pi b) { return (a.Se - a.Fi) < (b.Se - b.Fi); });
  for (int i = 1, j = 0; i <= m; i++) {
    while (j < n && p[j].Se - p[j].Fi < i) {
      update(p[j].Fi, p[j].Se);
      ++j;
    }
    int cnt = n - j;
    for (int k = i; k <= m; k += i) {
      cnt += read(k);
    }
    printf("%d\n", cnt);
  }
}

int main() {
  int Tc = 1; // scanf("%d", &Tc);
  for (int tc = 1; tc <= Tc; tc++) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>

using namespace std;
typedef pair<int, int> Pi;
typedef long long ll;
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) (int)x.size()
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> PL;

int T[300010], n, m;

void update(int l, int r) {
  for (int i = r; i; i -= (i & -i))
    T[i]++;
  for (int i = l - 1; i; i -= (i & -i))
    T[i]--;
}

int read(int x) {
  int res = 0;
  for (int i = x; i <= m; i += (i & -i))
    res += T[i];
  return res;
}

void solve() {
  scanf("%d%d", &n, &m);
  Pi p[300010];
  rep(i, n) {
    int x, y;
    scanf("%d%d", &x, &y);
    p[i] = Pi(x, y);
  }
  sort(p, p + n, [&](Pi a, Pi b) { return (a.Se - a.Fi) < (b.Se - b.Fi); });
  for (int i = 1, j = 0; i <= m; i++) {
    while (j < n && p[j].Se - p[j].Fi < i) {
      update(p[j].Fi, p[j].Se);
      ++j;
    }
    int cnt = n - j;
    for (int k = i; k <= m; k += i) {
      cnt += read(k);
    }
    printf("%d\n", cnt);
  }
}

int main() {
  int Tc = 1; // scanf("%d", &Tc);
  for (int tc = 1; tc <= Tc; tc++) {
    solve();
  }
  return 0;
}
Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int ans[500005], cf[500005], l[500005], r[500005];
const int bl = 3;
int main(int argc, char **argv) {
  int n, m, t;
  ios::sync_with_stdio(false);
  cin >> n >> m;
  t = m / bl;
  for (int i = 1; i <= n; i++)
    cin >> l[i] >> r[i];
  for (int i = 1; i <= n; i++) {
    int lst = t;
    for (int j = bl; j >= 1; j--) {
      int L = (l[i] - 1) / j + 1, R = r[i] / j;
      L = max(L, lst + 1);
      if (R < L)
        continue;
      ++cf[L], --cf[R + 1];
      lst = max(lst, R);
    }
  }
  for (int i = 1; i <= t; i++) {
    int ans = 0;
    for (int j = 1; j <= n; j++)
      ans += (l[j] - 1) / i != r[j] / i;
    cout << ans << "\n";
  }
  for (int i = t + 1; i <= m; i++) {
    cf[i] += cf[i - 1];
    cout << cf[i] << "\n";
  }
  return 0;
}
// dlstxdy

Fixed submission:
#include <iostream>
using namespace std;
int ans[500005], cf[500005], l[500005], r[500005];
const int bl = 550;
int main(int argc, char **argv) {
  int n, m, t;
  ios::sync_with_stdio(false);
  cin >> n >> m;
  t = m / bl;
  for (int i = 1; i <= n; i++)
    cin >> l[i] >> r[i];
  for (int i = 1; i <= n; i++) {
    int lst = t;
    for (int j = bl; j >= 1; j--) {
      int L = (l[i] - 1) / j + 1, R = r[i] / j;
      L = max(L, lst + 1);
      if (R < L)
        continue;
      ++cf[L], --cf[R + 1];
      lst = max(lst, R);
    }
  }
  for (int i = 1; i <= t; i++) {
    int ans = 0;
    for (int j = 1; j <= n; j++)
      ans += (l[j] - 1) / i != r[j] / i;
    cout << ans << "\n";
  }
  for (int i = t + 1; i <= m; i++) {
    cf[i] += cf[i - 1];
    cout << cf[i] << "\n";
  }
  return 0;
}
// dlstxdy
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 100010
using namespace std;
struct node {
  int l, r;
  bool operator<(const node a) const { return r - l < a.r - a.l; }
} p[N];
int tree[N << 2];
inline int lowbit(int x) { return x & (-x); }
void insert(int u, int v) {
  for (; u < N << 2; u += lowbit(u))
    tree[u] += v;
}
int answer(int u) {
  int ans = 0;
  for (; u; u -= lowbit(u))
    ans += tree[u];
  return ans;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &p[i].l, &p[i].r), p[i].l++, p[i].r++;
  sort(p + 1, p + n + 1);
  int u = 1;
  for (int i = 1; i <= m; i++) {
    while (p[u].r - p[u].l + 1 < i && u <= n) {
      insert(p[u].l, 1);
      insert(p[u].r + 1, -1);
      u++;
    }
    int ans = n - u + 1;
    for (int j = 0; j <= m; j += i)
      ans += answer(j + 1);
    printf("%d\n", ans);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#define N 300010
using namespace std;
struct node {
  int l, r;
  bool operator<(const node a) const { return r - l < a.r - a.l; }
} p[N];
int tree[N << 2];
inline int lowbit(int x) { return x & (-x); }
void insert(int u, int v) {
  for (; u < N << 2; u += lowbit(u))
    tree[u] += v;
}
int answer(int u) {
  int ans = 0;
  for (; u; u -= lowbit(u))
    ans += tree[u];
  return ans;
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d%d", &p[i].l, &p[i].r), p[i].l++, p[i].r++;
  sort(p + 1, p + n + 1);
  int u = 1;
  for (int i = 1; i <= m; i++) {
    while (p[u].r - p[u].l + 1 < i && u <= n) {
      insert(p[u].l, 1);
      insert(p[u].r + 1, -1);
      u++;
    }
    int ans = n - u + 1;
    for (int j = 0; j <= m; j += i)
      ans += answer(j + 1);
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define CLEAR(a) memset(a, 0, sizeof a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define fr freopen("input.txt", "r", stdin);
#define fw freopen("output.txt", "w", stdout);
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;
vector<pair<int, pii>> v;
int n, m, t[MAX];

void update(int l, int r, int value) {
  for (l += m + 1, r += m + 1; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      t[l++] += value;
    if (r & 1)
      t[--r] += value;
  }
}

int query(int p) {
  int res = 0;
  for (p += m + 1; p > 0; p >>= 1)
    res += t[p];
  return res;
}

int main() {
  cin >> n >> m;
  REP(i, n) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    v.pb(mp(r - l + 1, mp(l, r)));
  }

  sort(v.begin(), v.end());

  int j = 0;

  FOR(i, 1, m) {

    while (j < n && v[j].F < i) {
      update(v[j].S.F, v[j].S.S + 1, 1);
      j++;
    }

    int ans = n - j;

    for (int k = i - 1; k <= m; k += i) {
      ans += query(k);
    }
    cout << ans << "\n";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define CLEAR(a) memset(a, 0, sizeof a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i <= b; i++)
#define fr freopen("input.txt", "r", stdin);
#define fw freopen("output.txt", "w", stdout);
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MAX = 1e6 + 5;
vector<pair<int, pii>> v;
int n, m, t[MAX];

void update(int l, int r, int value) {
  for (l += m + 1, r += m + 1; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      t[l++] += value;
    if (r & 1)
      t[--r] += value;
  }
}

int query(int p) {
  int res = 0;
  for (p += m + 1; p > 0; p >>= 1)
    res += t[p];
  return res;
}

int main() {
  cin >> n >> m;
  REP(i, n) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    v.pb(mp(r - l + 1, mp(l, r)));
  }

  sort(v.begin(), v.end());

  int j = 0;

  FOR(i, 1, m) {

    while (j < n && v[j].F < i) {
      update(v[j].S.F, v[j].S.S + 1, 1);
      j++;
    }

    int ans = n - j;

    for (int k = i - 1; k <= m; k += i) {
      ans += query(k);
    }
    cout << ans << "\n";
  }
  return 0;
}
Problem p03819 (C++)

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

using namespace std;
typedef long long LL;

int main() {
  LL N, M;
  cin >> N >> M;
  vector<LL> delta(M + 2);
  for (int i = 0; i < N; ++i) {
    LL l, r;
    cin >> l >> r;
    LL w = r + 1 - l;
    delta[0] += 1;
    delta[w] -= 1;
    LL div = w + 1;
    LL rest = 0;
    while (div <= M) {
      // cerr << "div=" << div << " l=" << l << endl;
      LL v = (l + div - 1) / div;
      if (v <= 0)
        break;
      LL div2 = r / v;
      // cerr << "div2=" << div << endl;
      if (div2 >= div) {
        delta[div - 1] += 1;
        delta[div2] -= 1;
      }
      if (v <= 1)
        break;
      div = max(div2 + 1, l / (v - 1));
    }
  }
  LL ans = 0;
  for (int i = 0; i < M; ++i) {
    ans += delta[i];
    cout << ans << endl;
  }
  return 0;
}

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

using namespace std;
typedef long long LL;

int main() {
  LL N, M;
  cin >> N >> M;
  vector<LL> delta(M + 2);
  for (int i = 0; i < N; ++i) {
    LL l, r;
    cin >> l >> r;
    LL w = r + 1 - l;
    delta[0] += 1;
    delta[w] -= 1;
    LL div = w + 1;
    LL rest = 0;
    while (div <= M) {
      // cerr << "div=" << div << " l=" << l << endl;
      LL v = (l + div - 1) / div;
      if (v <= 0)
        break;
      LL div2 = r / v;
      // cerr << "div2=" << div << endl;
      if (div2 >= div) {
        delta[div - 1] += 1;
        delta[div2] -= 1;
      }
      if (v <= 1)
        break;
      div = max(div2 + 1, (l + (v - 1) - 1) / (v - 1));
    }
  }
  LL ans = 0;
  for (int i = 0; i < M; ++i) {
    ans += delta[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p03819 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cstdio>
#define repu(i, x, y) for (int i = x; i <= y; ++i)
using namespace std;

int n, tot, ans[100100], bit[100100];
struct data {
  int l, r, id;
  bool operator<(const data &t) const { return l > t.l || l == t.l && !id; }
} a[15000100];

int getint() {
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  int x = ch - '0';
  for (; isdigit(ch = getchar()); x = x * 10 + ch - '0')
    ;
  return x;
}

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

int query(int x) {
  int ret = 0;
  for (; x; ret += bit[x], x -= x & -x)
    ;
  return ret;
}

int main() {
  scanf("%d%d", &tot, &n);
  repu(i, 1, tot) a[i].l = getint(), a[i].r = getint();
  repu(i, 1, n) ans[i] = tot;
  repu(i, 2, n) for (int j = 0; j < n; j += i) a[++tot] =
      (data){j + 1, min(j + i - 1, n), i};
  sort(a + 1, a + 1 + tot);
  repu(i, 1, tot) if (a[i].id) ans[a[i].id] -= query(a[i].r);
  else add(a[i].r);
  repu(i, 1, n) printf("%d\n", ans[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#define repu(i, x, y) for (int i = x; i <= y; ++i)
using namespace std;

int n, tot, ans[100100], bit[100100];
struct data {
  int l, r, id;
  bool operator<(const data &t) const {
    return l > t.l || l == t.l && id < t.id;
  }
} a[15000100];

int getint() {
  char ch;
  while (!isdigit(ch = getchar()))
    ;
  int x = ch - '0';
  for (; isdigit(ch = getchar()); x = x * 10 + ch - '0')
    ;
  return x;
}

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

int query(int x) {
  int ret = 0;
  for (; x; ret += bit[x], x -= x & -x)
    ;
  return ret;
}

int main() {
  scanf("%d%d", &tot, &n);
  repu(i, 1, tot) a[i].l = getint(), a[i].r = getint();
  repu(i, 1, n) ans[i] = tot;
  repu(i, 2, n) for (int j = 0; j < n; j += i) a[++tot] =
      (data){j + 1, min(j + i - 1, n), i};
  sort(a + 1, a + 1 + tot);
  repu(i, 1, tot) if (a[i].id) ans[a[i].id] -= query(a[i].r);
  else add(a[i].r);
  repu(i, 1, n) printf("%d\n", ans[i]);
  return 0;
}
Problem p03819 (C++)

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

const int MAXN = 3.1e5;
const int MAXM = 1.1e5;
const int S = 1 << 17;

int seg[2 * S];
void update(int i) {
  for (int a = S + i; a; a /= 2) {
    seg[a]++;
  }
}
int query(int l, int r) {
  int ans = 0;
  for (int a = S + l, b = S + r; a < b; a /= 2, b /= 2) {
    if (a & 1)
      ans += seg[a++];
    if (b & 1)
      ans += seg[--b];
  }
  return ans;
}

int N, M;
vector<int> facs[MAXM];
vector<int> rights[MAXM];
long long ans[MAXM];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int l, r;
    cin >> l >> r;
    rights[l].push_back(r);
  }
  for (int d = 1; d <= M; d++) {
    for (int v = d; v <= M; v += d)
      facs[v].push_back(d);
  }
  for (int v = 1; v <= M; v++) {
    for (int r : rights[v])
      update(r);
    for (int d : facs[v])
      ans[d] += query(v, v + d);
  }
  for (int d = 1; d <= M; d++)
    cout << ans[d] << '\n';
}

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

const int MAXN = 3.1e5;
const int MAXM = 1.1e5;
const int S = 1 << 17;

int seg[2 * S];
void update(int i) {
  for (int a = S + i; a; a /= 2) {
    seg[a]++;
  }
}
int query(int l, int r) {
  int ans = 0;
  for (int a = S + l, b = S + r; a < b; a /= 2, b /= 2) {
    if (a & 1)
      ans += seg[a++];
    if (b & 1)
      ans += seg[--b];
  }
  return ans;
}

int N, M;
vector<int> facs[MAXM];
vector<int> rights[MAXM];
long long ans[MAXM];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    int l, r;
    cin >> l >> r;
    rights[l].push_back(r);
  }
  for (int d = 1; d <= M; d++) {
    for (int v = d; v <= M; v += d)
      facs[v].push_back(d);
  }
  for (int v = 1; v <= M; v++) {
    for (int r : rights[v])
      update(r);
    for (int d : facs[v])
      ans[d] += query(v, min(S, v + d));
  }
  for (int d = 1; d <= M; d++)
    cout << ans[d] << '\n';
}
Problem p03819 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt;
struct sell {
  int l, r;
} a[100005];
struct Query {
  int l, r, d;
} b[2000005];
int f[100005], g[100005], ans[100005];

int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x;
}

void print(int x) {
  if (x >= 10)
    print(x / 10);
  putchar(x % 10 + '0');
}

int cmp(Query u, Query v) { return u.l > v.l; }

int cmp1(sell u, sell v) { return u.l > v.l; }

void updata(int x) {
  for (int i = x; i <= m; i += i & -i)
    f[i]++;
}

void updata1(int x) {
  for (int i = x; i <= m; i += i & -i)
    g[i]++;
}

int query(int x) {
  int res = 0;
  for (int i = x; i; i -= i & -i)
    res += f[i];
  return res;
}

int query1(int x) {
  int res = 0;
  for (int i = x; i; i -= i & -i)
    res += g[i];
  return res;
}

int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++)
    a[i] = (sell){read(), read()};
  for (int i = 1; i <= m; i++) {
    for (int j = i; j <= m; j += i)
      b[++cnt] = (Query){j - i, j, i};
  }
  sort(b + 1, b + cnt + 1, cmp);
  sort(a + 1, a + n + 1, cmp1);
  for (int i = 1; i <= n; i++)
    updata(a[i].l);
  int L = 0;
  for (int i = 1; i <= cnt; i++) {
    while (L < n && a[L + 1].l > b[i].l)
      L++, updata1(a[L].r);
    ans[b[i].d] += L - query1(b[i].r - 1) - (n - query(b[i].r));
  }
  for (int i = 1; i <= m; i++)
    print(ans[i]), puts("");

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, cnt;
struct sell {
  int l, r;
} a[300005];
struct Query {
  int l, r, d;
} b[2000005];
int f[100005], g[100005], ans[100005];

int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch))
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x;
}

void print(int x) {
  if (x >= 10)
    print(x / 10);
  putchar(x % 10 + '0');
}

int cmp(Query u, Query v) { return u.l > v.l; }

int cmp1(sell u, sell v) { return u.l > v.l; }

void updata(int x) {
  for (int i = x; i <= m; i += i & -i)
    f[i]++;
}

void updata1(int x) {
  for (int i = x; i <= m; i += i & -i)
    g[i]++;
}

int query(int x) {
  int res = 0;
  for (int i = x; i; i -= i & -i)
    res += f[i];
  return res;
}

int query1(int x) {
  int res = 0;
  for (int i = x; i; i -= i & -i)
    res += g[i];
  return res;
}

int main() {
  n = read(), m = read();
  for (int i = 1; i <= n; i++)
    a[i] = (sell){read(), read()};
  for (int i = 1; i <= m; i++) {
    for (int j = i; j <= m; j += i)
      b[++cnt] = (Query){j - i, j, i};
  }
  sort(b + 1, b + cnt + 1, cmp);
  sort(a + 1, a + n + 1, cmp1);
  for (int i = 1; i <= n; i++)
    updata(a[i].l);
  int L = 0;
  for (int i = 1; i <= cnt; i++) {
    while (L < n && a[L + 1].l > b[i].l)
      L++, updata1(a[L].r);
    ans[b[i].d] += L - query1(b[i].r - 1) - (n - query(b[i].r));
  }
  for (int i = 1; i <= m; i++)
    print(ans[i]), puts("");

  return 0;
}