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

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define moca(a, s, b) a = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

class sumSegTree {
public:
  ll n, height;
  vector<ll> data;

  sumSegTree(ll _n) {
    n = 1;
    height = 1;
    while (n < _n) {
      n *= 2;
      height++;
    }
    data = vector<ll>(2 * n - 1);
  }

  void add(ll i, ll x) {
    i += n - 1;
    data[i] += x;
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] += x;
    }
  }

  //[a, b):要求区間，[l, r):対象区間
  ll _sum(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return data[k];
    else {
      ll s = _sum(a, b, 2 * k + 1, l, (l + r) / 2);
      ll t = _sum(a, b, 2 * k + 2, (l + r) / 2, r);
      return s + t;
    }
  }

  ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); }
};

ll c[MAX * 5];
ll pos[MAX * 5];

int main(void) {
  ll i, j, k;

  ll N, Q;
  cin >> N >> Q;
  for (i = 0; i < N; i++) {
    cin >> c[i];
    c[i]--;
  }

  vector<pair<pll, ll>> v;

  for (i = 0; i < Q; i++) {
    ll l, r;
    cin >> l >> r;
    l--;
    r--;
    v.push_back({{r, l}, i});
  }
  sort(v.begin(), v.end());

  for (i = 0; i < Q; i++)
    swap(v[i].ft.ft, v[i].ft.sc);

  sumSegTree sst(N);

  set<ll> st;
  memset(pos, -1, sizeof(pos));

  ll ans[MAX * 5];
  for (ll q = 0; q < Q; q++) {
    ll l = v[q].ft.ft;
    ll r = v[q].ft.sc;

    for (i = (q == 0) ? 0 : v[q - 1].ft.sc + 1; i <= r; i++) {
      if (st.find(c[i]) != st.end())
        sst.add(pos[c[i]], -1);

      st.insert(c[i]);
      pos[c[i]] = i;
      sst.add(i, 1);
    }

    ans[v[q].sc] = sst.sum(l, r + 1);
  }

  for (i = 0; i < Q; i++)
    pt(ans[i]);
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\\n"
#define moca(a, s, b) a = ((a)s(b) + MOD) % MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
static const ll INF = 1e18;
static const ll MAX = 101010;
static const ll MOD = 1e9 + 7;

// for(i=0; i<N; i++) cin >> a[i];

class sumSegTree {
public:
  ll n, height;
  vector<ll> data;

  sumSegTree(ll _n) {
    n = 1;
    height = 1;
    while (n < _n) {
      n *= 2;
      height++;
    }
    data = vector<ll>(2 * n - 1);
  }

  void add(ll i, ll x) {
    i += n - 1;
    data[i] += x;
    while (i > 0) {
      i = (i - 1) / 2;
      data[i] += x;
    }
  }

  //[a, b):要求区間，[l, r):対象区間
  ll _sum(ll a, ll b, ll k, ll l, ll r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return data[k];
    else {
      ll s = _sum(a, b, 2 * k + 1, l, (l + r) / 2);
      ll t = _sum(a, b, 2 * k + 2, (l + r) / 2, r);
      return s + t;
    }
  }

  ll sum(ll a, ll b) { return _sum(a, b, 0, 0, n); }
};

ll c[MAX * 5];
ll pos[MAX * 5];

int main(void) {
  ll i, j, k;

  ll N, Q;
  cin >> N >> Q;
  for (i = 1; i <= N; i++)
    cin >> c[i];

  vector<pair<pll, ll>> v;
  v.push_back({{1, 1}, -1});

  for (i = 1; i <= Q; i++) {
    ll l, r;
    cin >> l >> r;
    // l--; r--;
    v.push_back({{r, l}, i});
  }
  sort(v.begin(), v.end());

  for (i = 1; i <= Q; i++)
    swap(v[i].ft.ft, v[i].ft.sc);

  sumSegTree sst(N);

  set<ll> st;
  memset(pos, -1, sizeof(pos));

  ll ans[MAX * 5];
  for (ll q = 1; q <= Q; q++) {
    ll l = v[q].ft.ft;
    ll pr = v[q - 1].ft.sc;
    ll r = v[q].ft.sc;

    for (i = pr + 1; i <= r; i++) {
      if (st.find(c[i]) != st.end())
        sst.add(pos[c[i]], -1);

      st.insert(c[i]);
      pos[c[i]] = i;
      sst.add(i, 1);
    }

    ans[v[q].sc] = sst.sum(l, r + 1);
  }

  for (i = 1; i <= Q; i++)
    pt(ans[i]);
}