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

#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);
  scanf("%d", &q);

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

  int block_size = sqrt(n);

  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: 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;

#define ll long long
#define pll pair<ll, ll>

#define left 2 * node + 1
#define right left + 1
#define mid (s + e >> 1)

const ll N = 5e5 + 5;
vector<ll> T[4 * N];

void build(ll A[], ll node, ll s, ll e) {
  if (s == e) {
    T[node].push_back(A[s]);
    return;
  }
  build(A, left, s, mid);
  build(A, right, mid + 1, e);
  merge(T[left].begin(), T[left].end(), T[right].begin(), T[right].end(),
        back_inserter(T[node]));
}

ll query(ll node, ll s, ll e, ll x, ll y) {
  if (e < s || s > y || e < x)
    return 0;
  if (x <= s && e <= y) {
    ll a = upper_bound(T[node].begin(), T[node].end(), y) - T[node].begin();
    return T[node].size() - a;
  }
  ll p1 = query(left, s, mid, x, y);
  ll p2 = query(right, 1 + mid, e, x, y);
  return p1 + p2;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, q;
  cin >> n >> q;
  ll A[n];
  ll B[n];
  for (ll i = 0; i < n; i++)
    cin >> A[i];
  map<ll, ll> M;
  for (ll i = n - 1; i >= 0; i--) {
    if (M.count(A[i]))
      B[i] = M[A[i]];
    else
      B[i] = INT_MAX;
    M[A[i]] = i;
  }

  build(B, 0, 0, n - 1);

  while (q--) {
    ll x, y;
    cin >> x >> y;
    ll ans = query(0, 0, n - 1, x - 1, y - 1);
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

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

#define ll long long
#define pll pair<ll, ll>

#define left 2 * node + 1
#define right left + 1
#define mid (s + e >> 1)

const ll N = 5e5 + 5;
vector<ll> T[4 * N];

void build(ll A[], ll node, ll s, ll e) {
  if (s == e) {
    T[node].push_back(A[s]);
    return;
  }
  build(A, left, s, mid);
  build(A, right, mid + 1, e);
  merge(T[left].begin(), T[left].end(), T[right].begin(), T[right].end(),
        back_inserter(T[node]));
}

ll query(ll node, ll s, ll e, ll x, ll y) {
  if (e < s || s > y || e < x)
    return 0;
  if (x <= s && e <= y) {
    ll a = upper_bound(T[node].begin(), T[node].end(), y) - T[node].begin();
    return T[node].size() - a;
  }
  ll p1 = query(left, s, mid, x, y);
  ll p2 = query(right, 1 + mid, e, x, y);
  return p1 + p2;
}

int main() {

  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  ll n, q;
  cin >> n >> q;
  ll A[n];
  ll B[n];
  for (ll i = 0; i < n; i++)
    cin >> A[i];
  map<ll, ll> M;
  for (ll i = n - 1; i >= 0; i--) {
    if (M.count(A[i]))
      B[i] = M[A[i]];
    else
      B[i] = INT_MAX;
    M[A[i]] = i;
  }

  build(B, 0, 0, n - 1);

  while (q--) {
    ll x, y;
    cin >> x >> y;
    ll ans = query(0, 0, n - 1, x - 1, y - 1);
    cout << ans << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
const int M = 2002018, N = 2002018;
int n, m, bl[N], col[N], blo;
int ans, sum[N];
struct md {
  int A1, A2;
  int id, l, r;
} q[M];
bool cmp1(const md &a, const md &b) {
  return bl[a.l] == bl[b.l] ? a.r < b.r : a.l < b.l;
}
bool cmp2(const md &a, const md &b) { return a.id < b.id; }
void cg(int x, int add) {
  if (add == -1 && sum[col[x]] == 1)
    --ans;
  if (add == 1 && sum[col[x]] == 0)
    ++ans;
  sum[col[x]] += add;
}
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int main() {
  n = read();
  m = read();
  blo = sqrt(n) + 1;
  for (int i = 1; i <= n; i++) {
    col[i] = read();
    bl[i] = (i - 1) / blo + 1;
  }
  for (int i = 1; i <= m; i++)
    q[i].l = read(), q[i].r = read(), q[i].id = i;
  sort(q + 1, q + m + 1, cmp1);
  int l(1), r(0);
  for (int i = 1; i <= m; i++) {
    while (l < q[i].l)
      cg(l, -1), ++l;
    while (l > q[i].l)
      cg(l - 1, 1), --l;
    while (r < q[i].r)
      cg(r + 1, 1), ++r;
    while (r > q[i].r)
      cg(r, -1), --r;
    q[i].A1 = ans;
    //		q[i].A2=1LL*(q[i].r-q[i].l)*(q[i].r-q[i].l+1);
    //		LL gcd=getgcd(q[i].A1,q[i].A2);q[i].A1/=gcd;q[i].A2/=gcd;
  }
  sort(q + 1, q + m + 1, cmp2);
  for (int i = 1; i <= m; i++)
    printf("%d\\n", q[i].A1);
  return 0;
}
/*

*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#define LL long long
using namespace std;
const int M = 1002018, N = 1002018;
LL n, m, bl[N], col[N], blo;
LL ans, sum[N];
struct md {
  LL A1, A2;
  LL id, l, r;
} q[M];
bool cmp1(const md &a, const md &b) {
  return bl[a.l] == bl[b.l] ? a.r < b.r : a.l < b.l;
}
bool cmp2(const md &a, const md &b) { return a.id < b.id; }
LL cg(LL x, LL add) {
  if (add == -1 && sum[col[x]] == 1)
    --ans;
  if (add == 1 && sum[col[x]] == 0)
    ++ans;
  sum[col[x]] += add;
}
LL read() {
  LL x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int main() {
  n = read();
  m = read();
  blo = sqrt(n);
  for (int i = 1; i <= n; i++) {
    col[i] = read();
    bl[i] = (i - 1) / blo + 1;
  }
  for (int i = 1; i <= m; i++)
    q[i].l = read(), q[i].r = read(), q[i].id = i;
  sort(q + 1, q + m + 1, cmp1);
  LL l(1), r(0);
  for (int i = 1; i <= m; i++) {

    while (l < q[i].l)
      cg(l, -1), ++l;
    while (l > q[i].l)
      cg(l - 1, 1), --l;
    while (r < q[i].r)
      cg(r + 1, 1), ++r;
    while (r > q[i].r)
      cg(r, -1), --r;

    q[i].A1 = ans;
    //		q[i].A2=1LL*(q[i].r-q[i].l)*(q[i].r-q[i].l+1);
    //		LL gcd=getgcd(q[i].A1,q[i].A2);q[i].A1/=gcd;q[i].A2/=gcd;
  }
  sort(q + 1, q + m + 1, cmp2);
  for (int i = 1; i <= m; i++)
    printf("%lld\\n", q[i].A1);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (a); i > (n); i--)
typedef long long ll;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
using namespace std;

int block, curl, curr, res;
struct qry {
  int id, l, r;
} q[maxn];
int ans[maxn], a[maxn], mp[maxn];

void add(int pos) {
  if (mp[a[pos]] == 0) {
    res++;
    mp[a[pos]] = 1;
  } else {
    mp[a[pos]]++;
  }
}

void del(int pos) {
  if (mp[a[pos]] - 1 == 0) {
    mp[a[pos]] = 0;
    res--;
  } else {
    mp[a[pos]]--;
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  block = sqrt(n);
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 1, m + 1) cin >> q[i].l >> q[i].r, q[i].id = i;
  sort(q + 1, q + m + 1, [](qry x, qry y) {
    if (x.l / block == y.l / block)
      return x.r < y.r;
    return x.l / block < y.l / block;
  });
  curl = curr = res = 0;
  for (int i = 1; i <= m; i++) {
    while (curr > q[i].r)
      del(curr--);
    while (curr < q[i].r)
      add(++curr);
    while (curl > q[i].l)
      add(--curl);
    while (curl < q[i].l)
      del(curl++);
    ans[q[i].id] = res;
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << '\\n';
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef DEBUG
  freopen("C:/Users/Fish_Brother/Desktop/in", "r", stdin);
  // freopen("C:/Users/Fish_Brother/Desktop/out", "w", stdout);
#endif

  // int t; cin >> t; while(t--)
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define per(i, a, n) for (int i = (a); i > (n); i--)
typedef long long ll;
const int maxn = 5e5 + 5;
const int mod = 1e9 + 7;
using namespace std;

int block, curl, curr, res;
struct qry {
  int id, l, r;
} q[maxn];
int ans[maxn], a[maxn];

unordered_map<int, int> mp;

void add(int pos) {
  if (mp[a[pos]] == 0) {
    res++;
    mp[a[pos]] = 1;
  } else {
    mp[a[pos]]++;
  }
}

void del(int pos) {
  if (mp[a[pos]] - 1 == 0) {
    mp[a[pos]] = 0;
    res--;
  } else {
    mp[a[pos]]--;
  }
}

void solve() {
  ll n, m;
  cin >> n >> m;
  block = sqrt(n);
  rep(i, 1, n + 1) cin >> a[i];
  rep(i, 1, m + 1) cin >> q[i].l >> q[i].r, q[i].id = i;
  sort(q + 1, q + m + 1, [](qry x, qry y) {
    if (x.l / block == y.l / block)
      return x.r < y.r;
    return x.l / block < y.l / block;
  });
  curl = curr = res = 0;
  for (int i = 1; i <= m; i++) {
    while (curr > q[i].r)
      del(curr--);
    while (curr < q[i].r)
      add(++curr);
    while (curl > q[i].l)
      add(--curl);
    while (curl < q[i].l)
      del(curl++);
    ans[q[i].id] = res;
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << '\\n';
}

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifdef DEBUG
  freopen("C:/Users/Fish_Brother/Desktop/in", "r", stdin);
  // freopen("C:/Users/Fish_Brother/Desktop/out", "w", stdout);
#endif

  // int t; cin >> t; while(t--)
  solve();

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct BIT {
  int N;
  vector<int> bit;
  BIT(int x) {
    N = x;
    bit.resize(x + 1);
  }
  void add(int x, int y) {
    while (x <= N) {
      bit[x] += y;
      x += x & -x;
    }
  }
  int sum(int x) {
    int res = 0;
    while (x) {
      res += bit[x];
      x -= x & -x;
    }
    return res;
  }
  int sum(int l, int r) { return sum(r) - sum(l - 1); }
};
signed main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<int> cnt(N, -1);
  vector<vector<int>> lr(N);
  for (int i = 0; i < N; i++) {
    int l = cnt[c[i]];
    if (l != -1) {
      // l -> i
      lr[l].push_back(i);
    }
    cnt[c[i]] = i;
  }
  vector<vector<P>> q(N);
  // query
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    q[l].push_back({r, i});
  }
  BIT bt(N);
  vector<int> ans(Q);
  for (int i = N - 1; i >= 0; i--) {
    for (int y : lr[i]) {
      bt.add(y, 1);
    }
    for (P query : q[i]) {
      int r = query.first, l = query.second;
      ans[l] = (r - i + 1) - bt.sum(r);
    }
  }
  // 出力
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
signed main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++)
    cin >> c[i];
  vector<P> lr(Q);
  vector<P> cnt(Q);
  map<P, int> ans;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    lr[i].first = l;
    lr[i].second = r;
    cnt[i].first = r;
    cnt[i].second = -l;
  }
  sort(cnt.rbegin(), cnt.rend());
  map<int, int> Mp;
  int l = cnt[0].first, r = cnt[0].first + 1;
  for (int i = 0; i < Q; i++) {
    for (int j = l; j > (int)cnt[i].first; j--) {
      Mp[c[j]]--;
      if (Mp[c[j]] == 0) {
        Mp.erase(c[j]);
      }
    }
    if (r <= -cnt[i].second) {
      for (int j = r; j < (int)-cnt[i].second; j++) {
        Mp[c[j]]--;
        if (Mp[c[j]] == 0) {
          Mp.erase(c[j]);
        }
      }
    } else {
      for (int j = r - 1; j >= (int)-cnt[i].second; j--) {
        Mp[c[j]]++;
      }
    }
    l = cnt[i].first;
    r = -cnt[i].second;
    ans[{-cnt[i].second, cnt[i].first}] = (int)Mp.size();
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[{lr[i].first, lr[i].second}] << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct BIT {
  int N;
  vector<int> bit;
  BIT(int x) {
    N = x;
    bit.resize(x + 1);
  }
  void add(int x, int y) {
    while (x <= N) {
      bit[x] += y;
      x += x & -x;
    }
  }
  int sum(int x) {
    int res = 0;
    while (x) {
      res += bit[x];
      x -= x & -x;
    }
    return res;
  }
  int sum(int l, int r) { return sum(r + 1) - sum(l); }
};
signed main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<int> ans(Q);
  vector<vector<P>> cnt(N);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cnt[r].push_back({l, i});
  }
  BIT bt(N);
  vector<int> res(N, -1);
  for (int i = 0; i < N; i++) {
    if (res[c[i]] != -1) {
      bt.add(res[c[i]], -1);
    }
    bt.add(i + 1, 1);
    res[c[i]] = i + 1;
    for (int j = 0; j < cnt[i].size(); j++) {
      ans[cnt[i][j].second] = bt.sum(cnt[i][j].first, i);
    }
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
int INF = 1e9 + 7;
int mod = 998244353;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct BIT {
  int N;
  vector<int> bit;
  BIT(int x) {
    N = x;
    bit.resize(x + 1);
  }
  void add(int x, int y) {
    while (x <= N) {
      bit[x] += y;
      x += x & -x;
    }
  }
  int sum(int x) {
    int res = 0;
    while (x) {
      res += bit[x];
      x -= x & -x;
    }
    return res;
  }
  int sum(int l, int r) { return sum(r) - sum(l); }
};
signed main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  vector<int> ans(Q);
  vector<vector<P>> cnt(N);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cnt[r].push_back({l, i});
  }
  BIT bt(N);
  vector<int> res(N, -1);
  for (int i = 0; i < N; i++) {
    if (res[c[i]] != -1) {
      bt.add(res[c[i]], -1);
    }
    bt.add(i, 1);
    res[c[i]] = i;
    for (int j = 0; j < cnt[i].size(); j++) {
      ans[cnt[i][j].second] = bt.sum(cnt[i][j].first, i);
    }
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;
const double eps = 1e-8;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

template <typename T> class BIT {
public:
  int n;
  vector<T> nodes;
  BIT(int _n) : n(_n), nodes(_n + 1, 0) {}
  void add(int i, T v) {
    for (++i; i <= n; i += i & (-i))
      nodes[i] += v;
  }
  T sum(int i) {
    T res = 0;
    for (++i; i > 0; i -= i & (-i))
      res += nodes[i];
    return res;
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; ++i)
    cin >> c[i];
  vector<tuple<int, int, int, int>> lr; // (l, r, index, answer)
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    lr.emplace_back(l, r, i, 0);
  }

  sort(lr.begin(), lr.end(),
       [](auto a, auto b) { return get<1>(a) < get<1>(b); });

  map<int, int> last; // (color, index)
  BIT<int> indices(N + 10);

  int k = 0;
  for (int i = 0; i < Q; ++i) {
    while (k < get<1>(lr[i])) {
      if (last.count(c[k])) {
        indices.add(last[c[k]], -1);
      }
      last[c[k]] = k + 1;
      indices.add(last[c[k]], 1);
      k++;
    }
    get<3>(lr[i]) = indices.sum(get<1>(lr[i])) - indices.sum(get<0>(lr[i]) - 1);
  }

  sort(lr.begin(), lr.end(),
       [](auto a, auto b) { return get<2>(a) < get<2>(b); });

  for (int i = 0; i < Q; ++i) {
    cout << get<3>(lr[i]) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll mod = 1000000007;
const double eps = 1e-8;

#ifdef DEBUG
#include "inc/debug.hpp"
#else
#define debug(...) 42
#endif

template <typename T> class BIT {
public:
  int n;
  vector<T> nodes;
  BIT(int _n) : n(_n), nodes(_n + 1, 0) {}
  void add(int i, T v) {
    for (++i; i <= n; i += i & (-i))
      nodes[i] += v;
  }
  T sum(int i) {
    T res = 0;
    for (++i; i > 0; i -= i & (-i))
      res += nodes[i];
    return res;
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; ++i)
    cin >> c[i];
  vector<pair<int, int>> lr(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> lr[i].first >> lr[i].second;
  }

  auto lr2 = lr;
  sort(lr2.begin(), lr2.end(),
       [](auto a, auto b) { return a.second < b.second; });

  map<int, int> last; // (color, index)
  BIT<int> indices(N + 10);
  // debug(indices.nodes);

  map<pair<int, int>, int> ans;

  int k = 0;
  for (int i = 0; i < Q; ++i) {
    while (k < lr2[i].second) {
      if (last.count(c[k])) {
        indices.add(last[c[k]], -1);
      }
      last[c[k]] = k + 1;
      indices.add(last[c[k]], 1);
      k++;
      // debug(indices.nodes);
    }
    ans[lr2[i]] = indices.sum(lr2[i].second) - indices.sum(lr2[i].first - 1);
  }

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

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

#pragma GCC optimize("O3")
#include <algorithm>
#include <functional>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kBlk = 710;

int c[kN], cnt[kN], ans[kN];
pair<int, int> p[kN];
vector<int> v[kBlk];

int main() {
  int n, q, now = 0, lpos = 0, rpos = -1;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= q; i++)
    scanf("%d%d", &p[i].F, &p[i].S);
  for (int i = 1; i <= q; i++)
    v[p[i].F / kBlk].PB(i);

  for (int i = 0; i < kBlk; i++) {
    sort(v[i].begin(), v[i].end(), [&](int a, int b) {
      return p[a].S == p[b].S ? p[a].F < p[b].F : p[a].S < p[b].S;
    });
    // now = 0, lpos = kBlk * i, rpos = kBlk * i - 1;
    for (int j : v[i]) {
      while (rpos < p[j].S)
        if (cnt[c[++rpos]]++ == 0)
          now++;
      while (lpos > p[j].F)
        if (cnt[c[--lpos]]++ == 0)
          now++;
      while (lpos < p[j].F)
        if (--cnt[c[lpos++]] == 0)
          now--;
      while (rpos > p[j].S)
        if (--cnt[c[rpos--]] == 0)
          now--;
      ans[j] = now;
    }
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <stdio.h>
#include <utility>
#include <vector>
using namespace std;
#define PB push_back
#define F first
#define S second
constexpr int kN = int(5E5 + 10), kBlk = 710;

int c[kN], cnt[kN], ans[kN];
pair<int, int> p[kN];
vector<pair<int, int>> v[kBlk];

int main() {
  int n, q, now = 0, lpos = 0, rpos = -1;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  for (int i = 1; i <= q; i++)
    scanf("%d%d", &p[i].F, &p[i].S);
  for (int i = 1; i <= q; i++)
    v[p[i].F / kBlk].PB({p[i].S, i});
  function<void(int)> insert = [&](int x) {
    if (cnt[c[x]]++ == 0)
      now++;
    return;
  };
  function<void(int)> remove = [&](int x) {
    if (--cnt[c[x]] == 0)
      now--;
    return;
  };

  for (int i = 0; i < kBlk; i++) {
    sort(v[i].begin(), v[i].end());
    for (pair<int, int> j : v[i]) {
      while (rpos < j.F)
        insert(++rpos);
      while (lpos > p[j.S].F)
        insert(--lpos);
      while (lpos < p[j.S].F)
        remove(lpos++);
      while (rpos > j.F)
        remove(rpos--);
      ans[j.S] = now;
    }
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ans[i]);
}