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

#include <bits/stdc++.h>

using namespace std;

template <typename T> class BIT {
  vector<T> tree_;

public:
  BIT(int n) : tree_(n, 0) {}
  void Add(int i, T x) {
    for (i++; i <= tree_.size(); i += i & -i) {
      tree_[i - 1] += x;
    }
  }

  T Sum(int s) {
    T ans = 0;
    for (s++; s > 0; s -= s & -s) {
      ans += tree_[s - 1];
    }

    return ans;
  }

  T Sum(int l, int r) { return Sum(r) - Sum(l - 1); }
};

int main(int argc, const char *argv[]) {
  int n, q;
  cin >> n >> q;

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

  vector<array<int, 3>> vq(q);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    vq[i][0] = r - 1;
    vq[i][1] = l - 1;
    vq[i][2] = i;
  }

  sort(vq.begin(), vq.end());

  BIT<int> bit(n);
  vector<int> vp(n, -1), ans(q);
  int prev = 0;
  for (auto &q : vq) {
    int l, r, idx;
    r = q[0];
    l = q[1];
    idx = q[2];
    for (int i = prev; i <= r; ++i) {
      int c = vc[i];
      if (vp[c] < i) {
        if (vp[c] != -1) {
          bit.Add(vp[c], -1);
        }

        bit.Add(i, 1);
        vp[c] = i;
      }

      ans[idx] = bit.Sum(l, r);
    }

    prev = r;
  }

  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

template <typename T> class BIT {
  vector<T> tree_;

public:
  BIT(int n) : tree_(n, 0) {}
  void Add(int i, T x) {
    for (i++; i <= tree_.size(); i += i & -i) {
      tree_[i - 1] += x;
    }
  }

  T Sum(int s) {
    T ans = 0;
    for (s++; s > 0; s -= s & -s) {
      ans += tree_[s - 1];
    }

    return ans;
  }

  T Sum(int l, int r) { return Sum(r) - Sum(l - 1); }
};

int main(int argc, const char *argv[]) {
  int n, q;
  cin >> n >> q;

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

  vector<array<int, 3>> vq(n);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    vq[i][0] = r - 1;
    vq[i][1] = l - 1;
    vq[i][2] = i;
  }

  sort(vq.begin(), vq.end());

  BIT<int> bit(n);
  vector<int> vp(n, -1), ans(q);
  int prev = 0;
  for (auto &q : vq) {
    int l, r, idx;
    r = q[0];
    l = q[1];
    idx = q[2];
    for (int i = prev; i <= r; ++i) {
      int c = vc[i];
      if (vp[c] < i) {
        if (vp[c] != -1) {
          bit.Add(vp[c], -1);
        }

        bit.Add(i, 1);
        vp[c] = i;
      }

      ans[idx] = bit.Sum(l, r);
    }

    prev = r;
  }

  for (int i = 0; i < ans.size(); ++i) {
    cout << ans[i] << '\\n';
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

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

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

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

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

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

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

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

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

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

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

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

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

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

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

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

  int block_size = 707;

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

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

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

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

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

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

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

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

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

  int block_size = 450;

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

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

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

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

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

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

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

template <typename T> class BIT {
public:
  explicit BIT(int n) : N(calcN_(n)), mVal(calcN_(n) + 1, 0) {}
  void add(int idx, T val) {
    for (int x = idx; x <= N; x += x & -x)
      mVal[x] += val;
  }
  T sum(int idx) {
    T res = 0;
    for (int x = idx; x > 0; x -= x & -x)
      res += mVal[x];
    return res;
  }

private:
  int calcN_(int n) {
    int res = 1;
    while (res < n)
      res *= 2;
    return res;
  }
  const int N;
  vector<T> mVal;
};

struct Query {
  explicit Query(int l, int r, int id) : l(l), r(r), id(id) {}
  int l, r, id;
  bool operator<(const Query &q) const { return r < q.r; }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (auto &t : c) {
    cin >> t;
    --t;
  }
  vector<Query> vq;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    --l;
    vq.emplace_back(l, r, i);
  }
  sort(vq.begin(), vq.end());
  vector<int> res(Q);
  vector<int> last(N, -1);
  int end = 0;
  BIT<int> bit(N);
  for (auto &q : vq) {
    for (int i = end; i < q.r; i++) {
      if (last[c[i]] != -1)
        bit.add(last[c[i]], -1);
      bit.add(i + 1, 1);
      last[c[i]] = i + 1;
    }
    end = q.r;
    res[q.id] = bit.sum(q.r) - bit.sum(q.l);
  }
  for (auto &t : res)
    cout << t << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

struct Query {
  explicit Query(int l, int r, int id, int block)
      : l(l), r(r), id(id), block(block) {}
  int l, r, id, block;
  bool operator<(const Query &q) const {
    return block != q.block ? block < q.block : r < q.r;
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (auto &t : c) {
    cin >> t;
    --t;
  }
  int sq = 0;
  while (sq * sq < Q)
    sq++;
  vector<Query> vq;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    --l;
    vq.emplace_back(l, r, i, l / sq);
  }
  sort(vq.begin(), vq.end());
  vector<int> res(Q);
  vector<int> cnt(N, 0);
  int cur = 0;
  int curL = 0, curR = 0;
  auto add = [&](int id) {
    if (cnt[c[id]] == 0)
      ++cur;
    ++cnt[c[id]];
  };
  auto remove = [&](int id) {
    --cnt[c[id]];
    if (cnt[c[id]] == 0)
      --cur;
  };
  for (auto &q : vq) {
    for (int i = q.l; i < curL; i++)
      add(i);
    for (int i = curR; i < q.r; i++)
      add(i);
    for (int i = curL; i < q.l; i++)
      remove(i);
    for (int i = q.r; i < curR; i++)
      remove(i);
    res[q.id] = cur;
    curL = q.l;
    curR = q.r;
  }
  for (auto &t : res)
    cout << t << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int const N = 5e5 + 5;
int const M = 5e5 + 5;

int n, m;
int a[N];

pair<int, pair<int, int>> q[M];

bool sort_queries(pair<int, pair<int, int>> const &a,
                  pair<int, pair<int, int>> const &b) {
  return a.second.second < b.second.second;
}

int bit[N];

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

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

int ans[M];

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

  for (int i = 1; i <= n; ++i)
    scanf("%d", a + i);

  for (int i = 0; i < m; ++i)
    scanf("%d%d", &q[i].second.first, &q[i].second.second), q[i].first = i;

  sort(q, q + m, sort_queries);

  map<int, int> last;

  int R = 1;
  for (int i = 0; i < m; ++i) {
    int idx = q[i].first;
    int l = q[i].second.first;
    int r = q[i].second.second;

    while (R <= r) {
      int x = a[R];

      auto f = last.find(x);

      if (f == last.end())
        last[x] = R;
      else
        update(f->second, -1), f->second = R;

      update(R++, 1);
    }

    ans[idx] = query(r) - query(l - 1);
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int const N = 5e5 + 5;

int n, m;
int a[N], b[N], last[N];

struct tree {
  int v;
  tree *lf, *rg;

  tree(tree *tmp) { *this = *tmp; }

  tree(int l, int r) {
    v = 0;
    if (l == r)
      return;
    int m = l + r >> 1;
    lf = new tree(l, m);
    rg = new tree(m + 1, r);
  }

  int query(int l, int r, int L, int R) {
    if (L <= l && r <= R)
      return v;
    int m = l + r >> 1;
    if (R <= m)
      return lf->query(l, m, L, R);
    else if (m < L)
      return rg->query(m + 1, r, L, R);
    else
      return lf->query(l, m, L, m) + rg->query(m + 1, r, m + 1, R);
  }

  tree *update(int l, int r, int i, int v) {
    auto tmp = new tree(this);
    if (l == i && i == r) {
      tmp->v = v;
    } else {
      int m = l + r >> 1;
      if (i <= m)
        tmp->lf = lf->update(l, m, i, v);
      else
        tmp->rg = rg->update(m + 1, r, i, v);
      tmp->v = tmp->lf->v + tmp->rg->v;
    }
    return tmp;
  }
};

tree *ver[N];

int main() {
  scanf("%d", &n);
  int q;
  scanf("%d", &q);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    a[i] = b[i - 1] = x;
  }
  sort(b, b + n);
  m = unique(b, b + n) - b;

  ver[0] = new tree(1, n);

  for (int i = 1; i <= n; ++i) {
    int x = lower_bound(b, b + m, a[i]) - b;
    auto old = ver[i - 1];
    if (last[x])
      old = old->update(1, n, last[x], 0);
    ver[i] = old->update(1, n, i, 1);
    last[x] = i;
  }

  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\\n", ver[r]->query(1, n, l, r));
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
int N, Q;

// FenwickTree
vector<int> FenTree;

void init(void) {
  FenTree.resize(N + 1);
  rep(i, N + 1) FenTree[i] = 0;
}

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

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

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

int main(void) {
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  vector<int> C(N);
  rep(i, N) cin >> C[i];
  vector<vector<pair<int, int>>> LR(N + 1);
  rep(i, Q) {
    int L, R;
    cin >> L >> R;
    LR[R].emplace_back(L, i);
  }

  init();
  vector<int> answer(Q);
  vector<int> pos(N + 1, 0);
  for (int i = 1; i <= N; ++i) {
    if (pos[C[i - 1]] > 0) {
      add(pos[C[i - 1]], -1);
    }
    add(i, +1);
    pos[C[i - 1]] = i;
    rep(j, (int)LR[i].size()) {
      answer[LR[i][j].second] = query(LR[i][j].first, i);
    }
  }
  rep(i, Q) cout << answer[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int64_t N, K;
vector<int64_t> A;

int main(void) {
  ios::sync_with_stdio(false);
  int N, Q;
  cin >> N >> Q;

  // SegTree?
  int heap_size = 1;
  while (heap_size < 2 * N)
    heap_size *= 2;
  vector<unordered_set<int>> tree(heap_size);
  rep(i, N) {
    int C;
    cin >> C;
    int pos = i + N;
    while (pos) {
      tree[pos].emplace(C);
      pos /= 2;
    }
  }

  vector<int> answer(Q);
  rep(i, Q) {
    int L, R;
    cin >> L >> R;

    L = L - 1 + N;
    R = R - 1 + N;
    unordered_set<int> tmp_ans;
    while (L <= R) {
      if (L % 2) {
        for (auto &tmp : tree[L])
          tmp_ans.emplace(tmp);
        L++;
      }
      if ((R % 2) == 0) {
        for (auto &tmp : tree[R])
          tmp_ans.emplace(tmp);
        R--;
      }
      L /= 2;
      R /= 2;
    }
    answer[i] = tmp_ans.size();
  }

  rep(i, Q) { cout << answer[i] << endl; }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define NUM 1000000007
#define MAX 100001
#define INF LLONG_MAX
vector<int> BIT;
void update(int i, int val, int n) {
  for (; i <= n; i += (i & (-i)))
    BIT[i] += val;
}
int query(int i) {
  int sum = 0;
  for (; i > 0; i -= (i & (-i)))
    sum += BIT[i];
  return sum;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m, l, r, tmp;
  cin >> n >> m;
  vector<int> v;
  v.pb(0);
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    v.pb(tmp);
  }
  vector<pair<int, int>> Q[n + 1];
  for (int i = 0; i < m; i++) {
    cin >> l >> r;
    Q[r].push_back({l, i});
  }
  BIT.assign(n + 1, 0);
  vector<int> lastvis(n + 1, 0);
  vector<int> ans(m);
  for (int i = 1; i <= n; i++) {
    if (lastvis[v[i]])
      update(lastvis[v[i]], -1, n + 1);
    lastvis[v[i]] = i;
    update(i, 1, n + 1);
    for (int j = 0; j < Q[i].size(); j++) {
      ans[Q[i][j].second] = query(i) - query(Q[i][j].first - 1);
    }
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long int
set<ll> *segment;
void build(ll v, ll st, ll en, vector<ll> &vt) {
  if (st == en) {
    segment[v].insert(vt[st]);
    return;
  }
  ll mid = (st + en) / 2;
  build(2 * v, st, mid, vt);
  build(2 * v + 1, mid + 1, en, vt);
  segment[v].insert(segment[2 * v].begin(), segment[2 * v].end());
  segment[v].insert(segment[2 * v + 1].begin(), segment[2 * v + 1].end());
}
set<ll> query(ll v, ll st, ll en, ll a, ll b) {
  set<ll> left, right, rst;
  if (st > b || a > en)
    return rst;
  if (st >= a && en <= b)
    return segment[v];
  ll mid = (st + en) / 2;
  left = query(2 * v, st, mid, a, b);
  rst.insert(left.begin(), left.end());
  right = query(2 * v + 1, mid + 1, en, a, b);
  rst.insert(right.begin(), right.end());
  return rst;
}
void init(ll n) {
  ll h = (ll)ceil(log2(n));
  h = (2 * (pow(2, h))) - 1;
  segment = new set<ll>[h];
}
int main() {
  ll n, q;
  cin >> n >> q;
  ll tmp;
  init(4 * n + 1);
  vector<ll> vt;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    vt.pb(tmp);
  }
  build(1, 0, n - 1, vt);
  ll a, b;
  while (q--) {
    cin >> a >> b;
    a--;
    b--;
    set<ll> ans = query(1, 0, n - 1, a, b);
    cout << ans.size() << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second
const int N = 5e5 + 5;
const int K = 1e6 + 5;
using namespace std;

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

int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> q[i].f >> q[i].s.f, q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
#define ll long long
const ll N = 5e5 + 10;

struct node {
  ll l;
  ll r;
  ll id;
} u[N];

ll z;
bool cool(node a, node b) {
  if (a.l / z < b.l / z)
    return true;
  if (a.l / z > b.l / z)
    return false;
  return a.r < b.r;
}
ll cnt[1111111];
ll ans[N];
ll v[N];

int main() {

  // ios_base:: sync_with_stdio(false); cin.tie(0);

  // freopen("input.in","r",stdin);

  ll n, q;
  cin >> n >> q;
  ll fuk = 0;
  z = sqrt(n);
  //  vector<ll>v(n);

  for (ll i = 0; i < n; i++)
    cin >> v[i];

  for (ll i = 0; i < q; i++) {
    ll l, r;
    cin >> l >> r;
    node tmp;
    tmp.l = l - 1;
    tmp.r = r - 1;
    tmp.id = i;
    u[i] = tmp;
  }

  sort(u, u + q, cool);

  ll st = 0, e = 0;
  fuk = 1;
  cnt[v[0]]++;

  for (ll i = 0; i < q; i++) {
    ll l = u[i].l;
    ll r = u[i].r;
    // cout<<l<<" "<<r<<" "<<st<<" "<<e<<" "<<fuk<<endl;

    while (st < l) {
      cnt[v[st]]--;
      if (cnt[v[st]] == 0)
        fuk--;
      st++;
    }
    while (st > l) {
      st--;
      cnt[v[st]]++;
      if (cnt[v[st]] == 1)
        fuk++;
    }
    while (e < r) {
      e++;
      cnt[v[e]]++;
      if (cnt[v[e]] == 1)
        fuk++;
    }
    while (e > r) {
      cnt[v[e]]--;
      if (cnt[v[e]] == 0)
        fuk--;
      e--;
    }
    ans[u[i].id] = fuk;
  }

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

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

template <typename T> class fenwick_tree {
private:
  int n;
  vector<T> fenw;

public:
  fenwick_tree(int _n) : n(_n) { fenw.resize(n); }

  size_t size() { return n; }

  void update(int x, T val) {
    while (x < n) {
      fenw[x] += val;
      x |= (x + 1);
    }
  }

  T query(int x) {
    T ans = 0;
    while (x >= 0) {
      ans += fenw[x];
      x = (x & (x + 1)) - 1;
    }
    return ans;
  }
};

int n, q;
vector<int> a;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<query> queries(q);
  for (int i = 0; i < q; i++) {
    queries[i].idx = i;
    cin >> queries[i].l >> queries[i].r;
    queries[i].l--;
    queries[i].r--;
  }
  sort(queries.begin(), queries.end(),
       [](const query &a, const query &b) { return a.r < b.r; });
  fenwick_tree<int> fenw(n + 1);
  vector<int> last(n + 1, -1);
  vector<int> ans(q);
  int j = 0;
  for (int i = 0; i < n; i++) {
    if (last[a[i]] != -1) {
      fenw.update(last[a[i]] + 1, -1);
    }
    last[a[i]] = i;
    fenw.update(i + 1, +1);
    while (j < q && queries[j].r == i) {
      ans[queries[j].idx] =
          fenw.query(queries[j].r + 1) - fenw.query(queries[j].l);
      j++;
    }
  }
  for (auto &x : ans) {
    cout << x << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

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

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

int n, q;
vector<int> a;
map<int, int> mp;
int cnt = 1;

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

void add(int x) {
  if (mp[a[x]] == 0) {
    cnt++;
  }
  mp[a[x]]++;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  vector<query> queries(q);
  for (int i = 0; i < q; i++) {
    queries[i].idx = i;
    cin >> queries[i].l >> queries[i].r;
    queries[i].l--;
    queries[i].r--;
  }
  const int k = floor(sqrt(n));
  sort(queries.begin(), queries.end(), [&](const query &a, const query &b) {
    if ((a.l / k) == (b.l / k)) {
      return a.r < b.r;
    }
    return (a.l / k) < (b.l < k);
  });
  int l = 0, r = 0;
  vector<int> ans(q);
  mp[a[0]]++;
  for (auto &q : queries) {
    while (l < q.l) {
      remove(l++);
    }
    while (l > q.l) {
      add(--l);
    }
    while (r < q.r) {
      add(++r);
    }
    while (r > q.r) {
      remove(r--);
    }
    ans[q.idx] = cnt;
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\\n';
  }
  return 0;
}