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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 750
#define mxn 5000001
using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l / BLOCK < b.l / BLOCK;

  return a.r < b.r;
}

void add(int pos) {
  fre[ar[pos]]++;
  if (fre[ar[pos]] == 1)
    cnt++;
}

void remove(int pos) {
  fre[ar[pos]]--;
  if (fre[ar[pos]] == 0)
    cnt--;
}

int main() {
  //  ios_base::sync_with_stdio(false);
  // cin.tie(NULL);

  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt","r",stdin);
  //     freopen("output.txt","w",stdout);
  // #endif

  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> ar[i];

  for (int i = 0; i < q; i++) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].i = i, Q[i].l--, Q[i].r--;
  }

  sort(Q, Q + q, comp);

  int ML = 0, MR = -1;
  for (int i = 0; i < q; i++) {
    int L = Q[i].l;
    int R = Q[i].r;

    while (ML > L)
      ML--, add(ML);

    while (MR < R)
      MR++, add(MR);

    while (ML < L)
      remove(ML), ML++;

    while (MR > R)
      remove(MR), MR--;

    ans[Q[i].i] = cnt;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define BLOCK 750
#define mxn 5000001
using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[mxn];
int ar[mxn], ans[mxn];
int fre[mxn];
int cnt = 0;
bool comp(query a, query b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l / BLOCK < b.l / BLOCK;

  return a.r < b.r;
}

void add(int pos) {
  fre[ar[pos]]++;
  if (fre[ar[pos]] == 1)
    cnt++;
}

void remove(int pos) {
  fre[ar[pos]]--;
  if (fre[ar[pos]] == 0)
    cnt--;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt","r",stdin);
  //     freopen("output.txt","w",stdout);
  // #endif

  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> ar[i];

  for (int i = 0; i < q; i++) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].i = i, Q[i].l--, Q[i].r--;
  }

  sort(Q, Q + q, comp);

  int ML = 0, MR = -1;
  for (int i = 0; i < q; i++) {
    int L = Q[i].l;
    int R = Q[i].r;

    while (ML > L)
      ML--, add(ML);

    while (MR < R)
      MR++, add(MR);

    while (ML < L)
      remove(ML), ML++;

    while (MR > R)
      remove(MR), MR--;

    ans[Q[i].i] = cnt;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << '\n';
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ii {
  int a, b;
  bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
  int a, b, c;
  bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};

const int MOD = 1000000007;
const int INF = 1000000000;
const int MAXN = 500005;

int N, sqN, Q, curUnique;
int arr[MAXN], cnt[MAXN], ans[MAXN];
iii query[MAXN];

bool cmp(iii x, iii y) {
  return make_tuple(x.b / sqN, x.c / sqN) < make_tuple(y.b / sqN, y.c / sqN);
}

void inc(int idx) {
  if (cnt[arr[idx]] == 0)
    curUnique++;
  cnt[arr[idx]]++;
}

void dec(int idx) {
  cnt[arr[idx]]--;
  if (cnt[arr[idx]] == 0)
    curUnique--;
}

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

  cin >> N >> Q;
  sqN = sqrt(N);
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }

  for (int i = 0; i < Q; i++) {
    query[i].a = i;
    cin >> query[i].b >> query[i].c;
    query[i].b--;
    query[i].c--;
  }

  sort(query, query + Q, cmp);

  int l = 0;
  int r = 0;
  cnt[arr[l]]++;
  curUnique = 1;
  for (int i = 0; i < Q; i++) {
    while (l < query[i].b)
      dec(l++);
    while (l > query[i].b)
      inc(--l);
    while (r < query[i].c)
      inc(++r);
    while (r > query[i].c)
      dec(r--);
    ans[query[i].a] = curUnique;
  }

  for (int i = 0; i < Q; i++)
    cout << ans[i] << "\n";
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ii {
  int a, b;
  bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
  int a, b, c;
  bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};

const int MOD = 1000000007;
const int INF = 1000000000;
const int MAXN = 500005;

int N, sqN, Q, curUnique;
int arr[MAXN], cnt[MAXN], ans[MAXN];
iii query[MAXN];

bool cmp(iii x, iii y) {
  return make_tuple(x.b / sqN, x.c) < make_tuple(y.b / sqN, y.c);
}

void inc(int idx) {
  if (cnt[arr[idx]] == 0)
    curUnique++;
  cnt[arr[idx]]++;
}

void dec(int idx) {
  cnt[arr[idx]]--;
  if (cnt[arr[idx]] == 0)
    curUnique--;
}

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

  cin >> N >> Q;
  sqN = sqrt(N);
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }

  for (int i = 0; i < Q; i++) {
    query[i].a = i;
    cin >> query[i].b >> query[i].c;
    query[i].b--;
    query[i].c--;
  }

  sort(query, query + Q, cmp);

  int l = 0;
  int r = 0;
  cnt[arr[l]]++;
  curUnique = 1;
  for (int i = 0; i < Q; i++) {
    while (l < query[i].b)
      dec(l++);
    while (l > query[i].b)
      inc(--l);
    while (r < query[i].c)
      inc(++r);
    while (r > query[i].c)
      dec(r--);
    ans[query[i].a] = curUnique;
  }

  for (int i = 0; i < Q; i++)
    cout << ans[i] << "\n";
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <math.h>
const int mx = 5e5 + 5;

int n, q, a[mx], blk;
struct node {
  int l, r, p;
  node() {}
  node(int _l, int _r, int _p) : l(_l), r(_r), p(_p) {}
  bool operator<(const node &ot) {
    if (l / blk != ot.l / blk)
      return l / blk < ot.l / blk;
    return r > ot.r;
  }
} qq[mx];

int ans = 0, cnt[mx], res[mx];
void add(int x) {
  if (!cnt[a[x]])
    ans++;
  cnt[a[x]]++;
}
void remove(int x) {
  cnt[a[x]]--;
  if (!cnt[a[x]])
    ans--;
}

int main() {
  scanf("%d %d", &n, &q);

  blk = sqrt(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  for (int l, r, i = 0; i < q; i++) {
    scanf("%d %d", &l, &r);
    qq[i] = node(l - 1, r - 1, i);
  }
  std ::sort(qq, qq + q);

  int cl = 0, cr = 0;
  for (int l, r, i = 0; i < q; i++) {
    l = qq[i].l, r = qq[i].r;
    while (cl < l)
      remove(cl++);
    while (cl > l)
      add(--cl);
    while (cr <= r)
      add(cr++);
    while (cr > r + 1)
      remove(--cr);
    res[qq[i].p] = ans;
  }

  for (int i = 0; i < q; i++)
    printf("%d\n", res[i]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <math.h>
const int mx = 5e5 + 5;

int n, q, a[mx], blk;
struct node {
  int l, r, p;
  node() {}
  node(int _l, int _r, int _p) : l(_l), r(_r), p(_p) {}
  bool operator<(const node &ot) {
    if (l / blk != ot.l / blk)
      return l < ot.l;
    return (bool)((r < ot.r) ^ ((l / blk) & 1));
  }
} qq[mx];

int ans = 0, cnt[mx], res[mx];
void add(int x) {
  if (!cnt[a[x]])
    ans++;
  cnt[a[x]]++;
}
void remove(int x) {
  cnt[a[x]]--;
  if (!cnt[a[x]])
    ans--;
}

int main() {
  scanf("%d %d", &n, &q);

  blk = sqrt(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);

  for (int l, r, i = 0; i < q; i++) {
    scanf("%d %d", &l, &r);
    qq[i] = node(l - 1, r - 1, i);
  }
  std ::sort(qq, qq + q);

  int cl = 0, cr = 0;
  for (int l, r, i = 0; i < q; i++) {
    l = qq[i].l, r = qq[i].r;
    while (cl < l)
      remove(cl++);
    while (cl > l)
      add(--cl);
    while (cr <= r)
      add(cr++);
    while (cr > r + 1)
      remove(--cr);
    res[qq[i].p] = ans;
  }

  for (int i = 0; i < q; i++)
    printf("%d\n", res[i]);

  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);
  memset(v, 0, sizeof(v));
  memset(sum, 0, sizeof(sum));
  memset(ans, 0, sizeof(ans));
  scanf("%d", &q);
  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 = 5e5 + 10;
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);
  memset(v, 0, sizeof(v));
  memset(sum, 0, sizeof(sum));
  memset(ans, 0, sizeof(ans));
  scanf("%d", &q);
  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 <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 500000 + 500;
const int B = 1000000 + 1000;
const int M = 200000 + 200;

int Yuyuko;
int n, m;
int b[B], a[N], block[N], ans[M], cans = 1;
struct operation {
  int l, r, id;
} o[M];

inline int read() {
  int a = 0;
  char ch;
  int f = 1;
  while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')))
    ;
  if (ch == '-')
    f = -1;
  else {
    a = a * 10;
    a += ch - '0';
  }
  while ((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')) {
    a = a * 10;
    a += ch - '0';
  }
  return a * f;
}

inline bool cmp(operation x, operation y) {
  if (block[x.l] != block[y.l])
    return block[x.l] < block[y.l];
  else
    return x.r < y.r;
}

inline void add(int x) {
  if (!b[a[x]])
    ++cans;
  ++b[a[x]];
}

inline void minu(int x) {
  --b[a[x]];
  if (!b[a[x]])
    --cans;
}

int main() {
  n = read(), m = read();
  Yuyuko = sqrt(n) + 1;
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    block[i] = (i - 1) / Yuyuko + 1;
  }
  for (int i = 1; i <= m; ++i) {
    o[i].l = read(), o[i].r = read();
    o[i].id = i;
  }
  sort(o + 1, o + 1 + m, cmp);
  int L = 1, R = 1;
  b[a[1]] = 1;
  for (int i = 1; i <= m; ++i) {
    while (L < o[i].l)
      minu(L++);
    while (L > o[i].l)
      add(--L);
    while (R < o[i].r)
      add(++R);
    while (R > o[i].r)
      minu(R--);
    ans[o[i].id] = cans;
  }
  for (int i = 1; i <= m; ++i)
    printf("%d\n", ans[i]);
  return 0;
}

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

const int N = 500000 + 500;
const int B = 1000000 + 1000;
const int M = 500000 + 200;

int Yuyuko;
int n, m;
int b[B], a[N], block[N], ans[M], cans = 1;
struct operation {
  int l, r, id;
} o[M];

inline int read() {
  int a = 0;
  char ch;
  int f = 1;
  while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')))
    ;
  if (ch == '-')
    f = -1;
  else {
    a = a * 10;
    a += ch - '0';
  }
  while ((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')) {
    a = a * 10;
    a += ch - '0';
  }
  return a * f;
}

inline bool cmp(operation x, operation y) {
  if (block[x.l] != block[y.l])
    return block[x.l] < block[y.l];
  else
    return x.r < y.r;
}

inline void add(int x) {
  if (!b[a[x]])
    ++cans;
  ++b[a[x]];
}

inline void minu(int x) {
  --b[a[x]];
  if (!b[a[x]])
    --cans;
}

int main() {
  n = read(), m = read();
  Yuyuko = sqrt(n) + 1;
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    block[i] = (i - 1) / Yuyuko + 1;
  }
  for (int i = 1; i <= m; ++i) {
    o[i].l = read(), o[i].r = read();
    o[i].id = i;
  }
  sort(o + 1, o + 1 + m, cmp);
  int L = 1, R = 1;
  b[a[1]] = 1;
  for (int i = 1; i <= m; ++i) {
    while (L < o[i].l)
      minu(L++);
    while (L > o[i].l)
      add(--L);
    while (R < o[i].r)
      add(++R);
    while (R > o[i].r)
      minu(R--);
    ans[o[i].id] = cans;
  }
  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;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};

int main() {
  ll n, q;
  cin >> n >> q;
  vl c(n), last(5e5 + 10, -1);
  rep(i, n) cin >> c[i];
  vector<pair<P, ll>> p(q);
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    p[i] = {{r, l}, i};
  }
  sort(all(p));
  BIT<ll> b(n + 10);
  vl ans(n);
  ll k = 0;
  rep(i, q) {
    ll l = p[i].first.second;
    ll r = p[i].first.first;
    ll j = p[i].second;
    for (; k <= r; k++) {
      ll now = last[c[k]];
      last[c[k]] = k;
      if (now >= 0)
        b.add(now, -1);
      b.add(k, 1);
    }
    ans[j] = b.sum(r);
    if (l > 0)
      ans[j] -= b.sum(l - 1);
  }
  rep(i, q) cout << ans[i] << '\n';
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
};

int main() {
  ll n, q;
  cin >> n >> q;
  vl c(n), last(5e5 + 10, -1);
  rep(i, n) cin >> c[i];
  vector<pair<P, ll>> p(q);
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    p[i] = {{r, l}, i};
  }
  sort(all(p));
  BIT<ll> b(n + 10);
  vl ans(q);
  ll k = 0;
  rep(i, q) {
    ll l = p[i].first.second;
    ll r = p[i].first.first;
    ll j = p[i].second;
    for (; k <= r; k++) {
      ll now = last[c[k]];
      last[c[k]] = k;
      if (now >= 0)
        b.add(now, -1);
      b.add(k, 1);
    }
    ans[j] = b.sum(r);
    if (l > 0)
      ans[j] -= b.sum(l - 1);
  }
  rep(i, q) cout << ans[i] << '\n';
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
/* 実装例　Atcoder Beginers Contest 174-F Range Set query */
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
signed main() {
  int n, q;
  std::cin >> n >> q;
  int sq = 2 * sqrt(n);
  std::vector<int> c(n);
  rep(i, n) {
    std::cin >> c[i];
    c[i]--;
  }
  std::vector<int> l(n), r(n);
  rep(i, q) {
    std::cin >> l[i] >> r[i];
    l[i]--;
  }

  std::vector<int> qu(q);
  rep(i, q) qu[i] = i;
  std::sort(ALL(qu), [&](int i, int j) {
    if (l[i] / sq != l[j] / sq)
      return l[i] < l[j];
    else
      return r[i] < r[j];
  });

  std::vector<int> ans(q);
  std::vector<int> cnt(n, 0);
  int nl = 0, nr = 0;
  int res = 0;
  for (int i : qu) {
    while (nl > l[i]) {
      nl--;
      if (cnt[c[nl]]++ == 0)
        res++;
    }
    while (nl < l[i]) {
      if (--cnt[c[nl]] == 0)
        res--;
      nl++;
    }
    while (nr < r[i]) {
      if (cnt[c[nr]]++ == 0)
        res++;
      nr++;
    }
    while (nr > r[i]) {
      nr--;
      if (--cnt[c[nr]] == 0)
        res--;
    }
    /* ここから結果 */
    ans[i] = res;
  }
  rep(i, q) std::cout << ans[i] << "\n";
}


Fixed submission:
/* 実装例　Atcoder Beginers Contest 174-F Range Set query */
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
signed main() {
  int n, q;
  std::cin >> n >> q;
  int sq = 2 * sqrt(n);
  std::vector<int> c(n);
  rep(i, n) {
    std::cin >> c[i];
    c[i]--;
  }
  std::vector<int> l(q), r(q);
  rep(i, q) {
    std::cin >> l[i] >> r[i];
    l[i]--;
  }

  std::vector<int> qu(q);
  rep(i, q) qu[i] = i;
  std::sort(ALL(qu), [&](int i, int j) {
    if (l[i] / sq != l[j] / sq)
      return l[i] < l[j];
    else
      return r[i] < r[j];
  });

  std::vector<int> ans(q);
  std::vector<int> cnt(n, 0);
  int nl = 0, nr = 0;
  int res = 0;
  for (int i : qu) {
    while (nl > l[i]) {
      nl--;
      if (cnt[c[nl]]++ == 0)
        res++;
    }
    while (nl < l[i]) {
      if (--cnt[c[nl]] == 0)
        res--;
      nl++;
    }
    while (nr < r[i]) {
      if (cnt[c[nr]]++ == 0)
        res++;
      nr++;
    }
    while (nr > r[i]) {
      nr--;
      if (--cnt[c[nr]] == 0)
        res--;
    }
    /* ここから結果 */
    ans[i] = res;
  }
  rep(i, q) std::cout << ans[i] << "\n";
}
Problem p02599 (C++)

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

#define N 511111
#define A 1111111
#define BLOCK 555 // ~sqrt(N)

int cnt[A], a[N], ans[N], answer = 0;

struct node {
  int L, R, i;
} q[N];

bool cmp(node x, node y) {
  if (x.L / BLOCK != y.L / BLOCK) {
    // different blocks, so sort by block.
    return x.L / BLOCK < y.L / BLOCK;
  }
  // same block, so sort by R value
  return x.R < y.R;
}

void add(int position) {
  cnt[a[position]]++;
  if (cnt[a[position]] == 1) {
    answer++;
  }
}

void remove(int position) {
  cnt[a[position]]--;
  if (cnt[a[position]] == 0) {
    answer--;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < m; i++) {
    cin >> q[i].L >> q[i].R;
    q[i].L--;
    q[i].R--;
    q[i].i = i;
  }

  sort(q, q + m, cmp);

  int currentL = 0, currentR = 0;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currentL < L) {
      remove(currentL);
      currentL++;
    }
    while (currentL > L) {
      add(currentL - 1);
      currentL--;
    }
    while (currentR <= R) {
      add(currentR);
      currentR++;
    }
    while (currentR > R + 1) {
      remove(currentR - 1);
      currentR--;
    }
    ans[q[i].i] = answer;
  }

  for (int i = 0; i < m; i++)
    cout << ans[i] << "\n";
}

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

#define N 511111
#define A 511111
#define BLOCK 710 // ~sqrt(N)

int cnt[A], a[N], ans[N], answer = 0;

struct node {
  int L, R, i;
} q[N];

bool cmp(node x, node y) {
  if (x.L / BLOCK != y.L / BLOCK) {
    // different blocks, so sort by block.
    return x.L / BLOCK < y.L / BLOCK;
  }
  // same block, so sort by R value
  return x.R < y.R;
}

void add(int position) {
  cnt[a[position]]++;
  if (cnt[a[position]] == 1) {
    answer++;
  }
}

void remove(int position) {
  cnt[a[position]]--;
  if (cnt[a[position]] == 0) {
    answer--;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  for (int i = 0; i < m; i++) {
    cin >> q[i].L >> q[i].R;
    q[i].L--;
    q[i].R--;
    q[i].i = i;
  }

  sort(q, q + m, cmp);

  int currentL = 0, currentR = 0;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currentL < L) {
      remove(currentL);
      currentL++;
    }
    while (currentL > L) {
      add(currentL - 1);
      currentL--;
    }
    while (currentR <= R) {
      add(currentR);
      currentR++;
    }
    while (currentR > R + 1) {
      remove(currentR - 1);
      currentR--;
    }
    ans[q[i].i] = answer;
  }

  for (int i = 0; i < m; i++)
    cout << ans[i] << "\n";
}
Problem p02599 (C++)

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

int MAX_N = 262144;
vector<int64_t> seg(MAX_N * 2 - 1);
int64_t qu(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return 0;
  } else if (a <= l && r <= b) {
    return seg[k];
  } else {
    int64_t X = qu(a, b, k * 2 + 1, l, (l + r) / 2);
    int64_t Y = qu(a, b, k * 2 + 2, (l + r) / 2, r);
    return X + Y;
  }
}
void pu(int64_t a, int b) {
  int D = MAX_N - 1 + b;
  while (D > 0) {
    seg[D] += a;
    D--;
    D /= 2;
  }
  seg[0] += a;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<tuple<int, int, int>> q(Q);
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    q[i] = make_tuple(b, a, i);
  }
  sort(q.begin(), q.end());
  vector<int> la(N, -1);
  vector<int> an(Q);
  int W = 0;
  for (int i = 0; i < Q; i++) {
    int a, b, c;
    tie(a, b, c) = q[i];
    while (W < a) {
      if (la[p[W]] != -1) {
        pu(-1, la[p[W]]);
      }
      la[p[W]] = W;
      pu(1, W);
      W++;
    }
    an[c] = qu(b - 1, a, 0, 0, MAX_N);
  }
  for (int i = 0; i < Q; i++) {
    cout << an[i] << endl;
  }
}

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

int MAX_N = 262144 * 2;
vector<int64_t> seg(MAX_N * 2 - 1);
int64_t qu(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return 0;
  } else if (a <= l && r <= b) {
    return seg[k];
  } else {
    int64_t X = qu(a, b, k * 2 + 1, l, (l + r) / 2);
    int64_t Y = qu(a, b, k * 2 + 2, (l + r) / 2, r);
    return X + Y;
  }
}
void pu(int64_t a, int b) {
  int D = MAX_N - 1 + b;
  while (D > 0) {
    seg[D] += a;
    D--;
    D /= 2;
  }
  seg[0] += a;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<tuple<int, int, int>> q(Q);
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    q[i] = make_tuple(b, a, i);
  }
  sort(q.begin(), q.end());
  vector<int> la(N, -1);
  vector<int> an(Q);
  int W = 0;
  for (int i = 0; i < Q; i++) {
    int a, b, c;
    tie(a, b, c) = q[i];
    while (W < a) {
      if (la[p[W]] != -1) {
        pu(-1, la[p[W]]);
      }
      la[p[W]] = W;
      pu(1, W);
      W++;
    }
    an[c] = qu(b - 1, a, 0, 0, MAX_N);
  }
  for (int i = 0; i < Q; i++) {
    cout << an[i] << endl;
  }
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second

int n, q, pos;
int a[30002], tree[4 * 30002], idx[1000002], ans[200002];
pair<int, pair<int, int>> qy[200002];

bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  return a.ss.ff < b.ss.ff;
}

void update(ll pos, ll l, ll r, ll idx, ll value) {
  if (l == r && l == idx) {
    tree[pos] = value;
    return;
  }
  ll mid = (l + r) / 2;
  if (idx >= l && idx <= mid)
    update(pos * 2, l, mid, idx, value);
  if (idx >= (mid + 1) && idx <= r)
    update(pos * 2 + 1, mid + 1, r, idx, value);
  tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
}

ll query(ll pos, ll l, ll r, ll L, ll R) {
  if (l >= L && r <= R)
    return tree[pos];
  if (r < L || l > R)
    return 0;
  ll mid = (l + r) / 2;
  ll q1 = query(pos * 2, l, mid, L, R);
  ll q2 = query(pos * 2 + 1, mid + 1, r, L, R);
  return q1 + q2;
}

int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &qy[i].ff, &qy[i].ss.ff);
    qy[i].ss.ss = i;
  }
  sort(qy, qy + q, cmp);
  pos = 1;
  for (int i = 0; i < q; i++) {
    while (pos <= qy[i].ss.ff) {
      int x = a[pos];
      if (idx[x])
        update(1, 1, n, idx[x], 0);
      update(1, 1, n, pos, 1);
      idx[x] = pos;
      pos++;
    }
    ans[qy[i].ss.ss] = query(1, 1, n, qy[i].ff, qy[i].ss.ff);
  }
  for (int i = 0; i < q; i++)
    printf("%d\n", ans[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define ff first
#define ss second

int n, q, pos;
int a[500002], tree[4 * 500002], idx[500002], ans[500002];
pair<int, pair<int, int>> qy[500002];

bool cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  return a.ss.ff < b.ss.ff;
}

void update(ll pos, ll l, ll r, ll idx, ll value) {
  if (l == r && l == idx) {
    tree[pos] = value;
    return;
  }
  ll mid = (l + r) / 2;
  if (idx >= l && idx <= mid)
    update(pos * 2, l, mid, idx, value);
  if (idx >= (mid + 1) && idx <= r)
    update(pos * 2 + 1, mid + 1, r, idx, value);
  tree[pos] = tree[pos * 2] + tree[pos * 2 + 1];
}

ll query(ll pos, ll l, ll r, ll L, ll R) {
  if (l >= L && r <= R)
    return tree[pos];
  if (r < L || l > R)
    return 0;
  ll mid = (l + r) / 2;
  ll q1 = query(pos * 2, l, mid, L, R);
  ll q2 = query(pos * 2 + 1, mid + 1, r, L, R);
  return q1 + q2;
}

int main() {
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &qy[i].ff, &qy[i].ss.ff);
    qy[i].ss.ss = i;
  }
  sort(qy, qy + q, cmp);
  pos = 1;
  for (int i = 0; i < q; i++) {
    while (pos <= qy[i].ss.ff) {
      int x = a[pos];
      if (idx[x])
        update(1, 1, n, idx[x], 0);
      update(1, 1, n, pos, 1);
      idx[x] = pos;
      pos++;
    }
    ans[qy[i].ss.ss] = query(1, 1, n, qy[i].ff, qy[i].ss.ff);
  }
  for (int i = 0; i < q; i++)
    printf("%d\n", ans[i]);
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll n;
ll dat[200005];
void init(ll n_) {
  n = 1;
  while (n <= n_)
    n *= 2;
}
void update(ll k, ll a) {
  k += n - 1;
  dat[k] = a;
  while (0 < k) {
    k = (k - 1) / 2;
    dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
  }
}
ll query(ll a, ll b, ll k, ll l, ll r) {
  if (a <= l && r <= b)
    return dat[k];
  else if (r <= a || b <= l)
    return 0;
  else {
    ll val = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll var = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return (val + var);
  }
}
ll N, Q;
int main() {
  cin >> N >> Q;
  init(N + 2);
  vector<ll> c(N);
  for (int i = 0; i < N; i++)
    cin >> c.at(i);
  vector<pair<P, ll>> A(Q);
  for (int i = 0; i < Q; i++) {
    ll l, r;
    cin >> l >> r;
    P p(r, l);
    pair<P, ll> p1(p, i);
    A[i] = p1;
  }
  sort(A.begin(), A.end());
  vector<ll> ans(Q);
  map<ll, ll> M;
  ll I = 0;
  for (int i = 0; i < N; i++) {
    if (!M.count(c[i])) {
      M[c[i]] = i;
      update(i, 1);
    } else {
      update(M.at(c[i]), 0);
      M.at(c[i]) = i;
      update(i, 1);
    }
    while (A[I].first.first == i + 1) {
      ll l = A[I].first.second;
      ll r = A[I].first.first;
      ll j = A[I].second;
      ll a = query(l - 1, r, 0, 0, n);
      ans[j] = a;
      I++;
    }
  }
  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll n;
ll dat[2000005];
void init(ll n_) {
  n = 1;
  while (n <= n_)
    n *= 2;
}
void update(ll k, ll a) {
  k += n - 1;
  dat[k] = a;
  while (0 < k) {
    k = (k - 1) / 2;
    dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
  }
}
ll query(ll a, ll b, ll k, ll l, ll r) {
  if (a <= l && r <= b)
    return dat[k];
  else if (r <= a || b <= l)
    return 0;
  else {
    ll val = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll var = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return (val + var);
  }
}
ll N, Q;
int main() {
  cin >> N >> Q;
  init(N + 2);
  vector<ll> c(N);
  for (int i = 0; i < N; i++)
    cin >> c.at(i);
  vector<pair<P, ll>> A(Q);
  for (int i = 0; i < Q; i++) {
    ll l, r;
    cin >> l >> r;
    P p(r, l);
    pair<P, ll> p1(p, i);
    A[i] = p1;
  }
  sort(A.begin(), A.end());
  vector<ll> ans(Q);
  map<ll, ll> M;
  ll I = 0;
  for (int i = 0; i < N; i++) {
    if (!M.count(c[i])) {
      M[c[i]] = i;
      update(i, 1);
    } else {
      update(M.at(c[i]), 0);
      M.at(c[i]) = i;
      update(i, 1);
    }
    while (A[I].first.first == i + 1) {
      ll l = A[I].first.second;
      ll r = A[I].first.first;
      ll j = A[I].second;
      ll a = query(l - 1, r, 0, 0, n);
      ans[j] = a;
      I++;
    }
  }
  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++)

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

using namespace std;

struct point {
  int left, right, block, id;
};

bool cmp(const point &lhs, const point &rhs) {
  if (lhs.block < rhs.block)
    return true;
  else if (lhs.block > rhs.block)
    return false;
  else
    return lhs.right < rhs.right;
}

int main() {
  int n, q;
  scanf("%d", &n);
  cin >> q;
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  int block_size = 450;

  vector<point> query(q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i].left, &query[i].right);
    query[i].left--, query[i].right--;
    query[i].block = query[i].left / block_size;
    query[i].id = i;
  }

  sort(query.begin(), query.end(), cmp);

  vector<int> solution(q);
  vector<int> hash(1000100);

  int left = 0, right = 0, answer = 1;
  hash[A[0]] = 1;

  for (const point &task : query) {
    while (left < task.left) {
      hash[A[left]]--;
      answer -= (hash[A[left]] == 0);
      left++;
    }
    while (left > task.left) {
      left--;
      hash[A[left]]++;
      answer += (hash[A[left]] == 1);
    }
    while (right < task.right) {
      right++;
      hash[A[right]]++;
      answer += (hash[A[right]] == 1);
    }
    while (right > task.right) {
      hash[A[right]]--;
      answer -= (hash[A[right]] == 0);
      right--;
    }
    solution[task.id] = answer;
  }

  for (int answer : solution)
    printf("%d\n", answer);

  return 0;
}

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

using namespace std;

struct point {
  int left, right, block, id;
};

bool cmp(const point &lhs, const point &rhs) {
  if (lhs.block < rhs.block)
    return true;
  else if (lhs.block > rhs.block)
    return false;
  else
    return lhs.right < rhs.right;
}

int main() {
  int n, q;
  scanf("%d", &n);
  cin >> q;
  vector<int> A(n);
  for (int i = 0; i < n; i++)
    scanf("%d", &A[i]);

  int block_size = 707;

  vector<point> query(q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i].left, &query[i].right);
    query[i].left--, query[i].right--;
    query[i].block = query[i].left / block_size;
    query[i].id = i;
  }

  sort(query.begin(), query.end(), cmp);

  vector<int> solution(q);
  vector<int> hash(1000100);

  int left = 0, right = 0, answer = 1;
  hash[A[0]] = 1;

  for (const point &task : query) {
    while (left < task.left) {
      hash[A[left]]--;
      answer -= (hash[A[left]] == 0);
      left++;
    }
    while (left > task.left) {
      left--;
      hash[A[left]]++;
      answer += (hash[A[left]] == 1);
    }
    while (right < task.right) {
      right++;
      hash[A[right]]++;
      answer += (hash[A[right]] == 1);
    }
    while (right > task.right) {
      hash[A[right]]--;
      answer -= (hash[A[right]] == 0);
      right--;
    }
    solution[task.id] = answer;
  }

  for (int answer : solution)
    printf("%d\n", answer);

  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

int main() {
  class BIT {
    ll n;
    vector<ll> bitree;

  public:
    BIT(unsigned long _n) : bitree(_n + 1, 0) { n = _n; }
    void add(ll id, ll x) {
      while (id <= n) {
        bitree[id] += x;
        id += id & -id;
      }
      return;
    }
    ll sum(ll id) {
      ll temp = 0;
      while (id > 0) {
        temp += bitree[id];
        id -= id & -id;
      }
      return temp;
    }
  };
  cin >> N >> Q;
  vec c(N), l(Q), r(Q), ord(Q + 1), ans(Q);
  rep(i, N) cin >> c[i];
  rep(i, Q) {
    cin >> l[i] >> r[i];
    --l[i];
    ord[i] = i;
  }
  ord[Q] = Q;
  l.push_back(-1);
  r.push_back(-1);
  sort(ALL(ord), [&](int x, int y) { return l[x] > l[y]; });
  vector<set<ll>> col(N);
  BIT bit(N + 10);
  ll i = 0;
  Rrep(L, N) {
    if (!col[c[L]].empty()) {
      bit.add(*col[c[L]].begin(), 1);
    }
    col[c[L]].insert(L);
    while (l[ord[i]] == L) {
      ll id = ord[i];
      ans[id] = r[id] - l[id] - bit.sum(r[id] - 1);
      ++i;
    }
  }
  rep(j, Q) { cout << ans[j] << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
// const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL << 58);

int main() {
  class BIT {
    ll n;
    vector<ll> bitree;

  public:
    BIT(unsigned long _n) : bitree(_n + 1, 0) { n = _n; }
    void add(ll id, ll x) {
      while (id <= n) {
        bitree[id] += x;
        id += id & -id;
      }
      return;
    }
    ll sum(ll id) {
      ll temp = 0;
      while (id > 0) {
        temp += bitree[id];
        id -= id & -id;
      }
      return temp;
    }
  };
  cin >> N >> Q;
  vec c(N), l(Q), r(Q), ord(Q + 1), ans(Q);
  rep(i, N) {
    cin >> c[i];
    --c[i];
  }
  rep(i, Q) {
    cin >> l[i] >> r[i];
    --l[i];
    ord[i] = i;
  }
  ord[Q] = Q;
  l.push_back(-1);
  r.push_back(-1);
  sort(ALL(ord), [&](int x, int y) { return l[x] > l[y]; });
  vector<set<ll>> col(N);
  BIT bit(N + 10);
  ll i = 0;
  Rrep(L, N) {
    if (!col[c[L]].empty()) {
      bit.add(*col[c[L]].begin(), 1);
    }
    col[c[L]].insert(L);
    while (l[ord[i]] == L) {
      ll id = ord[i];
      ans[id] = r[id] - l[id] - bit.sum(r[id] - 1);
      ++i;
    }
  }
  rep(j, Q) { cout << ans[j] << endl; }
}
Problem p02599 (C++)

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

using namespace std;

int A[500000], K[500001], k = 1, CL = 0, CR = -1, n, B;

void add(int I) {
  K[A[I]]++;
  if (K[A[I]] == 1) {
    k++;
  }
}

void Remove(int I) {
  K[A[I]]--;
  if (K[A[I]] == 0) {
    k--;
  }
}

int query(int L, int R) {
  while (CL < L) {
    Remove(CL);
    CL++;
  }
  while (CL > L) {
    CL--;
    add(CL);
  }
  while (CR < R) {
    CR++;
    add(CR);
  }
  while (CR > R) {
    Remove(CR);
    CR--;
  }
  return k;
}

bool Bsort(const pair<pair<int, int>, int> &a,
           const pair<pair<int, int>, int> &b) {
  if (a.first.first / B == b.first.first / B) {
    return a.first.second < b.first.second;
  } else {
    return a.first.first / B < b.first.first / B;
  }
}

int main() {
  int m, l, r, L, R, p, I;
  cin >> n >> m;
  B = sqrt(n);
  map<int, int> M;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (M[A[i]] == 0) {
      M[A[i]] = k;
      k++;
      K[A[i]] = 0;
    }
    A[i] = M[A[i]];
  }
  k = 0;
  int U[m];
  vector<pair<pair<int, int>, int>> V;
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    l--;
    r--;
    V.push_back({{l, r}, i});
  }
  sort(V.begin(), V.end(), Bsort);
  for (int i = 0; i < m; i++) {
    L = V[i].first.first;
    R = V[i].first.second;
    I = V[i].second;
    U[I] = query(L, R);
  }
  for (int i = 0; i < m; i++) {
    cout << U[i] << "\n";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;

int A[500000], K[500001], k = 1, CL = 0, CR = -1, n, B;

void add(int I) {
  K[A[I]]++;
  if (K[A[I]] == 1) {
    k++;
  }
}

void Remove(int I) {
  K[A[I]]--;
  if (K[A[I]] == 0) {
    k--;
  }
}

int query(int L, int R) {
  while (CL < L) {
    Remove(CL);
    CL++;
  }
  while (CL > L) {
    CL--;
    add(CL);
  }
  while (CR < R) {
    CR++;
    add(CR);
  }
  while (CR > R) {
    Remove(CR);
    CR--;
  }
  return k;
}

bool Bsort(const pair<pair<int, int>, int> &a,
           const pair<pair<int, int>, int> &b) {
  if (a.first.first / B == b.first.first / B) {
    return a.first.second < b.first.second;
  } else {
    return a.first.first / B < b.first.first / B;
  }
}

int main() {
  int m, l, r, L, R, p, I;
  cin >> n >> m;
  B = sqrt(n);
  map<int, int> M;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (M[A[i]] == 0) {
      M[A[i]] = k;
      k++;
      K[A[i]] = 0;
    }
    A[i] = M[A[i]];
  }
  k = 0;
  int U[m];
  vector<pair<pair<int, int>, int>> V;
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    l--;
    r--;
    V.push_back({{l, r}, i});
  }
  sort(V.begin(), V.end(), Bsort);
  for (int i = 0; i < m; i++) {
    L = V[i].first.first;
    R = V[i].first.second;
    I = V[i].second;
    U[I] = query(L, R);
  }
  for (int i = 0; i < m; i++) {
    cout << U[i] << "\n";
  }
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1LL << 60;
const int INF = 1LL << 17;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll bit[500500];
ll n;
#define N 500010

ll sum(ll i) {
  ll res = 0;
  while (0 < i) {
    res += bit[i];
    i -= i & -i;
  }
  return res;
}
void add(ll i, ll x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

ll rsum(ll l, ll r) { return sum(r) - sum(l - 1); }
vector<vector<ll>> qr(N), qri(N);

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

  vector<ll> c(n + 1);
  rep(i, n) cin >> c[i + 1];
  vector<ll> a(N);
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    qr[b].push_back(a);
    qri[b].push_back(i);
  }
  vector<ll> res(n + 1);
  FOR(i, 1, n + 1) {
    if (a[c[i]] > 0)
      add(a[c[i]], -1);
    add(i, 1);
    a[c[i]] = i;
    rep(j, qr[i].size()) { res[qri[i][j]] = rsum(qr[i][j], i); }
  }
  rep(i, q) cout << res[i] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1LL << 60;
const int INF = 1LL << 17;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll bit[500500];
ll n;
#define N 500010

ll sum(ll i) {
  ll res = 0;
  while (0 < i) {
    res += bit[i];
    i -= i & -i;
  }
  return res;
}
void add(ll i, ll x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

ll rsum(ll l, ll r) { return sum(r) - sum(l - 1); }
vector<vector<ll>> qr(N), qri(N);

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

  vector<ll> c(n + 1);
  rep(i, n) cin >> c[i + 1];
  vector<ll> a(N);
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    qr[b].push_back(a);
    qri[b].push_back(i);
  }
  vector<ll> res(q + 1);
  FOR(i, 1, n + 1) {
    if (a[c[i]] > 0)
      add(a[c[i]], -1);
    add(i, 1);
    a[c[i]] = i;
    rep(j, qr[i].size()) { res[qri[i][j]] = rsum(qr[i][j], i); }
  }
  rep(i, q) cout << res[i] << endl;
}
Problem p02599 (C++)

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

int val;
bool comp(vector<int> &v1, vector<int> &v2) {
  int l1 = v1[0] / val;
  int l2 = v2[0] / val;
  if (l1 != l2)
    return (l1 < l2);
  return v1[1] < v2[1];
}

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

  vector<vector<int>> query(q, vector<int>(3));
  vector<int> sol(q);

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i][0], &query[i][1]);
    query[i][2] = i;
  }
  val = sqrt(n);
  sort(query.begin(), query.end(), comp);

  int l = 1, r = 0, diff = 0;
  vector<int> fr(1000001, 0);
  for (const auto &x : query) {
    while (r > x[1]) {
      fr[arr[r]]--;

      if (fr[arr[r]] == 0)
        diff--;

      --r;
    }
    while (l < x[0]) {
      fr[arr[l]]--;

      if (fr[arr[l]] == 0)
        diff--;

      l++;
    }
    while (r < x[1]) {
      ++r;
      fr[arr[r]]++;

      if (fr[arr[r]] == 1)
        diff++;
    }
    while (l > x[0]) {
      l--;

      fr[arr[l]]++;

      if (fr[arr[l]] == 1)
        diff++;
    }
    sol[x[2]] = diff;
  }
  for (int x : sol)
    printf("%d\n", x);
}

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

int val;
bool comp(vector<int> &v1, vector<int> &v2) {
  int l1 = v1[0] / val;
  int l2 = v2[0] / val;
  if (l1 != l2)
    return (l1 < l2);
  return v1[1] < v2[1];
}

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

  vector<vector<int>> query(q, vector<int>(3));
  vector<int> sol(q);

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i][0], &query[i][1]);
    query[i][2] = i;
  }
  val = sqrt(n);
  sort(query.begin(), query.end(), comp);

  int l = 1, r = 0, diff = 0;
  vector<int> fr(1000001, 0);
  for (const auto &x : query) {
    while (r > x[1]) {
      fr[arr[r]]--;

      if (fr[arr[r]] == 0)
        diff--;

      --r;
    }
    while (l < x[0]) {
      fr[arr[l]]--;

      if (fr[arr[l]] == 0)
        diff--;

      l++;
    }
    while (r < x[1]) {
      ++r;
      fr[arr[r]]++;

      if (fr[arr[r]] == 1)
        diff++;
    }
    while (l > x[0]) {
      l--;

      fr[arr[l]]++;

      if (fr[arr[l]] == 1)
        diff++;
    }
    sol[x[2]] = diff;
  }
  for (int x : sol)
    printf("%d\n", x);
}
Problem p02599 (C++)

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

#define mp make_pair
#define eb emplace_back
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 100;
// #define int long long
const int INF = 1e9;

int f[MAXN], last[MAXN];

void add(int i, int x) {
  for (; i < MAXN; i = i | (i + 1))
    f[i] += x;
}

//[l, r)
int sum(int l, int r) {
  int res = 0;
  for (int i = r - 1; i > -1; i = (i & (i + 1)) - 1)
    res += f[i];
  for (int i = l - 1; i > -1; i = (i & (i + 1)) - 1)
    res -= f[i];
  return res;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, q;
  cin >> n >> q;
  vector<int> c(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> c[i];

  int l, r;
  vector<vector<pair<int, int>>> a(n + 1);
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    a[r].eb(l, i);
  }

  vector<int> ans(q);

  for (int i = 1; i <= n; i++) {
    if (last[c[i]])
      add(last[c[i]], -1);
    last[c[i]] = i;
    add(last[c[i]], 1);
    for (auto j : a[i])
      ans[j.ss] = sum(j.ff, i + 1);
  }

  for (auto i : ans)
    cout << i << '\n';
  return 0;
}


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

#define mp make_pair
#define eb emplace_back
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 5e5 + 5;
// #define int long long
const int INF = 1e9;

int f[MAXN], last[MAXN];

void add(int i, int x) {
  for (; i < MAXN; i = i | (i + 1))
    f[i] += x;
}

//[l, r)
int sum(int l, int r) {
  int res = 0;
  for (int i = r - 1; i > -1; i = (i & (i + 1)) - 1)
    res += f[i];
  for (int i = l - 1; i > -1; i = (i & (i + 1)) - 1)
    res -= f[i];
  return res;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int n, q;
  cin >> n >> q;
  vector<int> c(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> c[i];

  int l, r;
  vector<vector<pair<int, int>>> a(n + 1);
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    a[r].eb(l, i);
  }

  vector<int> ans(q);

  for (int i = 1; i <= n; i++) {
    if (last[c[i]])
      add(last[c[i]], -1);
    last[c[i]] = i;
    add(last[c[i]], 1);
    for (auto j : a[i])
      ans[j.ss] = sum(j.ff, i + 1);
  }

  for (auto i : ans)
    cout << i << '\n';
  return 0;
}