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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
int read() {
  int k = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    k = k * 10 + c - '0';
  return k * f;
}
const int N = 200055;
int n, m, tot, a[N], rt[N], lch[N * 30], rch[N * 30], sum[N * 30];
void add(int &k, int pre, int l, int r, int a, int b) {
  k = ++tot;
  sum[k] = sum[pre] + b;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (a <= mid)
    rch[k] = rch[pre], add(lch[k], lch[pre], l, mid, a, b);
  else
    lch[k] = lch[pre], add(rch[k], rch[pre], mid + 1, r, a, b);
}
int query(int k, int l, int r, int a, int b) {
  if (!k)
    return 0;
  if (a <= l && b >= r)
    return sum[k];
  int mid = l + r >> 1, res = 0;
  if (a <= mid)
    res = query(lch[k], l, mid, a, b);
  if (b > mid)
    res += query(rch[k], mid + 1, r, a, b);
  return res;
}
map<int, int> mp;
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (mp[a[i]])
      add(rt[i], rt[i], 1, n, mp[a[i]], -1);
    mp[a[i]] = i;
    add(rt[i], rt[i], 1, n, mp[a[i]], 1);
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    l = read();
    r = read();
    printf("%d\n", query(rt[r], 1, n, l, r));
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#define ll long long
using namespace std;
int read() {
  int k = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    k = k * 10 + c - '0';
  return k * f;
}
const int N = 1000055;
int n, m, tot, a[N], rt[N], lch[N * 30], rch[N * 30], sum[N * 30];
void add(int &k, int pre, int l, int r, int a, int b) {
  k = ++tot;
  sum[k] = sum[pre] + b;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (a <= mid)
    rch[k] = rch[pre], add(lch[k], lch[pre], l, mid, a, b);
  else
    lch[k] = lch[pre], add(rch[k], rch[pre], mid + 1, r, a, b);
}
int query(int k, int l, int r, int a, int b) {
  if (!k)
    return 0;
  if (a <= l && b >= r)
    return sum[k];
  int mid = l + r >> 1, res = 0;
  if (a <= mid)
    res = query(lch[k], l, mid, a, b);
  if (b > mid)
    res += query(rch[k], mid + 1, r, a, b);
  return res;
}
map<int, int> mp;
int main() {
  n = read();
  m = read();
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (mp[a[i]])
      add(rt[i], rt[i], 1, n, mp[a[i]], -1);
    mp[a[i]] = i;
    add(rt[i], rt[i], 1, n, mp[a[i]], 1);
  }
  for (int i = 1; i <= m; i++) {
    int l, r;
    l = read();
    r = read();
    printf("%d\n", query(rt[r], 1, n, l, r));
  }
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

class query {
public:
  int l, r, in;
};
long long int pole[2000079];
void ii(long long int kde, long long int r, int n) {
  while (kde <= n) {
    pole[kde] += r;
    kde += kde & -kde;
  }
}
long long int ss(long long int kde) {
  long long int sum = 0;
  while (kde > 0) {
    sum += pole[kde];
    kde = kde & (kde - 1);
  }
  return sum;
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  vector<int> ans(n, 0);
  vector<vector<query>> v(n);
  for (int i = 0; i < q; i++) {
    query a;
    cin >> a.l >> a.r;
    a.l--;
    a.r--;
    a.in = i;
    v[a.r].push_back(a);
  }
  vector<int> last(n + 1, -1);
  int in = 0;
  for (int r = 0; r < n; r++) {
    ii(r + 1, 1, n + 1);
    if (last[c[r]] != -1)
      ii(last[c[r]] + 1, -1, n + 1);
    last[c[r]] = r;
    for (query i : v[r])
      ans[i.in] = ss(r + 1) - ss(i.l);
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

class query {
public:
  int l, r, in;
};
long long int pole[2000079];
void ii(long long int kde, long long int r, int n) {
  while (kde <= n) {
    pole[kde] += r;
    kde += kde & -kde;
  }
}
long long int ss(long long int kde) {
  long long int sum = 0;
  while (kde > 0) {
    sum += pole[kde];
    kde = kde & (kde - 1);
  }
  return sum;
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  vector<int> ans(q, 0);
  vector<vector<query>> v(n);
  for (int i = 0; i < q; i++) {
    query a;
    cin >> a.l >> a.r;
    a.l--;
    a.r--;
    a.in = i;
    v[a.r].push_back(a);
  }
  vector<int> last(n + 1, -1);
  int in = 0;
  for (int r = 0; r < n; r++) {
    ii(r + 1, 1, n + 1);
    if (last[c[r]] != -1)
      ii(last[c[r]] + 1, -1, n + 1);
    last[c[r]] = r;
    for (query i : v[r])
      ans[i.in] = ss(r + 1) - ss(i.l);
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\n";
  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));

  int t = 0;
  scanf("%d", &q);
  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;
  }

  int ab;
  cin >> ab;
  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 = 500010;
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));

  int t = 0;
  scanf("%d", &q);
  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;
  }

  int ab;
  cin >> ab;
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>
using namespace std;
const int Max = 1e5 + 5;
int K, ans[Max], arra[Max], sum, cnt, mp[Max];
struct Query {
  int index, l, r;
};

bool cmp(Query a, Query b) {
  if (a.l / K != b.l / K) {
    return a.l / K < b.l / K;
  } else if ((a.l / K) & 1)
    return a.r < b.r;
  return a.r > b.r;
}
Query query[Max];
void add(int index) {
  if (!mp[index]) {
    cnt++;
  }
  mp[index]++;
}
void del(int index) {
  mp[index]--;
  if (!mp[index]) {
    cnt--;
  }
}

void MO(int n, int q) {
  K = sqrt(n);
  sort(query, query + q, cmp);
  int st = 0, en = -1;
  cnt = 0;
  for (int i = 0; i < q; i++) {
    int l = query[i].l - 1;
    int r = query[i].r - 1;
    int idx = query[i].index;

    while (st > l) {
      st--;
      add(arra[st]);
    }
    while (st < l) {
      del(arra[st]);
      st++;
    }
    while (en < r) {
      en++;
      add(arra[en]);
    }
    while (en > r) {
      del(arra[en]);
      en--;
    }
    ans[idx] = cnt;
  }
  for (int i = 0; i < q; i++)
    printf("%d\n", ans[i]);
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> arra[i];
  }

  for (int i = 0; i < q; i++) {
    cin >> query[i].l >> query[i].r;
    query[i].index = i;
  }
  MO(n, q);
}


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int Max = 5e5 + 5;
int K, ans[Max], arra[Max], sum, cnt, mp[Max];
struct Query {
  int index, l, r;
};

bool cmp(Query a, Query b) {
  if (a.l / K != b.l / K) {
    return a.l / K < b.l / K;
  } else if ((a.l / K) & 1)
    return a.r < b.r;
  return a.r > b.r;
}
Query query[Max];
void add(int index) {
  if (!mp[index]) {
    cnt++;
  }
  mp[index]++;
}
void del(int index) {
  mp[index]--;
  if (!mp[index]) {
    cnt--;
  }
}

void MO(int n, int q) {
  K = sqrt(n);
  sort(query, query + q, cmp);
  int st = 0, en = -1;
  cnt = 0;
  for (int i = 0; i < q; i++) {
    int l = query[i].l - 1;
    int r = query[i].r - 1;
    int idx = query[i].index;

    while (st > l) {
      st--;
      add(arra[st]);
    }
    while (st < l) {
      del(arra[st]);
      st++;
    }
    while (en < r) {
      en++;
      add(arra[en]);
    }
    while (en > r) {
      del(arra[en]);
      en--;
    }
    ans[idx] = cnt;
  }
  for (int i = 0; i < q; i++)
    printf("%d\n", ans[i]);
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++) {
    cin >> arra[i];
  }

  for (int i = 0; i < q; i++) {
    cin >> query[i].l >> query[i].r;
    query[i].index = i;
  }
  MO(n, q);
}
Problem p02599 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<vector<int>> seg;

int query(int l, int r, int u, int a, int b) {
  if (l <= a && b <= r)
    return lower_bound(seg[u].begin(), seg[u].end(), l) - seg[u].begin();

  int res = 0;
  if (l < (a + b) / 2)
    res += query(l, r, 2 * u + 1, a, (a + b) / 2);
  if ((a + b) / 2 < r)
    res += query(l, r, 2 * u + 2, (a + b) / 2, b);
  return res;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  vector<int> a(n);
  rep(i, n) scanf("%d", &a[i]), a[i]--;

  vector<int> pre(1e5, -1);
  rep(i, n) {
    int x = a[i];
    a[i] = pre[x];
    pre[x] = i;
  }

  int N;
  for (N = 1; N < n; N <<= 1)
    ;
  seg.resize(2 * N - 1);
  rep(i, n) seg[N - 1 + i].emplace_back(a[i]);
  for (int i = N - 2; i >= 0; i--) {
    merge(seg[2 * i + 1].begin(), seg[2 * i + 1].end(), seg[2 * i + 2].begin(),
          seg[2 * i + 2].end(), back_inserter(seg[i]));
  }

  rep(_, q) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    printf("%d\n", query(l, r, 0, 0, N));
  }

  return 0;
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<vector<int>> seg;

int query(int l, int r, int u, int a, int b) {
  if (l <= a && b <= r)
    return lower_bound(seg[u].begin(), seg[u].end(), l) - seg[u].begin();

  int res = 0;
  if (l < (a + b) / 2)
    res += query(l, r, 2 * u + 1, a, (a + b) / 2);
  if ((a + b) / 2 < r)
    res += query(l, r, 2 * u + 2, (a + b) / 2, b);
  return res;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  vector<int> a(n);
  rep(i, n) scanf("%d", &a[i]), a[i]--;

  vector<int> pre(n, -1);
  rep(i, n) {
    int x = a[i];
    a[i] = pre[x];
    pre[x] = i;
  }

  int N;
  for (N = 1; N < n; N <<= 1)
    ;
  seg.resize(2 * N - 1);
  rep(i, n) seg[N - 1 + i].emplace_back(a[i]);
  for (int i = N - 2; i >= 0; i--) {
    merge(seg[2 * i + 1].begin(), seg[2 * i + 1].end(), seg[2 * i + 2].begin(),
          seg[2 * i + 2].end(), back_inserter(seg[i]));
  }

  rep(_, q) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    printf("%d\n", query(l, r, 0, 0, N));
  }

  return 0;
}
Problem p02599 (C++)

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

template <typename T> class BIT {
  vector<T> array;
  int n;

public:
  BIT(int num) {
    n = num;
    array.resize(n + 1);
  }
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += array[i];
      i -= i & -i;
    }
    return s;
  }
  T sum(int i, int j) { return sum(j) - sum(i - 1); }
  void add(int i, T x) {
    while (i <= n) {
      array[i] += x;
      i += i & -i;
    }
  }
};

int c[500010];
TU que[500010];

int main() {
  int n, q;
  cin >> n >> q;
  BIT<int> bit(n);
  for (int i = 1; i <= n; ++i)
    cin >> c[i];
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    que[i] = TU(r, l, i);
  }
  sort(que, que + q);
  rep(i, q) swap(get<0>(que[i]), get<1>(que[i]));
  vector<int> x(n + 1, -1), ans(q);
  int now = 0;
  rep(i, q) {
    for (int j = now + 1; j <= get<1>(que[i]); ++j) {
      if (x[c[j]] != -1) {
        bit.add(x[c[j]], -1);
      }
      x[c[j]] = j;
      bit.add(x[c[j]], 1);
    }
    ans[get<2>(que[i])] = bit.sum(get<0>(que[i]), get<1>(que[i]));
  }
  rep(i, q) cout << ans[i] << endl;
}

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

template <typename T> class BIT {
  vector<T> array;
  int n;

public:
  BIT(int num) {
    n = num;
    array.resize(n + 1);
  }
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += array[i];
      i -= i & -i;
    }
    return s;
  }
  T sum(int i, int j) { return sum(j) - sum(i - 1); }
  void add(int i, T x) {
    while (i <= n) {
      array[i] += x;
      i += i & -i;
    }
  }
};

int c[500010];
TU que[500010];

int main() {
  int n, q;
  cin >> n >> q;
  BIT<int> bit(n);
  for (int i = 1; i <= n; ++i)
    cin >> c[i];
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    que[i] = TU(r, l, i);
  }
  sort(que, que + q);
  rep(i, q) swap(get<0>(que[i]), get<1>(que[i]));
  vector<int> x(n + 1, -1), ans(q);
  int now = 0;
  rep(i, q) {
    for (int j = now + 1; j <= get<1>(que[i]); ++j) {
      if (x[c[j]] != -1) {
        bit.add(x[c[j]], -1);
      }
      x[c[j]] = j;
      bit.add(x[c[j]], 1);
    }
    ans[get<2>(que[i])] = bit.sum(get<0>(que[i]), get<1>(que[i]));
    now = get<1>(que[i]);
  }
  rep(i, q) cout << ans[i] << endl;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define Fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
int block;
int m[2000000];
int a[2000000];
struct query {
  int l, r, idx;
};
int value = 0;
bool comper(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) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}

int main(void) {
  Fast;
  int n, q;
  cin >> n >> q;
  block = sqrt(n);

  for (int i = 1; i < n + 1; i++)
    cin >> a[i];

  query qr[q];

  for (int i = 0; i < q; i++)
    cin >> qr[i].l >> qr[i].r, qr[i].idx = i;

  sort(qr, qr + q, comper);

  set<int> s;
  int L = 1, R = 1, ml = 0, mr = 0;
  int ans[q];

  for (int i = 0; i < q; i++) {
    int L = qr[i].l;
    int R = qr[i].r;
    while (mr < R)
      mr++, add(mr);

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

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

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

    ans[qr[i].idx] = value;
  }

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

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
#define Fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
int block;
int m[2000000];
int a[2000000];
struct query {
  int l, r, idx;
};
int value = 0;
bool comper(query a, query b) {
  if (a.l / block != b.l / block)
    return a.l < b.l;
  return a.l / block % 2 ? a.r < b.r : a.r > b.r;
}

void add(int pos) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}

int main(void) {
  Fast;
  int n, q;
  cin >> n >> q;
  block = sqrt(n);

  for (int i = 1; i < n + 1; i++)
    cin >> a[i];

  query qr[q];

  for (int i = 0; i < q; i++)
    cin >> qr[i].l >> qr[i].r, qr[i].idx = i;

  sort(qr, qr + q, comper);

  set<int> s;
  int L = 1, R = 1, ml = 0, mr = 0;
  int ans[q];

  for (int i = 0; i < q; i++) {
    int L = qr[i].l;
    int R = qr[i].r;
    while (mr < R)
      mr++, add(mr);

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

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

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

    ans[qr[i].idx] = value;
  }

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

Buggy submission (status: Time Limit Exceeded):
# from sys import stdin
# input = stdin.readline


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


N, Q = map(int, input().split())
c = list(map(int, input().split()))

lr = []
for i in range(Q):
    l, r = map(int, input().split())
    lr.append([l, r, i])

lr.sort(key=lambda x: x[1])
# print("lr", lr)
rightest = [-1] * (N + 1)
current_q = 0
bit = Bit(N)
ans = [0] * Q

for i in range(N):
    if rightest[c[i]] != -1:
        bit.add(rightest[c[i]] + 1, -1)
    rightest[c[i]] = i
    bit.add(i + 1, 1)
    # print(bit.tree)

    while current_q < Q and lr[current_q][1] == i + 1:
        ans[lr[current_q][2]] = bit.sum(lr[current_q][1]) - bit.sum(
            lr[current_q][0] - 1
        )
        current_q += 1

for i in range(Q):
    print(ans[i])


Fixed submission:
from sys import stdin

input = stdin.readline


class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)

    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s

    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i


N, Q = map(int, input().split())
c = list(map(int, input().split()))

lr = []
for i in range(Q):
    l, r = map(int, input().split())
    lr.append([l, r, i])

lr.sort(key=lambda x: x[1])
# print("lr", lr)
rightest = [-1] * (N + 1)
current_q = 0
bit = Bit(N)
ans = [0] * Q

for i in range(N):
    if rightest[c[i]] != -1:
        bit.add(rightest[c[i]] + 1, -1)
    rightest[c[i]] = i
    bit.add(i + 1, 1)
    # print(bit.tree)

    while current_q < Q and lr[current_q][1] == i + 1:
        ans[lr[current_q][2]] = bit.sum(lr[current_q][1]) - bit.sum(
            lr[current_q][0] - 1
        )
        current_q += 1

for i in range(Q):
    print(ans[i])
Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

#define N 300001

typedef struct Query {
  int l, r, idx;
} query;
int block;

bool cmp(query a, query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}

int ans[N], c[N], cnt[N];
query q[N];
int main() {
  fastIO;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> c[i];
  block = (int)sqrt(n);
  for (int i = 0; i < m; i++)
    cin >> q[i].l >> q[i].r, q[i].idx = i;
  sort(q, q + m, cmp);

  int l = 0, r = -1, temp = 0;
  for (int i = 0; i < m; i++) {
    int ql = q[i].l - 1, qr = q[i].r - 1;
    while (r < qr)
      temp += !cnt[c[++r]]++;
    while (r > qr)
      temp -= !--cnt[c[r--]];
    while (l < ql)
      temp -= !--cnt[c[l++]];
    while (l > ql)
      temp += !cnt[c[--l]]++;
    ans[q[i].idx] = temp;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

#define N 500001

typedef struct Query {
  int l, r, idx;
} query;
int block;

bool cmp(query a, query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}

int ans[N], c[N], cnt[N];
query q[N];
int main() {
  fastIO;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> c[i];
  block = (int)sqrt(n);
  for (int i = 0; i < m; i++)
    cin >> q[i].l >> q[i].r, q[i].idx = i;
  sort(q, q + m, cmp);

  int l = 0, r = -1, temp = 0;
  for (int i = 0; i < m; i++) {
    int ql = q[i].l - 1, qr = q[i].r - 1;
    while (r < qr)
      temp += !cnt[c[++r]]++;
    while (r > qr)
      temp -= !--cnt[c[r--]];
    while (l < ql)
      temp -= !--cnt[c[l++]];
    while (l > ql)
      temp += !cnt[c[--l]]++;
    ans[q[i].idx] = temp;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << endl;

  return 0;
}
Problem p02599 (C++)

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

// solution:
#define int int64_t
#define block(i) (i / B)
constexpr int N = 500001, A = 500'001, Q = 500'001;
int f[A], a[N], ans[Q], B = 707, answer;

struct query {
  int lo, hi, idx;
  query() {}
  query(int lo, int hi, int idx) : lo(lo), hi(hi), idx(idx) {}
  bool operator<(const query &q) {
    return block(lo) < block(q.lo) || (block(lo) == block(q.lo) and hi < q.hi);
  }
} q[Q];

void hike() {
  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].lo >> q[i].hi;
    --q[i].lo;
    --q[i].hi;
    q[i].idx = i;
  }
  sort(q, q + m);
  auto insert = [&](int i) {
    f[a[i]]++;
    if (f[a[i]] == 1)
      ++answer;
  };
  auto remove = [&](int i) {
    f[a[i]]--;
    if (f[a[i]] == 0)
      --answer;
  };
  int l = 0, r = -1;
  for (int i = 0; i < m; ++i) {
    int lo = q[i].lo, hi = q[i].hi;
    while (lo < l)
      insert(--l);
    while (hi > r)
      insert(++r);
    while (lo > l)
      remove(l++);
    while (hi < r)
      remove(r--);
    ans[q[i].idx] = answer;
  }
  for (int i = 0; i < m; ++i)
    cout << ans[i] << "\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1; // cin >> t;
  while (t--)
    hike();
  return 0;
} // farewell, until we meet again..


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

// solution:
#define block(i) (i / B)
constexpr int N = 500001, A = 500'001, Q = 500'001;
int f[A], a[N], ans[Q], B = 707, answer;

struct query {
  int lo, hi, idx;
  query() {}
  query(int lo, int hi, int idx) : lo(lo), hi(hi), idx(idx) {}
  bool operator<(const query &q) {
    return block(lo) < block(q.lo) || (block(lo) == block(q.lo) and hi < q.hi);
  }
} q[Q];

void hike() {
  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].lo >> q[i].hi;
    --q[i].lo;
    --q[i].hi;
    q[i].idx = i;
  }
  sort(q, q + m);
  auto insert = [&](int i) {
    f[a[i]]++;
    if (f[a[i]] == 1)
      ++answer;
  };
  auto remove = [&](int i) {
    f[a[i]]--;
    if (f[a[i]] == 0)
      --answer;
  };
  int l = 0, r = -1;
  for (int i = 0; i < m; ++i) {
    int lo = q[i].lo, hi = q[i].hi;
    while (lo < l)
      insert(--l);
    while (hi > r)
      insert(++r);
    while (lo > l)
      remove(l++);
    while (hi < r)
      remove(r--);
    ans[q[i].idx] = answer;
  }
  for (int i = 0; i < m; ++i)
    cout << ans[i] << "\n";
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t = 1; // cin >> t;
  while (t--)
    hike();
  return 0;
} // farewell, until we meet again..
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
#define ll long long
struct node {
  int l, r;
  int id;
} ask[1000005];
ll q[1000005];
ll Ans[1000005];
int block;
bool cmp(node a, node b) {
  if (a.l / block == b.l / block) {
    return a.r < b.r;
  }
  return a.l / block < b.l / block;
}
#define il inline
inline ll read() {
  char c = getchar();
  ll x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0', c = getchar();
  }
  return x * f;
}
inline void out(ll x) {
  if (x > 9)
    out(x / 10);
  putchar(x % 10 + '0');
}
ll ans = 0;
il void add(int pos) {
  if (cnt[q[pos]] == 0)
    ans++;
  cnt[q[pos]]++;
}
il void del(int pos) {
  cnt[q[pos]]--;
  if (cnt[q[pos]] == 0)
    ans--;
}
int main() {
  int n, t;
  int m;
  n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    q[i] = read();
  }
  block = 500;
  for (int i = 1; i <= m; ++i) {
    ask[i].l = read();
    ask[i].r = read();
    // cin>>ask[i].l>>ask[i].r;
    ask[i].id = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int l = 1;
  int r = 0;
  for (int i = 1; i <= m; ++i) {
    int L = ask[i].l;
    int R = ask[i].r;
    int id = ask[i].id;
    while (l < L)
      del(l++);
    while (l > L)
      add(--l);
    while (r < R)
      add(++r);
    while (r > R)
      del(r--);
    Ans[id] = ans;
  }
  for (int i = 1; i <= m; ++i) {
    out(Ans[i]);
    puts("");
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int cnt[1000005];
#define ll long long
struct node {
  int l, r;
  int id;
} ask[1000005];
ll q[1000005];
ll Ans[1000005];
int block;
bool cmp(node a, node b) {
  if (a.l / block == b.l / block) {
    return a.r < b.r;
  }
  return a.l / block < b.l / block;
}
#define il inline
inline ll read() {
  char c = getchar();
  ll x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0', c = getchar();
  }
  return x * f;
}
inline void out(ll x) {
  if (x > 9)
    out(x / 10);
  putchar(x % 10 + '0');
}
ll ans = 0;
il void add(int pos) {
  if (cnt[q[pos]] == 0)
    ans++;
  cnt[q[pos]]++;
}
il void del(int pos) {
  cnt[q[pos]]--;
  if (cnt[q[pos]] == 0)
    ans--;
}
int main() {
  int n, t;
  int m;
  n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    q[i] = read();
  }
  block = sqrt(n);
  for (int i = 1; i <= m; ++i) {
    ask[i].l = read();
    ask[i].r = read();
    // cin>>ask[i].l>>ask[i].r;
    ask[i].id = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int l = 1;
  int r = 0;
  for (int i = 1; i <= m; ++i) {
    int L = ask[i].l;
    int R = ask[i].r;
    int id = ask[i].id;
    while (l < L)
      del(l++);
    while (l > L)
      add(--l);
    while (r < R)
      add(++r);
    while (r > R)
      del(r--);
    Ans[id] = ans;
  }
  for (int i = 1; i <= m; ++i) {
    out(Ans[i]);
    puts("");
  }
  return 0;
}
Problem p02599 (C++)

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

using namespace std;

#define N 511111
#define A 1111111
#define block 200

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

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

bool _compare(node x, node y) {

  if (x.L / block != y.L / block)
    return x.L < y.L;
  return x.R < y.R;
}

void _add(int pos) {

  cnt[v[pos]]++;
  if (cnt[v[pos]] == 1)
    ++answer;
}
void _remove(int pos) {

  cnt[v[pos]]--;
  if (cnt[v[pos]] == 0)
    --answer;
}
int main() {
  int n, Q;
  scanf("%d%d", &n, &Q);
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
  }
  for (int i = 0; i < Q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    --l, --r;
    q[i].L = l;
    q[i].R = r;
    q[i].i = i;
  }
  sort(q, q + Q, _compare);
  int curr_L = 0, curr_R = 0;
  for (int i = 0; i < Q; i++) {
    int L = q[i].L, R = q[i].R;

    while (curr_L < L) {
      _remove(curr_L);
      curr_L++;
    }
    while (curr_L > L) {
      _add(curr_L - 1);
      curr_L--;
    }
    while (curr_R <= R) {
      _add(curr_R);
      curr_R++;
    }
    while (curr_R > R + 1) {
      _remove(curr_R - 1);
      curr_R--;
    }
    ans[q[i].i] = answer;
  }
  for (int i = 0; i < Q; i++) {
    printf("%d\n", ans[i]);
  }
  return 0;
}


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

using namespace std;

#define N 511111
#define A 1111111
#define block 800

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

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

bool _compare(node x, node y) {

  if (x.L / block != y.L / block)
    return x.L < y.L;
  return x.R < y.R;
}

void _add(int pos) {

  cnt[v[pos]]++;
  if (cnt[v[pos]] == 1)
    ++answer;
}
void _remove(int pos) {

  cnt[v[pos]]--;
  if (cnt[v[pos]] == 0)
    --answer;
}
int main() {
  int n, Q;
  scanf("%d%d", &n, &Q);
  for (int i = 0; i < n; i++) {
    scanf("%d", &v[i]);
  }
  for (int i = 0; i < Q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    --l, --r;
    q[i].L = l;
    q[i].R = r;
    q[i].i = i;
  }
  sort(q, q + Q, _compare);
  int curr_L = 0, curr_R = 0;
  for (int i = 0; i < Q; i++) {
    int L = q[i].L, R = q[i].R;

    while (curr_L < L) {
      _remove(curr_L);
      curr_L++;
    }
    while (curr_L > L) {
      _add(curr_L - 1);
      curr_L--;
    }
    while (curr_R <= R) {
      _add(curr_R);
      curr_R++;
    }
    while (curr_R > R + 1) {
      _remove(curr_R - 1);
      curr_R--;
    }
    ans[q[i].i] = answer;
  }
  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;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

const int maxn = 1e6, maxk = 1e6 + 1;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}

void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}

void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\n";
  }

#ifdef LOCAL
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

const int maxn = 5e6, maxk = 5e6 + 1;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}

void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}

void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\n";
  }

#ifdef LOCAL
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}
Problem p02599 (C++)

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

using namespace std;
const int MAXN = 1e5 + 5;
int n, q, x, y, arr[MAXN], ans[MAXN], t[MAXN], last[MAXN];

struct Q {
  int x, y, i;
};

void update(int i, int v) {
  for (; i <= n; i += i & -i)
    t[i] += v;
}

int query(int r) {
  int sum = 0;
  for (; r > 0; r -= r & -r)
    sum += t[r];
  return sum;
}

bool cmp(Q q1, Q q2) { return q1.y < q2.y; }

int main() {
  memset(last, -1, sizeof last);
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &arr[i]);
  Q qs[q];
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &x, &y);
    x--, y--;
    qs[i] = {x, y, i};
  }

  sort(qs, qs + q, cmp);
  int qn = 0;
  for (int i = 0; i < n; i++) {
    if (last[arr[i]] != -1)
      update(last[arr[i]] + 1, -1);
    last[arr[i]] = i;
    update(i + 1, 1);
    while (qn < q && qs[qn].y == i) {
      ans[qs[qn].i] = query(qs[qn].y + 1) - query(qs[qn].x);
      qn++;
    }
  }

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

  return 0;
}


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

using namespace std;
const int MAXN = 5 * 1e5 + 5;
int n, q, x, y, arr[MAXN], ans[MAXN], t[MAXN], last[MAXN];

struct Q {
  int x, y, i;
};

void update(int i, int v) {
  for (; i <= n; i += i & -i)
    t[i] += v;
}

int query(int r) {
  int sum = 0;
  for (; r > 0; r -= r & -r)
    sum += t[r];
  return sum;
}

bool cmp(Q q1, Q q2) { return q1.y < q2.y; }

int main() {
  memset(last, -1, sizeof last);
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &arr[i]);
  Q qs[q];
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &x, &y);
    x--, y--;
    qs[i] = {x, y, i};
  }

  sort(qs, qs + q, cmp);
  int qn = 0;
  for (int i = 0; i < n; i++) {
    if (last[arr[i]] != -1)
      update(last[arr[i]] + 1, -1);
    last[arr[i]] = i;
    update(i + 1, 1);
    while (qn < q && qs[qn].y == i) {
      ans[qs[qn].i] = query(qs[qn].y + 1) - query(qs[qn].x);
      qn++;
    }
  }

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

  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

typedef struct omaru {
  ll l, r, id;
} polka;

#define FENWICK_NMAX 500010
struct fenwick_tree {
  ll N;
  ll bit[FENWICK_NMAX];
  fenwick_tree(ll n) {
    N = n;
    for (ll x = 1; x <= N; x++) {
      bit[x] = 0;
    }
  }
  void add(ll a, ll w) {
    for (ll x = a; x <= N; x += x & -x) {
      bit[x] += w;
    }
  }
  ll sum(ll a) {
    ll ret = 0;
    for (ll x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
  ll range_sum(ll l, ll r) { // [l, r]
    return sum(r) - sum(l - 1);
  }
};

int main() {
  // input
  ll n, Q;
  cin >> n >> Q;
  vector<ll> c(n);
  rep(i, 0, n) cin >> c[i];
  vector<polka> q(Q);
  rep(i, 0, Q) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }

  // sort with right pos
  sort(q.begin(), q.end(), [](polka l, polka r) {
    if (l.r < r.r)
      return true;
    else
      return false;
  });

  // query
  fenwick_tree t(n + 1);
  vector<ll> a(n + 1, -1), res(Q);
  ll itr = 0;
  rep(i, 0, n) {
    if (a[c[i]] > -1)
      t.add(a[c[i]], -1);
    t.add(i + 1, 1);
    a[c[i]] = i + 1;
    while (q[itr].r == i + 1) {
      cerr << itr << endl;
      res[q[itr].id] = t.range_sum(q[itr].l, i + 1);
      itr++;
    }
  }

  // print ans
  rep(i, 0, Q) cout << res[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

typedef struct omaru {
  ll l, r, id;
} polka;

#define FENWICK_NMAX 500010
struct fenwick_tree {
  ll N;
  ll bit[FENWICK_NMAX];
  fenwick_tree(ll n) {
    N = n;
    for (ll x = 1; x <= N; x++) {
      bit[x] = 0;
    }
  }
  void add(ll a, ll w) {
    for (ll x = a; x <= N; x += x & -x) {
      bit[x] += w;
    }
  }
  ll sum(ll a) {
    ll ret = 0;
    for (ll x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
  ll range_sum(ll l, ll r) { // [l, r]
    return sum(r) - sum(l - 1);
  }
};

int main() {
  // input
  ll n, Q;
  cin >> n >> Q;
  vector<ll> c(n);
  rep(i, 0, n) cin >> c[i];
  vector<polka> q(Q);
  rep(i, 0, Q) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }

  // sort with right pos
  sort(q.begin(), q.end(), [](polka l, polka r) {
    if (l.r < r.r)
      return true;
    else
      return false;
  });

  // query
  fenwick_tree t(n + 1);
  vector<ll> a(n + 1, -1), res(Q);
  ll itr = 0;
  rep(i, 0, n) {
    if (a[c[i]] > -1)
      t.add(a[c[i]], -1);
    t.add(i + 1, 1);
    a[c[i]] = i + 1;
    while (q[itr].r == i + 1) {
      res[q[itr].id] = t.range_sum(q[itr].l, i + 1);
      itr++;
    }
  }

  // print ans
  rep(i, 0, Q) cout << res[i] << endl;
  return 0;
}
Problem p02599 (C++)

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

using namespace std;

int ans = 0;
const int X = 333;
vector<int> a;
vector<int> cnt;

void add(int i) {
  ans += (cnt[a[i]] == 0);
  cnt[a[i]]++;
}

void del(int i) {
  cnt[a[i]]--;
  ans -= (cnt[a[i]] == 0);
}

int que(int oldL, int oldR, int L, int R) {
  for (int i = oldL; i < L; i++)
    del(i);
  for (int i = L; i < oldL; i++)
    add(i);
  for (int i = oldR; i > R; i--)
    del(i);
  for (int i = R; i > oldR; i--)
    add(i);
  return ans;
}

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / X != b.first.first / X)
    return a.first.first / X < b.first.first / X;
  return a.first.second < b.first.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  a.resize(n), cnt.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<pair<pair<int, int>, int>> b(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i].first.first >> b[i].first.second;
    b[i].first.first--, b[i].first.second--;
    b[i].second = i;
  }
  sort(b.begin(), b.end(), cmp);
  vector<int> answer(q);
  int oldL = 1, oldR = 0;
  for (int i = 0; i < q; i++) {
    int id = b[i].second;
    int L = b[i].first.first;
    int R = b[i].first.second;
    answer[id] = que(oldL, oldR, L, R);
    oldL = L, oldR = R;
  }
  for (auto &now : answer)
    cout << now << '\n';
  return 0;
}

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

using namespace std;

int ans = 0;
const int X = 2000;
vector<int> a;
vector<int> cnt;

void add(int i) {
  ans += (cnt[a[i]] == 0);
  cnt[a[i]]++;
}

void del(int i) {
  cnt[a[i]]--;
  ans -= (cnt[a[i]] == 0);
}

int que(int oldL, int oldR, int L, int R) {
  for (int i = oldL; i < L; i++)
    del(i);
  for (int i = L; i < oldL; i++)
    add(i);
  for (int i = oldR; i > R; i--)
    del(i);
  for (int i = R; i > oldR; i--)
    add(i);
  return ans;
}

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / X != b.first.first / X)
    return a.first.first / X < b.first.first / X;
  return a.first.second < b.first.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  a.resize(n), cnt.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<pair<pair<int, int>, int>> b(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i].first.first >> b[i].first.second;
    b[i].first.first--, b[i].first.second--;
    b[i].second = i;
  }
  sort(b.begin(), b.end(), cmp);
  vector<int> answer(q);
  int oldL = 1, oldR = 0;
  for (int i = 0; i < q; i++) {
    int id = b[i].second;
    int L = b[i].first.first;
    int R = b[i].first.second;
    answer[id] = que(oldL, oldR, L, R);
    oldL = L, oldR = R;
  }
  for (auto &now : answer)
    cout << now << '\n';
  return 0;
}
Problem p02599 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int bl, t, n, m;
int res, a[N], cnt[N], ans[N];
int curR, curL;
struct node {
  int l, r, p;
  bool operator<(const node &W) const { return l < W.l; }
} e[N];
bool cmp(const node &a, const node &b) {
  return (a.l / bl) == (b.l / bl) ? a.r < b.r : a.l > b.l;
}
void remove(int i) {
  if ((--cnt[a[i]]) == 0)
    res--;
}
void add(int i) {
  if ((++cnt[a[i]]) == 1)
    res++;
}
int main() {

  memset(ans, 0, sizeof(ans));
  memset(cnt, 0, sizeof(cnt));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  bl = sqrt(n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &e[i].l, &e[i].r);
    e[i].p = i;
  }
  sort(e + 1, e + 1 + m, cmp);
  res = 0;
  curL = 1;
  curR = 0;
  for (int i = 1; i <= m; i++) {
    int L = e[i].l, R = e[i].r;
    while (curL < L)
      remove(curL++);
    while (curL > L)
      add(--curL);
    while (curR < R)
      add(++curR);
    while (curR > R)
      remove(curR--);
    ans[e[i].p] = res;
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);
}


Fixed submission:
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int bl, t, n, m;
int res, a[N], cnt[N], ans[N];
int curR, curL;
struct node {
  int l, r, p;
  bool operator<(const node &W) const { return l < W.l; }
} e[N];
bool cmp(const node &a, const node &b) {
  return (a.l / bl) == (b.l / bl) ? a.r < b.r : a.l > b.l;
}
void remove(int i) {
  if ((--cnt[a[i]]) == 0)
    res--;
}
void add(int i) {
  if ((++cnt[a[i]]) == 1)
    res++;
}
int main() {

  memset(ans, 0, sizeof(ans));
  memset(cnt, 0, sizeof(cnt));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  bl = sqrt(n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &e[i].l, &e[i].r);
    e[i].p = i;
  }
  sort(e + 1, e + 1 + m, cmp);
  res = 0;
  curL = 1;
  curR = 0;
  for (int i = 1; i <= m; i++) {
    int L = e[i].l, R = e[i].r;
    while (curL < L)
      remove(curL++);
    while (curL > L)
      add(--curL);
    while (curR < R)
      add(++curR);
    while (curR > R)
      remove(curR--);
    ans[e[i].p] = res;
  }
  for (int i = 1; i <= m; i++)
    printf("%d\n", ans[i]);
}
Problem p02599 (C++)

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;

constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
array<pair<int, int>, 500050> query;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i) {
    cin >> query[i].first >> query[i].second;
    query[i].first--;
    mo[query[i].first / nr].push_back({query[i].second, i});
  }
  int x;
  array<int, 500050> num{};
  array<int, 500050> ret{};
  for (int i = 0; i < nr; ++i) {
    sort(mo[i].begin(), mo[i].end());
    fill(num.begin(), num.end(), 0);
    int curl, curr;
    curl = curr = 0;
    int count = 0;
    for (auto &&a : mo[i]) {
      x = a.second;
      while (curr < query[x].second) {
        if (num[c[curr]] == 0) {
          count++;
        }
        num[c[curr]]++;
        curr++;
      }
      while (curl < query[x].first) {
        num[c[curl]]--;
        if (num[c[curl]] == 0)
          count--;
        curl++;
      }
      while (curl > query[x].first) {
        curl--;
        num[c[curl]]++;
        if (num[c[curl]] == 1)
          count++;
      }
      ret[x] = count;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ret[i] << endl;
  return 0;
}

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;

constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
pair<int, int> query[500050];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i) {
    cin >> query[i].first >> query[i].second;
    query[i].first--;
    mo[query[i].first / nr].push_back({query[i].second, i});
  }
  int x;
  array<int, 500050> num{};
  array<int, 500050> ret{};
  for (int i = 0; i < nr; ++i) {
    sort(mo[i].begin(), mo[i].end());
    fill(num.begin(), num.end(), 0);
    int curl, curr;
    curl = curr = 0;
    int count = 0;
    for (auto &&a : mo[i]) {
      x = a.second;
      while (curr < query[x].second) {
        if (num[c[curr]] == 0) {
          count++;
        }
        num[c[curr]]++;
        curr++;
      }
      while (curl < query[x].first) {
        num[c[curl]]--;
        if (num[c[curl]] == 0)
          count--;
        curl++;
      }
      while (curl > query[x].first) {
        curl--;
        num[c[curl]]++;
        if (num[c[curl]] == 1)
          count++;
      }
      ret[x] = count;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ret[i] << endl;
  return 0;
}