alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: 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 = 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;
}

Error: TLE

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 = 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;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

const int N = 5e5 + 10;
vector<int> ql[N], qid[N], tree(N);
int n, q;

void update(int id, int val) {
  while (id <= n) {
    tree[id] += val;
    id += (id & -id);
  }
}
int query(int id) {
  int ans = 0;
  while (id > 0) {
    ans += tree[id];
    id -= (id & -id);
  }
  return ans;
}
int get(int l, int r) { return query(r) - query(l - 1); }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> q;
  int l, r, x;
  int c[n + 1], a[n + 1] = {0}, ans[q];
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    ql[r].push_back(l);
    qid[r].push_back(i);
  }

  for (int i = 1; i <= n; i++) {
    if (a[c[i]] > 0)
      update(a[c[i]], -1);
    update(i, 1);
    a[c[i]] = i;

    for (int j = 0; j < ql[i].size(); j++) {
      ans[qid[i][j]] = get(ql[i][j], i);
    }
  }

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


Error: 0

Fixed submission:

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

const int N = 5e5 + 7;
set<int> segment[N];
int n, q;
vector<int> a;
void build(int i, int s, int e) {

  if (s == e) {
    segment[i].insert(a[s]);
    return;
  }
  int mid = (s + e) >> 1;
  build(i << 1, s, mid);
  build(i << 1 | 1, mid + 1, e);
  segment[i].insert(segment[i << 1].begin(), segment[i << 1].end());

  segment[i].insert(segment[i << 1 | 1].begin(), segment[i << 1 | 1].end());
}
set<int> query(int id, 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[id];

  int mid = (l + r) >> 1;
  left = query(id << 1, l, mid, a, b);
  result.insert(left.begin(), left.end());

  right = query(id << 1 | 1, mid + 1, r, a, b);
  result.insert(right.begin(), right.end());

  return result;
}
void get(int l, int r) {
  set<int> ans = query(1, 0, n - 1, l, r);
  cout << ans.size() << endl;
}
int main() {
  cin >> n >> q;
  int l, r, x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
  build(1, 0, n - 1);

  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    --l;
    --r;
    get(l, r);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

// https://cses.fi/problemset/task/1734/
#include <bits/stdc++.h>

using namespace std;
using iii = tuple<int, int, int>;
using mii = map<int, int>;
using vi = vector<int>;
using viii = vector<iii>;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, q, a, b;
  cin >> n >> q;
  vi x(n);
  mii m;
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    if (m.count(y))
      x[i] = m[y];
    else
      x[i] = m.size(), m[y] = x[i];
  }
  viii y(q);
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    y[i] = {a - 1, b, i};
  }
  int k = sqrt(n);
  sort(y.begin(), y.end(), [&](iii x, iii y) -> bool {
    int a1, b1, i1, a2, b2, i2;
    tie(a1, b1, i1) = x;
    tie(a2, b2, i2) = y;
    int a1q = a1 / k, a2q = a2 / k;
    if (a1q < a2q)
      return true;
    if (a1q > a2q)
      return false;
    return b1 < b2;
  });
  vi res(q);
  vi count(m.size());
  int l = 0, r = 0, ans = 0;
  for (iii z : y) {
    int i;
    tie(a, b, i) = z;
    while (r < b)
      if (++count[x[r++]] == 1)
        ans++;
    while (r > b)
      if (--count[x[--r]] == 0)
        ans--;
    while (l < a)
      if (--count[x[l++]] == 0)
        ans--;
    while (l > a)
      if (++count[x[--l]] == 1)
        ans++;
    res[i] = ans;
  }
  for (int z : res)
    cout << z << '\\n';
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1E5 + 5;
const int M = 5E3 + 5;
int a[N];
int b[N];
bitset<M> rst;
bitset<M> segt_a[4 * N];
bitset<M> segt_b[4 * N];
void build_a(int node, int start, int end) {
  if (start == end) {
    segt_a[node].set(a[start]);
    return;
  }
  int mid = (start + end) >> 1;
  build_a(node << 1, start, mid);
  build_a((node << 1) + 1, mid + 1, end);
  segt_a[node] = segt_a[node << 1] | segt_a[(node << 1) + 1];
  return;
}
void build_b(int node, int start, int end) {
  if (start == end) {
    segt_b[node].set(b[start]);
    return;
  }
  int mid = (start + end) >> 1;
  build_b(node << 1, start, mid);
  build_b((node << 1) + 1, mid + 1, end);
  segt_b[node] = segt_b[node << 1] | segt_b[(node << 1) + 1];
  return;
}
bitset<M> query_a(int node, int start, int end, int L, int R) {
  if (start > R || end < L)
    return rst;
  if (start >= L && end <= R)
    return segt_a[node];
  int mid = (start + end) >> 1;
  return query_a(node << 1, start, mid, L, R) |
         query_a((node << 1) + 1, mid + 1, end, L, R);
}
bitset<M> query_b(int node, int start, int end, int L, int R) {
  if (start > R || end < L)
    return rst;
  if (start >= L && end <= R)
    return segt_b[node];
  int mid = (start + end) >> 1;
  return query_b(node << 1, start, mid, L, R) |
         query_b((node << 1) + 1, mid + 1, end, L, R);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    b[i] = a[i];
  build_a(1, 1, n);
  build_b(1, 1, n);
  int L1, R1;
  while (q--) {
    cin >> L1 >> R1;
    int ans = (query_a(1, 1, n, L1, R1) | query_b(1, 1, n, L1, R1)).count();
    cout << ans << '\\n';
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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


Error: TLE

Fixed submission:

#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;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> vec(n + 1, -1);
  vector<vector<int>> range(n);
  rep(i, n) {
    int last = vec[a[i]];
    if (last != -1)
      range[last].push_back(i);
    vec[a[i]] = i;
  }
  vector<vector<P>> qs(n);
  rep(qi, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    qs[l].emplace_back(
        r,
        qi); // x軸で走査するのでxをindexにとる。出力のためにクエリ番号を残す。上のrangeも構造を合わせた。
  }
  BIT<int> d(n);
  vector<int> ans(q);
  for (int x = n - 1; x >= 0; x--) {
    for (int y : range[x])
      d.add(y, 1);
    for (P query : qs[x]) {
      int qy = query.first, qn = query.second;
      ans[qn] = (qy - x + 1) - d.sum(qy);
    }
  }
  rep(i, q) cout << ans[i] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;
// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) a[i]--;
  vector<int> vec(n, -1);
  vector<vector<int>> range(n);
  rep(i, n) {
    int last = vec[a[i]];
    if (last != -1)
      range[last].push_back(i);
    vec[a[i]] = i;
  }
  vector<vector<P>> qs(n);
  rep(qi, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    qs[l].emplace_back(
        r,
        qi); // x軸で走査するのでxをindexにとる。出力のためにクエリ番号を残す。上のrangeも構造を合わせた。
  }
  BIT<int> d(n);
  vector<int> ans(q);
  for (int x = n; x > 0; x--) {
    for (int y : range[x])
      d.add(y, 1);
    for (P query : qs[x]) {
      int qy = query.first, qn = query.second;
      ans[qn] = (qy - x + 1) - d.sum(qy);
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

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

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

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

signed 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.r == b.r)
      return a.l < b.l;
    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';
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N, Q;
  cin >> N >> Q;
  vector<unsigned long> c(N);
  for (auto &&i : c) {
    cin >> i;
    --i;
  }
  vector<tuple<unsigned long, unsigned long, unsigned long>> queries(Q);
  {
    unsigned long cnt{0};
    for (auto &&[i, l, r] : queries) {
      cin >> l >> r;
      --l;
      i = cnt++;
    }
  }
  constexpr unsigned long B{700};
  sort(begin(queries), end(queries), [&B](const auto &lhs, const auto &rhs) {
    const auto &[i, li, ri] = lhs;
    const auto &[j, lj, rj] = rhs;
    return li / B == lj / B ? (li / B) & 1 ? ri > rj : ri < rj : li < lj;
  });
  unsigned long nl{0}, nr{0}, now{0};
  vector<unsigned long> cnt(N), ans(Q);
  for (const auto &[i, l, r] : queries) {
    while (nr < r)
      now += !cnt[c[nr++]]++;
    while (nr > r)
      now -= !--cnt[c[--nr]];
    while (nl < l)
      now -= !--cnt[c[nl++]];
    while (nl > l)
      now += !cnt[c[--nl]]++;
    ans[i] = now;
  }
  for (const auto &i : ans)
    cout << i << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

int main() {
  using namespace std;
  unsigned long N, Q;
  cin >> N >> Q;
  vector<unsigned long> c(N);
  for (auto &&i : c) {
    cin >> i;
    --i;
  }
  vector<tuple<unsigned long, unsigned long, unsigned long>> queries(Q);
  {
    unsigned long cnt{0};
    for (auto &&[i, l, r] : queries) {
      cin >> l >> r;
      --l;
      i = cnt++;
    }
  }
  constexpr unsigned long B{700};
  sort(begin(queries), end(queries), [&B](const auto &lhs, const auto &rhs) {
    const auto &[i, li, ri] = lhs;
    const auto &[j, lj, rj] = rhs;
    return li / B == lj / B ? ri < rj : li < lj;
  });
  unsigned long nl{0}, nr{0}, now{0};
  vector<unsigned long> cnt(N), ans(Q);
  for (const auto &[i, l, r] : queries) {
    if (nr > r) {
      nl = nr = now = 0;
      fill(begin(cnt), end(cnt), 0);
    }
    while (nr < r)
      now += !cnt[c[nr++]]++;
    while (nl < l)
      now -= !--cnt[c[nl++]];
    while (nl > l)
      now += !cnt[c[--nl]]++;
    ans[i] = now;
  }
  for (const auto &i : ans)
    cout << i << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

// https://codeforces.com/blog/entry/10315
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

typedef struct node {
  ll s, e, val, pos;
  bool operator<(const node &n) const {
    if (e != n.e)
      return e < n.e;
    return val > n.val;
  }
  node(){};
  node(ll ss, ll ee, ll v, ll p) { s = ss, e = ee, val = v, pos = p; }
} node;
#define max 2000007

ll occs[max], tree[max], n, ans[max];

void update(ll x, ll val) {
  while (x <= n) {
    tree[x] += val;
    x += x & -x;
  }
}

ll query(ll x) {
  ll ret = 0;
  while (x > 0) {
    ret += tree[x];
    x -= x & -x;
  }
  return ret;
}

int main() {
  ll q, s, e;
  vector<node> v;
  // scanf("%d", &n);
  cin >> n >> q;
  for (ll i = 1; i <= n; i++) {
    // scanf("%d", &s);
    cin >> s;

    v.push_back(node(i, i, s, -1));
  }
  for (ll i = 0; i < q; i++) {
    // scanf("%d %d", &s, &e);
    cin >> s >> e;
    v.push_back(node(s, e, 0, i));
  }

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

  for (ll i = 0; i < v.size(); i++) {
    if (v[i].val == 0) {
      ans[v[i].pos] = query(v[i].e) - query(v[i].s - 1);
    } else {
      if (occs[v[i].val] != 0)
        update(occs[v[i].val], -1);
      update(v[i].s, 1);
      occs[v[i].val] = v[i].s;
    }
  }

  for (ll i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
    // printf("%d\\n", ans[i]);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef int ll;
ll block = 555, yo = 0, aj[1000001], ans[300001];
ll ar[1000001];

struct node {
  ll l, r, p;
} q[300000];

bool cmp(node x, node y) {
  if (x.l / block != y.l / block) {
    return x.l / block < y.l / block;
  }
  return x.r < y.r;
}

void remove1(ll n) {
  aj[ar[n]]--;
  if (aj[ar[n]] == 0) {
    yo--;
  }
}

void add(ll n) {
  aj[ar[n]]++;
  if (aj[ar[n]] == 1) {
    yo++;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n, i, t;
  scanf("%d%d", &n, &t);
  for (i = 0; i < n; i++)
    scanf("%d", &ar[i]);

  for (i = 0; i < t; i++) {
    ll a, b;
    scanf("%d%d", &a, &b);
    q[i].l = --a;
    q[i].r = --b;
    q[i].p = i;
  }
  sort(q, q + t, cmp);
  /*  for(i=0;i<t;i++)
     cout<<q[i].l<<" "<<q[i].r<<endl; */
  ll Current_left = 0, Current_right = 0;
  for (i = 0; i < t; i++) {
    ll L = q[i].l, R = q[i].r;
    while (Current_left < L) {
      remove1(Current_left);
      Current_left++;
    }
    while (Current_left > L) {
      add(Current_left - 1);
      Current_left--;
    }
    while (Current_right <= R) {
      add(Current_right);
      Current_right++;
    }
    while (Current_right > R + 1) {
      remove1(Current_right - 1);
      Current_right--;
    }
    ans[q[i].p] = yo;
  }
  for (i = 0; i < t; i++)
    printf("%d\\n", ans[i]);
  return 0;
}