alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#define N 511111
#define A 511111
#define BLOCK 714 // ~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::sync_with_stdio(false);
  cin.tie(NULL);
  ll 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++)
    printf("%d\\n", ans[i]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

#define N 511111
#define A 1111111
#define BLOCK 714 // ~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::sync_with_stdio(false);
  cin.tie(NULL);
  ll 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++)
    printf("%d\\n", ans[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
int n, c[maxn], last[maxn];
vector<pair<int, int>> queries[maxn];
int sum[maxn];
void add(int pos, int val) {
  while (pos <= n) {
    sum[pos] += val;
    pos += pos & (-pos);
  }
  return;
}
int query(int pos) {
  int res = 0;
  while (pos) {
    res += sum[pos];
    pos -= pos & (-pos);
  }
  return res;
}
int ans[maxn];
int main() {
  int m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= m; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    queries[r].push_back(make_pair(l, i));
  }
  memset(last, -1, sizeof(last));
  for (int i = 1; i <= n; i++) {
    if (~last[c[i]])
      add(last[c[i]], -1);
    last[c[i]] = i;
    add(i, 1);
    for (int j = 0; j < (int)queries[i].size(); j++) {
      int s = queries[i][j].first;
      ans[queries[i][j].second] = query(i) - query(s - 1);
    }
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
/*
4 3
1 2 1 3
1 3
2 4
3 3

10 10
2 5 6 5 2 1 7 9 7 2
5 5
2 4
6 7
2 2
7 8
7 9
1 8
6 9
8 10
6 8
 */


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

using namespace std;

const int N = (int)5e5 + 7;
const int M = 777;

int n, q, v[N], sol[N], magic, bucket[N], last[N], first[N], f[N];

struct T {
  int l;
  int r;
  int i;
};

vector<T> lol[M];

bool operator<(T a, T b) { return a.r < b.r; }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  magic = sqrt(n);
  for (int i = 1; i <= n; i++) {
    cin >> v[i];
    bucket[i] = (i - 1) / magic + 1;
    last[bucket[i]] = i;
  }
  for (int i = n; i >= 1; i--) {
    first[bucket[i]] = i;
  }
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    lol[bucket[l]].push_back({l, r, i});
  }
  for (int b = bucket[1]; b <= bucket[n]; b++) {
    for (int i = 1; i <= n; i++) {
      f[v[i]] = 0;
    }
    sort(lol[b].begin(), lol[b].end());
    int until = last[b], jmek = 0;
    for (auto &it : lol[b]) {
      if (it.r <= last[b]) {
        int element = 0;
        for (int j = it.l; j <= it.r; j++) {
          f[v[j]]++;
          element += (f[v[j]] == 1);
        }
        for (int j = it.l; j <= it.r; j++) {
          f[v[j]] = 0;
        }
        sol[it.i] = element;
      } else {
        while (until < it.r) {
          until++;
          f[v[until]]++;
          jmek += (f[v[until]] == 1);
        }
        int element = jmek;
        for (int j = it.l; j <= last[b]; j++) {
          f[v[j]]++;
          element += (f[v[j]] == 1);
        }
        for (int j = it.l; j <= last[b]; j++) {
          f[v[j]]--;
        }
        sol[it.i] = element;
      }
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << sol[i] << "\\n";
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
int a[maxn], ans[maxn], cnt[maxn], res = 0;
struct node {
  int l, r, id;
  int block;
} e[maxn];

bool cmp(const node &a, const node &b) {
  if (a.block == b.block) {
    if (a.block & 1)
      return a.r < b.r;
    return a.r > b.r;
  }
  return a.l < b.l;
}

inline void del(int x) {
  --cnt[a[x]];
  if (cnt[a[x]] == 0)
    --res;
}
inline void add(int x) {
  if (cnt[a[x]] == 0)
    ++res;
  ++cnt[a[x]];
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  int bl = n / sqrt(q * 2 / 3);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &e[i].l, &e[i].r);
    e[i].id = i;
    e[i].block = e[i].l / bl;
  }
  sort(e + 1, e + 1 + q, cmp);
  int l = e[1].l, r = e[1].l - 1;
  for (int i = 1; i <= q; ++i) {
    int stdl = e[i].l, stdr = e[i].r;
    while (l < stdl)
      del(l++);
    while (l > stdl)
      add(--l);
    while (r < stdr)
      add(++r);
    while (r > stdr)
      del(r--);
    ans[e[i].id] = res;
  }
  for (int i = 1; i <= q; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int read() {
  char x;
  while ((x = getchar()) > '9' || x < '0')
    ;
  int u = x - '0';
  while ((x = getchar()) <= '9' && x >= '0')
    u = (u << 3) + (u << 1) + x - '0';
  return u;
}
int buf[105];
inline void write(int i) {
  int p = 0;
  if (i == 0)
    p++;
  else
    while (i) {
      buf[p++] = i % 10;
      i /= 10;
    }
  for (int j = p - 1; j >= 0; j--)
    putchar('0' + buf[j]);
}
int a[500005], Ans[500005], cnt[500005], ans = 0, n, m, bl;
struct node {
  int l, r, id;
} q[500005];

inline bool cmp(const node a, const node b) {
  return (a.l / bl) ^ (b.l / bl) ? a.l < b.l
                                 : (((a.l / bl) & 1) ? a.r < b.r : a.r > b.r);
}

inline void del(int x) {
  --cnt[a[x]];
  if (!cnt[a[x]])
    --ans;
}
inline void add(int x) {
  if (!cnt[a[x]])
    ++ans;
  ++cnt[a[x]];
}

int main() {
  int n, m;
  n = read();
  m = read();
  bl = n / sqrt(2 / 3 * m);
  for (int i = 1; i <= n; ++i)
    a[i] = read();

  for (int i = 1; i <= m; ++i) {
    q[i].l = read(), q[i].r = read();
    q[i].id = i;
  }
  sort(q + 1, q + 1 + m, cmp);

  int l = q[1].l, r = q[1].l - 1;
  for (int i = 1; i <= m; ++i) {
    int stdl = q[i].l, stdr = q[i].r;

    while (l < stdl)
      del(l++);

    while (l > stdl)
      add(--l);

    while (r < stdr)
      add(++r);

    while (r > stdr)
      del(r--);

    Ans[q[i].id] = ans;
  }

  for (int i = 1; i <= m; ++i)
    write(Ans[i]), printf("\\n");
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int read() {
  char x;
  while ((x = getchar()) > '9' || x < '0')
    ;
  int u = x - '0';
  while ((x = getchar()) <= '9' && x >= '0')
    u = (u << 3) + (u << 1) + x - '0';
  return u;
}
int buf[105];
inline void write(int i) {
  int p = 0;
  if (i == 0)
    p++;
  else
    while (i) {
      buf[p++] = i % 10;
      i /= 10;
    }
  for (int j = p - 1; j >= 0; j--)
    putchar('0' + buf[j]);
}

#define il inline
#define re register

int block, ans = 0, cnt[1000001];
int n, m, a[500010], Ans[500010];

struct node {
  int l, r, id;
} q[500010];

il bool cmp(node a, node b) {
  return (a.l / block) ^ (b.l / block)
             ? a.l < b.l
             : (((a.l / block) & 1) ? a.r < b.r : a.r > b.r);
}

il void add(int x) {
  if (!cnt[a[x]])
    ans++;
  cnt[a[x]]++;
}

il void del(int x) {
  cnt[a[x]]--;
  if (!cnt[a[x]])
    ans--;
}
int i;

int main() {
  n = read();
  m = read();
  for (i = 1; i <= n; ++i)
    a[i] = read();
  block = n / sqrt(m * 2 / 3);
  for (i = 1; i <= m; ++i) {
    q[i].l = read();
    q[i].r = read();
    q[i].id = i;
  }
  sort(q + 1, q + m + 1, cmp);
  int l = 0, r = 0;
  for (i = 1; i <= m; ++i) {
    int ql = q[i].l, qr = q[i].r;
    while (l < ql)
      del(l++);
    while (l > ql)
      add(--l);
    while (r < qr)
      add(++r);
    while (r > qr)
      del(r--);
    Ans[q[i].id] = ans;
  }
  for (i = 1; i <= m; ++i)
    write(Ans[i]), printf("\\n");
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e5 + 5;
int a[maxn];
struct node {
  int l, r, id;
  int ans;
} e[maxn];

bool cmp(const node &a, const node &b) {
  if (a.l == b.l)
    return a.r < b.r;
  return a.l < b.l;
}
bool recmp(const node &a, const node &b) { return a.id < b.id; }
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);

  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &e[i].l, &e[i].r);
    e[i].id = i;
  }
  sort(e + 1, e + 1 + q, cmp);
  int l = e[1].l, r = e[1].l - 1, cnt = 0;
  map<int, int> mp;
  for (int i = 1; i <= q; ++i) {
    while (l < e[i].l) {
      if (mp[a[l]]) {
        if (mp[a[l]] == 1)
          --cnt;
      }
      --mp[a[l]];
      ++l;
    }

    while (r < e[i].r) {
      ++r;
      if (!mp[a[r]])
        ++cnt;
      ++mp[a[r]];
      // cout<<cnt<<" "<<i<<endl;
    }

    while (r > e[i].r) {
      if (mp[a[r]]) {
        if (mp[a[r]] == 1)
          --cnt;
      }
      --mp[a[r]];
      --r;
    }
    e[i].ans = cnt;
  }

  sort(e + 1, e + 1 + q, recmp);
  for (int i = 1; i <= q; ++i)
    printf("%d\\n", e[i].ans);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: 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';
}


Error: TLE

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, verdict: Runtime Error

#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';
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define INF 1000000000
#define endl '\\n'
#define BLOCK 750
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[200001];
int ar[30001], ans[200001];
int fre[1000001];
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::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

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

  cin >> q;
  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);

  // why ML = 0 , and MR = -1?
  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, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define endl '\\n'
#define f first
#define s second

using ld = long double;
using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
using vpll = vector<pll>;

vector<vector<int>> seg;
vi a, b;
void build(int pos, int l, int r) {
  if (l == r) {
    seg[pos].push_back(b[l]);
    return;
  }
  int m = (l + r) / 2;
  build(2 * pos, l, m);
  build(2 * pos + 1, m + 1, r);
  seg[pos].insert(seg[pos].end(), seg[2 * pos].begin(), seg[2 * pos].end());
  seg[pos].insert(seg[pos].end(), seg[2 * pos + 1].begin(),
                  seg[2 * pos + 1].end());
  sort(seg[pos].begin(), seg[pos].end());
}

int query(int pos, int l, int r, int ql, int qr) {
  if (qr < l || ql > r)
    return 0;

  if (l >= ql && r <= qr) {
    int qtd =
        lower_bound(seg[pos].begin(), seg[pos].end(), ql) - seg[pos].begin();
    return qtd;
  }

  int m = (l + r) / 2;
  int left = query(2 * pos, l, m, ql, qr);
  int right = query(2 * pos + 1, m + 1, r, ql, qr);

  return left + right;
}
int main() {

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

  int n, q;
  cin >> n >> q;
  a.resize(n);
  b.resize(n);
  seg.resize(4 * n);

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

  vi last(n + 1, -1);

  for (int i = 0; i < n; i++) {
    b[i] = last[a[i]];
    last[a[i]] = i;
  }

  build(1, 0, n - 1);

  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    cout << query(1, 0, n - 1, l, r) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl '\\n'
#define f first
#define s second

using ld = long double;
using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<ii>;
using vpll = vector<pll>;

vector<set<int>> seg;
vi a;
void build(int pos, int l, int r) {
  if (l == r) {
    seg[pos].insert(a[l]);
    return;
  }
  int m = (l + r) / 2;
  build(2 * pos, l, m);
  build(2 * pos + 1, m + 1, r);
  seg[pos].insert(seg[2 * pos].begin(), seg[2 * pos].end());
  seg[pos].insert(seg[2 * pos + 1].begin(), seg[2 * pos + 1].end());
}

set<int> query(int pos, int l, int r, int ql, int qr) {
  set<int> left, right, ret;
  if (qr < l || ql > r)
    return ret;
  if (l >= ql && r <= qr)
    return seg[pos];

  int m = (l + r) / 2;
  left = query(2 * pos, l, m, ql, qr);
  right = query(2 * pos + 1, m + 1, r, ql, qr);

  ret.insert(left.begin(), left.end());
  ret.insert(right.begin(), right.end());

  return ret;
}
int main() {

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

  int n, q;
  cin >> n >> q;
  a.resize(n);
  seg.resize(4 * n);

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

  build(1, 0, n - 1);

  while (q--) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    cout << query(1, 0, n - 1, l, r).size() << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#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;
}


Error: 0

Fixed submission:

#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;
}