Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e6 + 10;
const int maxM = 1e6 + 10;
const int inf = 0x3f3f3f3f;

int N, M;
int a[maxn], pos[maxn], curpos = 1;
int tr[maxn];
int ans[maxn];
struct node {
  int l, r, id;
  bool operator<(const node &o) const { return r < o.r; }
} lr[maxn];
int lowbit(int x) { return x & -x; }
void add(int x, int v) {
  for (int i = x; i <= N; i += lowbit(i))
    tr[i] += v;
}
int query(int r) {
  int sum = 0;
  for (int i = r; i >= 1; i -= lowbit(i))
    sum += tr[i];
  return sum;
}
inline void read(int &x) {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  x = s * w;
}
void todo(int r) {
  // cout<<"curpos = "<<curpos<<'\\n';
  for (int i = curpos; i <= r; i++) {
    if (pos[a[i]] == 0) {
      pos[a[i]] = i;
      add(i, 1);
    } else {
      add(pos[a[i]], -1);
      pos[a[i]] = i;
      add(i, 1);
    }
  }
  curpos = r + 1;
}
void solve() {
  sort(lr + 1, lr + M + 1);
  for (int i = 1; i <= M; i++) {
    todo(lr[i].r);
    ans[lr[i].id] = query(lr[i].r) - query(lr[i].l - 1);
  }
  for (int i = 1; i <= M; i++)
    printf("%d\\n", ans[i]);
}
int main() {
  // debug;
  // ios;

  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    read(a[i]);
  for (int i = 1; i <= M; i++) {
    lr[i].id = i;
    read(lr[i].l);
    read(lr[i].r);
  }
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define debug freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5 + 10;
const int maxM = 1e6 + 10;
const int inf = 0x3f3f3f3f;

int N, Q;
int c[maxn];

struct node {
  int l, r;
  set<int> st;
} tr[maxn * 4];
int cnt = 0;
void pushup(node &cur, node &L, node &R) {
  for (auto v : L.st)
    cur.st.insert(v);
  for (auto v : R.st)
    cur.st.insert(v);
}
void build(int l, int r, int u = 1) {
  tr[u] = {l, r};
  if (l == r) {
    tr[u].st.insert(c[l]);
  } else {
    int mid = (l + r) >> 1;
    build(l, mid, u * 2);
    build(mid + 1, r, u * 2 + 1);
    pushup(tr[u], tr[u * 2], tr[u * 2 + 1]);
  }
}
node query(int l, int r, int u = 1) {
  if (l <= tr[u].l && tr[u].r <= r)
    return tr[u];
  else {
    int mid = (tr[u].l + tr[u].r) >> 1;
    if (r <= mid)
      return query(l, r, u * 2);
    else if (l > mid)
      return query(l, r, u * 2 + 1);
    else {
      node L = query(l, r, u * 2);
      node R = query(l, r, u * 2 + 1);
      node ans;
      pushup(ans, L, R);
      return ans;
    }
  }
}
int main() {
  // debug;
  ios;

  cin >> N >> Q;
  for (int i = 1; i <= N; i++)
    cin >> c[i];
  build(1, N);
  while (Q--) {
    int l, r;
    cin >> l >> r;
    node ans = query(l, r);
    cout << ans.st.size() << '\\n';
  }

  return 0;
}