alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

struct FenwickTree {
  vector<int> bit;
  int siz;

  FenwickTree(int N) : bit(N) {
    siz = N;
    for (int i = 0; i < siz; i++) {
      bit[i] = 0;
    }
  }

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

  int sum(int i) {
    int sm = 0;
    for (; i > 0; i -= i & -i) {
      sm += bit[i];
    }
    return sm;
  }

  int range_sum(int l, int r) { return sum(r) - sum(l - 1); }
};

struct query {
  int id;
  int l;
  int r;
};
bool operator<(const query &a, const query &b) { return a.r < b.r; }

int main() {
  int n, q;
  cin >> n >> q;
  int c[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  query qs[q];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    qs[i] = {i, l, r};
  }
  sort(qs, qs + q);

  FenwickTree FT(n + 1);
  int right_pos[n + 1];
  for (int i = 1; i <= n; i++) {
    right_pos[i] = 0;
  }
  int ans[q];
  int now_q = 0;
  for (int i = 1; i <= n; i++) {
    if (right_pos[c[i]] > 0) {
      FT.add(right_pos[c[i]], -1);
    }
    FT.add(i, 1);
    right_pos[c[i]] = i;
    while (now_q < q && qs[now_q].r == i) {
      ans[qs[now_q].id] = FT.range_sum(qs[now_q].l, qs[now_q].r);
      now_q++;
    }
  }

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


Error: 0

Fixed submission:

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

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

  vector<int> dp[n];
  for (int i = 0; i < n - 1; i++) {
    vector<bool> used(n + 1, false);
    used[c[i]] = true;
    dp[i].push_back(1);
    for (int j = i + 1; j < n; j++) {
      dp[i].push_back(dp[i][j - i - 1]);
      if (!used[c[j]]) {
        dp[i][j - i]++;
        used[c[j]] = true;
      }
    }
  }

  while (q-- > 0) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << dp[l][r - l] << endl;
  }

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const long long MAX = 500005;

const long long MAX1 = 1e6 + 5;

long long tree[MAX], p[MAX1], ans[MAX], a[MAX];

pair<long long, pair<long long, long long>> pr[MAX];

void update(long long idx, long long val) {

  while (idx < MAX) {

    tree[idx] += val;

    idx += (idx & -idx);
  }
}

long long read(long long idx) {

  long long sum = 0;

  while (idx > 0) {

    sum += tree[idx];

    idx -= (idx & -idx);
  }

  return sum;
}

int32_t main() {

  long long n, q, x;

  cin >> n >> q;

  for (long long i = 1; i <= n; ++i)

    cin >> a[i];

  for (long long i = 0; i < q; ++i) {

    cin >> pr[i].second.first >> pr[i].first;

    pr[i].second.second = i;
  }

  sort(pr, pr + q);

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

  memset(tree, 0, sizeof(tree));

  x = 0;

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

    if (p[a[i]] != -1)

      update(p[a[i]], -1);

    p[a[i]] = i;

    update(i, 1);

    while (x < q and pr[x].first == i) {

      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);

      x++;
    }
  }

  for (long long i = 0; i < q; ++i)

    cout << ans[i] << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MAX = 300005;

const int MAX1 = 1e6 + 5;

int tree[MAX], p[MAX1], ans[MAX], a[MAX];

pair<int, pair<int, int>> pr[MAX];

void update(int idx, int val) {

  while (idx < MAX) {

    tree[idx] += val;

    idx += (idx & -idx);
  }
}

int read(int idx) {

  int sum = 0;

  while (idx > 0) {

    sum += tree[idx];

    idx -= (idx & -idx);
  }

  return sum;
}

int main() {

  int n, q, x;

  scanf("%d", &n);

  scanf("%d", &q);

  for (int i = 1; i <= n; ++i)

    scanf("%d", &a[i]);

  for (int i = 0; i < q; ++i) {

    scanf("%d %d", &pr[i].second.first, &pr[i].first);

    pr[i].second.second = i;
  }

  sort(pr, pr + q);

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

  memset(tree, 0, sizeof(tree));

  x = 0;

  for (int i = 1; i <= n; ++i) {

    if (p[a[i]] != -1)

      update(p[a[i]], -1);

    p[a[i]] = i;

    update(i, 1);

    while (x < q and pr[x].first == i) {

      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);

      x++;
    }
  }

  for (int i = 0; i < q; ++i)

    printf("%d\\n", ans[i]);

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MAX 500005
int bit[MAX], n, q;
map<int, int> mp;
struct Query {
  int l, r, id;
  bool operator<(const Query &a) const { return r < a.r; }
} b[MAX];
int gsum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}
void add(int i, int k) {
  while (i <= n) {
    bit[i] += k;
    i += i & -i;
  }
}
int main() {
  scanf("%d %d", &n, &q);
  mp.clear();
  vector<int> a(n + 10), ans(q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < q; i++) {
    int l, r;
    scanf("%d %d", &l, &r);
    b[i].l = l;
    b[i].r = r;
    b[i].id = i;
  }
  sort(b, b + q);
  int pre = 1;
  for (int i = 0; i < q; i++) {
    for (int j = pre; j <= b[i].r; j++) {
      if (mp[a[j]]) {
        add(mp[a[j]], -1);
      }
      add(j, 1);
      mp[a[j]] = j;
    }
    pre = b[i].r + 1;
    ans[b[i].id] = gsum(b[i].r) - gsum(b[i].l - 1);
  }
  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }
}

Error: 0

Fixed submission:


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int maxn = 5e5 + 7;

int root[maxn], tot;
int m;
int a[maxn], b[maxn];
void quchong(int n) {
  sort(b + 1, b + 1 + n);
  m = unique(b + 1, b + 1 + n) - b - 1;
}

int getid(int x) { return lower_bound(b + 1, b + 1 + m, x) - b; }

struct Tree {
  int lc, rc;
  int sum;
} tree[maxn * 10];

void pushup(int k) {
  tree[k].sum = tree[tree[k].lc].sum + tree[tree[k].rc].sum;
}

int build(int l, int r) {
  int k = ++tot;
  tree[k].sum = 0;
  if (l == r)
    return k;
  int mid = (l + r) >> 1;
  tree[k].lc = build(l, mid);
  tree[k].rc = build(mid + 1, r);
  return k;
}

int updata(int p, int id, int v, int l, int r) {
  int k = ++tot;
  tree[k] = tree[p];
  if (l == r) {
    tree[k].sum += v;
    return k;
  }
  int mid = (l + r) >> 1;
  if (id <= mid)
    tree[k].lc = updata(tree[p].lc, id, v, l, mid);
  else
    tree[k].rc = updata(tree[p].rc, id, v, mid + 1, r);
  pushup(k);
  return k;
}

int myfind(int p, int id, int l, int r) {
  if (l == r)
    return tree[p].sum;
  int mid = (l + r) >> 1;
  if (id <= mid)
    return tree[tree[p].rc].sum + myfind(tree[p].lc, id, l, mid);
  return myfind(tree[p].rc, id, mid + 1, r);
}

int belong[maxn];

int main() {

  int n;
  int q;
  scanf("%d", &n);
  scanf("%d", &q);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &a[i]);
    b[i] = a[i];
  }
  int l, r;
  quchong(n);
  root[0] = build(1, n);
  for (int i = 1; i <= n; ++i) {
    int id = getid(a[i]);
    if (belong[id]) {
      int t = updata(root[i - 1], belong[id], -1, 1, n);
      root[i] = updata(t, i, 1, 1, n);
    } else {
      root[i] = updata(root[i - 1], i, 1, 1, n);
    }
    belong[id] = i;
  }
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\\n", myfind(root[r], l, 1, n));
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int a[N];
int res;
int tr[N];
int ans[N];
int n;
int lowbit(int x) { return x & -x; }
void add(int x, int d) {
  for (int i = x; i <= n; i += lowbit(i))
    tr[i] += d;
}
int query(int x) {
  int res = 0;
  for (int i = x; i; i -= lowbit(i))
    res += tr[i];
  return res;
}
struct node {
  int l;
  int r;
  int id;
  bool operator<(const node &W) const { return r < W.r; }
} q[N];
int c[N], b[N];
int pos[N];
int m;
void solve() {
  int cur = 0;
  int res = 0;
  for (int i = 1; i <= q[0].r; i++) {
    if (pos[a[i]]) {
      add(pos[a[i]], -1);
      add(i, 1);
      pos[a[i]] = i;
    } else {
      pos[a[i]] = i;
      add(i, 1);
    }
  }
  cur = q[0].r + 1;
  ans[q[0].id] = query(q[0].r) - query(q[0].l - 1);
  for (int i = 1; i < m; i++) {
    for (int j = cur; j <= q[i].r; j++) {
      if (pos[a[j]]) {
        add(pos[a[j]], -1);
        pos[a[j]] = j;
        add(j, 1);
      } else {
        pos[a[j]] = j;
        add(j, 1);
      }
    }
    cur = q[i].r + 1;
    ans[q[i].id] = query(q[i].r) - query(q[i].l - 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  sort(q, q + m);
  solve();
  for (int i = 0; i < m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 500010;
int a[N];
int block;
int bl[N];
int cnt[N];
int res;
int tr[N];
int ans[N];
int n;
int lowbit(int x) { return x & -x; }
void add(int x, int d) {
  for (int i = x; i <= n; i += lowbit(i))
    tr[i] += d;
}
int query(int x) {
  int res = 0;
  for (int i = x; i; i -= lowbit(i))
    res += tr[i];
  return res;
}
struct node {
  int l;
  int r;
  int id;
  bool operator<(const node &W) const { return r < W.r; }
} q[N];
int c[N], b[N];
int pos[N];
int m;
void solve() {
  int cur = 0;
  int res = 0;
  for (int i = 1; i <= q[0].r; i++) {
    if (pos[a[i]]) {
      add(pos[a[i]], -1);
      add(i, 1);
    } else {
      pos[a[i]] = i;
      add(i, 1);
    }
  }
  cur = q[0].r + 1;
  ans[q[0].id] = query(q[0].r) - query(q[0].l - 1);
  for (int i = 1; i < m; i++) {
    for (int j = cur; j <= q[i].r; j++) {
      if (pos[a[j]]) {
        add(pos[a[j]], -1);
        pos[a[j]] = j;
        add(j, 1);
      } else {
        pos[a[j]] = j;
        add(j, 1);
      }
    }
    ans[q[i].id] = query(q[i].r) - query(q[i].l - 1);
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].id = i;
  }
  sort(q, q + m);
  solve();
  for (int i = 0; i < m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define ll long long
#define ar array

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

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

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

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

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bits/stdc++.h>
#include <deque>
#include <iostream>
#include <map>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
#define ll long long
#define ar array

int main() {
  int n, q;
  cin >> n >> q;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<int, int> idx;
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (idx.find(a[i]) == idx.end()) {
      idx[a[i]] = c++;
    }
  }
  vector<vector<int>> count(n + 1, vector<int>(c, 0));
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < c; j++)
      count[i][j] = count[i - 1][j];
    count[i][idx[a[i - 1]]]++;
  }
  // for (int i=0; i<=n; i++) {
  // for (int j=0; j<c; j++){
  // cout << count[i][j] << " ";
  // }
  // cout << endl;
  // }

  while (q--) {
    int x, y;
    cin >> x >> y;
    x--;
    int ans = 0;
    for (int i = 0; i < c; i++) {
      if (count[y][i] - count[x][i])
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded



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

int blk = 550;

struct node {
  int l;
  int r;
  int ind;
};

bool cmp(node &a, node &b) {
  if (a.l / blk != b.l / blk)
    return a.l / blk < b.l / blk;
  return a.r < b.r;
}

int m[1000010];
int cnt = 0;

void add(int &x) {
  m[x]++;
  if (m[x] == 1)
    cnt++;
}
void remove(int &x) {
  m[x]--;
  if (m[x] == 0)
    cnt--;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (int i = 0; i <= 1000009; i++)
    m[i] = 0;
  int n, q;
  cin >> n >> q;
  blk = sqrt(n) + 1;
  int a[n];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  node qry[q];
  for (int i = 0; i < q; i++) {
    node a;
    cin >> a.l >> a.r;
    a.l--, a.r--;
    a.ind = i;
    qry[i] = a;
  }
  sort(qry, qry + q, cmp);
  int lp = 0, rp = -1;
  int arr[q];
  for (int i = 0; i < q; i++) {
    int l = qry[i].l;
    int r = qry[i].r;
    int ind = qry[i].ind;
    while (lp < l)
      remove(a[lp]), lp++;
    while (l < lp)
      add(a[lp - 1]), lp--;
    while (rp < r)
      rp++, add(a[rp]);
    while (rp > r)
      remove(a[rp]), rp--;
    arr[ind] = cnt;
  }
  for (int i = 0; i < q; i++)
    cout << arr[i] << "\\n";
}


Error: TLE

Fixed submission:



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

ll blk = 550;

struct node {
  ll l;
  ll r;
  ll ind;
};

bool cmp(node &a, node &b) {
  if (a.l / blk != b.l / blk)
    return a.l / blk < b.l / blk;
  return a.r < b.r;
}

ll m[1000010];
ll cnt = 0;

void add(ll x) {
  m[x]++;
  if (m[x] == 1)
    cnt++;
}
void remove(ll x) {
  m[x]--;
  if (m[x] == 0)
    cnt--;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  for (ll i = 0; i <= 1000009; i++)
    m[i] = 0;
  ll n, q;
  cin >> n >> q;
  blk = sqrt(n) + 1;
  ll a[n];
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  node qry[q];
  for (ll i = 0; i < q; i++) {
    node a;
    cin >> a.l >> a.r;
    a.l--, a.r--;
    a.ind = i;
    qry[i] = a;
  }
  sort(qry, qry + q, cmp);
  ll lp = 0, rp = -1;
  ll arr[q];
  for (ll i = 0; i < q; i++) {
    ll l = qry[i].l;
    ll r = qry[i].r;
    ll ind = qry[i].ind;
    while (lp < l)
      remove(a[lp]), lp++;
    while (l < lp)
      add(a[lp - 1]), lp--;
    while (rp < r)
      rp++, add(a[rp]);
    while (rp > r)
      remove(a[rp]), rp--;
    arr[ind] = cnt;
  }
  for (ll i = 0; i < q; i++)
    cout << arr[i] << "\\n";
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

// 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) {
    if (block(lo) != block(q.lo))
      return block(lo) < block(q.lo);
    return (block(lo) & 1) ? (hi < q.hi) : (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..


Error: TLE

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 = 555, 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, verdict: Time Limit Exceeded

// 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..

Error: TLE

Fixed submission:

// 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..
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int MAX = 5 * 1e5 + 1;

struct Query {
  int l, r, idx;
};

bool cmp(Query x, Query y) { return x.r < y.r; }

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

int query(int idx, int bit[], int n) {
  int sum = 0;
  for (; idx > 0; idx -= idx & -idx)
    sum += bit[idx];
  return sum;
}

void answeringQueries(vector<int> arr, int n, vector<Query> queries, int q) {
  int bit[n + 1];
  memset(bit, 0, sizeof(bit));

  int last_visit[MAX];
  memset(last_visit, -1, sizeof(last_visit));

  int ans[q];
  int query_counter = 0;
  for (int i = 0; i < n; i++) {
    if (last_visit[arr[i]] != -1)
      update(last_visit[arr[i]] + 1, -1, bit, n);

    last_visit[arr[i]] = i;
    update(i + 1, 1, bit, n);

    while (query_counter < q && queries[query_counter].r == i) {
      ans[queries[query_counter].idx] =
          query(queries[query_counter].r + 1, bit, n) -
          query(queries[query_counter].l, bit, n);
      query_counter++;
    }
  }

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

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  vector<Query> queries;
  for (int &x : a)
    cin >> x;
  int i = 0;
  while (i < q) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    queries.push_back({l, r, i});
    i++;
  }
  sort(queries.begin(), queries.end(), cmp);
  answeringQueries(a, n, queries, q);
  return 0;
}

Error: TLE

Fixed submission:

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

vector<set<int>> segment;

void build(int i, int s, int e, vector<int> arr) {
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());
  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  if (b < l || a > r)
    return result;
  if (a <= l && r <= b)
    return segment[node];
  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());
  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());

  return result;
}

void init(int n) {
  int h = 1;
  while (h < n)
    h *= 2;
  segment.assign(h * 2, set<int>());
}

void getDistinct(int l, int r, int n) {
  cout << (int)query(1, 0, n - 1, l, r).size() << endl;
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  init(n);
  build(1, 0, n - 1, a);
  while (q--) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    getDistinct(l, r, n);
  }

  return 0;
}