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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 5e5 + 5, rt = 1000;

int a[sz], cnt[sz], ans[sz], now;

struct dtt {
  int l, r, id;
} quers[sz];

bool cmp(dtt a, dtt b) {
  if (a.l / rt == b.l / rt)
    return a.r < b.r;
  return a.l < b.l;
}

inline void add(int x) {
  if (!cnt[a[x]])
    now++;
  cnt[a[x]]++;
}

inline void del(int x) {
  cnt[a[x]]--;
  if (!cnt[a[x]])
    now--;
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    quers[i] = {x - 1, y - 1, i};
  }
  sort(quers, quers + q, cmp);

  for (int i = 0, l = 0, r = -1; i < q; i++) {
    while (l < quers[i].l)
      del(l++);
    while (quers[i].l < l)
      add(--l);
    while (r < quers[i].r)
      add(++r);
    while (quers[i].r < r)
      del(r--);
    ans[quers[i].id] = now;
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 5e5 + 5, rt = 700;

int a[sz], cnt[sz], ans[sz], now;

struct dtt {
  int l, r, id;
} quers[sz];

bool cmp(dtt a, dtt b) {
  if (a.l / rt == b.l / rt)
    return a.r < b.r;
  return a.l < b.l;
}

void add(int x) {
  if (!cnt[a[x]])
    now++;
  cnt[a[x]]++;
}

void del(int x) {
  cnt[a[x]]--;
  if (!cnt[a[x]])
    now--;
}

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    quers[i] = {x - 1, y - 1, i};
  }
  sort(quers, quers + q, cmp);

  for (int i = 0, l = 0, r = -1; i < q; i++) {
    while (l < quers[i].l)
      del(l++);
    while (quers[i].l < l)
      add(--l);
    while (r < quers[i].r)
      add(++r);
    while (quers[i].r < r)
      del(r--);
    ans[quers[i].id] = now;
  }
  for (int i = 0; i < q; i++)
    printf("%d\\n", ans[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

// atcoder ABC 174 F
// https://www.youtube.com/watch?v=h0MGG8rxrYc&t=5781s
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, x, n) for (int i = x; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> a(N);
  rep(i, N) cin >> a[i];
  vector<int> pi(N + 1, -1); // 数字が出てきた位置
  vector<vector<int>> ps(N); // red arrow
  rep(i, N) {
    int l = pi[a[i]];
    if (l != -1)
      ps[l].push_back(i);
    pi[a[i]] = i;
  }

  vector<vector<P>> qs(N);
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    qs[l].emplace_back(r, i);
  }
  BIT<int> d(N);
  vector<int> ans(Q);
  for (int x = N - 1; x >= 0; --x) {
    for (int y : ps[x])
      d.add(y, 1);
    for (P query : qs[x]) {
      int r = query.first, i = query.second;
      ans[i] = (r - x + 1) - d.sum(r);
    }
  }
  rep(i, Q) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> a(N);
  vector<int> l(Q);
  vector<int> r(Q);
  rep(i, N) cin >> a[i];
  // cout << Q << endl;
  rep(i, Q) {
    cin >> l[i];
    cin >> r[i];
  }
  rep(i, Q) {
    set<int> s;
    rep2(j, l[i] - 1, r[i]) { s.insert(a[j]); }
    cout << s.size() << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;

constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
pair<int, int> query[500050];
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i) {
    cin >> query[i].first >> query[i].second;
    query[i].first--;
    mo[query[i].first / nr].push_back({query[i].second, i});
  }
  int x;
  array<int, 500050> num{};
  array<int, 500050> ret{};
  for (int i = 0; i < nr; ++i) {
    sort(mo[i].begin(), mo[i].end());
    fill(num.begin(), num.end(), 0);
    int curl, curr;
    curl = curr = 0;
    int count = 0;
    for (auto &&a : mo[i]) {
      x = a.second;
      while (curr < query[x].second) {
        if (num[c[curr]] == 0) {
          count++;
        }
        num[c[curr]]++;
        curr++;
      }
      while (curl < query[x].first) {
        num[c[curl]]--;
        if (num[c[curl]] == 0)
          count--;
        curl++;
      }
      while (curl > query[x].first) {
        curl--;
        num[c[curl]]++;
        if (num[c[curl]] == 1)
          count++;
      }
      ret[x] = count;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ret[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;

constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
array<pair<int, int>, 500050> query;
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i) {
    cin >> query[i].first >> query[i].second;
    query[i].first--;
    mo[query[i].first / nr].push_back({query[i].second, i});
  }
  int x;
  array<int, 500050> num{};
  array<int, 500050> ret{};
  for (int i = 0; i < nr; ++i) {
    sort(mo[i].begin(), mo[i].end());
    fill(num.begin(), num.end(), 0);
    int curl, curr;
    curl = curr = 0;
    int count = 0;
    for (auto &&a : mo[i]) {
      x = a.second;
      while (curr < query[x].second) {
        if (num[c[curr]] == 0) {
          count++;
        }
        num[c[curr]]++;
        curr++;
      }
      while (curl < query[x].first) {
        num[c[curl]]--;
        if (num[c[curl]] == 0)
          count--;
        curl++;
      }
      while (curl > query[x].first) {
        curl--;
        num[c[curl]]++;
        if (num[c[curl]] == 1)
          count++;
      }
      ret[x] = count;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ret[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;

constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
vector<pair<int, int>> query(500050);
vector<int> ret(500050);
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i) {
    cin >> query[i].first >> query[i].second;
    query[i].first--;
    mo[query[i].first / nr].push_back({query[i].second, i});
  }
  int x;
  array<int, 500050> num{};
  for (int i = 0; i < nr; ++i) {
    sort(mo[i].begin(), mo[i].end());
    fill(num.begin(), num.end(), 0);
    int curl, curr;
    curl = curr = 0;
    int count = 0;
    for (auto &&a : mo[i]) {
      x = a.second;
      while (curr < query[x].second) {
        if (num[c[curr]] == 0) {
          count++;
        }
        num[c[curr]]++;
        curr++;
      }
      while (curl < query[x].first) {
        num[c[curl]]--;
        if (num[c[curl]] == 0)
          count--;
        curl++;
      }
      while (curl > query[x].first) {
        curl--;
        num[c[curl]]++;
        if (num[c[curl]] == 1)
          count++;
      }
      ret[x] = count;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ret[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int inf = 1 << 30;
constexpr int mod = 1e9 + 7;
constexpr ll infl = 1ll << 60;

constexpr int nr = 800;
vector<pair<int, int>> mo[nr];
vector<pair<int, int>> query(500050);
vector<int> ret(500050);
vector<int> num(500050);
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i) {
    cin >> query[i].first >> query[i].second;
    query[i].first--;
    mo[query[i].first / nr].push_back({query[i].second, i});
  }

  for (int i = 0; i < nr; ++i) {
    sort(mo[i].begin(), mo[i].end());
    fill(num.begin(), num.end(), 0);
    int curl, curr;
    curl = curr = 0;
    int count = 0;
    for (auto &&a : mo[i]) {
      while (curr < query[a.second].second) {
        if (num[c[curr]] == 0) {
          count++;
        }
        num[c[curr]]++;
        curr++;
      }
      while (curl < query[a.second].first) {
        num[c[curl]]--;
        if (num[c[curl]] == 0)
          count--;
        curl++;
      }
      while (curl > query[a.second].first) {
        curl--;
        num[c[curl]]++;
        if (num[c[curl]] == 1)
          count++;
      }
      ret[a.second] = count;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ret[i] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#define N 500010
#define lowbit(x) x & -x
using namespace std;
int a[N], pos[N], ans[N], fwt[N];
vector<pair<int, int>> v[N];
void update(int x, int p) {
  for (int i = x; i < N; i += lowbit(i))
    fwt[i] += p;
}
int sum(int x) {
  int ss = 0;
  for (int i = x; i > 0; i -= lowbit(i))
    ss += fwt[i];
  return ss;
}
int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    v[r].push_back({l, i});
  }
  memset(pos, -1, sizeof(pos));
  for (int i = 1; i <= n; i++) {
    if (pos[a[i]] == -1) {
      update(i, 1);
      pos[a[i]] = i;
    } else {
      update(pos[a[i]], -1);
      update(i, 1);
      pos[a[i]] = i;
    }
    for (int j = 0; j < v[i].size(); j++)
      ans[v[i][j].second] = sum(i) - sum(v[i][j].first - 1);
  }
  for (int i = 1; i <= q; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define N 500010
#define int long long
using namespace std;
int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch))
    ch = getchar();
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
int a[N], cnt[N], res[N], block, ans = 1;
struct mo {
  int l, r, id;
} query[N];
bool cmp(mo x, mo y) {
  if (x.l / block == y.l / block)
    return x.r < y.r;
  if (x.l / block & 1)
    return x.l > y.l;
  else
    return x.l < y.l;
}
void add(int x) {
  ++cnt[a[x]];
  if (cnt[a[x]] == 1)
    ++ans;
}
void del(int x) {
  --cnt[a[x]];
  if (cnt[a[x]] == 0)
    --ans;
}
signed main() {

  int n = read(), q = read();
  block = sqrt(n);
  for (int i = 1; i <= n; i++)
    a[i] = read();
  for (int i = 1; i <= q; i++) {
    int L = read(), R = read();
    query[i].l = L;
    query[i].r = R;
    query[i].id = i;
  }
  sort(query + 1, query + 1 + q, cmp);
  //	for(int i=1;i<=q;i++) cout<<query[i].id<<" "<<query[i].l<<"
  //"<<query[i].r<<" ##"<<endl;
  int nl = 1, nr = 1;
  ++cnt[a[1]];
  for (int i = 1; i <= q; i++) {

    while (nl < query[i].l)
      del(nl++);
    //		for(int i=1;i<=n;i++) cout<<cnt[i]<<" ";
    //		cout<<endl;
    while (nl > query[i].l)
      add(--nl);
    //		for(int i=1;i<=n;i++) cout<<cnt[i]<<" ";
    //		cout<<endl;
    while (nr < query[i].r)
      add(++nr);
    //		for(int i=1;i<=n;i++) cout<<cnt[i]<<" ";
    //		cout<<endl;
    while (nr > query[i].r)
      del(nr--);
    //		for(int i=1;i<=n;i++) cout<<cnt[i]<<" ";
    //		cout<<endl;
    res[query[i].id] = ans;
    //		cout<<endl;
  }
  for (int i = 1; i <= q; i++)
    cout << res[i] << endl;

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

/*
    Mbak Sana Dong
    Mbak Chaeyoung Dong
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int arr[500500], bit[500500], sana[500500], last[500500];
vector<pair<int, int>> vi[500500];
void add(int x, int val) {
  for (x; x <= 500000; x += (x & -x))
    bit[x] += val;
}
int get(int x) {
  int res = 0;
  for (x; x > 0; x -= (x & -x))
    res += bit[x];
  return res;
}
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  int tc;
  int q;
  cin >> n >> q;
  for (i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
  }
  for (i = 1; i <= q; i++) {
    cin >> x >> y;
    vi[y].push_back({i, x});
  }
  for (i = 1; i <= n; i++) {
    int now = arr[i];
    if (last[now] > 0) {
      add(last[now], -1);
    }
    last[now] = i;
    add(i, 1);
    for (auto x : vi[i]) {
      sana[x.first] = get(i) - get(x.second - 1);
    }
  }
  for (i = 1; i <= q; i++)
    cout << sana[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*
    Mbak Sana Dong
    Mbak Chaeyoung Dong
*/
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int arr[500500];
set<int> seg[1500050];
void build(int idx, int l, int r) {
  if (l == r) {
    seg[idx].insert(arr[l]);
    return;
  }
  int mid = (l + r) / 2;
  build(idx * 2, l, mid);
  build(idx * 2 + 1, mid + 1, r);
  merge(seg[idx * 2].begin(), seg[idx * 2].end(), seg[idx * 2 + 1].begin(),
        seg[idx * 2 + 1].end(), inserter(seg[idx], seg[idx].end()));
}
set<int> query(int idx, int l, int r, int kiri, int kanan) {
  if (l > kanan || r < kiri) {
    set<int> s;
    s.clear();
    return s;
  }
  if (l >= kiri && r <= kanan)
    return seg[idx];
  int mid = (l + r) / 2;
  set<int> a = query(idx * 2, l, mid, kiri, kanan);
  set<int> b = query(idx * 2 + 1, mid + 1, r, kiri, kanan);
  set<int> c;
  merge(a.begin(), a.end(), b.begin(), b.end(), inserter(c, c.end()));
  return c;
}
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  int tc;
  int q;
  cin >> n >> q;
  for (i = 1; i <= n; i++) {
    scanf("%d", &arr[i]);
  }
  build(1, 1, n);
  while (q--) {
    scanf("%d %d", &x, &y);
    ans = query(1, 1, n, x, y).size();
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

/*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x & (-x));
using namespace std;

const int A = 1e6 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  return x * f;
}

struct node {
  int l, r, pos;
} ask[A];
int n, m, c[A], num[A], tree[A], booll[A], nnn[A], ww;

void change(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}

int query(int x) {
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= lowbit(x);
  }
  return ans;
}

bool cmp(node a, node b) { return a.r < b.r; }

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &num[i]);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &ask[i].l, &ask[i].r);
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (booll[num[j]])
        change(booll[num[j]], -1);
      change(j, 1), booll[num[j]] = j;
    }
    next = ask[i].r + 1;
    nnn[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    cout << nnn[i] << "\\n";
  return 0;
}


Error: 0

Fixed submission:

/*
Author:loceaner
*/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#define lowbit(x) (x & (-x));
using namespace std;

const int A = 1e5 + 11;
const int B = 1e6 + 11;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = getchar())
    x = x * 10 + (c ^ 48);
  return x * f;
}

struct node {
  int l, r, pos;
} ask[A];
int n, m, c[A], num[A], tree[A], booll[A], nnn[A], ww;

void change(int x, int k) {
  while (x <= n) {
    c[x] += k;
    x += lowbit(x);
  }
}

int query(int x) {
  int ans = 0;
  while (x) {
    ans += c[x];
    x -= lowbit(x);
  }
  return ans;
}

bool cmp(node a, node b) { return a.r < b.r; }

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &num[i]);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &ask[i].l, &ask[i].r);
    ask[i].pos = i;
  }
  sort(ask + 1, ask + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (booll[num[j]])
        change(booll[num[j]], -1);
      change(j, 1), booll[num[j]] = j;
    }
    next = ask[i].r + 1;
    nnn[ask[i].pos] = query(ask[i].r) - query(ask[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    cout << nnn[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;

pair<int, pair<int, int>> q[1000001];
int n, m, a[500001], ans[1000001];
int st[5 * 500001], 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;
}

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: Runtime Error

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

using namespace std;

vector<int> tree;

void lowDigits(vector<int> &a) {
  int n = a.size();
  vector<pair<int, int>> tmp(n);
  for (int i = 0; i < n; i++) {
    tmp[i] = {a[i], i};
  }
  sort(tmp.begin(), tmp.end());
  int val = 0;
  a[tmp[0].second] = val;
  for (int i = 1; i < n; i++) {
    if (tmp[i - 1].first != tmp[i].first) {
      val++;
    }
    a[tmp[i].second] = val;
  }
}

void update(int v, int vl, int vr, int ind, int del) {
  if (ind < vl || vr <= ind) {
    return;
  }
  if (vr - vl == 1) {
    tree[v] += del;
    return;
  }
  int vm = (vl + vr) / 2;
  update(2 * v, vl, vm, ind, del);
  update(2 * v + 1, vm, vr, ind, del);
  tree[v] = tree[2 * v] + tree[2 * v + 1];
}

int get(int v, int vl, int vr, int l, int r) {
  if (r <= vl || vr <= l) {
    return 0;
  }
  if (l <= vl && vr <= r) {
    return tree[v];
  }
  int vm = (vl + vr) / 2;
  return get(2 * v, vl, vm, l, r) + get(2 * v + 1, vm, vr, l, r);
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  lowDigits(a);

  vector<vector<int>> E(m, vector<int>(3));
  for (int i = 0; i < m; i++) {
    cin >> E[i][1] >> E[i][0];
    E[i][2] = i;
  }
  sort(E.begin(), E.end());
  tree.resize(4 * n);
  vector<int> pos(n, -1);
  vector<int> ans(m);
  for (int i = 0; i < E[0][0]; i++) {
    update(1, 0, n, i, 1);
    update(1, 0, n, pos[a[i]], -1);
    pos[a[i]] = i;
  }
  for (int i = 0; i < m; i++) {
    if (i != 0 && E[i - 1][0] != E[i][0]) {
      for (int j = E[i - 1][0]; j < E[i][0]; j++) {
        update(1, 0, n, j, 1);
        update(1, 0, n, pos[a[j]], -1);
        pos[a[j]] = j;
      }
    }
    ans[E[i][2]] = get(1, 0, n, E[i][1] - 1, E[i][0]);
  }
  for (auto i : ans) {
    cout << i << '\\n';
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e6 + 10000, maxk = 1e6 + 10000;
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;
}