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


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>

#define data _data

using namespace std;
const int maxn = 5e5 + 50;
map<int, int> mp;
int data[maxn];
int a[maxn];
int ans[maxn];
struct node {
  int l, r, id;
  bool operator<(node t) const { return r < t.r; }
} q[maxn];
int sum(int i) {
  int ans = 0;
  while (i > 0) {
    ans += data[i];
    i -= i & -i;
  }
  return ans;
}
void add(int i, int x) {
  while (i < maxn) {
    data[i] += x;
    i += i & -i;
  }
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    int m;
    scanf("%d", &m);
    fill(data, data + n + 2, 0);
    mp.clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }

    for (int i = 0; i < m; i++) {
      scanf("%d%d", &q[i].l, &q[i].r);
      q[i].id = i;
    }
    sort(q, q + m);
    int pre = 1;
    for (int i = 0; i < m; i++) {
      for (int j = pre; j <= q[i].r; j++) {
        if (mp[a[j]] != 0) {
          add(mp[a[j]], -1);
        }
        add(j, 1);
        mp[a[j]] = j;
      }
      pre = q[i].r + 1;
      ans[q[i].id] = sum(q[i].r) - sum(q[i].l - 1);
    }
    for (int i = 0; i < m; i++) {
      printf("%d\\n", ans[i]);
    }
  }
  return 0;
}


Error: 0

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>

#define data _data

using namespace std;
const int maxn = 30000 + 5;
map<int, int> mp;
int data[maxn];
int a[maxn];
int ans[200000 + 5];
struct node {
  int l, r, id;
  bool operator<(node t) const { return r < t.r; }
} q[200000 + 5];
int sum(int i) {
  int ans = 0;
  while (i > 0) {
    ans += data[i];
    i -= i & -i;
  }
  return ans;
}
void add(int i, int x) {
  while (i < maxn) {
    data[i] += x;
    i += i & -i;
  }
}

int main() {
  int n;
  while (~scanf("%d", &n)) {
    int m;
    scanf("%d", &m);
    fill(data, data + n + 2, 0);
    mp.clear();
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
    }

    for (int i = 0; i < m; i++) {
      scanf("%d%d", &q[i].l, &q[i].r);
      q[i].id = i;
    }
    sort(q, q + m);
    int pre = 1;
    for (int i = 0; i < m; i++) {
      for (int j = pre; j <= q[i].r; j++) {
        if (mp[a[j]] != 0) {
          add(mp[a[j]], -1);
        }
        add(j, 1);
        mp[a[j]] = j;
      }
      pre = q[i].r + 1;
      ans[q[i].id] = sum(q[i].r) - sum(q[i].l - 1);
    }
    for (int i = 0; i < m; i++) {
      printf("%d\\n", ans[i]);
    }
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

typedef pair<int, int> pii;

const int N = 5e5 + 5;

struct node {
  int sum = 0;
  int lv = 0, rv = 0;
} t[N * 50];

int sz;
int n, q;

int upd(int v, int pos, int val, int tl = 1, int tr = n) {
  int nv = ++sz;
  t[nv] = t[v];
  t[nv].sum += val;
  if (tl == tr) {
    return nv;
  }
  int tm = tl + tr >> 1;
  if (pos <= tm) {
    t[nv].lv = upd(t[v].lv, pos, val, tl, tm);
  } else {
    t[nv].rv = upd(t[v].rv, pos, val, tm + 1, tr);
  }
  return nv;
}

int get(int v, int l, int r, int tl = 1, int tr = n) {
  if (l <= tl && tr <= r) {
    return t[v].sum;
  }
  if (r < tl || tr < l)
    return 0;
  int tm = tl + tr >> 1;
  return get(t[v].lv, l, r, tl, tm) + get(t[v].rv, l, r, tm + 1, tr);
}

int root[N];
int last[N], a[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    root[i] = root[i - 1];
    cin >> a[i];
    if (last[a[i]] == 0) {
      root[i] = upd(root[i], i, 1);
    } else {
      root[i] = upd(root[i], last[a[i]], -1);
      root[i] = upd(root[i], i, 1);
    }
    last[a[i]] = i;
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << get(root[r], l, r) << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int a[500001];
int N;
int cur = 0;
int cnt[500001];

struct MO {
  int l, r, id;
} q[500001];

bool cmp(MO x, MO y) {
  if (x.l / N != y.l / N) {
    return x.l / N < y.l / N;
  }
  return x.r < y.r;
}

void add(int x) {
  x = a[x];
  cnt[x]++;
  if (cnt[x] == 1)
    cur++;
}

void del(int x) {
  x = a[x];
  cnt[x]--;
  if (cnt[x] == 0)
    cur--;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, qq;
  cin >> n >> qq;
  N = sqrt(n);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= qq; i++) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }
  sort(q + 1, q + qq + 1, cmp);
  vector<int> ans(qq + 1);
  int L = 1, R = 0;
  for (int i = 1; i <= qq; i++) {
    while (L < q[i].l)
      del(L++);
    while (L > q[i].l)
      add(--L);
    while (R > q[i].r)
      del(R--);
    while (R < q[i].r)
      add(++R);
    ans[q[i].id] = cur;
  }
  for (int i = 1; i <= qq; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define mp make_pair
#define eb emplace_back
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 5e5 + 5;
// #define int long long
const int INF = 1e9;

int f[MAXN], last[MAXN];

void add(int i, int x) {
  for (; i < MAXN; i = i | (i + 1))
    f[i] += x;
}

//[l, r)
int sum(int l, int r) {
  int res = 0;
  for (int i = r - 1; i > -1; i = (i & (i + 1)) - 1)
    res += f[i];
  for (int i = l - 1; i > -1; i = (i & (i + 1)) - 1)
    res -= f[i];
  return res;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

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

  int l, r;
  vector<vector<pair<int, int>>> a(n + 1);
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    a[r].eb(l, i);
  }

  vector<int> ans(q);

  for (int i = 1; i <= n; i++) {
    if (last[c[i]])
      add(last[c[i]], -1);
    last[c[i]] = i;
    add(last[c[i]], 1);
    for (auto j : a[i])
      ans[j.ss] = sum(j.ff, i + 1);
  }

  for (auto i : ans)
    cout << i << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define mp make_pair
#define eb emplace_back
#define ff first
#define ss second

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 100;
// #define int long long
const int INF = 1e9;

int f[MAXN], last[MAXN];

void add(int i, int x) {
  for (; i < MAXN; i = i | (i + 1))
    f[i] += x;
}

//[l, r)
int sum(int l, int r) {
  int res = 0;
  for (int i = r - 1; i > -1; i = (i & (i + 1)) - 1)
    res += f[i];
  for (int i = l - 1; i > -1; i = (i & (i + 1)) - 1)
    res -= f[i];
  return res;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

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

  int l, r;
  vector<vector<pair<int, int>>> a(n + 1);
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    a[r].eb(l, i);
  }

  vector<int> ans(q);

  for (int i = 1; i <= n; i++) {
    if (last[c[i]])
      add(last[c[i]], -1);
    last[c[i]] = i;
    add(last[c[i]], 1);
    for (auto j : a[i])
      ans[j.ss] = sum(j.ff, i + 1);
  }

  for (auto i : ans)
    cout << i << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define ar array

typedef long long ll;
typedef string str;

/* void setIO(string filename = "") { */
/*     ios_base::sync_with_stdio(0); cin.tie(0); */
/*     if ((int)filename.size()) { */
/*         freopen((filename+".in").c_str(), "r", stdin); */
/*         freopen((filename+".out").c_str(), "w", stdout); */
/*     } */
/* } */

const int maxn = 5e5;
int N, Q;
int x[maxn], ft[maxn + 1], ans[maxn];
vector<ar<int, 2>> q_a[maxn];
map<int, int> last_occur;

void upd(int i, int x) {
  for (++i; i <= N; i += i & -i) {
    ft[i] += x;
  }
}

int query(int i) {
  int res = 0;
  for (; i > 0; i -= i & -i) {
    res += ft[i];
  }
  return res;
}

int main() {
  /* setIO(); */
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N >> Q;
  for (int i = 0; i < N; ++i) {
    cin >> x[i];
  }
  for (int i = 0; i < Q; ++i) {
    int l, r;
    cin >> l >> r;
    --l, --r;
    q_a[r].push_back({l, i});
  }
  for (int i = 0; i < N; ++i) {
    if (last_occur.find(x[i]) != last_occur.end())
      upd(last_occur[x[i]], -1);
    upd(i, 1);
    for (auto u : q_a[i]) {
      ans[u[1]] = query(N) - query(u[0]);
    }
    last_occur[x[i]] = i;
  }
  for (int i = 0; i < Q; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"
#define ar array

typedef long long ll;
typedef string str;

void setIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

const int maxn = 2e5;
int n, q;
int x[maxn], ft[maxn + 1], ans[maxn];
vector<ar<int, 2>> q_a[maxn];
map<int, int> last_occur;

void upd(int i, int x) {
  for (++i; i <= n; i += i & -i) {
    ft[i] += x;
  }
}

int query(int i) {
  int res = 0;
  for (; i > 0; i -= i & -i) {
    res += ft[i];
  }
  return res;
}

int main() {
  setIO();
  cin >> n >> q;
  for (int i = 0; i < n; ++i)
    cin >> x[i];
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    q_a[r - 1].push_back({l, i});
  }
  for (int i = 0; i < n; ++i) {
    if (last_occur.find(x[i]) != last_occur.end()) {
      upd(last_occur[x[i]], -1);
    }
    upd(i, 1);
    for (auto u : q_a[i]) {
      ans[u[1]] = query(n) - query(u[0] - 1);
    }
    last_occur[x[i]] = i;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = (m); i < (int)(n); i++)

template <typename T> struct fenwick_tree {
  int n;
  vector<T> d;
  fenwick_tree(int n = 0) : n(n), d(n + 1) {}

  void add(int i, T x) {
    i++;
    while (i <= n) {
      d[i] += x;
      i += i & -i;
    }
  }
  T sum(int i) {
    i++;
    T x = 0;
    while (i > 0) {
      x += d[i];
      i -= i & -i;
    }
    return x;
  }
};

int main() {
  ll N, Q, l, r, idx = 0;
  cin >> N >> Q;
  vector<ll> c(N), good(N, -1), ans(Q);
  REP(i, N) {
    cin >> c[i];
    c[i]--;
  }
  vector<tuple<ll, ll, ll>> query(Q);
  REP(i, Q) {
    cin >> l >> r;
    query[i] = {r - 1, l - 1, i};
  }

  sort(query.begin(), query.end());
  fenwick_tree<ll> fw(N);

  REP(i, N) {
    if (good[c[i]] != -1)
      fw.add(good[c[i]], -1);
    good[c[i]] = i;
    fw.add(i, 1);

    while (idx != Q && get<0>(query[idx]) == i) {
      ans[get<2>(query[idx])] =
          fw.sum(get<0>(query[idx])) - fw.sum(get<1>(query[idx]) - 1);
      idx++;
    }
  }

  REP(i, Q) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, m, n) for (int i = (m); i < (int)(n); i++)

template <typename T> struct fenwick_tree {
  int n;
  vector<T> d;
  fenwick_tree(int n = 0) : n(n), d(n + 1) {}

  void add(int i, T x) {
    i++;
    while (i <= n) {
      d[i] += x;
      i += i & -i;
    }
  }
  T sum(int i) {
    i++;
    T x = 0;
    while (i > 0) {
      x += d[i];
      i -= i & -i;
    }
    return x;
  }
};

int main() {
  ll N, Q, l, r, idx = 0;
  cin >> N >> Q;
  vector<ll> c(N), good(N, -1), ans(N);
  REP(i, N) {
    cin >> c[i];
    c[i]--;
  }
  vector<tuple<ll, ll, ll>> query(Q);
  REP(i, Q) {
    cin >> l >> r;
    query[i] = {r - 1, l - 1, i};
  }

  sort(query.begin(), query.end());
  fenwick_tree<ll> fw(N);

  REP(i, N) {
    if (good[c[i]] != -1)
      fw.add(good[c[i]], -1);
    good[c[i]] = i;
    fw.add(i, 1);

    while (idx != Q && get<0>(query[idx]) == i) {
      ans[get<2>(query[idx])] =
          fw.sum(get<0>(query[idx])) - fw.sum(get<1>(query[idx]) - 1);
      idx++;
    }
  }

  REP(i, Q) cout << ans[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ii {
  int a, b;
  bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
  int a, b, c;
  bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};

const int MOD = 1000000007;
const int INF = 1000000000;
const int MAXN = 500005;

int N, sqN, Q, curUnique;
int arr[MAXN], cnt[MAXN], ans[MAXN];
iii query[MAXN];

bool cmp(iii x, iii y) {
  return make_tuple(x.b / sqN, x.c) < make_tuple(y.b / sqN, y.c);
}

void inc(int idx) {
  if (cnt[arr[idx]] == 0)
    curUnique++;
  cnt[arr[idx]]++;
}

void dec(int idx) {
  cnt[arr[idx]]--;
  if (cnt[arr[idx]] == 0)
    curUnique--;
}

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

  cin >> N >> Q;
  sqN = sqrt(N);
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }

  for (int i = 0; i < Q; i++) {
    query[i].a = i;
    cin >> query[i].b >> query[i].c;
    query[i].b--;
    query[i].c--;
  }

  sort(query, query + Q, cmp);

  int l = 0;
  int r = 0;
  cnt[arr[l]]++;
  curUnique = 1;
  for (int i = 0; i < Q; i++) {
    while (l < query[i].b)
      dec(l++);
    while (l > query[i].b)
      inc(--l);
    while (r < query[i].c)
      inc(++r);
    while (r > query[i].c)
      dec(r--);
    ans[query[i].a] = curUnique;
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct ii {
  int a, b;
  bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
  int a, b, c;
  bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};

const int MOD = 1000000007;
const int INF = 1000000000;
const int MAXN = 500005;

int N, sqN, Q, curUnique;
int arr[MAXN], cnt[MAXN], ans[MAXN];
iii query[MAXN];

bool cmp(iii x, iii y) {
  return make_tuple(x.b / sqN, x.c / sqN) < make_tuple(y.b / sqN, y.c / sqN);
}

void inc(int idx) {
  if (cnt[arr[idx]] == 0)
    curUnique++;
  cnt[arr[idx]]++;
}

void dec(int idx) {
  cnt[arr[idx]]--;
  if (cnt[arr[idx]] == 0)
    curUnique--;
}

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

  cin >> N >> Q;
  sqN = sqrt(N);
  for (int i = 0; i < N; i++) {
    cin >> arr[i];
  }

  for (int i = 0; i < Q; i++) {
    query[i].a = i;
    cin >> query[i].b >> query[i].c;
    query[i].b--;
    query[i].c--;
  }

  sort(query, query + Q, cmp);

  int l = 0;
  int r = 0;
  cnt[arr[l]]++;
  curUnique = 1;
  for (int i = 0; i < Q; i++) {
    while (l < query[i].b)
      dec(l++);
    while (l > query[i].b)
      inc(--l);
    while (r < query[i].c)
      inc(++r);
    while (r > query[i].c)
      dec(r--);
    ans[query[i].a] = curUnique;
  }

  for (int i = 0; i < Q; i++)
    cout << ans[i] << "\\n";
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 5e5 + 10;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

int lowbit(int x) { return x & (-x); }

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  scanf("%d", &n);
  memset(v, 0, sizeof(v));
  memset(sum, 0, sizeof(sum));
  memset(ans, 0, sizeof(ans));
  scanf("%d", &q);
  int t = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
  }

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

  sort(query + 1, query + q + 1);

  int pre = 1;
  map<int, int> mymap;
  for (int i = 1; i <= q; i++) {
    for (int j = pre; j <= query[i].r; j++) {
      // 已存在
      if (mymap[v[j]]) {
        change(mymap[v[j]], -1);
      }
      mymap[v[j]] = j;
      change(j, 1);
    }
    pre = query[i].r + 1;
    ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
  }

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


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int maxn = 10000;
int n = 0, q = 0;

struct Query {
  int id, l, r;
  bool operator<(Query a) const { return r < a.r; }
} query[maxn];

int v[maxn], sum[maxn], ans[maxn];

int lowbit(int x) { return x & (-x); }

void change(int i, int a) {
  while (i <= n) {
    sum[i] += a;
    i += lowbit(i);
  }
}

int getsum(int i) {
  int sum_t = 0;
  while (i > 0) {
    sum_t += sum[i];
    i -= lowbit(i);
  }
  return sum_t;
}

int main() {

  scanf("%d", &n);
  memset(v, 0, sizeof(v));
  memset(sum, 0, sizeof(sum));
  memset(ans, 0, sizeof(ans));
  scanf("%d", &q);
  int t = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v[i]);
  }

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

  sort(query + 1, query + q + 1);

  int pre = 1;
  map<int, int> mymap;
  for (int i = 1; i <= q; i++) {
    for (int j = pre; j <= query[i].r; j++) {
      // 已存在
      if (mymap[v[j]]) {
        change(mymap[v[j]], -1);
      }
      mymap[v[j]] = j;
      change(j, 1);
    }
    pre = query[i].r + 1;
    ans[query[i].id] = getsum(query[i].r) - getsum(query[i].l - 1);
  }

  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;
using ll = long long;
using P = pair<ll, ll>;
ll n;
ll dat[2000005];
void init(ll n_) {
  n = 1;
  while (n <= n_)
    n *= 2;
}
void update(ll k, ll a) {
  k += n - 1;
  dat[k] = a;
  while (0 < k) {
    k = (k - 1) / 2;
    dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
  }
}
ll query(ll a, ll b, ll k, ll l, ll r) {
  if (a <= l && r <= b)
    return dat[k];
  else if (r <= a || b <= l)
    return 0;
  else {
    ll val = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll var = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return (val + var);
  }
}
ll N, Q;
int main() {
  cin >> N >> Q;
  init(N + 2);
  vector<ll> c(N);
  for (int i = 0; i < N; i++)
    cin >> c.at(i);
  vector<pair<P, ll>> A(Q);
  for (int i = 0; i < Q; i++) {
    ll l, r;
    cin >> l >> r;
    P p(r, l);
    pair<P, ll> p1(p, i);
    A[i] = p1;
  }
  sort(A.begin(), A.end());
  vector<ll> ans(Q);
  map<ll, ll> M;
  ll I = 0;
  for (int i = 0; i < N; i++) {
    if (!M.count(c[i])) {
      M[c[i]] = i;
      update(i, 1);
    } else {
      update(M.at(c[i]), 0);
      M.at(c[i]) = i;
      update(i, 1);
    }
    while (A[I].first.first == i + 1) {
      ll l = A[I].first.second;
      ll r = A[I].first.first;
      ll j = A[I].second;
      ll a = query(l - 1, r, 0, 0, n);
      ans[j] = a;
      I++;
    }
  }
  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
ll n;
ll dat[200005];
void init(ll n_) {
  n = 1;
  while (n <= n_)
    n *= 2;
}
void update(ll k, ll a) {
  k += n - 1;
  dat[k] = a;
  while (0 < k) {
    k = (k - 1) / 2;
    dat[k] = (dat[2 * k + 1] + dat[2 * k + 2]);
  }
}
ll query(ll a, ll b, ll k, ll l, ll r) {
  if (a <= l && r <= b)
    return dat[k];
  else if (r <= a || b <= l)
    return 0;
  else {
    ll val = query(a, b, 2 * k + 1, l, (l + r) / 2);
    ll var = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return (val + var);
  }
}
ll N, Q;
int main() {
  cin >> N >> Q;
  init(N + 2);
  vector<ll> c(N);
  for (int i = 0; i < N; i++)
    cin >> c.at(i);
  vector<pair<P, ll>> A(Q);
  for (int i = 0; i < Q; i++) {
    ll l, r;
    cin >> l >> r;
    P p(r, l);
    pair<P, ll> p1(p, i);
    A[i] = p1;
  }
  sort(A.begin(), A.end());
  vector<ll> ans(Q);
  map<ll, ll> M;
  ll I = 0;
  for (int i = 0; i < N; i++) {
    if (!M.count(c[i])) {
      M[c[i]] = i;
      update(i, 1);
    } else {
      update(M.at(c[i]), 0);
      M.at(c[i]) = i;
      update(i, 1);
    }
    while (A[I].first.first == i + 1) {
      ll l = A[I].first.second;
      ll r = A[I].first.first;
      ll j = A[I].second;
      ll a = query(l - 1, r, 0, 0, n);
      ans[j] = a;
      I++;
    }
  }
  for (int i = 0; i < Q; i++)
    cout << ans[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;
const int MN = 5e5 + 10;
int n, q, D[MN], t[MN], last[MN], ans[MN];
vector<pair<pair<int, int>, int>> que;

void akt(int ile, int x) {
  if (x > n)
    return;
  D[x] += ile;
  akt(ile, x + (x & (-x)));
}
int sum(int x) {
  if (x == 0)
    return 0;
  return D[x] + sum(x - (x & (-x)));
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> t[i];
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    que.push_back({{r, l}, i});
  }
  for (int i = 1; i <= n; ++i)
    last[i] = -1;
  sort(que.begin(), que.end());
  int wsk = 0;
  for (int i = 1; i <= n; ++i) {
    if (last[t[i]] != -1)
      akt(-1, last[t[i]]);
    last[t[i]] = i;
    akt(1, i);
    while (que[wsk].st.st == i && wsk < q) {
      auto p = que[wsk];
      ans[p.nd] = sum(p.st.st) - sum(p.st.nd - 1);
      wsk++;
    }
    if (wsk >= q)
      break;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int MN = 5e5 + 10;
int t[MN], R = 1, n;
set<int> tree[3 * MN];

int query(int a, int b) {
  set<int> sum;
  a = R + a - 1;
  b = R + b - 1;
  if (a == b)
    return (int)tree[a].size();
  sum.insert(tree[a].begin(), tree[a].end());
  sum.insert(tree[b].begin(), tree[b].end());
  while (a / 2 != b / 2) {
    if (a % 2 == 0)
      sum.insert(tree[a + 1].begin(), tree[a + 1].end());
    if (b % 2 == 1)
      sum.insert(tree[b - 1].begin(), tree[b - 1].end());
    a /= 2;
    b /= 2;
  }
  return (int)sum.size();
}
int main() {
  ios_base::sync_with_stdio(0);
  int q;
  cin >> n >> q;
  while (n > R)
    R *= 2;
  for (int i = 1; i <= n; ++i) {
    cin >> t[i];
    tree[R + i - 1].insert(t[i]);
  }
  for (int i = R - 1; i >= 1; --i) {
    tree[i].insert(tree[2 * i].begin(), tree[2 * i].end());
    tree[i].insert(tree[2 * i + 1].begin(), tree[2 * i + 1].end());
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << query(l, r) << '\\n';
  }
  return 0;
}