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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
typedef long long int ll;
using namespace std;
#define maxn 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const int mm = 2e5 + 100;
ll d[mm];
ll n, m;
ll check(double u) {
  int i;
  ll s = 0;
  double a;
  for (i = 0; i < n; i++) {
    a = d[i] / u;
    s += ceil(a) - 1;
  }
  if (s <= m)
    return 1;
  else
    return 0;
}
int main() {
  ll i, j, t, a, b, c, p, k, kk;
  cin >> n >> m;
  double l, r, mid;
  for (i = 0; i < n; i++) {
    scanf("%lld", &d[i]);
  }
  l = 0;
  r = 1000000000 + 5.0;
  while (l + 1e-7 <= r) {
    mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << (ll)ceil(l) << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
typedef long long int ll;
using namespace std;
#define maxn 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const int mm = 2e5 + 100;
ll d[mm];
ll n, m;
ll check(double u) {
  int i;
  ll s = 0;
  double a;
  for (i = 0; i < n; i++) {
    a = d[i] / u;
    s += ceil(a) - 1;
  }
  if (s <= m)
    return 1;
  else
    return 0;
}
int main() {
  ll i, j, t, a, b, c, p, k, kk;
  cin >> n >> m;
  double l, r, mid;
  for (i = 0; i < n; i++) {
    scanf("%lld", &d[i]);
  }
  l = 0;
  r = 1000000000 + 5.0;
  while (l + 1e-6 <= r) {
    mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << (ll)ceil(l) << endl;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):

/*
 *	Created By: 'Present_Sir'
 *	Created On: Monday 03 August 2020 08:11:06 PM IST
 */

#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define int long long
using namespace std;

const int N = 2e5 + 10;
int n, k;
vector<int> v(N);

bool valid(int mid) {
  int kc = k;
  for (int i = 0; i < n; i++) {
    if (v[i] <= mid) {
      continue;
    }
    int t = v[i] / mid;
    kc -= (t);
  }
  return kc >= 0;
}

void solve() {
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int i = 0, j = LLONG_MAX;
  int ans = 0;
  while (i <= j) {
    int mid = (i + j) / 2;
    if (valid(mid)) {
      j = mid - 1;
      ans = mid;
    } else {
      i = mid + 1;
    }
  }
  cout << ans << endl;
}

int32_t main() {
  IOS;
  // int t; cin>>t; while(t--)
  { solve(); }
  return 0;
}


Fixed submission:

/*
 *	Created By: 'Present_Sir'
 *	Created On: Monday 03 August 2020 08:11:06 PM IST
 */

#include <bits/stdc++.h>
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
#define int long long
using namespace std;

const int N = 2e5 + 10;
int n, k;
vector<int> v(N);

bool valid(int mid) {
  int kc = k;
  if (mid == 0) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    if (v[i] <= mid) {
      continue;
    }
    int t = v[i] / mid;
    kc -= (t);
  }
  return kc >= 0;
}

void solve() {
  cin >> n >> k;

  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  int i = 0, j = LLONG_MAX;
  int ans = 0;
  while (i <= j) {
    int mid = (i + j) / 2;
    if (valid(mid)) {
      j = mid - 1;
      ans = mid;
    } else {
      i = mid + 1;
    }
  }
  cout << ans << endl;
}

int32_t main() {
  IOS;
  // int t; cin>>t; while(t--)
  { solve(); }
  return 0;
}
Problem p02598 (C++)

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

#define Maxn 200010
#define LL long long

using namespace std;

const double eps = 0.00001;

inline LL read() {
  LL x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

LL n, k, a[Maxn], maxone;

LL calc(int len) {
  LL tim = 0;
  for (int i = 1; i <= n; ++i) {
    tim += (a[i] - 1) / len;
  }
  return tim;
}

int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), maxone = max(maxone, a[i]);
  int l = 0, r = maxone;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (calc(mid) > k)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
  return 0;
}

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

#define Maxn 200010
#define LL long long

using namespace std;

const double eps = 0.00001;

inline LL read() {
  LL x = 0, f = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}

LL n, k, a[Maxn], maxone;

LL calc(int len) {
  if (!len)
    return 1e9 + 1;
  LL tim = 0;
  for (int i = 1; i <= n; ++i) {
    tim += (a[i] - 1) / len;
  }
  return tim;
}

int main() {
  n = read();
  k = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), maxone = max(maxone, a[i]);
  int l = 0, r = maxone;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (calc(mid) > k)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()

// #define DEBUG 100

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const ll oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 2e6 + 100, maxm = 1e3 + 100;
const double PI = acos(-1);

ll solve(vector<ll> &vs, int n, ll k) {
  ll a = 0, b = oo, best;

  while (a <= b) {
    ll mid = (a + b) / 2, cnt = 0;
    for (auto it : vs) {
      cnt += (it / mid - (it % mid == 0 ? 1 : 0));
    }
    if (cnt > k) {
      a = mid + 1;
    } else {
      best = mid;
      b = mid - 1;
    }
  }
  return best;
}
int main() {
  ios_base::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;

  vector<ll> vs(n);

  for (int i = 0; i < n; i++) {
    cin >> vs[i];
  }

  ll ans = solve(vs, n, k);

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

Fixed submission:
#include <bits/stdc++.h>
#define ss second
#define ff first
#define all(x) x.begin(), x.end()

// #define DEBUG 100

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const ll oo = 1e9 + 7;
const ll mod = 1e9 + 7, maxn = 2e6 + 100, maxm = 1e3 + 100;
const double PI = acos(-1);

ll solve(vector<ll> &vs, int n, ll k) {
  ll a = 1, b = oo, best;

  while (a <= b) {
    ll mid = (a + b) / 2, cnt = 0;
    for (auto it : vs) {
      cnt += (it / mid - (it % mid == 0 ? 1 : 0));
    }
    if (cnt > k) {
      a = mid + 1;
    } else {
      best = mid;
      b = mid - 1;
    }
  }
  return best;
}
int main() {
  ios_base::sync_with_stdio(false);
  ll n, k;
  cin >> n >> k;

  vector<ll> vs(n);

  for (int i = 0; i < n; i++) {
    cin >> vs[i];
  }

  ll ans = solve(vs, n, k);

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

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

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

  int ok = 1e9 + 5, ng = 0;

  auto check = [&](int x) {
    ll cnt = 0;
    rep(i, n) {
      int now = a[i] / x;
      if (a[i] % x == 0)
        now--;
      cnt += now;
    }
    return cnt <= k;
  };

  rep(i, 100) {
    int mid = (ok + ng) >> 1;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
using namespace std;
using ll = long long;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T, typename U, typename... V>
typename enable_if<is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  u = U(v...);
}
template <typename T, typename U, typename... V>
typename enable_if<!is_same<T, U>::value>::type fill_v(U &u, const V... v) {
  for (auto &e : u)
    fill_v<T>(e, v...);
}

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

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

  int ok = 1e9 + 5, ng = 0;

  auto check = [&](int x) {
    ll cnt = 0;
    rep(i, n) {
      int now = a[i] / x;
      if (a[i] % x == 0)
        now--;
      cnt += now;
    }
    return cnt <= k;
  };

  while (ok - ng > 1) {
    int mid = (ok + ng) >> 1;
    if (check(mid))
      ok = mid;
    else
      ng = mid;
  }

  cout << ok << endl;
}
Problem p02598 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FILL0(x) memset(x, 0, sizeof(x))
#define FILL1(x) memset(x, -1, sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

  int n, k;

  cin >> n;
  cin >> k;

  int a[n];
  rep(i, n) { cin >> a[i]; }

  int l = -1;
  int r = 1e9;

  while (r - l > 1) {
    int mid = (r + l) / 2;

    auto judge = [&](int val) {
      ll count = 0;
      rep(i, n) { count += (a[i] - 1) / val; }
      return count <= k;
    };

    if (judge(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FILL0(x) memset(x, 0, sizeof(x))
#define FILL1(x) memset(x, -1, sizeof(x))

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

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

  int n, k;

  cin >> n;
  cin >> k;

  int a[n];
  rep(i, n) { cin >> a[i]; }

  int l = 0;
  int r = 1e9;

  while (r - l > 1) {
    int mid = (r + l) / 2;

    auto judge = [&](int val) {
      ll count = 0;
      rep(i, n) { count += (a[i] - 1) / val; }
      return count <= k;
    };

    if (judge(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }

  cout << r << endl;

  return 0;
}
Problem p02598 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(), v.end()

int k;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}

int main() {
  ll n, k;
  cin >> n;
  cin >> k;
  k += 1;

  vector<double> L(n);
  rep(i, n) cin >> L[i];
  // cin >> k;
  double l = 0;
  double r = inf;
  ll count;

  auto judge = [&](double x) {
    count = 0;
    rep(i, n) count += floor(L[i] / x);
    if (count >= k)
      return true;
    else
      return false;
  };

  for (int j = 0; j < 2000; j++) {
    double mid = (l + r) / 2.0;
    bool flag;
    flag = judge(mid);
    if (flag)
      l = mid;
    else
      r = mid;
  }
  cout << ll(ceil(l)) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define inf 1001001001
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(v) v.begin(), v.end()

int k;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}

int main() {
  ll n, k;
  cin >> n;
  cin >> k;
  k += 1;

  vector<double> L(n);
  rep(i, n) cin >> L[i];
  // cin >> k;
  double l = 0;
  double r = inf;
  ll count;

  auto judge = [&](double x) {
    count = 0;
    rep(i, n) count += floor(L[i] / x);
    if (count >= k)
      return true;
    else
      return false;
  };

  for (int j = 0; j < 100; j++) {
    double mid = (l + r) / 2.0;
    bool flag;
    flag = judge(mid);
    if (flag)
      l = mid;
    else
      r = mid;
  }
  cout << ll(ceil(l)) << endl;

  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

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

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  scanf("%d", &n);
  scanf("%d", &q);
  memset(v, 0, sizeof(v));
  memset(sum, 0, sizeof(sum));
  memset(ans, 0, sizeof(ans));

  int t = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
  }

  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &query[i].l, &query[i].r);
    query[i].id = i;
  }
  sort(query + 1, query + q + 1);

  int pre = 1;
  map<int, int> mymap;
  for (int i = 1; i <= q; i++) {
    for (int j = pre; j <= query[i].r; j++) {
      // 已存在
      if (mymap[v[j]]) {
        change(mymap[v[j]], -1);
      }
      mymap[v[j]] = j;
      change(j, 1);
    }
    pre = query[i].r + 1;
    ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
  }

  for (int i = 1; i <= q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 1e6 + 7;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

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

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  scanf("%d", &n);
  scanf("%d", &q);
  memset(v, 0, sizeof(v));
  memset(sum, 0, sizeof(sum));
  memset(ans, 0, sizeof(ans));

  int t = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
  }

  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &query[i].l, &query[i].r);
    query[i].id = i;
  }
  sort(query + 1, query + q + 1);

  int pre = 1;
  map<int, int> mymap;
  for (int i = 1; i <= q; i++) {
    for (int j = pre; j <= query[i].r; j++) {
      // 已存在
      if (mymap[v[j]]) {
        change(mymap[v[j]], -1);
      }
      mymap[v[j]] = j;
      change(j, 1);
    }
    pre = query[i].r + 1;
    ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
  }

  for (int i = 1; i <= q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02599 (C++)

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

using namespace std;

struct req {
  int l, r, val, idx;
  bool t;
};

bool operator<(req a, req b) {
  if (a.r == b.r) {
    return a.t < b.t;
  }
  return a.r < b.r;
}

int bit[500001];

void add(int y, int val) {
  while (y <= 500000) {
    bit[y] += val;
    y += (y & -y);
  }
}

int sum(int y) {
  int res = 0;
  while (y > 0) {
    res += bit[y];
    y -= (y & -y);
  }
  return res;
}

bool visited[2000001];
req arr[600000];
int main() {
  int N, Q, i;
  int *prev = new int[2000001];
  for (i = 0; i <= 2000000; ++i) {
    visited[i] = false;
    prev[i] = 0;
  }
  scanf("%d", &N);
  scanf("%d", &Q);
  for (i = 1; i <= N; ++i) {
    add(i, 1);
  }
  for (i = 0; i < N; ++i) {
    scanf("%d", &arr[i].val);
    arr[i].idx = i + 1;
    arr[i].t = 0;
    arr[i].r = i + 1;
  }
  for (i = N; i < N + Q; ++i) {
    scanf("%d%d", &arr[i].l, &arr[i].r);
    arr[i].idx = i - N;
    arr[i].t = 1;
  }
  sort(arr, arr + N + Q);
  int ans[Q];
  for (i = 0; i < N + Q; ++i) {
    if (arr[i].t) {
      ans[arr[i].idx] = sum(arr[i].r) - sum(arr[i].l - 1);
    } else {
      if (visited[arr[i].val]) {
        if (prev[arr[i].val] != 0) {
          add(prev[arr[i].val], -1);
        }
      }
      visited[arr[i].val] = true;
      prev[arr[i].val] = arr[i].idx;
    }
  }
  for (i = 0; i < Q; ++i) {
    printf("%d\n", ans[i]);
  }
  return 0;
}

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

using namespace std;

struct req {
  int l, r, val, idx;
  bool t;
};

bool operator<(req a, req b) {
  if (a.r == b.r) {
    return a.t < b.t;
  }
  return a.r < b.r;
}

int bit[500001];

void add(int y, int val) {
  while (y <= 500000) {
    bit[y] += val;
    y += (y & -y);
  }
}

int sum(int y) {
  int res = 0;
  while (y > 0) {
    res += bit[y];
    y -= (y & -y);
  }
  return res;
}

bool visited[2000001];
req arr[1000005];
int main() {
  int N, Q, i;
  int *prev = new int[2000001];
  for (i = 0; i <= 2000000; ++i) {
    visited[i] = false;
    prev[i] = 0;
  }
  scanf("%d", &N);
  scanf("%d", &Q);
  for (i = 1; i <= N; ++i) {
    add(i, 1);
  }
  for (i = 0; i < N; ++i) {
    scanf("%d", &arr[i].val);
    arr[i].idx = i + 1;
    arr[i].t = 0;
    arr[i].r = i + 1;
  }
  for (i = N; i < N + Q; ++i) {
    scanf("%d%d", &arr[i].l, &arr[i].r);
    arr[i].idx = i - N;
    arr[i].t = 1;
  }
  sort(arr, arr + N + Q);
  int ans[Q];
  for (i = 0; i < N + Q; ++i) {
    if (arr[i].t) {
      ans[arr[i].idx] = sum(arr[i].r) - sum(arr[i].l - 1);
    } else {
      if (visited[arr[i].val]) {
        if (prev[arr[i].val] != 0) {
          add(prev[arr[i].val], -1);
        }
      }
      visited[arr[i].val] = true;
      prev[arr[i].val] = arr[i].idx;
    }
  }
  for (i = 0; i < Q; ++i) {
    printf("%d\n", ans[i]);
  }
  return 0;
}
Problem p02599 (C++)

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

// #pragma GCC optimize("Ofast")

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const int NN = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 26;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q;
int c[N];
int t[N];
int res[N];
int last[N];
vector<pair<int, int>> qr[N];

void upd(int x, int y) {
  while (x < N) {
    t[x] += y;
    x += x & -x;
  }
}

int get(int x) {
  int res = 0;
  while (x > 0) {
    res += t[x];
    x -= x & -x;
  }
  return res;
}

void solve1() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qr[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (last[c[i]] != 0)
      upd(last[c[i]], -1);
    upd(i, 1);
    last[c[i]] = i;
    for (auto x : qr[i]) {
      res[x.se] = get(i) - get(x.fi - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << res[i] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen( "input.txt" , "r" , stdin );
  // freopen( "output.txt" , "w" , stdout );

  int cghf = 1; // cin >> cghf;
  while (cghf--) {
    solve1();
  }
}


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

// #pragma GCC optimize("Ofast")

#define fi first
#define se second
#define ll long long
#define dl double long

using namespace std;

const int NN = 1e9 + 7;
const int N = 1e6 + 7;
const int M = 26;
const ll mod = 1e9 + 7;
const int inf = 1e9 + 7;
const dl rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, q;
int c[N];
int t[N];
int res[N];
int last[N];
vector<pair<int, int>> qr[N];

void upd(int x, int y) {
  while (x < N) {
    t[x] += y;
    x += x & -x;
  }
}

int get(int x) {
  int res = 0;
  while (x > 0) {
    res += t[x];
    x -= x & -x;
  }
  return res;
}

void solve1() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qr[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (last[c[i]] != 0)
      upd(last[c[i]], -1);
    upd(i, 1);
    last[c[i]] = i;
    for (auto x : qr[i]) {
      res[x.se] = get(i) - get(x.fi - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << res[i] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen( "input.txt" , "r" , stdin );
  // freopen( "output.txt" , "w" , stdout );

  int cghf = 1; // cin >> cghf;
  while (cghf--) {
    solve1();
  }
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[500005];
int n, m, a[500005], ans[500005];
int st[25 * 100001], used[5000005];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}
Problem p02599 (C++)

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

using namespace std;

const int MAXN = 200010;

int A[MAXN], B[MAXN];
vector<int> T[4 * MAXN];

void build(int i, int l, int r) {
  if (l == r)
    T[i].push_back(B[r]);
  else {
    int m = (l + r) / 2;
    build(2 * i, l, m);
    build(2 * i + 1, m + 1, r);
    merge(T[2 * i].begin(), T[2 * i].end(), T[2 * i + 1].begin(),
          T[2 * i + 1].end(), back_inserter(T[i]));
  }
}

int ask(int i, int l, int r, int ql, int qr) {
  if (r < ql || qr < l)
    return 0;
  if (ql <= l && r <= qr)
    return lower_bound(T[i].begin(), T[i].end(), ql) - T[i].begin();
  int m = (l + r) / 2;
  return ask(2 * i, l, m, ql, qr) + ask(2 * i + 1, m + 1, r, ql, qr);
}

void solver() {
  int N, M;
  cin >> N >> M;
  // cin >> N;

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

  memset(B, -1, sizeof(B));

  map<int, int> last;

  for (int i = 0; i < N; ++i) {
    if (!last.count(A[i]))
      B[i] = -1;
    else
      B[i] = last[A[i]];

    last[A[i]] = i;
  }

  build(1, 0, N - 1);

  // cin >> M;

  while (M-- > 0) {
    int left, right;
    cin >> left >> right;
    cout << ask(1, 0, N - 1, left - 1, right - 1) << '\n';
  }
}

int main() {

  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);

  int t;
  // cin >> t;
  t = 1;

  while (t--)
    solver();

  return 0;
}

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

using namespace std;

const int MAXN = 500001;

int A[MAXN], B[MAXN];
vector<int> T[4 * MAXN];

void build(int i, int l, int r) {
  if (l == r)
    T[i].push_back(B[r]);
  else {
    int m = (l + r) / 2;
    build(2 * i, l, m);
    build(2 * i + 1, m + 1, r);
    merge(T[2 * i].begin(), T[2 * i].end(), T[2 * i + 1].begin(),
          T[2 * i + 1].end(), back_inserter(T[i]));
  }
}

int ask(int i, int l, int r, int ql, int qr) {
  if (r < ql || qr < l)
    return 0;
  if (ql <= l && r <= qr)
    return lower_bound(T[i].begin(), T[i].end(), ql) - T[i].begin();
  int m = (l + r) / 2;
  return ask(2 * i, l, m, ql, qr) + ask(2 * i + 1, m + 1, r, ql, qr);
}

void solver() {
  int N, M;
  cin >> N >> M;
  // cin >> N;

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

  memset(B, -1, sizeof(B));

  map<int, int> last;

  for (int i = 0; i < N; ++i) {
    if (!last.count(A[i]))
      B[i] = -1;
    else
      B[i] = last[A[i]];

    last[A[i]] = i;
  }

  build(1, 0, N - 1);

  // cin >> M;

  while (M-- > 0) {
    int left, right;
    cin >> left >> right;
    cout << ask(1, 0, N - 1, left - 1, right - 1) << '\n';
  }
}

int main() {

  ios_base ::sync_with_stdio(0);
  cin.tie(NULL);

  int t;
  // cin >> t;
  t = 1;

  while (t--)
    solver();

  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
int c[maxn], vis[maxn], ans[maxn], tree[maxn];
vector<pair<int, int>> v[maxn];
int n, q;
int lowbit(int x) { return x & (-x); }
void add(int x, int k) {
  while (x <= n) {
    tree[x] += k;
    x += lowbit(x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x != 0) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    v[r].push_back(make_pair(i, l));
  }
  for (int i = 1; i <= n; i++) {
    if (vis[c[i]] != 0)
      add(vis[c[i]], -1);
    vis[c[i]] = i;
    add(i, 1);
    for (auto j : v[i])
      ans[j.first] = sum(i) - sum(j.second - 1);
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 0x3f3f3f3f;
int c[maxn], vis[maxn], ans[maxn], tree[maxn];
vector<pair<int, int>> v[maxn];
int n, q;
int lowbit(int x) { return x & (-x); }
void add(int x, int k) {
  while (x <= n) {
    tree[x] += k;
    x += lowbit(x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x != 0) {
    ans += tree[x];
    x -= lowbit(x);
  }
  return ans;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    v[r].push_back(make_pair(i, l));
  }
  for (int i = 1; i <= n; i++) {
    if (vis[c[i]] != 0)
      add(vis[c[i]], -1);
    vis[c[i]] = i;
    add(i, 1);
    for (auto j : v[i])
      ans[j.first] = sum(i) - sum(j.second - 1);
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int bit[N], p[N], ans[N], a[N];
pair<int, pair<int, int>> pr[N];

void update(int idx, int val) {
  while (idx < N) {
    bit[idx] += val;
    idx += (idx & -idx);
  }
}

int read(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += bit[idx];
    idx -= (idx & -idx);
  }
  return sum;
}

int main() {
  int n, q, x;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 0; i < q; ++i) {
    cin >> pr[i].second.first >> pr[i].first;
    pr[i].second.second = i;
  }
  sort(pr, pr + q);
  memset(p, -1, sizeof(p));
  memset(bit, 0, sizeof(bit));
  x = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);
    while (x < q and pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);
      x++;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << "\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int bit[N], p[N], ans[N], a[N];
pair<int, pair<int, int>> pr[N];

void update(int idx, int val) {
  while (idx < N) {
    bit[idx] += val;
    idx += (idx & -idx);
  }
}

int read(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += bit[idx];
    idx -= (idx & -idx);
  }
  return sum;
}

int main() {
  int n, q, x;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 0; i < q; ++i) {
    cin >> pr[i].second.first >> pr[i].first;
    pr[i].second.second = i;
  }
  sort(pr, pr + q);
  memset(p, -1, sizeof(p));
  memset(bit, 0, sizeof(bit));
  x = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);
    while (x < q and pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);
      x++;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << "\n";
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;
const int N = 500009;
pair<int, pair<int, int>> q[N];
int n, m, a[N], ans[N];
int st[5 * N], used[5 * N];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
/*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x & (-x));
using namespace std;

const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  return x * f;
}

struct node {
  int l, r, pos;
} ask[A];
int n, m, c[A], num[A], tree[A], booll[A], nnn[A], ww;

void change(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}

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

bool cmp(node a, node b) { return a.r < b.r; }

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &num[i]);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &ask[i].l, &ask[i].r);
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (booll[num[j]])
        change(booll[num[j]], -1);
      change(j, 1), booll[num[j]] = j;
    }
    next = ask[i].r + 1;
    nnn[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    cout << nnn[i] << "\n";
  return 0;
}


Fixed submission:
/*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x & (-x));
using namespace std;

const int A = 1e6 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  return x * f;
}

struct node {
  int l, r, pos;
} ask[A];
int n, m, c[A], num[A], tree[A], booll[A], nnn[A], ww;

void change(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}

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

bool cmp(node a, node b) { return a.r < b.r; }

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &num[i]);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &ask[i].l, &ask[i].r);
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (booll[num[j]])
        change(booll[num[j]], -1);
      change(j, 1), booll[num[j]] = j;
    }
    next = ask[i].r + 1;
    nnn[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    cout << nnn[i] << "\n";
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[1000001];
int n, m, a[500001], ans[1000001];
int st[5 * 500001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);

  return 0;
}
Problem p02599 (C++)

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

using namespace std;

struct query {
  int l;
  int r;
  int i;
  int ans;
};

signed main() {
  int n, t;
  cin >> n >> t;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<query> q(t);
  for (int i = 0; i < t; ++i) {
    cin >> q[i].l >> q[i].r;
    q[i].i = i;
    q[i].l--;
    q[i].r--;
  }

  int sqrtn = sqrt(n) + 1;
  sort(q.begin(), q.end(), [&sqrtn](query &a, query &b) {
    if (a.r == b.r)
      return a.l < b.l;
    return a.r < b.r;
  });

  vector<int> m(n + 1);
  int l = 0;
  int r = -1;
  int cnt = 0;

  for (auto &q : q) {
    if (r < q.l || q.r < l) {
      m.assign(n + 1, 0);
      cnt = 0;
      l = q.l;
      r = q.l - 1;
    }
    while (r < q.r) {
      r++;
      if (m[a[r]] == 0)
        cnt++;
      m[a[r]]++;
    }
    while (r > q.r) {
      m[a[r]]--;
      if (m[a[r]] == 0)
        cnt--;
      r--;
    }
    while (l < q.l) {
      m[a[l]]--;
      if (m[a[l]] == 0)
        cnt--;
      l++;
    }
    while (l > q.l) {
      l--;
      if (m[a[l]] == 0)
        cnt++;
      m[a[l]]++;
    }
    q.ans = cnt;
  }
  sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });

  for (auto &q : q) {
    cout << q.ans << '\n';
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

struct query {
  int l;
  int r;
  int i;
  int ans;
};

signed main() {
  int n, t;
  cin >> n >> t;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  vector<query> q(t);
  for (int i = 0; i < t; ++i) {
    cin >> q[i].l >> q[i].r;
    q[i].i = i;
    q[i].l--;
    q[i].r--;
  }

  int sqrtn = sqrt(n) + 1;
  sort(q.begin(), q.end(), [&sqrtn](query &a, query &b) {
    if (a.l / sqrtn != b.l / sqrtn) {
      return a.l / sqrtn < b.l / sqrtn;
    }
    return a.r < b.r;
  });

  vector<int> m(n + 1);
  int l = 0;
  int r = -1;
  int cnt = 0;

  for (auto &q : q) {
    if (r < q.l || q.r < l) {
      m.assign(n + 1, 0);
      cnt = 0;
      l = q.l;
      r = q.l - 1;
    }
    while (r < q.r) {
      r++;
      if (m[a[r]] == 0)
        cnt++;
      m[a[r]]++;
    }
    while (r > q.r) {
      m[a[r]]--;
      if (m[a[r]] == 0)
        cnt--;
      r--;
    }
    while (l < q.l) {
      m[a[l]]--;
      if (m[a[l]] == 0)
        cnt--;
      l++;
    }
    while (l > q.l) {
      l--;
      if (m[a[l]] == 0)
        cnt++;
      m[a[l]]++;
    }
    q.ans = cnt;
  }
  sort(q.begin(), q.end(), [](query &a, query &b) { return a.i < b.i; });

  for (auto &q : q) {
    cout << q.ans << '\n';
  }
}
Problem p02599 (C++)

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

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;

#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair

template <typename T> using vv = vector<vector<T>>;

void solve() {
  int n, q;
  cin >> n >> q;
  vi a(n);
  for (auto &i : a)
    cin >> i, i--;
  ;
  vector<pair<pll, int>> qr(q);
  int cc = 0;
  for (auto &e : qr)
    cin >> e.first.first >> e.first.second, e.first.first--, e.first.second--,
        e.second = cc++;
  sort(all(qr), [&](pair<pll, int> aa, pair<pll, int> bb) {
    auto a = aa.first, b = bb.first;
    if (a.first / 1000 != b.first / 1000)
      return a.first / 1000 < b.first;
    return a.second < b.second;
  });

  vi cnt(n);
  ll tmp = 0;
  ll l = 0, r = -1;

  vi res(q);

  for (auto e : qr) {
    ll nl, nr;
    tie(nl, nr) = e.first;
    while (r < nr) {
      r++;
      if (cnt[a[r]]++ == 0)
        tmp++;
    }
    while (l > nl) {
      l--;
      if (cnt[a[l]]++ == 0)
        tmp++;
    }
    while (r > nr) {
      if (--cnt[a[r--]] == 0)
        tmp--;
    }
    while (l < nl) {
      if (--cnt[a[l++]] == 0)
        tmp--;
    }
    res[e.second] = tmp;
  }

  for (auto i : res)
    cout << i << '\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}


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

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;

#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair

template <typename T> using vv = vector<vector<T>>;

void solve() {
  int n, q;
  cin >> n >> q;
  vi a(n);
  for (auto &i : a)
    cin >> i, i--;
  ;
  vector<pair<pll, int>> qr(q);
  int cc = 0;
  for (auto &e : qr)
    cin >> e.first.first >> e.first.second, e.first.first--, e.first.second--,
        e.second = cc++;
  sort(all(qr), [&](pair<pll, int> aa, pair<pll, int> bb) {
    auto a = aa.first, b = bb.first;
    if (a.first / 1000 != b.first / 1000)
      return a.first / 1000 < b.first / 1000;
    return a.second < b.second;
  });

  vi cnt(n);
  ll tmp = 0;
  ll l = 0, r = -1;

  vi res(q);

  for (auto e : qr) {
    ll nl, nr;
    tie(nl, nr) = e.first;
    while (r < nr) {
      r++;
      if (cnt[a[r]]++ == 0)
        tmp++;
    }
    while (l > nl) {
      l--;
      if (cnt[a[l]]++ == 0)
        tmp++;
    }
    while (r > nr) {
      if (--cnt[a[r--]] == 0)
        tmp--;
    }
    while (l < nl) {
      if (--cnt[a[l++]] == 0)
        tmp--;
    }
    res[e.second] = tmp;
  }

  for (auto i : res)
    cout << i << '\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}