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

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int nax = 5e5 + 1;
vector<int> cnt(nax);

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<pair<int, int>, int>> vec(q);
  for (int i = 0; i < q; i++) {
    cin >> vec[i].first.first >> vec[i].first.second;
    vec[i].first.first--;
    vec[i].first.second--;
    vec[i].second = i;
  }
  int blk = sqrt(n);
  sort(vec.begin(), vec.end(),
       [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
         if ((x.first.first / blk) != (y.first.first / blk))
           return (x.first.first / blk) < (y.first.first / blk);
         else
           return x.first.second < y.first.second;
       });
  int l = 0, r = -1;
  vector<int> an(q);
  int ans = 0;
  auto add = [&](int id) {
    cnt[a[id]]++;
    if (cnt[a[id]] == 1)
      ans++;
  };

  auto sub = [&](int id) {
    cnt[a[id]]--;
    if (cnt[a[id]] == 0)
      ans--;
  };

  for (int i = 0; i < q; i++) {
    while (r < vec[i].first.second) {
      r++;
      add(r);
    }
    while (l < vec[i].first.first) {

      sub(l);
      l++;
    }
    while (r > vec[i].first.second) {
      sub(r);
      r--;
    }

    while (l > vec[i].first.first) {
      l--;
      add(l);
    }
    // if(l==r) ans--;
    an[vec[i].second] = ans;
    // cout<<ans<<endl;
  }
  for (auto it : an)
    cout << it << "\\n";
  ;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int nax = 5e5 + 1;
vector<int> cnt(nax);
int blk = 707;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<pair<int, int>, int>> vec(q);
  for (int i = 0; i < q; i++) {
    cin >> vec[i].first.first >> vec[i].first.second;
    vec[i].first.first--;
    vec[i].first.second--;
    vec[i].second = i;
  }

  sort(vec.begin(), vec.end(),
       [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
         if ((x.first.first / blk) != (y.first.first / blk))
           return (x.first.first / blk) < (y.first.first / blk);
         else
           return x.first.second < y.first.second;
       });
  int l = 0, r = -1;
  vector<int> an(q);
  int ans = 0;
  auto add = [&](int id) {
    cnt[a[id]]++;
    if (cnt[a[id]] == 1)
      ans++;
  };

  auto sub = [&](int id) {
    cnt[a[id]]--;
    if (cnt[a[id]] == 0)
      ans--;
  };

  for (int i = 0; i < q; i++) {
    while (r < vec[i].first.second) {
      r++;
      add(r);
    }
    while (l < vec[i].first.first) {

      sub(l);
      l++;
    }
    while (r > vec[i].first.second) {
      sub(r);
      r--;
    }

    while (l > vec[i].first.first) {
      l--;
      add(l);
    }
    // if(l==r) ans--;
    an[vec[i].second] = ans;
    // cout<<ans<<endl;
  }
  for (auto it : an)
    cout << it << endl;
  ;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

struct {
  int i, p;
} E[500005];
int A[500005], D[500005], P[500005]{}, F[500005]{}, T[500005]{}, N, Q;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  cin >> N >> Q;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }
  int K = 0;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    E[i + 1] = {l, F[r]}, F[r] = i + 1;
  }
  for (int r = 0; r < N; r++) {
    int x = A[r] - 1;
    if (P[x]) {
      for (int j = P[x]; j <= N; j += j & -j) {
        T[j]--;
      }
    } else {
      K++;
    }
    for (int j = P[x] = r + 1; j <= N; j += j & -j) {
      T[j]++;
    }
    for (int k = F[r]; k; k = E[k].p) {
      D[k] = K;
      for (int j = E[k].i; j; j -= j & -j) {
        D[k] -= T[j];
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    cout << D[i + 1] << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define inf 1000000000
typedef long long ll;
typedef pair<int, int> Pair;
typedef pair<Pair, Pair> PPair;

typedef struct {
  int l, r, id;
} Q;
int sz;
int arr[500005];
int cnt[500005];
int dis = 0;
int ans[500005];
bool cmp(Q a, Q b) {
  return make_tuple(a.r / sz, a.l, a.id) < make_tuple(b.r / sz, b.l, b.id);
}

void add(int i) {
  if (++cnt[i] == 1)
    dis++;
}
void erase(int i) {
  if (--cnt[i] == 0)
    dis--;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  vector<Q> v;
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    v.push_back({l - 1, r - 1, i});
  }
  sz = sqrt(n);
  sort(v.begin(), v.end(), cmp);
  int l = 0, r = 0;
  add(arr[0]);
  for (int i = 0; i < m; i++) {
    int nl = v[i].l, nr = v[i].r;
    for (int j = l; j < nl; j++)
      erase(arr[j]);
    for (int j = l - 1; j >= nl; j--)
      add(arr[j]);
    for (int j = r + 1; j <= nr; j++)
      add(arr[j]);
    for (int j = r; j > nr; j--)
      erase(arr[j]);
    l = nl;
    r = nr;
    ans[v[i].id] = dis;
  }
  for (int i = 0; i < m; i++) {
    cout << ans[i] << '\\n';
  }
  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;
const int N = 500009;
pair<int, pair<int, int>> q[N];
int n, m, a[N], ans[N];
int st[5 * N], used[5 * N];
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>
#define ll long long
#define pb push_back
using namespace std;
const int nax = 5e5 + 1;
vector<int> cnt(nax);
int blk = 707;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<pair<int, int>, int>> vec(q);
  for (int i = 0; i < q; i++) {
    cin >> vec[i].first.first >> vec[i].first.second;
    vec[i].first.first--;
    vec[i].first.second--;
    vec[i].second = i;
  }

  sort(vec.begin(), vec.end(),
       [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
         if ((x.first.first / blk) != (y.first.first / blk))
           return (x.first.first / blk) < (y.first.first / blk);
         else
           return x.first.second < y.first.second;
       });
  int l = 0, r = -1;
  vector<int> an(q);
  int ans = 0;
  auto add = [&](int id) {
    cnt[a[id]]++;
    if (cnt[a[id]] == 1)
      ans++;
  };

  auto sub = [&](int id) {
    cnt[a[id]]--;
    if (cnt[a[id]] == 0)
      ans--;
  };

  for (int i = 0; i < q; i++) {
    while (r < vec[i].first.second) {
      r++;
      add(r);
    }
    while (l < vec[i].first.first) {

      sub(l);
      l++;
    }
    while (r > vec[i].first.second) {
      sub(r);
      r--;
    }

    while (l > vec[i].first.first) {
      l--;
      add(l);
    }
    // if(l==r) ans--;
    an[vec[i].second] = ans;
    // cout<<ans<<endl;
  }
  for (auto it : an)
    cout << it << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int nax = 5e5 + 123;
vector<int> cnt(nax);
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  vector<pair<pair<int, int>, int>> vec(q);
  for (int i = 0; i < q; i++) {
    cin >> vec[i].first.first >> vec[i].first.second;
    vec[i].first.first--;
    vec[i].first.second--;
    vec[i].second = i;
  }
  int blk = sqrt(n);
  sort(vec.begin(), vec.end(),
       [&](pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
         if ((x.first.first / blk) != (y.first.first / blk))
           return (x.first.first / blk) < (y.first.first / blk);
         else
           return x.first.second < y.first.second;
       });
  int l = 0, r = -1;
  vector<int> an(q);
  int ans = 0;
  auto add = [&](int id) {
    cnt[a[id]]++;
    if (cnt[a[id]] == 1)
      ans++;
  };

  auto sub = [&](int id) {
    cnt[a[id]]--;
    if (cnt[a[id]] == 0)
      ans--;
  };

  for (int i = 0; i < q; i++) {
    while (r < vec[i].first.second) {
      r++;
      add(r);
    }
    while (l < vec[i].first.first) {

      sub(l);
      l++;
    }
    while (r > vec[i].first.second) {
      sub(r);
      r--;
    }

    while (l > vec[i].first.first) {
      l--;
      add(l);
    }
    // if(l==r) ans--;
    an[vec[i].second] = ans;
    // cout<<ans<<endl;
  }
  for (auto it : an)
    cout << it << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> P_;
int N, Q;
int c[500005];
int last[500005];
int a[500005];
vector<P_> p;

template <typename T> struct bit {
  int n;
  vector<T> data;

  bit(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    data = vector<T>(n + 1);
    for (int i = 0; i <= n; i++)
      data[i] = 0;
  }

  T sum(int i) {
    T ret = 0;
    while (i > 0) {
      ret += data[i];
      i -= i & -i;
    }
    return ret;
  }

  void add(int i, T x) {
    while (i <= n) {
      data[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N >> Q;
  for (int i = 1; i <= N; i++) {
    cin >> c[i];
    c[i]--;
    last[c[i]] = -1;
  }
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    p.push_back(P_(P(r, l), i));
  }
  sort(p.begin(), p.end());
  bit<int> bt(N);
  int pr = 0;
  for (auto q : p) {
    int l = q.first.second;
    int r = q.first.first;
    int idx = q.second;
    for (int i = pr + 1; i <= r; i++) {
      if (last[c[i]] != -1) {
        bt.add(last[c[i]], -1);
      }
      bt.add(i, 1);
      last[c[i]] = i;
    }
    a[idx] = bt.sum(r) - bt.sum(l - 1);
    pr = r;
  }
  for (int i = 0; i < Q; i++)
    cout << a[i] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> P_;
int N, Q;
int c[500000];
vector<P_> p[800];
int cnt[500000];
int a[500000];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << setprecision(10) << fixed;
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }
  int sq = sqrt(N);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    p[l / sq].push_back(P_(P(r, l), i));
  }
  for (int i = 0; i <= N / sq + 1; i++) {
    sort(p[i].begin(), p[i].end());
    int pl = 0, pr = -1;
    for (int j = 0; j < N; j++)
      cnt[j] = 0;
    int ans = 0;
    for (auto q : p[i]) {
      int l = q.first.second;
      int r = q.first.first;
      int idx = q.second;
      if (l < pl) {
        for (int j = pl - 1; j >= l; j--) {
          if (cnt[c[j]] == 0)
            ans++;
          cnt[c[j]]++;
        }
      }
      if (l > pl) {
        for (int j = pl; j < l; j++) {
          if (cnt[c[j]] == 1)
            ans--;
          cnt[c[j]]--;
        }
      }
      for (int j = pr + 1; j <= r; j++) {
        if (cnt[c[j]] == 0)
          ans++;
        cnt[c[j]]++;
      }
      a[idx] = ans;
      pl = l;
      pr = r;
    }
  }
  for (int i = 0; i < Q; i++)
    cout << a[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int BLOCK = 707;

int c[500005], ans[500005], res, cnt[500005];

struct dat {
  int l, r, id;

  bool operator<(const dat &T) const {
    if (l / BLOCK != T.l / BLOCK)
      return l / BLOCK < T.l / BLOCK;
    return r < T.r;
  }
} a[500005];

void remove(int id) {
  cnt[c[id]]--;
  if (cnt[c[id]] == 0)
    res--;
}

void add(int id) {
  cnt[c[id]]++;
  if (cnt[c[id]] == 1)
    res++;
}

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

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

  for (int i = 0; i < q; i++) {
    scanf("%d%d", &a[i].l, &a[i].r);
    a[i].l--;
    a[i].r--;
    a[i].id = i;
  }

  sort(a, a + q);

  int l = 0;
  int r = -1;
  res = 0;

  for (int i = 0; i < q; i++) {
    while (l < a[i].l)
      remove(l++);
    while (l > a[i].l)
      add(--l);
    while (r < a[i].r)
      add(++r);
    while (r > a[i].r)
      remove(r--);

    ans[a[i].id] = res;
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int BLOCK = 707;

int c[500005], ans[500005], res, cnt[500005];

struct dat {
  int l, r, id;

  bool operator<(const dat &T) const {
    if (l / BLOCK != T.l / BLOCK)
      return l / BLOCK < T.l / BLOCK;
    return r / BLOCK < T.r / BLOCK;
  }
} a[500005];

void remove(int id) {
  cnt[c[id]]--;
  if (cnt[c[id]] == 0)
    res--;
}

void add(int id) {
  cnt[c[id]]++;
  if (cnt[c[id]] == 1)
    res++;
}

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

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

  for (int i = 0; i < q; i++) {
    scanf("%d%d", &a[i].l, &a[i].r);
    a[i].l--;
    a[i].r--;
    a[i].id = i;
  }

  sort(a, a + q);

  int l = 0;
  int r = -1;
  res = 0;

  for (int i = 0; i < q; i++) {
    while (l < a[i].l)
      remove(l++);
    while (l > a[i].l)
      add(--l);
    while (r < a[i].r)
      add(++r);
    while (r > a[i].r)
      remove(r--);

    ans[a[i].id] = res;
  }

  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int bl, t, n, m;
int res, a[N], cnt[N], ans[N];
int curR, curL;
struct node {
  int l, r, p;
  bool operator<(const node &W) const { return l < W.l; }
} e[N];
bool cmp(const node &a, const node &b) {
  return (a.l / bl) == (b.l / bl) ? a.r < b.r : a.l > b.l;
}
void remove(int i) {
  if ((--cnt[a[i]]) == 0)
    res--;
}
void add(int i) {
  if ((++cnt[a[i]]) == 1)
    res++;
}
int main() {

  memset(ans, 0, sizeof(ans));
  memset(cnt, 0, sizeof(cnt));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  bl = sqrt(n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &e[i].l, &e[i].r);
    e[i].p = i;
  }
  sort(e + 1, e + 1 + m, cmp);
  res = 0;
  curL = 1;
  curR = 0;
  for (int i = 1; i <= m; i++) {
    int L = e[i].l, R = e[i].r;
    while (curL < L)
      remove(curL++);
    while (curL > L)
      add(--curL);
    while (curR < R)
      add(++curR);
    while (curR > R)
      remove(curR--);
    ans[e[i].p] = res;
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int bl, t, n, m;
int res, a[N], cnt[N], ans[N];
int curR, curL;
struct node {
  int l, r, p;
  bool operator<(const node &W) const { return l < W.l; }
} e[N];
bool cmp(const node &a, const node &b) {
  return (a.l / bl) == (b.l / bl) ? a.r < b.r : a.l > b.l;
}
void remove(int i) {
  if ((--cnt[a[i]]) == 0)
    res--;
}
void add(int i) {
  if ((++cnt[a[i]]) == 1)
    res++;
}
int main() {

  memset(ans, 0, sizeof(ans));
  memset(cnt, 0, sizeof(cnt));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  bl = sqrt(n);
  for (int i = 1; i <= m; i++) {
    scanf("%d %d", &e[i].l, &e[i].r);
    e[i].p = i;
  }
  sort(e + 1, e + 1 + m, cmp);
  res = 0;
  curL = 1;
  curR = 0;
  for (int i = 1; i <= m; i++) {
    int L = e[i].l, R = e[i].r;
    while (curL < L)
      remove(curL++);
    while (curL > L)
      add(--curL);
    while (curR < R)
      add(++curR);
    while (curR > R)
      remove(curR--);
    ans[e[i].p] = res;
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>
int bit[500001], n;

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

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += (i & -i);
  }
}
int main() {
  int q, a, l, r;

  vector<pair<pair<int, int>, int>> v;
  cin >> n >> q;
  vector<set<int>> s(n + 1);
  vector<int> ans(q);
  for (int i = 0; i < n; i++) {
    cin >> a;
    s[a].insert(i + 1);
  }
  for (int i = 0; i <= n; i++) {
    set<int>::iterator it = s[i].begin();
    for (; it != s[i].end(); it++) {
      set<int>::iterator it2 = it;
      it2++;
      if (it2 != s[i].end()) {
        v.push_back({{*it2, -1}, *it});
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    v.push_back({{r, i}, l});
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (v[i].first.second == -1) {
      add(v[i].second, 1);
    } else {
      ans[v[i].first.second] = v[i].first.first - v[i].second + 1 -
                               sum(v[i].first.first) + sum(v[i].second - 1);
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;
#include <cstring>
int bit[500001], n;

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

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += (i & -i);
  }
}
int main() {
  int q, a, l, r;

  vector<pair<pair<int, int>, int>> v;
  cin >> n >> q;
  vector<set<int>> s(n + 1);
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    cin >> a;
    s[a].insert(i + 1);
  }
  for (int i = 0; i <= n; i++) {
    set<int>::iterator it = s[i].begin();
    for (; it != s[i].end(); it++) {
      set<int>::iterator it2 = it;
      it2++;
      if (it2 != s[i].end()) {
        v.push_back({{*it2, -1}, *it});
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    v.push_back({{r, i}, l});
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (v[i].first.second == -1) {
      add(v[i].second, 1);
    } /* else {
       ans[v[i].first.second] = v[i].first.first - v[i].second + 1 -
                                sum(v[i].first.first) + sum(v[i].second - 1);
     }*/
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma gcc optimize(2)
#include <bits/stdc++.h>
using namespace std;
int n, q, x, y, l = 1, r, t, num, a[666666], p[2333333], ans[666666], dv;
struct node {
  int l, r, bel;
} ls[666666];
int cmp(node a, node b) {
  if (a.l / dv != b.l / dv)
    return a.l / dv < b.l / dv;
  return a.r / dv < b.r / dv;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++)
    cin >> x >> y, ls[i] = (node){x, y, i};
  dv = (int)(sqrt(n));
  sort(ls + 1, ls + q + 1, cmp);
  for (int i = 1; i <= q; i++) {
    while (ls[i].l < l)
      num += !p[a[--l]]++;
    while (ls[i].l > l)
      num -= !--p[a[l++]];
    while (ls[i].r > r)
      num += !p[a[++r]]++;
    while (ls[i].r < r)
      num -= !--p[a[r--]];
    ans[ls[i].bel] = num;
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, q, x, y, l = 1, r, t, num, a[666666], p[2333333], ans[666666], dv;
struct node {
  int l, r, bel;
} ls[666666];
int cmp(node a, node b) {
  if (a.l / dv != b.l / dv)
    return a.l / dv < b.l / dv;
  return a.r / dv < b.r / dv;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++)
    cin >> x >> y, ls[i] = (node){x, y, i};
  dv = (int)(pow(n, 0.3333333));
  sort(ls + 1, ls + q + 1, cmp);
  for (int i = 1; i <= q; i++) {
    while (ls[i].l < l)
      num += !p[a[--l]]++;
    while (ls[i].l > l)
      num -= !--p[a[l++]];
    while (ls[i].r > r)
      num += !p[a[++r]]++;
    while (ls[i].r < r)
      num -= !--p[a[r--]];
    ans[ls[i].bel] = num;
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
}