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

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

struct trip {
  int fi, se, th;
};

bool operator<(const trip &x, const trip &y) {
  return std::tie(x.fi, x.se, x.th) < std::tie(y.fi, y.se, y.th);
}

struct fenwickTree {
private:
  int n;
  vector<int> v;

public:
  fenwickTree(int n) {
    this->n = n;
    int i;
    for (i = 0; i < n; i++)
      v.push_back(0);
  }
  int query(int r) {
    int cur = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
      cur += v[r];
    return cur;
  }
  int query(int l, int r) { return query(r) - query(l - 1); }
  void add(int idx, int val) {
    for (; idx <= n; idx = idx | (idx + 1))
      v[idx] += val;
  }
  void rep(int idx, int val) {
    int cha = val - query(idx, idx);
    add(idx, cha);
  }
  void print() {
    int i;
    for (i = 0; i < n; i++)
      cout << query(i, i) << " ";
    cout << "\\n";
  }
};

int main() {
  int n, q, i, j;
  cin >> n >> q;
  int ans[q];
  int val[n];
  int last[n];
  vector<trip> ve;
  fenwickTree thisTree(n);
  for (i = 0; i < n; i++) {
    int a;
    cin >> a;
    val[i] = a - 1;
    last[i] = -1;
  }
  for (i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    ve.push_back({b - 1, a - 1, i});
  }
  sort(ve.begin(), ve.end());
  int pointer = 0;
  for (i = 0; i < n; i++) {
    int cur = last[val[i]];
    if (cur != -1) {
      thisTree.rep(cur, 0);
    }
    last[val[i]] = i;
    thisTree.rep(i, 1);
    while (pointer < q && ve[pointer].fi <= i) {
      int l = ve[pointer].se;
      int r = ve[pointer].fi;
      ans[ve[pointer].th] = thisTree.query(l, r);
      pointer++;
    }
  }
  for (i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }
}


Error: 0

Fixed submission:

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

struct trip {
  int fi, se, th;
};

bool operator<(const trip &x, const trip &y) {
  return std::tie(x.fi, x.se, x.th) < std::tie(y.fi, y.se, y.th);
}

struct fenwickTree {
private:
  int n;
  vector<int> v;

public:
  fenwickTree(int n) {
    this->n = n;
    int i;
    for (i = 0; i < n; i++)
      v.push_back(0);
  }
  int query(int r) {
    int cur = 0;
    for (; r >= 0; r = (r & (r + 1)) - 1)
      cur += v[r];
    return cur;
  }
  int query(int l, int r) { return query(r) - query(l - 1); }
  void add(int idx, int val) {
    for (; idx <= n; idx = idx | (idx + 1))
      v[idx] += val;
  }
  void rep(int idx, int val) {
    int cha = val - query(idx, idx);
    add(idx, cha);
  }
  void print() {
    int i;
    for (i = 0; i < n; i++)
      cout << query(i, i) << " ";
    cout << "\\n";
  }
};

int main() {
  int n, q, i, j;
  cin >> n >> q;
  int ans[n];
  int val[n];
  int last[n];
  vector<trip> ve;
  fenwickTree thisTree(2 * n);
  for (i = 0; i < n; i++) {
    int a;
    cin >> a;
    val[i] = a - 1;
    last[i] = -1;
  }
  for (i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    ve.push_back({b - 1, a - 1, i});
  }
  sort(ve.begin(), ve.end());
  int pointer = 0;
  for (i = 0; i < n; i++) {
    int cur = last[val[i]];
    if (cur != -1) {
      thisTree.rep(cur, 0);
    }
    last[val[i]] = i;
    thisTree.rep(i, 1);
    while (pointer < q && ve[pointer].fi <= i) {
      int l = ve[pointer].se;
      int r = ve[pointer].fi;
      ans[ve[pointer].th] = thisTree.query(l, r);
      pointer++;
    }
  }
  for (i = 0; i < q; i++) {
    cout << ans[i] << "\\n";
  }
}