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

#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 710
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[500001];
int ar[500001], ans[500001];
int fre[500001];
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 >> 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);

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


Error: TLE

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 555
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[500001];
int ar[500001], ans[500001];
int fre[500001];
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 >> 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);

  // 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: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 3e7 + 10;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x;
}
void write(int x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + 48);
}

struct Node {
  int sum, lc, rc;
};
Node dat[N];
int tot;

int insert(int pos, int v, int rt, int l, int r) {
  int p = ++tot;
  dat[p] = dat[rt];
  if (r - l == 1) {
    dat[p].sum += v;
    return p;
  }
  int m = (l + r) >> 1;
  if (pos < m)
    dat[p].lc = insert(pos, v, dat[rt].lc, l, m);
  else
    dat[p].rc = insert(pos, v, dat[rt].rc, m, r);
  dat[p].sum = dat[dat[p].lc].sum + dat[dat[p].rc].sum;
  return p;
}

int query(int left, int rt, int l, int r) {
  if (left <= l)
    return dat[rt].sum;
  int m = (l + r) >> 1, sum = 0;
  if (left < m)
    sum += query(left, dat[rt].lc, l, m);
  sum += query(left, dat[rt].rc, m, r);
  return sum;
}
int last[N], root[N];

int main() {
  int n, q;
  n = read();
  q = read();
  for (int i = 1; i <= n; ++i) {
    int x = read();
    if (last[x]) {
      root[i] = insert(last[x], -1, root[i - 1], 1, n + 1);
      root[i] = insert(i, 1, root[i], 1, n + 1);
    } else
      root[i] = insert(i, 1, root[i - 1], 1, n + 1);
    last[x] = i;
  }
  while (q--) {
    int x, y;
    x = read();
    y = read();
    write(query(x, root[y], 1, n + 1));
    putchar('\\n');
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int N = 6e6 + 10;
inline int read() {
  int x = 0;
  char c = getchar();
  while (c < '0' || c > '9')
    c = getchar();
  while (c >= '0' && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return x;
}
void write(int x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + 48);
}

struct Node {
  int sum, lc, rc;
};
Node dat[N];
int tot;

int insert(int pos, int v, int rt, int l, int r) {
  int p = ++tot;
  dat[p] = dat[rt];
  if (r - l == 1) {
    dat[p].sum += v;
    return p;
  }
  int m = (l + r) >> 1;
  if (pos < m)
    dat[p].lc = insert(pos, v, dat[rt].lc, l, m);
  else
    dat[p].rc = insert(pos, v, dat[rt].rc, m, r);
  dat[p].sum = dat[dat[p].lc].sum + dat[dat[p].rc].sum;
  return p;
}

int query(int left, int rt, int l, int r) {
  if (left <= l)
    return dat[rt].sum;
  int m = (l + r) >> 1, sum = 0;
  if (left < m)
    sum += query(left, dat[rt].lc, l, m);
  sum += query(left, dat[rt].rc, m, r);
  return sum;
}
int last[N], root[N];

int main() {
  int n, q;
  n = read();
  q = read();
  for (int i = 1; i <= n; ++i) {
    int x = read();
    if (last[x]) {
      root[i] = insert(last[x], -1, root[i - 1], 1, n + 1);
      root[i] = insert(i, 1, root[i], 1, n + 1);
    } else
      root[i] = insert(i, 1, root[i - 1], 1, n + 1);
    last[x] = i;
  }
  while (q--) {
    int x, y;
    x = read();
    y = read();
    write(query(x, root[y], 1, n + 1));
    putchar('\\n');
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int blk;
struct node {
  int left, right;
  int index;
};
static bool compare(node &p1, node &p2) {
  if (p1.left / blk == p2.left / blk)
    return p1.right < p2.right;
  return p1.left < p2.left;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  blk = sqrt(n);
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<node> q;
  q.resize(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    node p;
    p.left = l, p.right = r, p.index = i;
    q[i] = p;
  }
  sort(q.begin(), q.end(), compare);
  vector<int> freq(500001, 0);
  vector<int> ans(m, 0);
  int ml = 0, mr = -1, count = 0;
  for (int i = 0; i < m; i++) {
    int l = q[i].left - 1, r = q[i].right - 1;
    int ind = q[i].index;
    while (ml > l)
      if (++freq[a[--ml]] == 1)
        count++;
    while (mr < r)
      if (++freq[a[++mr]] == 1)
        count++;
    while (ml < l)
      if (--freq[a[ml++]] == 0)
        count--;
    while (mr > r)
      if (--freq[a[mr--]] == 0)
        count--;
    ans[ind] = count;
  }
  for (auto &i : ans)
    cout << i << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int blk;
struct node {
  int left, right;
  int index;
};
static bool compare(node &p1, node &p2) {
  if (p1.left / blk == p2.left / blk)
    return p1.right < p2.right;
  return p1.left < p2.left;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  blk = sqrt(n);
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<node> q;
  q.resize(m);
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    node p;
    p.left = l, p.right = r, p.index = i;
    q[i] = p;
  }
  sort(q.begin(), q.end(), compare);
  unordered_map<int, int> freq;
  vector<int> ans(m, 0);
  int ml = 0, mr = -1, count = 0;
  for (int i = 0; i < m; i++) {
    int l = q[i].left - 1, r = q[i].right - 1;
    int ind = q[i].index;
    while (ml > l)
      if (++freq[a[--ml]] == 1)
        count++;
    while (mr < r)
      if (++freq[a[++mr]] == 1)
        count++;
    while (ml < l)
      if (--freq[a[ml++]] == 0)
        count--;
    while (mr > r)
      if (--freq[a[mr--]] == 0)
        count--;
    ans[ind] = count;
  }
  for (auto &i : ans)
    cout << i << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#pragma gcc optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> c(n), la(n, -1), ans(q), S(n << 1);
  for (int i = 0; i < n; ++i)
    cin >> c[i], --c[i];
  vector<vector<pair<int, int>>> Q(n);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r, --l, --r;
    Q[r].emplace_back(l, i);
  }
  auto upd = [&](int i, int v) {
    for (S[i += n] = v; i >>= 1;)
      S[i] = S[i << 1] + S[i << 1 | 1];
  };
  auto query = [&](int l, int r) {
    int res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res += S[l++];
      if (r & 1)
        res += S[--r];
    }
    return res;
  };
  for (int i = 0; i < n; ++i) {
    if (la[c[i]] > -1)
      upd(la[c[i]], 0);
    la[c[i]] = i;
    upd(i, 1);
    for (auto &o : Q[i]) {
      auto &[l, id] = o;
      ans[id] = query(l, i + 1);
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << '\\n';
}

Error: 0

Fixed submission:

#pragma gcc optimize("Ofast")
#include "bits/stdc++.h"
using namespace std;

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> c(n), la(n, -1), ans(n), S(n << 1);
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  vector<vector<pair<int, int>>> Q(n);
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r, --l, --r;
    Q[r].emplace_back(l, i);
  }
  auto upd = [&](int i, int v) {
    for (S[i += n] = v; i >>= 1;)
      S[i] = S[i << 1] + S[i << 1 | 1];
  };
  auto query = [&](int l, int r) {
    int res = 0;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res += S[l++];
      if (r & 1)
        res += S[--r];
    }
    return res;
  };
  for (int i = 0; i < n; ++i) {
    if (la[c[i]] > -1)
      upd(la[c[i]], 0);
    la[c[i]] = i;
    upd(i, 1);
    for (auto &o : Q[i]) {
      auto &[l, id] = o;
      ans[id] = query(l, i + 1);
    }
  }
  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;
int temp[500004], a[500004];
int belong[500004], block, n, m;
int res[500004];
struct Ds {
  int l, r, sit;
  bool operator<(const Ds &b) {
    return (belong[l] ^ belong[b.l]) ? belong[l] < belong[b.l]
                                     : ((belong[l] & 1) ? r < b.r : r > b.r);
  }
} q[500004];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  block = sqrt(n);
  for (int i = 1; i <= n; i++)
    belong[i] = (i - 1) / block + 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].sit = i;
  }
  sort(q + 1, q + m + 1);
  int l = 1, r = 0, ans = 0;
  for (int i = 1; i <= m; i++) {
    while (l < q[i].l)
      ans -= (!--temp[a[l++]]);
    while (l > q[i].l)
      ans += (!temp[a[--l]]++);
    while (r < q[i].r)
      ans += (!temp[a[++r]]++);
    while (r > q[i].r)
      ans -= (!--temp[a[r--]]);
    res[q[i].sit] = ans;
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", res[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int temp[500004], a[500004];
int belong[500004], block, n, m;
int res[500004];
struct Ds {
  int l, r, sit;
  bool operator<(const Ds &b) {
    return (belong[l] ^ belong[b.l]) ? belong[l] < belong[b.l]
                                     : ((belong[l] & 1) ? r < b.r : r > b.r);
  }
} q[200004];
void solve() {
  int l = 1, r = 0, ans = 0;
  for (int i = 1; i <= m; i++) {
    while (l < q[i].l)
      ans -= (!--temp[a[l++]]);
    while (l > q[i].l)
      ans += (!temp[a[--l]]++);
    while (r < q[i].r)
      ans += (!temp[a[++r]]++);
    while (r > q[i].r)
      ans -= (!--temp[a[r--]]);
    res[q[i].sit] = ans;
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  block = 3e3;
  for (int i = 1; i <= n; i++)
    belong[i] = (i - 1) / block + 1;
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].sit = i;
  }
  sort(q + 1, q + m + 1);
  solve();
  for (int i = 1; i <= m; i++)
    printf("%d\\n", res[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[500001];
int n, m, a[500001], ans[500001];
int st[25 * 100001], used[5000001];
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() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &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++)
    printf("%d\\n", ans[i]);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
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() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &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++)
    printf("%d\\n", ans[i]);

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

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

int n, q, bs;
int a[500005];
pair<pair<int, int>, int> qs[500005];
int ans[5000005];
int mp[5000005], cnt;

int main() {

  scanf("%d%d", &n, &q);
  bs = sqrt(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &qs[i].first.first, &qs[i].first.second);
    qs[i].first.first--;
    qs[i].first.second--;
    qs[i].second = i;
  }
  sort(qs, qs + q,
       [](const pair<pair<int, int>, int> &i,
          const pair<pair<int, int>, int> &j) {
         if (i.first.first / bs != j.first.first / bs)
           return i.first.first / bs < j.first.first / bs;
         return i.first.second < j.first.second;
       });
  int l = 0, r = -1;
  for (int i = 0; i < q; i++) {
    const pair<pair<int, int>, int> &p = qs[i];
    while (r < p.first.second) {
      r++;
      mp[a[r]]++;
      cnt += mp[a[r]] == 1;
    }
    while (l > p.first.first) {
      l--;
      mp[a[l]]++;
      cnt += mp[a[l]] == 1;
    }
    while (l < p.first.first) {
      mp[a[l]]--;
      cnt -= mp[a[l]] == 0;
      l++;
    }
    while (r > p.first.second) {
      mp[a[r]]--;
      cnt -= mp[a[r]] == 0;
      r--;
    }
    ans[p.second] = cnt;
  }
  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int n, q, bs;
int a[500005];
pair<pair<int, int>, int> qs[500005];
int ans[5000005];

int main() {

  scanf("%d%d", &n, &q);
  bs = sqrt(n);
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < q; i++) {
    scanf("%d%d", &qs[i].first.first, &qs[i].first.second);
    qs[i].first.first--;
    qs[i].first.second--;
    qs[i].second = i;
  }
  sort(qs, qs + q,
       [](const pair<pair<int, int>, int> &i,
          const pair<pair<int, int>, int> &j) {
         if (i.first.first / bs != j.first.first / bs)
           return i.first.first / bs < j.first.first / bs;
         return i.first.second < j.first.second;
       });
  int l = 0, r = -1;
  map<int, int> mp;
  for (int i = 0; i < q; i++) {
    const pair<pair<int, int>, int> &p = qs[i];
    while (r < p.first.second) {
      r++;
      mp[a[r]]++;
    }
    while (l > p.first.first) {
      l--;
      mp[a[l]]++;
    }
    while (l < p.first.first) {
      mp[a[l]]--;
      if (mp[a[l]] <= 0)
        mp.erase(a[l]);
      l++;
    }
    while (r > p.first.second) {
      mp[a[r]]--;
      if (mp[a[r]] <= 0)
        mp.erase(a[r]);
      r--;
    }
    ans[p.second] = mp.size();
  }
  for (int i = 0; i < q; i++) {
    printf("%d\\n", ans[i]);
  }

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

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

struct BIT {
  vector<int> array;
  int n;

  BIT(int _n) : array(_n + 1, 0), n(_n) {}

  // [1, i] の和
  int sum(int i) {
    int ans = 0;
    while (i != 0) {
      ans += array[i];
      i -= i & (-i);
    }
    return ans;
  }

  // [i, j] の和
  int sum(int i, int j) { return sum(j) - sum(i - 1); }

  // i に x を加える
  void add(int i, int x) {
    while (i <= n) {
      array[i] += x;
      i += i & (-i);
    }
  }
};

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int n, q;
  cin >> n >> q;

  vector<int> c(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }

  vector<tuple<int, int, int>> rl(q);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    rl[i] = make_tuple(r, l, i);
  }
  sort(rl.begin(), rl.end());

  vector<int> ans(q);

  BIT bit(n);
  int prevr = 0;
  // 同色で一番右にあるものの場所
  map<int, int> mp;
  for (int i = 0; i < q; i++) {
    for (int rr = prevr + 1; rr <= get<0>(rl[i]); rr++) {
      if (mp[c[rr]] != 0) {
        bit.add(mp[c[rr]], -1);
      }
      bit.add(rr, 1);
      mp[c[rr]] = rr;
    }
    ans[get<2>(rl[i])] = bit.sum(get<1>(rl[i]), get<0>(rl[i]));
    prevr = get<0>(rl[i]);
  }

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

  return 0;
}

Error: TLE

Fixed submission:

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

bool isprime(int n) {
  double rootn = sqrt(n);
  if (n < 2) {
    return false;
  } else if (n == 2) {
    return true;
  } else if (n % 2 == 0) {
    return false;
  } else {
    for (int i = 3; i <= rootn; i += 2) {
      if (n % i == 0) {
        return false;
      }
    }
    return true;
  }
}

// 空のベクトルを二つ渡す。N (>=2) 以下の素数を primes に順番に入れる。
void prime_vectors(int N, vector<int> &primes, vector<bool> &is_prime) {
  primes = {};
  is_prime = {};
  for (int i = 0; i <= N; i++) {
    is_prime.push_back(true);
  }

  primes.push_back(2);
  for (int j = 4; j <= N; j += 2) {
    is_prime[j] = false;
  }

  int i;
  for (i = 3; i * i <= N; i += 2) {
    if (!is_prime[i])
      continue;
    primes.push_back(i);
    for (int j = 2 * i; j <= N; j += i) {
      is_prime[j] = false;
    }
  }

  for (; i <= N; i += 2) {
    if (is_prime[i]) {
      primes.push_back(i);
    }
  }
}

signed main() {
  // cout << fixed << setprecision(10) << flush;

  int N, Q;
  cin >> N >> Q;
  int c[N + 1];
  vector<map<int, int>> v_mp;
  for (int i = 0; i <= N; i++) {
    v_mp.push_back({});
  }

  for (int i = 1; i <= N; i++) {
    cin >> c[i];
    for (int j = i; j <= N; j++) {
      v_mp[j][c[i]] = v_mp[j][c[i]] + 1;
    }
  }

  int l[Q], r[Q];

  for (int i = 0; i < Q; i++) {
    cin >> l[i] >> r[i];
  }
  for (int i = 0; i < Q; i++) {
    int cnt = 0;
    for (auto m : v_mp[r[i]]) {
      if (m.second - v_mp[l[i] - 1][m.first] > 0) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}