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

// Program to compute sum of ranges for different range
// queries
#include <bits/stdc++.h>
using namespace std;
#define ll int
ll block, n, m;
ll a[500001];
struct Query {
  ll L, R, i;
};
Query q[500001];

bool compare(Query x, Query y) {
  if (x.L / block != y.L / block)
    return x.L / block < y.L / block;
  return x.R < y.R;
}
void queryResults() {
  block = (ll)sqrt(n);
  sort(q, q + m, compare);
  ll currL = 0, currR = 0;
  ll currSum = 0;
  ll ans = 0;
  ll aa[m];
  ll f[500001] = {0};
  for (ll i = 0; i < m; i++) {
    ll L = q[i].L, R = q[i].R;
    while (currL < L) {
      f[a[currL]]--;
      if (f[a[currL]] == 0)
        ans--;
      currL++;
    }
    while (currL > L) {
      if (f[a[currL - 1]] == 0)
        ans++;
      f[a[currL - 1]]++;
      currL--;
    }
    while (currR <= R) {
      if (f[a[currR]] == 0)
        ans++;
      f[a[currR]]++;
      currR++;
    }
    while (currR > R + 1) {
      f[a[currR - 1]]--;
      if (f[a[currR - 1]] == 0)
        ans--;
      currR--;
    }
    aa[q[i].i] = ans;
  }
  for (ll i = 0; i < m; i++)
    cout << aa[i] << endl;
}

// Driver program
int main() {
  ll i, j, k;
  cin >> n >> m;

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

  for (i = 0; i < m; i++) {
    cin >> j >> k;
    q[i].L = j - 1;
    q[i].R = k - 1;
    q[i].i = i;
  }
  queryResults();
}

Error: TLE

Fixed submission:

// Program to compute sum of ranges for different range
// queries
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
ll block, n, m;
ll a[500001];
struct Query {
  ll L, R, i;
};
Query q[500001];

bool compare(Query x, Query y) {
  if (x.L / block != y.L / block)
    return x.L / block < y.L / block;
  return x.R < y.R;
}
void queryResults() {
  block = (ll)sqrt(n);
  sort(q, q + m, compare);
  ll currL = 0, currR = 0;
  ll currSum = 0;
  ll ans = 0;
  ll aa[m];
  ll f[500001] = {0};
  for (ll i = 0; i < m; i++) {
    ll L = q[i].L, R = q[i].R;
    while (currL < L) {
      f[a[currL]]--;
      if (f[a[currL]] == 0)
        ans--;
      currL++;
    }
    while (currL > L) {
      if (f[a[currL - 1]] == 0)
        ans++;
      f[a[currL - 1]]++;
      currL--;
    }
    while (currR <= R) {
      if (f[a[currR]] == 0)
        ans++;
      f[a[currR]]++;
      currR++;
    }
    while (currR > R + 1) {
      f[a[currR - 1]]--;
      if (f[a[currR - 1]] == 0)
        ans--;
      currR--;
    }
    aa[q[i].i] = ans;
  }
  for (ll i = 0; i < m; i++)
    cout << aa[i] << endl;
}

// Driver program
int main() {
  ll i, j, k;
  cin >> n >> m;

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

  for (i = 0; i < m; i++) {
    cin >> j >> k;
    q[i].L = j - 1;
    q[i].R = k - 1;
    q[i].i = i;
  }
  queryResults();
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int ff;
int n, m, col[500100], blo, tot, le, ri, ans[500100], cnt[500100];
pair<pair<int, int>, int> que[500100];
bool pp(pair<pair<int, int>, int> x, pair<pair<int, int>, int> y) {
  return x.first.second < y.first.second;
}
inline void inser(int val) {
  cnt[val]++;
  if (cnt[val] == 1)
    ff++;
}
inline void eras(int val) {
  cnt[val]--;
  if (cnt[val] == 0)
    ff--;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &col[i]);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &que[i].first.first, &que[i].first.second);
    que[i].second = i;
  }
  sort(que + 1, que + m + 1);
  blo = max(1, (int)(sqrt(m)));
  tot = (m + blo - 1) / blo;
  for (int i = 1; i <= tot; i++) {
    le = blo * (i - 1) + 1;
    ri = min(blo * i, m);
    sort(que + le, que + ri + 1, pp);
    for (int j = 1; j <= n; j++)
      cnt[j] = 0;
    ff = 0;
    for (int j = que[le].first.first; j <= que[le].first.second; j++)
      inser(col[j]);
    ans[que[le].second] = ff;
    for (int j = le + 1; j <= ri; j++) {
      for (int kk = que[j - 1].first.first; kk < que[j].first.first; kk++)
        eras(col[kk]);
      for (int kk = que[j].first.first; kk < que[j - 1].first.first; kk++)
        inser(col[kk]);
      for (int kk = que[j - 1].first.second + 1; kk <= que[j].first.second;
           kk++)
        inser(col[kk]);
      ans[que[j].second] = ff;
    }
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, q, c[500100], l[500100], r[500100], res[500100];
template <typename query> struct MoAlgorithm {
  vector<query> Q;
  int BSZ;
  query operator[](int id) { return Q[id]; }
  void add_query(query que) { Q.push_back(que); }
  void specsort() {
    BSZ = sqrt(Q.size());
    sort(Q.begin(), Q.end());
    for (int i = 0; i < Q.size(); i += BSZ)
      reverse(Q.begin() + i, Q.begin() + min((int)(Q.size()), i + BSZ));
  }
};
MoAlgorithm<pair<pair<int, int>, int>> modui;
int tot, cnt[500100];
void add(int pos) {
  if (!cnt[pos])
    tot++;
  cnt[pos]++;
}
void del(int pos) {
  cnt[pos]--;
  if (!cnt[pos])
    tot--;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &c[i]);
  int lef, rig;
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &lef, &rig);
    modui.add_query(make_pair(make_pair(lef, rig), i));
  }
  modui.specsort();
  for (int i = modui[0].first.first; i <= modui[0].first.second; i++)
    add(c[i]);
  res[modui[0].second] = tot;
  for (int i = 1; i < q; i++) {
    for (int j = modui[i - 1].first.first; j < modui[i].first.first; j++)
      del(c[j]);
    for (int j = modui[i].first.first; j < modui[i - 1].first.first; j++)
      add(c[j]);
    for (int j = modui[i - 1].first.second + 1; j <= modui[i].first.second; j++)
      add(c[j]);
    for (int j = modui[i].first.second + 1; j <= modui[i - 1].first.second; j++)
      del(c[j]);
    res[modui[i].second] = tot;
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", res[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

// Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 15e5 + 1;
ll root[N], L[16 * N], R[16 * N], sum[16 * N];
ll rt = 1, sz = 1;
ll lpos[N];
ll copy(ll v, ll &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}
void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}
void add(ll pos, ll x, ll v = root[rt - 1], ll l = 0, ll r = N) {
  sum[v] += x;
  if (r - l == 1)
    return;
  ll m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

ll get(ll a, ll b, ll v, ll l = 0, ll r = N) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  ll m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  rt = sz = 1;
  ll n, q;
  cin >> n >> q;
  for (ll i = 1; i <= n; i++) {
    ll t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    ll l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << '\\n';
  }
}


Error: -11

Fixed submission:

// Author: HarshKumar
#include <bits/stdc++.h>
using namespace std;
#define ll int
const ll N = 1e6 + 1;
ll root[N], L[16 * N], R[16 * N], sum[16 * N];
ll rt = 1, sz = 1;
ll lpos[N];
ll copy(ll v, ll &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}
void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}
void add(ll pos, ll x, ll v = root[rt - 1], ll l = 0, ll r = N) {
  sum[v] += x;
  if (r - l == 1)
    return;
  ll m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

ll get(ll a, ll b, ll v, ll l = 0, ll r = N) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  ll m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  rt = sz = 1;
  ll n, q;
  cin >> n >> q;
  for (ll i = 1; i <= n; i++) {
    ll t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    ll l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << '\\n';
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
constexpr int MX = 500005;
int n, q, c[MX], l, r, p[MX], ans[MX];
struct Q {
  int l, r, i;
} qs[MX];
struct BIT {
  vector<int> dat;
  int sz;
  BIT(int n) : sz(n + 1), dat(n + 1){};
  void set(int x, int n) {
    for (int i = n; i < sz; i += i & -i)
      dat[i] += x;
  }
  int get(int n) {
    int ret = 0;
    for (int i = n; i > 0; i -= i & -i)
      ret += dat[i];
    return ret;
  }
};
int main() {
  scanf("%d%d", &n, &q);
  BIT bt(n);
  rep(i, n) scanf("%d", c + i + 1);
  rep(i, q) {
    scanf("%d%d", &l, &r);
    qs[i] = Q{l, r, i};
  }
  sort(qs, qs + q, [](Q &a, Q &b) { return a.r < b.r; });
  r = 1;
  rep(i, q) {
    for (; r <= qs[i].r; r++) {
      if (p[c[r]])
        bt.set(-1, p[c[r]]);
      bt.set(1, p[c[r]] = r);
    }
    ans[qs[i].i] = bt.get(qs[i].r) - bt.get(qs[i].l - 1);
  }
  rep(i, q) printf("%d\\n", ans[i]);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
constexpr int mx = 500005, sz = 708;
struct Range {
  int l, r, i;
} ran[mx];
int n, q, c[mx], cnt[mx], l, r, ans[mx], a;

int main() {
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d", c + i);
  rep(i, q) {
    scanf("%d%d", &l, &r);
    ran[i] = Range{l - 1, r, i};
  }
  sort(ran, ran + q, [](Range &a, Range &b) {
    return a.l / sz == b.l / sz ? a.r < b.r : a.l < b.l;
  });
  l = r = 0;
  rep(i, q) {
    while (r < ran[i].r)
      if (!cnt[c[r++]]++)
        a++;
    while (ran[i].l < l)
      if (!cnt[c[--l]]++)
        a++;
    while (r > ran[i].r)
      if (!--cnt[c[--r]])
        a--;
    while (ran[i].l > l)
      if (!--cnt[c[l++]])
        a--;
    ans[ran[i].i] = a;
  }
  rep(i, q) printf("%d\\n", ans[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;

#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair

template <typename T> using vv = vector<vector<T>>;

void solve() {
  int n, q;
  cin >> n >> q;
  vi a(n);
  for (auto &i : a)
    cin >> i, i--;
  ;
  vector<pair<pll, int>> qr(q);
  int cc = 0;
  for (auto &e : qr)
    cin >> e.first.first >> e.first.second, e.first.first--, e.first.second--,
        e.second = cc++;
  sort(all(qr), [&](pair<pll, int> aa, pair<pll, int> bb) {
    auto a = aa.first, b = bb.first;
    if (a.first / 1000 != b.first / 1000)
      return a.first / 1000 < b.first / 1000;
    return a.second < b.second;
  });

  vi cnt(n);
  ll tmp = 0;
  ll l = 0, r = -1;

  vi res(q);

  for (auto e : qr) {
    ll nl, nr;
    tie(nl, nr) = e.first;
    while (r < nr) {
      r++;
      if (cnt[a[r]]++ == 0)
        tmp++;
    }
    while (l > nl) {
      l--;
      if (cnt[a[l]]++ == 0)
        tmp++;
    }
    while (r > nr) {
      if (--cnt[a[r--]] == 0)
        tmp--;
    }
    while (l < nl) {
      if (--cnt[a[l++]] == 0)
        tmp--;
    }
    res[e.second] = tmp;
  }

  for (auto i : res)
    cout << i << '\\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using pll = pair<ll, ll>;

#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair

template <typename T> using vv = vector<vector<T>>;

void solve() {
  int n, q;
  cin >> n >> q;
  vi a(n);
  for (auto &i : a)
    cin >> i, i--;
  ;
  vector<pair<pll, int>> qr(q);
  int cc = 0;
  for (auto &e : qr)
    cin >> e.first.first >> e.first.second, e.first.first--, e.first.second--,
        e.second = cc++;
  sort(all(qr), [&](pair<pll, int> aa, pair<pll, int> bb) {
    auto a = aa.first, b = bb.first;
    if (a.first / 1000 != b.first / 1000)
      return a.first / 1000 < b.first;
    return a.second < b.second;
  });

  vi cnt(n);
  ll tmp = 0;
  ll l = 0, r = -1;

  vi res(q);

  for (auto e : qr) {
    ll nl, nr;
    tie(nl, nr) = e.first;
    while (r < nr) {
      r++;
      if (cnt[a[r]]++ == 0)
        tmp++;
    }
    while (l > nl) {
      l--;
      if (cnt[a[l]]++ == 0)
        tmp++;
    }
    while (r > nr) {
      if (--cnt[a[r--]] == 0)
        tmp--;
    }
    while (l < nl) {
      if (--cnt[a[l++]] == 0)
        tmp--;
    }
    res[e.second] = tmp;
  }

  for (auto i : res)
    cout << i << '\\n';
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, q, v[500001], aib[500001], sol[500001];
vector<int> pos[500001];
struct date {
  ll l, r, poz;
};
date qry[500001];
ll lsb(ll x) { return x & (-x); }
void update(ll poz, ll val) {
  for (int i = poz; i <= n; i += lsb(i))
    aib[i] += val;
}
ll query(ll poz) {
  ll rez = 0;
  for (int i = poz; i >= 1; i -= lsb(i))
    rez += aib[i];
  return rez;
}
bool comp(date a, date b) {
  if (a.l != b.l)
    return a.l < b.l;
  return a.r < b.r;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  for (int i = 1; i <= q; i++) {
    cin >> qry[i].l >> qry[i].r;
    qry[i].poz = i;
  }
  sort(qry + 1, qry + q + 1, comp);
  for (int i = n; i >= 1; i--)
    pos[v[i]].push_back(i);
  for (int i = 1; i <= n; i++)
    if (!pos[i].empty())
      update(pos[i].back(), 1);
  ll st = 1;
  for (int i = 1; i <= q; i++) {
    while (st < qry[i].l) {
      update(st, -1);
      pos[v[st]].pop_back();
      if (!pos[v[st]].empty())
        update(pos[v[st]].back(), 1);
      st++;
    }
    sol[qry[i].poz] = query(qry[i].r);
  }
  for (int i = 1; i <= q; i++)
    cout << sol[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll n, q, v[500001], f[500001], sol[500001];
struct date {
  ll l, r, poz;
};
date query[500001];
bool comp(date a, date b) {
  if (a.l != b.l)
    return a.l < b.l;
  return a.r < b.r;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> v[i];
  for (int i = 1; i <= q; i++) {
    cin >> query[i].l >> query[i].r;
    query[i].poz = i;
  }
  sort(query + 1, query + q + 1, comp);
  ll ans = 0;
  for (int i = query[1].l; i <= query[1].r; i++) {
    f[v[i]]++;
    if (f[v[i]] == 1)
      ans++;
  }
  sol[query[1].poz] = ans;
  ll st = query[1].l;
  ll dr = query[1].r;
  for (int j = 2; j <= q; j++) {
    if (query[j].l > st) {
      for (int i = st; i < query[j].l; i++) {
        f[v[i]]--;
        if (f[v[i]] == 0)
          ans--;
      }
    }
    if (query[j].r < dr) {
      for (int i = dr; i > query[j].r; i--) {
        f[v[i]]--;
        if (f[v[i]] == 0)
          ans--;
      }
    } else {
      for (int i = dr + 1; i <= query[j].r; i++) {
        f[v[i]]++;
        if (f[v[i]] == 1)
          ans++;
      }
    }
    st = query[j].l;
    dr = query[j].r;
    sol[query[j].poz] = ans;
  }
  for (int i = 1; i <= q; i++)
    cout << sol[i] << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
ll s[2000005];
void update(ll id, ll l, ll r, ll p) {
  if (l == r) {
    s[id]++;
    return;
  }
  ll mid = (l + r) / 2;
  if (p <= mid)
    update(id * 2, l, mid, p);
  else
    update(id * 2 + 1, mid + 1, r, p);
  s[id] = s[id * 2] + s[id * 2 + 1];
}
ll query(ll id, ll l, ll r, ll L, ll R) {
  if (L > r)
    return 0;
  if (r < L || R < l)
    return 0;
  if (L <= l && r <= R)
    return s[id];
  ll mid = (l + r) / 2;
  return query(id * 2, l, mid, L, R) + query(id * 2 + 1, mid + 1, r, L, R);
}
int main() {
  ll n, q;
  cin >> n >> q;
  ll a[n], b[n], ans[q], prev[n + 1], next[n], pos = 0;
  fill(prev, prev + n + 1, -1);
  fill(next, next + n, n);
  set<ll> s;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    s.emplace(a[i]);
    b[i] = s.size();
    if (prev[a[i]] != -1)
      next[prev[a[i]]] = i;
    prev[a[i]] = i;
  }
  pair<pair<ll, ll>, ll> p[q];
  for (ll i = 0; i < q; i++) {
    cin >> p[i].ff.ff >> p[i].ff.ss;
    p[i].ff.ff--, p[i].ff.ss--, p[i].ss = i;
  }
  sort(p, p + q);
  vector<ll> v;
  for (ll i = 0; i < n; i++) {
    while (pos < q && p[pos].ff.ff == i) {
      ans[p[pos].ss] = b[p[pos].ff.ss] - query(1, 0, n, p[pos].ff.ss + 1, n);
      pos++;
    }
    update(1, 0, n, next[i]);
  }
  for (ll i = 0; i < q; i++)
    printf("%lld\\n", ans[i]);
  printf("\\n");
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
int main() {
  ll n, q;
  cin >> n >> q;
  ll a[n], b[n], ans[q], prev[n + 1], next[n], pos = 0;
  fill(prev, prev + n + 1, -1);
  fill(next, next + n, n);
  set<ll> s;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    s.emplace(a[i]);
    b[i] = s.size();
    if (prev[a[i]] != -1)
      next[prev[a[i]]] = i;
    prev[a[i]] = i;
  }
  pair<pair<ll, ll>, ll> p[q];
  for (ll i = 0; i < q; i++) {
    cin >> p[i].ff.ff >> p[i].ff.ss;
    p[i].ff.ff--, p[i].ff.ss--, p[i].ss = i;
  }
  sort(p, p + q);
  vector<ll> v;
  for (ll i = 0; i < n; i++) {
    while (pos < q && p[pos].ff.ff == i) {
      ans[p[pos].ss] =
          b[p[pos].ff.ss] -
          (v.size() -
           (upper_bound(v.begin(), v.end(), p[pos].ff.ss) - v.begin()));
      pos++;
    }
    v.insert(lower_bound(v.begin(), v.end(), next[i]), next[i]);
  }
  for (ll i = 0; i < q; i++)
    printf("%lld\\n", ans[i]);
  printf("\\n");
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 500005
ll arr[N], bit[N], ans[N], vis[N];

struct kj {
  ll l, r, id;
};

bool comp(kj &a, kj &b) { return (a.r < b.r); }

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

ll ask(ll id) {
  ll s = 0;
  while (id > 0) {
    s += bit[id];
    id -= (id & -id);
  }
  return s;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k, i, j;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> arr[i];
  kj query[k];

  for (i = 0; i < k; i++) {
    cin >> query[i].l >> query[i].r;
    query[i].id = i;
  }
  sort(query, query + k, comp);

  memset(vis, -1, sizeof(vis));

  ll cnt = 0;
  for (i = 0; i < n; i++) {
    if (vis[arr[i]] != -1)
      update(vis[arr[i]] + 1, -1, n);

    vis[arr[i]] = i;
    update(i + 1, 1, n);

    while (cnt < k && query[cnt].r - 1 == i) {
      ans[query[cnt].id] = ask(query[cnt].r) - ask(query[cnt].l - 1);
      cnt++;
    }
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define N 500005
ll arr[N];
set<ll> s[4 * N];

void build(ll x, ll l, ll r) {
  if (l == r) {
    s[x].insert(arr[l]);
    return;
  }
  ll m = (l + r) >> 1;

  build(2 * x, l, m);
  build(2 * x + 1, m + 1, r);
  s[x].insert(s[2 * x].begin(), s[2 * x].end());
  s[x].insert(s[2 * x + 1].begin(), s[2 * x + 1].end());
}

set<ll> query(ll v, ll l, ll r, ll a, ll b) {
  set<ll> aux;
  if (b < l || a > r)
    return aux;
  if (a <= l && r <= b)
    return s[v];

  ll m = (l + r) >> 1;

  set<ll> left = query(2 * v, l, m, a, b);
  aux.insert(left.begin(), left.end());

  set<ll> right = query(2 * v + 1, m + 1, r, a, b);
  aux.insert(right.begin(), right.end());

  return aux;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n, k, i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> arr[i];

  build(1, 0, n - 1);

  while (k--) {
    ll x, y;
    cin >> x >> y;
    cout << query(1, 0, n - 1, x - 1, y - 1).size() << "\\n";
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define re cin >>
#define pr cout <<
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)

const int N = 6e5 + 5;
const int mod = 1e9 + 7;

vector<pii> qq[N];
int ar[N], q, n, ans[N];
int old[N];

struct bit {
  int nn;
  vector<ll> v;
  bit() {}
  bit(int x) {
    nn = x;
    v = vector<ll>(nn + 1, 0);
  }
  ll query(ll x) {
    ll res = 0;
    while (x)
      res += v[x], x -= (x & -x);
    return res;
  }
  void update(ll x, ll by) {
    while (x <= nn)
      v[x] += by, x += (x & -x);
  }
} bb(N);

int main() {

  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);

  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> ar[i];

  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    qq[r].push_back({l, i});
  }

  for (int i = 1; i <= n; i++) {
    if (old[ar[i]])
      bb.update(old[ar[i]], -1);
    old[ar[i]] = i;
    bb.update(i, 1);
    for (auto pp : qq[i]) {
      ans[pp.second] = bb.query(N - 2) - bb.query(pp.first - 1);
    }
  }

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

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define vi vector<int>
#define pb push_back
#define pii pair<int, int>
#define fi first
#define se second
#define re cin >>
#define pr cout <<
#define all(x) x.begin(), x.end()
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)

const int N = 2e5 + 5;
const int mod = 1e9 + 7;

vector<pii> qq[N];
int ar[N], q, n, ans[N];
int old[N];

struct bit {
  int nn;
  vector<ll> v;
  bit() {}
  bit(int x) {
    nn = x;
    v = vector<ll>(nn + 1, 0);
  }
  ll query(ll x) {
    ll res = 0;
    while (x)
      res += v[x], x -= (x & -x);
    return res;
  }
  void update(ll x, ll by) {
    while (x <= nn)
      v[x] += by, x += (x & -x);
  }
} bb(N);

int main() {

  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);

  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> ar[i];

  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    qq[r].push_back({l, i});
  }

  for (int i = 1; i <= n; i++) {
    if (old[ar[i]])
      bb.update(old[ar[i]], -1);
    old[ar[i]] = i;
    bb.update(i, 1);
    for (auto pp : qq[i]) {
      ans[pp.second] = bb.query(n) - bb.query(pp.first - 1);
    }
  }

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