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

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
#define double long double
using namespace std;
const ll inf = 1000000000000000000;
const int tree_size = (1 << 19);

struct zapr {
  ll l, r, ind;
};
ll n, q, mas[500005], x, y, ans[500005], kz, last[500005], uc,
    tree[tree_size * 2];
zapr a[500005], p;
set<ll> myset;
bool fcmp(const zapr &a, const zapr &b) { return (a.r < b.r); }
void upd(ll ind, ll elem) {
  ind += (tree_size - 1);
  tree[ind] = elem;
  ind >>= 1;
  while (ind > 0) {
    tree[ind] = tree[ind * 2] + tree[ind * 2 + 1];
    ind >>= 1;
  }
}
ll sum(ll l, ll r, ll tecl, ll tecr, ll ind) {
  if (tecr < l || tecl > r)
    return 0;
  if (tecr <= r && tecl >= l)
    return tree[ind];
  ll one = sum(l, r, tecl, (tecr + tecl) / 2, ind * 2);
  ll two = sum(l, r, (tecr + tecl) / 2 + 1, tecr, ind * 2 + 1);
  return (one + two);
}
ll get_sum(ll l, ll r) { return sum(l, r, 1, tree_size, 1); }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (ll i = 1; i <= n; ++i)
    cin >> mas[i];
  for (ll i = 1; i <= q; ++i) {
    cin >> x >> y;
    p.l = x;
    p.r = y;
    p.ind = i;
    a[++kz] = p;
  }
  sort(a + 1, a + q + 1, fcmp);
  for (ll i = 1; i <= n; ++i)
    last[i] = -inf;
  uc = 1;
  for (ll i = 1; i <= q; ++i) {
    while (uc <= a[i].r) {
      if (last[mas[uc]] != -inf)
        upd(last[mas[uc]], 0);
      last[mas[uc]] = uc;
      upd(uc, 1);
      ++uc;
    }
    ans[a[i].ind] = get_sum(a[i].l, a[i].r);
  }
  for (ll i = 1; i <= q; ++i)
    cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
#define ll long long
#define double long double
using namespace std;
const ll inf = 1000000000000000000;

struct zapr {
  ll l, r, ind;
};
ll n, q, mas[500005], x, y, ans[500005], kz, last[500005], uc;
zapr a[500005], p;
set<ll> myset;
bool fcmp(const zapr &a, const zapr &b) { return (a.r < b.r); }
void upd(ll elem, ll index) {
  if (last[elem] != -inf)
    myset.erase(last[elem]);
  myset.insert(index);
  last[elem] = index;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (ll i = 1; i <= n; ++i)
    cin >> mas[i];
  for (ll i = 1; i <= q; ++i) {
    cin >> x >> y;
    p.l = x;
    p.r = y;
    p.ind = i;
    a[++kz] = p;
  }
  sort(a + 1, a + q + 1, fcmp);
  for (ll i = 1; i <= n; ++i)
    last[i] = -inf;
  uc = 1;
  for (ll i = 1; i <= q; ++i) {
    while (uc <= a[i].r) {
      upd(mas[uc], uc);
      ++uc;
    }
    ans[a[i].ind] = (ll)distance(myset.lower_bound(a[i].l), myset.end());
  }
  for (ll i = 1; i <= q; ++i)
    cout << ans[i] << endl;
  return 0;
}
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;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int N;

ll bit[2][1000000];

void init() { rep(i, 2) rep(j, 1000000) bit[i][j] = 0; }

void add(int l, int r, int val) {
  for (int i = l; i <= N; i += (i & -i))
    bit[0][i] += -val * (l - 1);
  for (int i = l; i <= N; i += (i & -i))
    bit[1][i] += val;
  for (int i = r + 1; i <= N; i += (i & -i))
    bit[0][i] += val * r;
  for (int i = r + 1; i <= N; i += (i & -i))
    bit[1][i] += -val;
}

ll sum(int i) {
  vector<ll> ret(2);
  for (int idx = i; idx > 0; idx -= (idx & -idx))
    ret[0] += bit[0][idx];
  for (int idx = i; idx > 0; idx -= (idx & -idx))
    ret[1] += bit[1][idx];
  ret[1] *= i;
  return ret[0] + ret[1];
}

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  init();
  int Q;
  cin >> N >> Q;
  vector<int> A(N + 1);
  rep(i, N) cin >> A[i + 1];
  vector<int> prev(N + 1, 0);
  vector<pair<pair<int, int>, int>> query(Q);
  vector<ll> ans(Q);

  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    query[i] = make_pair(pair<int, int>(r, l), i);
  }
  sort(query.begin(), query.end());

  int idx = 1;
  rep(q, Q) {
    int right = query[q].first.first, left = query[q].first.second;
    while (idx <= right) {
      add(prev[A[idx]] + 1, idx, 1);
      prev[A[idx]] = idx;
      idx++;
    }
    ans[query[q].second] = sum(left) - sum(left - 1);
  }
  rep(i, Q) cout << ans[i] << endl;

  /*
  add(1, 3, 1);
  rep(i, N) cout << sum(i+1) << " "; cout << endl;
  add(1, 1, 1);
  rep(i, N) cout << sum(i+1) << " "; cout << endl;
  add(3, 5, 1);
  rep(i, N) cout << sum(i+1) << " "; cout << 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;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  vector<int> appear(1000000, 0);
  int N, Q;
  cin >> N >> Q;
  vector<int> A(N + 1, 0);
  pair<pair<int, int>, int> query[Q]; //(r, l) で入れる
  vector<int> ans(Q);

  rep(i, N) cin >> A[i + 1];
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    query[i] = make_pair(pair<int, int>(r, l), i);
  }
  sort(query, query + Q);
  set<pair<int, int>> st;
  int id = 1;
  for (int i = 0; i < Q; i++) {
    while (id <= N && query[i].first.first >= id) {
      if (appear[A[id]]) {
        st.erase(pair<int, int>(appear[A[id]], A[id]));
        st.insert(pair<int, int>(id, A[id]));
      } else
        st.insert(make_pair(id, A[id]));
      appear[A[id]] = id;
      id++;
    }
    // キーの値がquery[i].first.second以上になるものはmpの中にいくつあるか
    int res;
    auto it = st.lower_bound(pair<int, int>(query[i].first.second, 0));
    res = distance(it, st.end());
    ans[query[i].second] = res;
  }
  rep(i, Q) cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int ans = 0;
const int X = 2000;
vector<int> a;
vector<int> cnt;

void add(int i) {
  ans += (cnt[a[i]] == 0);
  cnt[a[i]]++;
}

void del(int i) {
  cnt[a[i]]--;
  ans -= (cnt[a[i]] == 0);
}

int que(int oldL, int oldR, int L, int R) {
  for (int i = oldL; i < L; i++)
    del(i);
  for (int i = L; i < oldL; i++)
    add(i);
  for (int i = oldR; i > R; i--)
    del(i);
  for (int i = R; i > oldR; i--)
    add(i);
  return ans;
}

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / X != b.first.first / X)
    return a.first.first / X < b.first.first / X;
  return a.first.second < b.first.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  a.resize(n), cnt.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<pair<pair<int, int>, int>> b(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i].first.first >> b[i].first.second;
    b[i].first.first--, b[i].first.second--;
    b[i].second = i;
  }
  sort(b.begin(), b.end(), cmp);
  vector<int> answer(q);
  int oldL = 1, oldR = 0;
  for (int i = 0; i < q; i++) {
    int id = b[i].second;
    int L = b[i].first.first;
    int R = b[i].first.second;
    answer[id] = que(oldL, oldR, L, R);
    oldL = L, oldR = R;
  }
  for (auto &now : answer)
    cout << now << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int ans = 0;
const int X = 333;
vector<int> a;
vector<int> cnt;

void add(int i) {
  ans += (cnt[a[i]] == 0);
  cnt[a[i]]++;
}

void del(int i) {
  cnt[a[i]]--;
  ans -= (cnt[a[i]] == 0);
}

int que(int oldL, int oldR, int L, int R) {
  for (int i = oldL; i < L; i++)
    del(i);
  for (int i = L; i < oldL; i++)
    add(i);
  for (int i = oldR; i > R; i--)
    del(i);
  for (int i = R; i > oldR; i--)
    add(i);
  return ans;
}

bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
  if (a.first.first / X != b.first.first / X)
    return a.first.first / X < b.first.first / X;
  return a.first.second < b.first.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  a.resize(n), cnt.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    a[i]--;
  }
  vector<pair<pair<int, int>, int>> b(q);
  for (int i = 0; i < q; i++) {
    cin >> b[i].first.first >> b[i].first.second;
    b[i].first.first--, b[i].first.second--;
    b[i].second = i;
  }
  sort(b.begin(), b.end(), cmp);
  vector<int> answer(q);
  int oldL = 1, oldR = 0;
  for (int i = 0; i < q; i++) {
    int id = b[i].second;
    int L = b[i].first.first;
    int R = b[i].first.second;
    answer[id] = que(oldL, oldR, L, R);
    oldL = L, oldR = R;
  }
  for (auto &now : answer)
    cout << now << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

typedef struct omaru {
  ll l, r, id;
} polka;

#define FENWICK_NMAX 500010
struct fenwick_tree {
  ll N;
  ll bit[FENWICK_NMAX];
  fenwick_tree(ll n) {
    N = n;
    for (ll x = 1; x <= N; x++) {
      bit[x] = 0;
    }
  }
  void add(ll a, ll w) {
    for (ll x = a; x <= N; x += x & -x) {
      bit[x] += w;
    }
  }
  ll sum(ll a) {
    ll ret = 0;
    for (ll x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
  ll range_sum(ll l, ll r) { // [l, r]
    return sum(r) - sum(l - 1);
  }
};

int main() {
  // input
  ll n, Q;
  cin >> n >> Q;
  vector<ll> c(n);
  rep(i, 0, n) cin >> c[i];
  vector<polka> q(Q);
  rep(i, 0, Q) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }

  // sort with right pos
  sort(q.begin(), q.end(), [](polka l, polka r) {
    if (l.r < r.r)
      return true;
    else
      return false;
  });

  // query
  fenwick_tree t(n + 1);
  vector<ll> a(n + 1, -1), res(Q);
  ll itr = 0;
  rep(i, 0, n) {
    if (a[c[i]] > -1)
      t.add(a[c[i]], -1);
    t.add(i + 1, 1);
    a[c[i]] = i + 1;
    while (q[itr].r == i + 1) {
      res[q[itr].id] = t.range_sum(q[itr].l, i + 1);
      itr++;
    }
  }

  // print ans
  rep(i, 0, Q) cout << res[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

typedef struct omaru {
  ll l, r, id;
} polka;

#define FENWICK_NMAX 500010
struct fenwick_tree {
  ll N;
  ll bit[FENWICK_NMAX];
  fenwick_tree(ll n) {
    N = n;
    for (ll x = 1; x <= N; x++) {
      bit[x] = 0;
    }
  }
  void add(ll a, ll w) {
    for (ll x = a; x <= N; x += x & -x) {
      bit[x] += w;
    }
  }
  ll sum(ll a) {
    ll ret = 0;
    for (ll x = a; x > 0; x -= x & -x) {
      ret += bit[x];
    }
    return ret;
  }
  ll range_sum(ll l, ll r) { // [l, r]
    return sum(r) - sum(l - 1);
  }
};

int main() {
  // input
  ll n, Q;
  cin >> n >> Q;
  vector<ll> c(n);
  rep(i, 0, n) cin >> c[i];
  vector<polka> q(Q);
  rep(i, 0, Q) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }

  // sort with right pos
  sort(q.begin(), q.end(), [](polka l, polka r) {
    if (l.r < r.r)
      return true;
    else
      return false;
  });

  // query
  fenwick_tree t(n + 1);
  vector<ll> a(n + 1, -1), res(Q);
  ll itr = 0;
  rep(i, 0, n) {
    if (a[c[i]] > -1)
      t.add(a[c[i]], -1);
    t.add(i + 1, 1);
    a[c[i]] = i + 1;
    while (q[itr].r == i + 1) {
      cerr << itr << endl;
      res[q[itr].id] = t.range_sum(q[itr].l, i + 1);
      itr++;
    }
  }

  // print ans
  rep(i, 0, Q) cout << res[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int maxn = 15e5, maxk = 1e7;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &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(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int 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);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int 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(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }

  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\\n";
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e7, maxk = 1e7;
int root[maxn], L[maxn], R[maxn], sum[maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &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(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int 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);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int 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(0);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }

  int l, r;
  while (q--) {
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\\n";
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1LL << 60;
const int INF = 1LL << 17;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll bit[500500];
ll n;
#define N 500010

ll sum(ll i) {
  ll res = 0;
  while (0 < i) {
    res += bit[i];
    i -= i & -i;
  }
  return res;
}
void add(ll i, ll x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

ll rsum(ll l, ll r) { return sum(r) - sum(l - 1); }
vector<vector<ll>> qr(N), qri(N);

int main() {
  ll q;
  cin >> n >> q;

  vector<ll> c(n + 1);
  rep(i, n) cin >> c[i + 1];
  vector<ll> a(N);
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    qr[b].push_back(a);
    qri[b].push_back(i);
  }
  vector<ll> res(q + 1);
  FOR(i, 1, n + 1) {
    if (a[c[i]] > 0)
      add(a[c[i]], -1);
    add(i, 1);
    a[c[i]] = i;
    rep(j, qr[i].size()) { res[qri[i][j]] = rsum(qr[i][j], i); }
  }
  rep(i, q) cout << res[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MOD = 1000000007;
const ld PI = acos(-1);
const ld EPS = 0.0000000001;
const ll LINF = 1LL << 60;
const int INF = 1LL << 17;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repd(i, n) for (ll i = n - 1; 0 <= i; i--)
#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define FORD(i, a, b) for (ll i = b - 1; (ll)(a) <= i; i--)
#define ALL(x) x.begin(), x.end()
#define MAX(x) *max_element(ALL(x))
#define MIN(x) *min_element(ALL(x))
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll bit[500500];
ll n;
#define N 500010

ll sum(ll i) {
  ll res = 0;
  while (0 < i) {
    res += bit[i];
    i -= i & -i;
  }
  return res;
}
void add(ll i, ll x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

ll rsum(ll l, ll r) { return sum(r) - sum(l - 1); }
vector<vector<ll>> qr(N), qri(N);

int main() {
  ll q;
  cin >> n >> q;

  vector<ll> c(n + 1);
  rep(i, n) cin >> c[i + 1];
  vector<ll> a(N);
  rep(i, q) {
    ll a, b;
    cin >> a >> b;
    qr[b].push_back(a);
    qri[b].push_back(i);
  }
  vector<ll> res(n + 1);
  FOR(i, 1, n + 1) {
    if (a[c[i]] > 0)
      add(a[c[i]], -1);
    add(i, 1);
    a[c[i]] = i;
    rep(j, qr[i].size()) { res[qri[i][j]] = rsum(qr[i][j], i); }
  }
  rep(i, q) cout << res[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int n, q, xl, xr, res;
int a[500010], cnt[500010], ans[500010];
struct Node {
  int l, r, num;
} f[500010];
inline bool cmp(Node a, Node b) {
  if (a.l / 800 == b.l / 800)
    return a.r < b.r;
  return a.l < b.l;
}
inline void Add(int x) {
  if (cnt[x] == 0)
    res++;
  cnt[x]++;
}
inline void Delete(int x) {
  if (cnt[x] == 1)
    res--;
  cnt[x]--;
}
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &f[i].l, &f[i].r);
    f[i].num = i;
  }
  sort(f + 1, f + q + 1, cmp);
  xl = 1, xr = 1, res = 1;
  cnt[a[1]] = 1;
  for (int i = 1; i <= q; i++) {
    while (xl < f[i].l) {
      Delete(a[xl]);
      xl++;
    }
    while (xl > f[i].l) {
      xl--;
      Add(a[xl]);
    }
    while (xr < f[i].r) {
      xr++;
      Add(a[xr]);
    }
    while (xr > f[i].r) {
      Delete(a[xr]);
      xr--;
    }
    ans[f[i].num] = res;
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, q, xl, xr, res;
int a[500010], cnt[500010], ans[500010];
struct Node {
  int l, r, num;
} f[500010];
inline bool cmp(Node a, Node b) {
  if (a.l / 800 == b.l / 800)
    return a.r < b.r;
  return a.l < b.l;
}
inline void Add(int x) {
  if (cnt[x] == 0)
    res++;
  cnt[x]++;
}
inline void Delete(int x) {
  if (cnt[x] == 1)
    res--;
  cnt[x]--;
}
int main() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++) {
    cin >> f[i].l >> f[i].r;
    f[i].num = i;
  }
  sort(f + 1, f + q + 1, cmp);
  xl = 1, xr = 1, res = 1;
  cnt[a[1]] = 1;
  for (int i = 1; i <= q; i++) {
    while (xl < f[i].l) {
      Delete(a[xl]);
      xl++;
    }
    while (xl > f[i].l) {
      xl--;
      Add(a[xl]);
    }
    while (xr < f[i].r) {
      xr++;
      Add(a[xr]);
    }
    while (xr > f[i].r) {
      Delete(a[xr]);
      xr--;
    }
    ans[f[i].num] = res;
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

// FenwickTree↓
int dat[N];
void init() { rep(i, N) dat[i] = 0; }
void add(int i, int x) {
  for (; i < N; i += i & -i)
    dat[i] += x;
}
int sum(int i) {
  int res = 0;
  for (; i > 0; i -= i & -i)
    res += dat[i];
  return res;
}
int rangesum(int l, int r) { //[l,r]
  return sum(r) - sum(l - 1);
}
// FenwickTree↑

int a[N];
vector<int> ql[N], qid[N];
int ans[N];

int n, q, c[N];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> c[i];
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    ql[r].push_back(l);
    qid[r].push_back(i);
  }
  init();
  for (int i = 1; i <= n; i++)
    a[i] = 0;
  for (int i = 1; i <= n; i++) {
    if (a[c[i]] > 0)
      add(a[c[i]], -1);
    add(i, +1);
    a[c[i]] = i;
    rep(j, ql[i].size()) { ans[qid[i][j]] = rangesum(ql[i][j], i); }
  }
  rep(i, q) cout << ans[i] << "\\n";
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

/*
Saurabh Joshi
IIIT Jabalpur
*/
#include <bits/stdc++.h>
#define LL long long int
#define M 1000000007
#define endl "\\n"
#define eps 0.00000001
LL pow(LL a, LL b, LL m) {
  LL x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y);
      if (x > m)
        x %= m;
    }
    y = (y * y);
    if (y > m)
      y %= m;
    b /= 2;
  }
  return x % m;
}
LL gcd(LL a, LL b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
LL gen(LL start, LL end) {
  LL diff = end - start;
  LL temp = rand() % start;
  return temp + diff;
}
using namespace std;
const int MAXN = 500002;
vector<int> a(MAXN);
vector<bitset<MAXN>> tree1(MAXN);
void build(int node, int i, int j) {
  if (i > j)
    return;
  if (i == j) {
    tree1[node].set(a[i]);
    return;
  }
  int mid = (i + j) / 2;
  build(2 * node, i, mid);
  build(2 * node + 1, mid + 1, j);
  tree1[node] = tree1[2 * node] | tree1[2 * node + 1];
}
bitset<MAXN> query1(int node, int i, int j, int l, int r) {
  if (i > j || i > r || j < l)
    return 0;
  if (i >= l && j <= r)
    return tree1[node];
  int mid = (i + j) / 2;
  return query1(2 * node, i, mid, l, r) |
         query1(2 * node + 1, mid + 1, j, l, r);
}
/*bitset<5001>query2(int node,int i,int j,int l,int r){
    if(i > j || i > r || j < l)
        return 0;
    if(i >= l && j <= r)
        return tree2[node];
    int mid = (i + j) / 2;
    return query2(2 * node , i , mid , l , r) | query2(2 * node + 1 , mid + 1 ,
j , l , r);
}*/
int main() {
  ios_base::sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  build(1, 1, n);
  while (q--) {
    int a, b;
    cin >> a >> b;
    cout << query1(1, 1, n, a, b).count() << endl;
  }
}