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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
const int MAX = 500005;
const int MAX1 = 1e6 + 5;
int tree[MAX], p[MAX1], ans[MAX], a[MAX];
pair<int, pair<int, int>> pr[MAX];

void update(int idx, int val) {
  while (idx < MAX) {
    tree[idx] += val;
    idx += (idx & -idx);
  }
}

int read(int idx) {
  int sum = 0;
  while (idx > 0) {
    sum += tree[idx];
    idx -= (idx & -idx);
  }
  return sum;
}

int main() {
  int n, q, x;

  scanf("%d", &n);
  scanf("%d", &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 0; i < q; ++i) {
    scanf("%d %d", &pr[i].second.first, &pr[i].first);
    pr[i].second.second = i;
  }
  sort(pr, pr + q);
  memset(p, -1, sizeof(p));
  memset(tree, 0, sizeof(tree));
  x = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);
    while (x < q and pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);
      x++;
    }
  }
  for (int i = 0; i < q; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: TLE

Fixed submission:


// Problem : F - Range Set Query
// Contest : AtCoder - AtCoder Beginner Contest 174
// URL : https://atcoder.jp/contests/abc174/tasks/abc174_f
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 1; i <= n; i++)
#define mod 1000000007
#define pb push_back
#define ff first
#define ss second
#define ii pair<int, int>
#define vi vector<int>
#define vii vector<ii>
#define lli long long int
#define INF 1000000000
#define endl '\\n'
#define BLOCK 707
const double PI = 3.141592653589793238460;
typedef std::complex<double> Complex;
typedef std::valarray<Complex> CArray;

using namespace std;
struct query {
  int l;
  int r;
  int i;
};
query Q[500008];
int ar[500008], ans[500008];
int fre[1000001];
int cnt = 0;

bool comp(query a, query b) {
  if (a.l / BLOCK != b.l / BLOCK)
    return a.l / BLOCK < b.l / BLOCK;

  return a.r < b.r;
}

void add(int pos) {
  fre[ar[pos]]++;
  if (fre[ar[pos]] == 1)
    cnt++;
}

void remove(int pos) {
  fre[ar[pos]]--;
  if (fre[ar[pos]] == 0)
    cnt--;
}

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

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

  for (int i = 0; i < q; i++) {
    cin >> Q[i].l >> Q[i].r;
    Q[i].i = i, Q[i].l--, Q[i].r--;
  }

  sort(Q, Q + q, comp);

  // why ML = 0 , and MR = -1?
  int ML = 0, MR = -1;
  for (int i = 0; i < q; i++) {
    int L = Q[i].l;
    int R = Q[i].r;

    while (ML > L)
      ML--, add(ML);

    while (MR < R)
      MR++, add(MR);

    while (ML < L)
      remove(ML), ML++;

    while (MR > R)
      remove(MR), MR--;

    ans[Q[i].i] = cnt;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << '\\n';
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int N = 5e5 + 11;

namespace IO {
struct q_instream {
  template <typename classT> inline q_instream operator>>(classT &e) const {
    e = 0;
    classT f = 1, c = 0;
    while (c < '0' || c > '9') {
      if (c == '-')
        f = -1;
      c = getchar();
    }
    while (c >= '0' && c <= '9')
      e = e * 10 + c - '0', c = getchar();
    return e = e * f, (*this);
  }
} in;

struct q_outstream {
  template <typename classT>
  inline q_outstream operator<<(const classT &e) const {
    if (e < 0) {
      putchar('-');
      (*this) << -e;
    } else {
      if (e > 9)
        (*this) << (e / 10);
      putchar(e % 10 + '0');
    }
    return (*this);
  }
  inline q_outstream operator<<(const char &c) const {
    return putchar(c), (*this);
  }
} out;
} // namespace IO
using namespace IO;
int B;
struct Query {
  int le, ri;
  int id;
  bool operator<(const Query &b) const {
    if (le / B == b.le / B)
      return ri < b.ri;
    else
      return le / B < b.le / B;
  }
} Q[N];
int ANS[N], cnt[N], a[N], ans;
void Add(int x) {
  x = a[x];
  if (cnt[x] == 0)
    ans++;
  cnt[x]++;
}
void Del(int x) {
  x = a[x];
  cnt[x]--;
  if (cnt[x] == 0)
    ans--;
}

void solve(int q) {
  sort(Q + 1, Q + q + 1);
  memset(cnt, 0, sizeof(cnt));
  int L = 1, R = 0;
  ans = 0;
  for (int i = 1; i <= q; i++) {
    while (R < Q[i].ri)
      Add(++R);
    while (R > Q[i].ri)
      Del(R--);
    while (L < Q[i].le)
      Del(L++);
    while (L > Q[i].le)
      Add(--L);

    ANS[Q[i].id] = ans;
  }
  for (int i = 1; i <= q; i++) {
    out << ANS[i];
    puts("");
  }
}

int main() {
  int n, q;
  in >> n;
  in >> q;
  B = (int)sqrt(n * 1.0);
  for (int i = 1; i <= n; i++)
    in >> a[i];
  for (int i = 1; i <= q; i++) {
    in >> Q[i].le;
    in >> Q[i].ri;
    Q[i].id = i;
  }
  solve(q);
  return 0;
}

Error: TLE

Fixed submission:

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

const int N = 5e5 + 11;

int B;
struct Query {
  int le, ri;
  int id;
  bool operator<(const Query &b) const {
    if (le / B == b.le / B)
      return ri < b.ri;
    else
      return le / B < b.le / B;
  }
} Q[N];
int ANS[N], cnt[N], a[N], ans;
void Add(int x) {
  x = a[x];
  if (cnt[x] == 0)
    ans++;
  cnt[x]++;
}
void Del(int x) {
  x = a[x];
  cnt[x]--;
  if (cnt[x] == 0)
    ans--;
}

void solve(int q) {
  sort(Q + 1, Q + q + 1);
  memset(cnt, 0, sizeof(cnt));
  int L = 1, R = 0;
  ans = 0;
  for (int i = 1; i <= q; i++) {
    while (R < Q[i].ri)
      Add(++R);
    while (R > Q[i].ri)
      Del(R--);
    while (L < Q[i].le)
      Del(L++);
    while (L > Q[i].le)
      Add(--L);

    ANS[Q[i].id] = ans;
  }
  for (int i = 1; i <= q; i++)
    printf("%d\\n", ANS[i]);
}

int main() {
  int n, q;
  while (scanf("%d%d", &n, &q) != EOF) {
    B = (int)sqrt(n * 1.0);
    for (int i = 1; i <= n; i++)
      scanf("%d", &a[i]);
    for (int i = 1; i <= q; i++) {
      scanf("%d%d", &Q[i].le, &Q[i].ri);
      Q[i].id = i;
    }
    solve(q);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

// by ynymxiaolongbao
#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: 0

Fixed submission:

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

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define INF 10000000000
#define MOD 1000000007
using ll = long long;
using pint = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;

int N, Q;

// BIT,O(log(N))
int bit[500010];
// v[a] += w
void add(int a, int w) {
  for (int x = a; x <= N; x += x & -x)
    bit[x] += w;
}
// v[1] + ... + v[a]
int sum(int a) {
  int ans = 0;
  for (int x = a; x > 0; x -= x & -x)
    ans += bit[x];
  return ans;
}
// v[l] + ... + v[r]
int range_sum(int l, int r) { return sum(r) - sum(l - 1); }

int main() {
  cin >> N >> Q;

  vector<int> res(N, 0); // res[i] クエリiの答え
  // 1-index
  vector<int> A(N + 1, 0); // A[i] 種類iの玉の最も右側のインデックス。
  vector<int> C(N + 1, 0); // C[i] i番目の玉
  vector<vector<int>> q_l(N + 1); // q_l[r] 右端がrのときの左端のindexnの配列。
  vector<vector<int>> q_ind(N + 1); // q_ind[r] 右端がrのときの問題の0-index

  rep1(i, N) cin >> C[i];
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    q_l[r].push_back(l);
    q_ind[r].push_back(i);
  }

  rep(i, 5e5 + 1) bit[i] =
      0; // bit[i] = i番目の玉がその種類の中で最も右端だったら１
  rep1(i, N) A[i] = 0;
  rep1(i, N) {
    // 左端から順に玉を見ていく。
    if (A[C[i]] > 0)
      add(A[C[i]], -1); // bit[A[C[i]]] = 0にする。
    add(i, 1);          // bit[i] = 1にする。
    A[C[i]] = i;        // C[i]の最も右側のindはiである。
    rep(j, q_l[i].size()) { res[q_ind[i][j]] = range_sum(q_l[i][j], i); }
  }
  rep(i, Q) cout << res[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

#define N 500001

typedef struct Query {
  int l, r, idx;
} query;
int block;

bool cmp(query a, query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}

int ans[N], c[N], cnt[N];
query q[N];
int main() {
  fastIO;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> c[i];
  block = (int)sqrt(n);
  for (int i = 0; i < m; i++)
    cin >> q[i].l >> q[i].r, q[i].idx = i;
  sort(q, q + m, cmp);

  int l = 0, r = -1, temp = 0;
  for (int i = 0; i < m; i++) {
    int ql = q[i].l - 1, qr = q[i].r - 1;
    while (r < qr)
      temp += !cnt[c[++r]]++;
    while (r > qr)
      temp -= !--cnt[c[r--]];
    while (l < ql)
      temp -= !--cnt[c[l++]];
    while (l > ql)
      temp += !cnt[c[--l]]++;
    ans[q[i].idx] = temp;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x7fffffff
typedef long long ll;
typedef long double ld;

#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()

#define N 300001

typedef struct Query {
  int l, r, idx;
} query;
int block;

bool cmp(query a, query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}

int ans[N], c[N], cnt[N];
query q[N];
int main() {
  fastIO;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> c[i];
  block = (int)sqrt(n);
  for (int i = 0; i < m; i++)
    cin >> q[i].l >> q[i].r, q[i].idx = i;
  sort(q, q + m, cmp);

  int l = 0, r = -1, temp = 0;
  for (int i = 0; i < m; i++) {
    int ql = q[i].l - 1, qr = q[i].r - 1;
    while (r < qr)
      temp += !cnt[c[++r]]++;
    while (r > qr)
      temp -= !--cnt[c[r--]];
    while (l < ql)
      temp -= !--cnt[c[l++]];
    while (l > ql)
      temp += !cnt[c[--l]]++;
    ans[q[i].idx] = temp;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << endl;

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<vector<int>> seg;

int query(int l, int r, int u, int a, int b) {
  if (l <= a && b <= r)
    return lower_bound(seg[u].begin(), seg[u].end(), l) - seg[u].begin();

  int res = 0;
  if (l < (a + b) / 2)
    res += query(l, r, 2 * u + 1, a, (a + b) / 2);
  if ((a + b) / 2 < r)
    res += query(l, r, 2 * u + 2, (a + b) / 2, b);
  return res;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  vector<int> a(n);
  rep(i, n) scanf("%d", &a[i]), a[i]--;

  vector<int> pre(n, -1);
  rep(i, n) {
    int x = a[i];
    a[i] = pre[x];
    pre[x] = i;
  }

  int N;
  for (N = 1; N < n; N <<= 1)
    ;
  seg.resize(2 * N - 1);
  rep(i, n) seg[N - 1 + i].emplace_back(a[i]);
  for (int i = N - 2; i >= 0; i--) {
    merge(seg[2 * i + 1].begin(), seg[2 * i + 1].end(), seg[2 * i + 2].begin(),
          seg[2 * i + 2].end(), back_inserter(seg[i]));
  }

  rep(_, q) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    printf("%d\\n", query(l, r, 0, 0, N));
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<vector<int>> seg;

int query(int l, int r, int u, int a, int b) {
  if (l <= a && b <= r)
    return lower_bound(seg[u].begin(), seg[u].end(), l) - seg[u].begin();

  int res = 0;
  if (l < (a + b) / 2)
    res += query(l, r, 2 * u + 1, a, (a + b) / 2);
  if ((a + b) / 2 < r)
    res += query(l, r, 2 * u + 2, (a + b) / 2, b);
  return res;
}

int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  vector<int> a(n);
  rep(i, n) scanf("%d", &a[i]), a[i]--;

  vector<int> pre(1e5, -1);
  rep(i, n) {
    int x = a[i];
    a[i] = pre[x];
    pre[x] = i;
  }

  int N;
  for (N = 1; N < n; N <<= 1)
    ;
  seg.resize(2 * N - 1);
  rep(i, n) seg[N - 1 + i].emplace_back(a[i]);
  for (int i = N - 2; i >= 0; i--) {
    merge(seg[2 * i + 1].begin(), seg[2 * i + 1].end(), seg[2 * i + 2].begin(),
          seg[2 * i + 2].end(), back_inserter(seg[i]));
  }

  rep(_, q) {
    int l, r;
    scanf("%d%d", &l, &r);
    l--;
    printf("%d\\n", query(l, r, 0, 0, N));
  }

  return 0;
}
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 = 500010;
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));

  int t = 0;
  scanf("%d", &q);
  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;
  }

  int ab;
  cin >> ab;
  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));

  int t = 0;
  scanf("%d", &q);
  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;
  }

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

#include <bits/stdc++.h>
#define maxi 1000001
using namespace std;
struct trie {
  int l;
  int r;
  int idx;
};
void updatr(vector<int> &bits, int val, int id, int n) {

  while (id <= n) {
    bits[id] += val;
    id += (id & -id);
  }
}
int getsum(vector<int> &bits, int id, int n) {
  int sum = 0;

  while (id > 0) {
    sum += bits[id];
    id -= (id & (-id));
  }
  return sum;
}
void findr(vector<int> arr, vector<trie> q, int n, int qc) {
  vector<int> bits(n + 1, 0);
  vector<int> first(maxi, -1);
  vector<int> ans(qc);
  int qi = 0;
  for (int i = 0; i < n; i++) {

    if (first[arr[i]] != -1)
      updatr(bits, -1, first[arr[i]] + 1, n);
    first[arr[i]] = i;
    updatr(bits, 1, i + 1, n);

    while (qi < qc && q[qi].r == i) {
      // cout<<1<<endl;
      ans[q[qi].idx] = getsum(bits, q[qi].r + 1, n) - getsum(bits, q[qi].l, n);
      qi++;
    }
  }

  for (int i = 0; i < qc; i++) {

    cout << ans[i] << endl;
  }
}
bool cmp(trie a, trie b) { return a.r < b.r; }
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> arr(maxi);
  for (int i = 0; i < n; i++) {

    cin >> arr[i];
  }
  vector<trie> v(q);
  for (int i = 0; i < q; i++) {
    cin >> v[i].l >> v[i].r;
    v[i].idx = i;
    v[i].l--;
    v[i].r--;
  }
  sort(v.begin(), v.end(), cmp);
  findr(arr, v, n, q);
  // vector<int> freq(maxi);
  return 0;
}


Error: TLE

Fixed submission:

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

signed main() {
  int n, q, t;
  cin >> n >> q;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    cin >> t;
    v.push_back(t);
  }
  int i1, i2;
  for (int i = 0; i < q; i++) {
    cin >> i1 >> i2;
    int c = 0;
    set<int> s;
    for (int j = i1 - 1; j < i2; j++) {
      // cout<<v[j]<<" ";
      if (s.insert(v[j]).second)
        c++;
    }
    cout << c << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const int MAX = 500005;
const int MAX_V = 500005;

int sqrtN;
int N, M, A[MAX], result[MAX];

struct QueryNode {
  int s, e, n;
  QueryNode() : QueryNode(0, 0, -1) {}
  QueryNode(int s1, int e1, int n1) : s(s1), e(e1), n(n1) {}
  bool operator<(const QueryNode &O) const {
    if (s / sqrtN != O.s / sqrtN)
      return (s / sqrtN < O.s / sqrtN);
    return (e < O.e);
  }
};

int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sqrtN = sqrt(N);

  QueryNode Q[MAX];
  for (int i = 0; i < M; ++i) {
    int l, r;
    cin >> l >> r;
    Q[i] = QueryNode(l - 1, r, i);
  }
  sort(Q, Q + M);

  int dcnt = 0, cnt[MAX_V + 1] = {0};
  int s = Q[0].s, e = Q[0].e;
  for (int i = s; i < e; i++)
    if (cnt[A[i]]++ == 0)
      ++dcnt;
  result[Q[0].n] = dcnt;

  for (int i = 1; i < M; ++i) {
    while (Q[i].s < s)
      if (cnt[A[--s]]++ == 0)
        ++dcnt;
    while (e < Q[i].e)
      if (cnt[A[e++]]++ == 0)
        ++dcnt;
    while (Q[i].s > s)
      if (--cnt[A[s++]] == 0)
        --dcnt;
    while (e > Q[i].e)
      if (--cnt[A[--e]] == 0)
        --dcnt;
    result[Q[i].n] = dcnt;
  }

  for (int i = 0; i < M; ++i)
    cout << result[i] << '\\n';
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int MAX = 100000;
const int MAX_V = 1000000;

int sqrtN;
int N, M, A[MAX], result[MAX];

struct QueryNode {
  int s, e, n;
  QueryNode() : QueryNode(0, 0, -1) {}
  QueryNode(int s1, int e1, int n1) : s(s1), e(e1), n(n1) {}
  bool operator<(const QueryNode &O) const {
    if (s / sqrtN != O.s / sqrtN)
      return (s / sqrtN < O.s / sqrtN);
    return (e < O.e);
  }
};

int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> N >> M;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  sqrtN = sqrt(N);

  QueryNode Q[MAX];
  for (int i = 0; i < M; ++i) {
    int l, r;
    cin >> l >> r;
    Q[i] = QueryNode(l - 1, r, i);
  }
  sort(Q, Q + M);

  int dcnt = 0, cnt[MAX_V + 1] = {0};
  int s = Q[0].s, e = Q[0].e;
  for (int i = s; i < e; ++i)
    if (cnt[A[i]]++ == 0)
      ++dcnt;
  result[Q[0].n] = dcnt;

  for (int i = 1; i < M; ++i) {
    while (Q[i].s < s)
      if (cnt[A[--s]]++ == 0)
        ++dcnt;
    while (e < Q[i].e)
      if (cnt[A[e++]]++ == 0)
        ++dcnt;
    while (Q[i].s > s)
      if (--cnt[A[s++]] == 0)
        --dcnt;
    while (e > Q[i].e)
      if (--cnt[A[--e]] == 0)
        --dcnt;
    result[Q[i].n] = dcnt;
  }

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

class query {
public:
  int l, r, in;
};
long long int pole[2000079];
void ii(long long int kde, long long int r, int n) {
  while (kde <= n) {
    pole[kde] += r;
    kde += kde & -kde;
  }
}
long long int ss(long long int kde) {
  long long int sum = 0;
  while (kde > 0) {
    sum += pole[kde];
    kde = kde & (kde - 1);
  }
  return sum;
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  vector<int> ans(q, 0);
  vector<vector<query>> v(n);
  for (int i = 0; i < q; i++) {
    query a;
    cin >> a.l >> a.r;
    a.l--;
    a.r--;
    a.in = i;
    v[a.r].push_back(a);
  }
  vector<int> last(n + 1, -1);
  int in = 0;
  for (int r = 0; r < n; r++) {
    ii(r + 1, 1, n + 1);
    if (last[c[r]] != -1)
      ii(last[c[r]] + 1, -1, n + 1);
    last[c[r]] = r;
    for (query i : v[r])
      ans[i.in] = ss(r + 1) - ss(i.l);
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
// #pragma optimize GCC("O3")
typedef long long ll;
typedef long double ld;
using namespace std;

class query {
public:
  int l, r, in;
};
long long int pole[2000079];
void ii(long long int kde, long long int r, int n) {
  while (kde <= n) {
    pole[kde] += r;
    kde += kde & -kde;
  }
}
long long int ss(long long int kde) {
  long long int sum = 0;
  while (kde > 0) {
    sum += pole[kde];
    kde = kde & (kde - 1);
  }
  return sum;
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> c(n);
  for (int i = 0; i < n; i++)
    cin >> c[i];
  vector<int> ans(n, 0);
  vector<vector<query>> v(n);
  for (int i = 0; i < q; i++) {
    query a;
    cin >> a.l >> a.r;
    a.l--;
    a.r--;
    a.in = i;
    v[a.r].push_back(a);
  }
  vector<int> last(n + 1, -1);
  int in = 0;
  for (int r = 0; r < n; r++) {
    ii(r + 1, 1, n + 1);
    if (last[c[r]] != -1)
      ii(last[c[r]] + 1, -1, n + 1);
    last[c[r]] = r;
    for (query i : v[r])
      ans[i.in] = ss(r + 1) - ss(i.l);
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
  return 0;
}