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

#include <bits/stdc++.h>

#define FAST_IO ios_base::sync_with_stdio(false), cin.tie(nullptr)
#define setIO(i, o) freopen(i, "r", stdin), freopen(o, "w", stdout)
#define trav(e, x) for (auto &(e) : (x))
#define pb(x) push_back(x)
#define eb(x...) emplace_back(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define lc(i) 2 * i
#define rc(i) 2 * i + 1
// #define int long long
using namespace std;
using vi = vector<int>;
using ii = pair<int, int>;

const int MAXN = 1e5, INF = 1e9 + 5, MOD = 1e9 + 7;

struct SegmentTree {
  int n;
  vi a, t;

  int merge(int x, int y) { return x + y; }

  void build(int i, int l, int r) {
    if (l == r) {
      t[i] = a[l];
      return;
    }
    int mid = (l + r) / 2;
    build(lc(i), l, mid);
    build(rc(i), mid + 1, r);
    t[i] = merge(t[lc(i)], t[rc(i)]);
  }

  SegmentTree(int x, vi y) {
    n = x;
    a = y;
    t.assign(4 * n, 0);
    build(1, 0, n - 1);
  }

  void modify(int i, int l, int r, int pos, int val) {
    if (l > pos || r < pos)
      return;
    if (l >= pos && r <= pos) {
      t[i] = val;
      return;
    }
    int mid = (l + r) / 2;
    modify(lc(i), l, mid, pos, val);
    modify(rc(i), mid + 1, r, pos, val);
    t[i] = merge(t[lc(i)], t[rc(i)]);
  }

  int query(int i, int l, int r, int ql, int qr) {
    if (l > qr || r < ql)
      return 0;
    if (l >= ql && r <= qr)
      return t[i];
    int mid = (l + r) / 2;
    int resL = query(lc(i), l, mid, ql, qr);
    int resR = query(rc(i), mid + 1, r, ql, qr);
    return merge(resL, resR);
  }

  void modify(int p, int v) { modify(1, 0, n - 1, p, v); }

  int query(int l, int r) { return query(1, 0, n - 1, l, r); }
};

signed main() {
  FAST_IO;
  //    setIO("input.txt", "output.txt");

  int n, q;
  cin >> n >> q;
  vi a(n);
  trav(x, a) {
    cin >> x;
    x--;
  }
  vector<ii> queries[n];
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    queries[r].eb(l, i);
  }
  vi last(n, -1), ans(q);
  SegmentTree freq(n, vi(n, 0));
  for (int i = 0; i < n; i++) {
    if (last[a[i]] != -1)
      freq.modify(last[a[i]], 0);
    last[a[i]] = i;
    freq.modify(i, 1);
    for (auto query : queries[i]) {
      int l = query.first;
      int idx = query.second;
      ans[idx] = freq.query(l, i);
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define LL long long int
#define s(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define ss(a) scanf("%s", a)
#define w(t) while (t--)
#define f(i, n) for (i = 0; i < n; i++)
#define fd(i, n) for (i = n - 1; i >= 0; i--)
#define p(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define ps(a) printf("%s", a)
#define pc(a) printf("%c", a)
#define ent printf("\\n")
#define mod 1000000007
#define PI 3.14159265
#define gs getline(cin, s)
#define pb push_back
#define mp make_pair
#define INF 1e18

using namespace std;

unordered_map<int, int> pos;
int ar[500005];
int tree[16 * 300000 + 5];
int lft[16 * 300000 + 5];
int rht[16 * 300000 + 5];
int root[600005];
int rt[500005];
int idx;

inline void build(int node, int start, int end) {
  if (start == end) {
    tree[node] = 0;
    return;
  }
  lft[node] = idx++;
  rht[node] = idx++;
  int mid = (start + end) / 2;
  build(lft[node], start, mid);
  build(rht[node], mid + 1, end);
  tree[node] = 0;
}

inline int update(int node, int start, int end, int pos, int val) {
  int x;
  x = idx++;
  if (start == end) {
    tree[x] = val;
    return x;
  }
  lft[x] = lft[node];
  rht[x] = rht[node];
  int mid = (start + end) / 2;
  if (pos <= mid)
    lft[x] = update(lft[x], start, mid, pos, val);
  else
    rht[x] = update(rht[x], mid + 1, end, pos, val);

  tree[x] = tree[lft[x]] + tree[rht[x]];

  return x;
}

inline int query(int node, int start, int end, int l, int r) {
  if (start > r || end < l)
    return 0;
  else if (start >= l && end <= r)
    return tree[node];
  int mid = (start + end) / 2;
  int q1 = query(lft[node], start, mid, l, r);
  int q2 = query(rht[node], mid + 1, end, l, r);

  return (q1 + q2);
}

int main() {
std:
  ios_base::sync_with_stdio(false);
  int n, m;
  s(n);
  s(m);

  int i;

  idx = 0;
  root[0] = 0;
  build(0, 1, n);

  int t = 1;
  for (i = 1; i <= n; i++) {
    s(ar[i]);
    int k = pos[ar[i]];
    if (k != 0) {
      root[t] = update(root[t - 1], 1, n, k, 0);
      t++;
      root[t] = update(root[t - 1], 1, n, i, 1);
      t++;
    } else {
      root[t] = update(root[t - 1], 1, n, i, 1);
      t++;
    }

    rt[i] = t - 1;
    pos[ar[i]] = i;
  }

  while (m--) {
    int a, b;
    s(a);
    s(b);

    p(query(root[rt[b]], 1, n, a, b));
    ent;
  }

  return 0;
}