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

/* 実装例　Atcoder Beginers Contest 174-F Range Set query */
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
signed main() {
  int n, q;
  std::cin >> n >> q;
  int sq = 2 * sqrt(n);
  std::vector<int> c(n);
  rep(i, n) {
    std::cin >> c[i];
    c[i]--;
  }
  std::vector<int> l(q), r(q);
  rep(i, q) {
    std::cin >> l[i] >> r[i];
    l[i]--;
  }

  std::vector<int> qu(q);
  rep(i, q) qu[i] = i;
  std::sort(ALL(qu), [&](int i, int j) {
    if (l[i] / sq != l[j] / sq)
      return l[i] < l[j];
    else
      return r[i] < r[j];
  });

  std::vector<int> ans(q);
  std::vector<int> cnt(n, 0);
  int nl = 0, nr = 0;
  int res = 0;
  for (int i : qu) {
    while (nl > l[i]) {
      nl--;
      if (cnt[c[nl]]++ == 0)
        res++;
    }
    while (nl < l[i]) {
      if (--cnt[c[nl]] == 0)
        res--;
      nl++;
    }
    while (nr < r[i]) {
      if (cnt[c[nr]]++ == 0)
        res++;
      nr++;
    }
    while (nr > r[i]) {
      nr--;
      if (--cnt[c[nr]] == 0)
        res--;
    }
    /* ここから結果 */
    ans[i] = res;
  }
  rep(i, q) std::cout << ans[i] << "\\n";
}


Error: 0

Fixed submission:

/* 実装例　Atcoder Beginers Contest 174-F Range Set query */
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
signed main() {
  int n, q;
  std::cin >> n >> q;
  int sq = 2 * sqrt(n);
  std::vector<int> c(n);
  rep(i, n) {
    std::cin >> c[i];
    c[i]--;
  }
  std::vector<int> l(n), r(n);
  rep(i, q) {
    std::cin >> l[i] >> r[i];
    l[i]--;
  }

  std::vector<int> qu(q);
  rep(i, q) qu[i] = i;
  std::sort(ALL(qu), [&](int i, int j) {
    if (l[i] / sq != l[j] / sq)
      return l[i] < l[j];
    else
      return r[i] < r[j];
  });

  std::vector<int> ans(q);
  std::vector<int> cnt(n, 0);
  int nl = 0, nr = 0;
  int res = 0;
  for (int i : qu) {
    while (nl > l[i]) {
      nl--;
      if (cnt[c[nl]]++ == 0)
        res++;
    }
    while (nl < l[i]) {
      if (--cnt[c[nl]] == 0)
        res--;
      nl++;
    }
    while (nr < r[i]) {
      if (cnt[c[nr]]++ == 0)
        res++;
      nr++;
    }
    while (nr > r[i]) {
      nr--;
      if (--cnt[c[nr]] == 0)
        res--;
    }
    /* ここから結果 */
    ans[i] = res;
  }
  rep(i, q) std::cout << ans[i] << "\\n";
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define N 511111
#define A 511111
#define BLOCK 710 // ~sqrt(N)

int cnt[A], a[N], ans[N], answer = 0;

struct node {
  int L, R, i;
} q[N];

bool cmp(node x, node y) {
  if (x.L / BLOCK != y.L / BLOCK) {
    // different blocks, so sort by block.
    return x.L / BLOCK < y.L / BLOCK;
  }
  // same block, so sort by R value
  return x.R < y.R;
}

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

void remove(int position) {
  cnt[a[position]]--;
  if (cnt[a[position]] == 0) {
    answer--;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];

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

  sort(q, q + m, cmp);

  int currentL = 0, currentR = 0;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currentL < L) {
      remove(currentL);
      currentL++;
    }
    while (currentL > L) {
      add(currentL - 1);
      currentL--;
    }
    while (currentR <= R) {
      add(currentR);
      currentR++;
    }
    while (currentR > R + 1) {
      remove(currentR - 1);
      currentR--;
    }
    ans[q[i].i] = answer;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define N 511111
#define A 1111111
#define BLOCK 555 // ~sqrt(N)

int cnt[A], a[N], ans[N], answer = 0;

struct node {
  int L, R, i;
} q[N];

bool cmp(node x, node y) {
  if (x.L / BLOCK != y.L / BLOCK) {
    // different blocks, so sort by block.
    return x.L / BLOCK < y.L / BLOCK;
  }
  // same block, so sort by R value
  return x.R < y.R;
}

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

void remove(int position) {
  cnt[a[position]]--;
  if (cnt[a[position]] == 0) {
    answer--;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];

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

  sort(q, q + m, cmp);

  int currentL = 0, currentR = 0;
  for (int i = 0; i < m; i++) {
    int L = q[i].L, R = q[i].R;
    while (currentL < L) {
      remove(currentL);
      currentL++;
    }
    while (currentL > L) {
      add(currentL - 1);
      currentL--;
    }
    while (currentR <= R) {
      add(currentR);
      currentR++;
    }
    while (currentR > R + 1) {
      remove(currentR - 1);
      currentR--;
    }
    ans[q[i].i] = answer;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

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;
  }
};

int main() {
  ll n, q;
  cin >> n >> q;
  vl c(n), last(5e5 + 10, -1);
  rep(i, n) cin >> c[i];
  vector<pair<P, ll>> p(q);
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    p[i] = {{r, l}, i};
  }
  sort(all(p));
  BIT<ll> b(n + 10);
  vl ans(q);
  ll k = 0;
  rep(i, q) {
    ll l = p[i].first.second;
    ll r = p[i].first.first;
    ll j = p[i].second;
    for (; k <= r; k++) {
      ll now = last[c[k]];
      last[c[k]] = k;
      if (now >= 0)
        b.add(now, -1);
      b.add(k, 1);
    }
    ans[j] = b.sum(r);
    if (l > 0)
      ans[j] -= b.sum(l - 1);
  }
  rep(i, q) cout << ans[i] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

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;
  }
};

int main() {
  ll n, q;
  cin >> n >> q;
  vl c(n), last(5e5 + 10, -1);
  rep(i, n) cin >> c[i];
  vector<pair<P, ll>> p(q);
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    p[i] = {{r, l}, i};
  }
  sort(all(p));
  BIT<ll> b(n + 10);
  vl ans(n);
  ll k = 0;
  rep(i, q) {
    ll l = p[i].first.second;
    ll r = p[i].first.first;
    ll j = p[i].second;
    for (; k <= r; k++) {
      ll now = last[c[k]];
      last[c[k]] = k;
      if (now >= 0)
        b.add(now, -1);
      b.add(k, 1);
    }
    ans[j] = b.sum(r);
    if (l > 0)
      ans[j] -= b.sum(l - 1);
  }
  rep(i, q) cout << ans[i] << '\\n';
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Query {
  int i, l, r;
};

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

  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++)
    cin >> c[i];
  vector<Query> queries(Q);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    queries[i] = {i, l - 1, r - 1};
  }
  int S = (int)sqrt(N);
  sort(queries.begin(), queries.end(), [&](const auto &q1, const auto &q2) {
    if (q1.l / S != q2.l / S)
      return q1.l < q2.l;
    return q1.r < q2.r;
  });

  vector<int> colors(N + 1);
  vector<int> ans(Q);
  int curL = 0, curR = 0, cnt = 1;
  colors[c[0]] = 1;
  for (auto &q : queries) {
    while (curL < q.l) {
      colors[c[curL]]--;
      if (colors[c[curL]] == 0)
        cnt--;
      curL++;
    }
    while (curL > q.l) {
      curL--;
      if (colors[c[curL]] == 0)
        cnt++;
      colors[c[curL]]++;
    }
    while (curR < q.r) {
      curR++;
      if (colors[c[curR]] == 0)
        cnt++;
      colors[c[curR]]++;
    }
    while (curR > q.r) {
      colors[c[curR]]--;
      if (colors[c[curR]] == 0)
        cnt--;
      curR--;
    }
    ans[q.i] = cnt;
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Query {
  int i, l, r;
};

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

  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++)
    cin >> c[i];
  vector<Query> queries(Q);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    queries[i] = {i, l - 1, r - 1};
  }
  int S = 700;
  sort(queries.begin(), queries.end(), [&](const auto &q1, const auto &q2) {
    if (q1.l / S != q2.l / S)
      return q1.l < q2.l;
    return q1.r < q2.r;
  });

  map<int, int> colors;
  vector<int> ans(Q);
  int curL = 0, curR = 0;
  colors[c[0]] = 1;
  for (auto &q : queries) {
    // cout << q.i << " " << q.l << " " << q.r << endl;
    while (curL < q.l) {
      colors[c[curL]]--;
      if (colors[c[curL]] == 0)
        colors.erase(c[curL]);
      curL++;
    }
    while (curL > q.l) {
      curL--;
      if (colors.find(c[curL]) != colors.end()) {
        colors[c[curL]]++;
      } else {
        colors[c[curL]] = 1;
      }
    }
    while (curR < q.r) {
      curR++;
      if (colors.find(c[curR]) != colors.end()) {
        colors[c[curR]]++;
      } else {
        colors[c[curR]] = 1;
      }
    }
    while (curR > q.r) {
      colors[c[curR]]--;
      if (colors[c[curR]] == 0)
        colors.erase(c[curR]);
      curR--;
    }
    // for (auto& p : colors) cout << p.first << " " << p.second << endl;
    // cout << endl;
    ans[q.i] = colors.size();
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << '\\n';
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<ll, ll> mp;
ll bit[1000000];
class Query {
public:
  ll l, r, index;
};
void update(ll i, ll value, ll n) {
  while (i <= n) {
    bit[i] += value;
    i += i & (-i);
  }
}
bool compare(Query q1, Query q2) { return q1.r < q2.r; }
ll query(ll index, ll n) {
  ll ans = 0;
  while (index > 0) {
    ans += bit[index];
    index -= index & (-index);
  }
  return ans;
}
int main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt","r",stdin);
  // 	freopen("output.txt","w",stdout);
  // #endif
  ll n, q;
  cin >> n >> q;
  ll a[n];
  ll last_visited[1000000];
  ll query_counter = 0;
  memset(last_visited, -1, sizeof(last_visited));
  memset(bit, 0, sizeof(bit));
  ll ans[q];
  for (ll h = 0; h < n; h++) {
    cin >> a[h];
  }
  vector<Query> v;
  ll i = 0;
  ll z = q;
  while (q--) {
    ll lk, rk;
    cin >> lk >> rk;
    lk--;
    rk--;
    Query qw;
    qw.l = lk;
    qw.r = rk;
    qw.index = i;
    i++;
    v.push_back(qw);
  }

  sort(v.begin(), v.end(), compare);

  for (ll h = 0; h < n; h++) {
    if (last_visited[a[h]] != -1) {
      update(last_visited[a[h]] + 1, -1, n);
    }
    last_visited[a[h]] = h;
    update(h + 1, 1, n);
    while (query_counter < z && v[query_counter].r == h) {
      ans[v[query_counter].index] =
          query(v[query_counter].r + 1, n) - query(v[query_counter].l, n);
      query_counter++;
    }
  }
  for (ll p = 0; p < z; p++) {
    cout << ans[p] << endl;
  }
}


Error: TLE

Fixed submission:

// #include<bits/stdc++.h>
// #define ll long long
// using namespace std;

// int main()
// {
// #ifndef ONLINE_JUDGE
// 	freopen("input.txt","r",stdin);
// 	freopen("output.txt","w",stdout);
// #endif
// 	ll k;
// 	cin>>k;
// 	ll seq[5]={7,77,777,7777,77777};
// 	ll r=0;
// 	for( ;r<5;r++)
// 	{
// 		if(seq[r]%k==0)
// 		{
// 			cout<<r+1;
// 			break;
// 		}
// 	}
// 	if(r==5)
// 		cout<<-1<<endl;
// }
#include <bits/stdc++.h>
#define ll long long
using namespace std;
map<ll, ll> mp;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, q;
  cin >> n >> q;
  ll a[n];
  for (ll h = 0; h < n; h++) {
    cin >> a[h];
  }
  while (q--) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    for (ll y = l; y <= r; y++) {
      mp[a[y]];
    }
    cout << mp.size() << endl;
    mp.clear();
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
typedef long long ll;
int a[maxn], rt[maxn];
int lc[maxn * 40], rc[maxn * 40], tol;
ll sum[maxn * 40];
map<int, int> vis;
void init() {
  tol = 1;
  vis.clear();
}
void update(int &u, int x, int y, int p, ll v) {
  sum[tol] = sum[u] + v, lc[tol] = lc[u], rc[tol] = rc[u];
  u = tol++;
  if (x == y)
    return;
  int m = (x + y) >> 1;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
ll query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr)
    return sum[u];
  int m = (x + y) >> 1;
  ll ans = 0;
  if (ql <= m)
    ans += query(lc[u], x, m, ql, qr);
  if (qr > m)
    ans += query(rc[u], m + 1, y, ql, qr);
  return ans;
}
int main() {
  int n, q;
  init();
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (vis.find(a[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[a[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\\n", query(rt[r], 1, n, l, r));
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5;
typedef long long ll;
int a[maxn], rt[maxn];
int lc[maxn << 4], rc[maxn << 4], tol;
ll sum[maxn << 4];
map<int, int> vis;
void init() {
  tol = 1;
  vis.clear();
}
void update(int &u, int x, int y, int p, ll v) {
  sum[tol] = sum[u] + v, lc[tol] = lc[u], rc[tol] = rc[u];
  u = tol++;
  if (x == y)
    return;
  int m = (x + y) >> 1;
  if (p <= m)
    update(lc[u], x, m, p, v);
  else
    update(rc[u], m + 1, y, p, v);
}
ll query(int u, int x, int y, int ql, int qr) {
  if (ql <= x && y <= qr)
    return sum[u];
  int m = (x + y) >> 1;
  ll ans = 0;
  if (ql <= m)
    ans += query(lc[u], x, m, ql, qr);
  if (qr > m)
    ans += query(rc[u], m + 1, y, ql, qr);
  return ans;
}
int main() {
  int n, q;
  init();
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i]);
  for (int i = 1; i <= n; i++) {
    rt[i] = rt[i - 1];
    if (vis.find(a[i]) != vis.end()) {
      int tmp = rt[i - 1];
      update(tmp, 1, n, vis[a[i]], -1);
      rt[i] = tmp;
      update(rt[i], 1, n, i, 1);
    } else {
      rt[i] = rt[i - 1];
      update(rt[i], 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%lld\\n", query(rt[r], 1, n, l, r));
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int MAX_N = 262144 * 2;
vector<int64_t> seg(MAX_N * 2 - 1);
int64_t qu(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return 0;
  } else if (a <= l && r <= b) {
    return seg[k];
  } else {
    int64_t X = qu(a, b, k * 2 + 1, l, (l + r) / 2);
    int64_t Y = qu(a, b, k * 2 + 2, (l + r) / 2, r);
    return X + Y;
  }
}
void pu(int64_t a, int b) {
  int D = MAX_N - 1 + b;
  while (D > 0) {
    seg[D] += a;
    D--;
    D /= 2;
  }
  seg[0] += a;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<tuple<int, int, int>> q(Q);
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    q[i] = make_tuple(b, a, i);
  }
  sort(q.begin(), q.end());
  vector<int> la(N, -1);
  vector<int> an(Q);
  int W = 0;
  for (int i = 0; i < Q; i++) {
    int a, b, c;
    tie(a, b, c) = q[i];
    while (W < a) {
      if (la[p[W]] != -1) {
        pu(-1, la[p[W]]);
      }
      la[p[W]] = W;
      pu(1, W);
      W++;
    }
    an[c] = qu(b - 1, a, 0, 0, MAX_N);
  }
  for (int i = 0; i < Q; i++) {
    cout << an[i] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int MAX_N = 262144;
vector<int64_t> seg(MAX_N * 2 - 1);
int64_t qu(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l) {
    return 0;
  } else if (a <= l && r <= b) {
    return seg[k];
  } else {
    int64_t X = qu(a, b, k * 2 + 1, l, (l + r) / 2);
    int64_t Y = qu(a, b, k * 2 + 2, (l + r) / 2, r);
    return X + Y;
  }
}
void pu(int64_t a, int b) {
  int D = MAX_N - 1 + b;
  while (D > 0) {
    seg[D] += a;
    D--;
    D /= 2;
  }
  seg[0] += a;
}

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> p(N);
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    p[i]--;
  }
  vector<tuple<int, int, int>> q(Q);
  for (int i = 0; i < Q; i++) {
    int a, b;
    cin >> a >> b;
    q[i] = make_tuple(b, a, i);
  }
  sort(q.begin(), q.end());
  vector<int> la(N, -1);
  vector<int> an(Q);
  int W = 0;
  for (int i = 0; i < Q; i++) {
    int a, b, c;
    tie(a, b, c) = q[i];
    while (W < a) {
      if (la[p[W]] != -1) {
        pu(-1, la[p[W]]);
      }
      la[p[W]] = W;
      pu(1, W);
      W++;
    }
    an[c] = qu(b - 1, a, 0, 0, MAX_N);
  }
  for (int i = 0; i < Q; i++) {
    cout << an[i] << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 500000 + 500;
const int B = 1000000 + 1000;
const int M = 500000 + 200;

int Yuyuko;
int n, m;
int b[B], a[N], block[N], ans[M], cans = 1;
struct operation {
  int l, r, id;
} o[M];

inline int read() {
  int a = 0;
  char ch;
  int f = 1;
  while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')))
    ;
  if (ch == '-')
    f = -1;
  else {
    a = a * 10;
    a += ch - '0';
  }
  while ((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')) {
    a = a * 10;
    a += ch - '0';
  }
  return a * f;
}

inline bool cmp(operation x, operation y) {
  if (block[x.l] != block[y.l])
    return block[x.l] < block[y.l];
  else
    return x.r < y.r;
}

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

inline void minu(int x) {
  --b[a[x]];
  if (!b[a[x]])
    --cans;
}

int main() {
  n = read(), m = read();
  Yuyuko = sqrt(n) + 1;
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    block[i] = (i - 1) / Yuyuko + 1;
  }
  for (int i = 1; i <= m; ++i) {
    o[i].l = read(), o[i].r = read();
    o[i].id = i;
  }
  sort(o + 1, o + 1 + m, cmp);
  int L = 1, R = 1;
  b[a[1]] = 1;
  for (int i = 1; i <= m; ++i) {
    while (L < o[i].l)
      minu(L++);
    while (L > o[i].l)
      add(--L);
    while (R < o[i].r)
      add(++R);
    while (R > o[i].r)
      minu(R--);
    ans[o[i].id] = cans;
  }
  for (int i = 1; i <= m; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

const int N = 500000 + 500;
const int B = 1000000 + 1000;
const int M = 200000 + 200;

int Yuyuko;
int n, m;
int b[B], a[N], block[N], ans[M], cans = 1;
struct operation {
  int l, r, id;
} o[M];

inline int read() {
  int a = 0;
  char ch;
  int f = 1;
  while (!((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')))
    ;
  if (ch == '-')
    f = -1;
  else {
    a = a * 10;
    a += ch - '0';
  }
  while ((((ch = getchar()) >= '0') && (ch <= '9')) || (ch == '-')) {
    a = a * 10;
    a += ch - '0';
  }
  return a * f;
}

inline bool cmp(operation x, operation y) {
  if (block[x.l] != block[y.l])
    return block[x.l] < block[y.l];
  else
    return x.r < y.r;
}

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

inline void minu(int x) {
  --b[a[x]];
  if (!b[a[x]])
    --cans;
}

int main() {
  n = read(), m = read();
  Yuyuko = sqrt(n) + 1;
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
    block[i] = (i - 1) / Yuyuko + 1;
  }
  for (int i = 1; i <= m; ++i) {
    o[i].l = read(), o[i].r = read();
    o[i].id = i;
  }
  sort(o + 1, o + 1 + m, cmp);
  int L = 1, R = 1;
  b[a[1]] = 1;
  for (int i = 1; i <= m; ++i) {
    while (L < o[i].l)
      minu(L++);
    while (L > o[i].l)
      add(--L);
    while (R < o[i].r)
      add(++R);
    while (R > o[i].r)
      minu(R--);
    ans[o[i].id] = cans;
  }
  for (int i = 1; i <= m; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} _io;
typedef long long ll;
typedef long double db;
const int N = 5e5 + 5, M = 1e9 + 7;

typedef pair<int, int> pii;
#define fi first
#define se second

int bit[N];
void add(int p, int v) {
  for (; p < N; p += p & -p)
    bit[p] += v;
}
int get(int p) {
  int res = 0;
  for (; p; p -= p & -p)
    res += bit[p];
  return res;
}
vector<pii> q[N];
int ans[N], lst[N], a[N];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int l, r;
  for (int i = 1; i <= m; i++) {
    cin >> l >> r;
    q[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (lst[a[i]])
      add(lst[a[i]], -1);
    add(i, 1);
    lst[a[i]] = i;
    for (auto &p : q[i]) {
      ans[p.se] = get(i) - get(p.fi - 1);
    }
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct _IO {
  _IO() {
    ios::sync_with_stdio(0);
    cin.tie(0);
  }
} _io;
typedef long long ll;
typedef long double db;
const int N = 3e5 + 5, M = 1e9 + 7;

typedef pair<int, int> pii;
#define fi first
#define se second

int bit[N];
void add(int p, int v) {
  for (; p < N; p += p & -p)
    bit[p] += v;
}
int get(int p) {
  int res = 0;
  for (; p; p -= p & -p)
    res += bit[p];
  return res;
}
vector<pii> q[N];
int ans[N], lst[N], a[N];

int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int l, r;
  for (int i = 1; i <= m; i++) {
    cin >> l >> r;
    q[r].push_back({l, i});
  }
  for (int i = 1; i <= n; i++) {
    if (lst[a[i]])
      add(lst[a[i]], -1);
    add(i, 1);
    lst[a[i]] = i;
    for (auto &p : q[i]) {
      ans[p.se] = get(i) - get(p.fi - 1);
    }
  }
  for (int i = 1; i <= m; i++) {
    cout << ans[i] << '\\n';
  }
}