Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vll vector<long long>
#define pll pair<long long, long long>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_tree;

const int N = 2e5 + 6;
ll lo[4 * N + 1], hi[4 * N + 1], delta[4 * N + 1], mini[4 * N + 1];

// change update and rmq according to question

void buildSeg(ll node, ll l, ll r) {
  lo[node] = l;
  hi[node] = r;
  if (l == r) {
    return;
  }
  ll mid = l + (r - l) / 2;
  buildSeg(2 * node + 1, l, mid);
  buildSeg(2 * node + 2, mid + 1, r);
}

void prop(ll i) {
  delta[2 * i + 1] += delta[i];
  delta[2 * i + 2] += delta[i];
  delta[i] = 0;
}

void update(ll i) {
  mini[i] =
      mini[2 * i + 1] + delta[2 * i + 1] + mini[2 * i + 2] + delta[2 * i + 2];
}

void increment(ll i, ll l, ll r, ll val) {
  if (r < lo[i] || l > hi[i]) {
    return;
  }
  if (l <= lo[i] && r >= hi[i]) {
    delta[i] += val;
    return;
  }
  prop(i);

  increment(2 * i + 1, l, r, val);
  increment(2 * i + 2, l, r, val);
  update(i);
}

ll rmq(ll i, ll l, ll r) {
  if (r < lo[i] || l > hi[i]) {
    return 0;
  }
  if (l <= lo[i] && r >= hi[i]) {
    return (mini[i] + delta[i]);
  }
  prop(i);
  ll lefmin = rmq(2 * i + 1, l, r);
  ll rigmin = rmq(2 * i + 2, l, r);
  update(i);
  return lefmin + rigmin;
}

void solve() {
  ll n, q;
  cin >> n >> q;
  ll i, j;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  array<ll, 3> op[q];
  for (i = 0; i < q; i++) {
    cin >> op[i][1] >> op[i][0];
    op[i][1]--;
    op[i][0]--;
    op[i][2] = i;
  }
  sort(op, op + q);
  buildSeg(1, 1, n + 5);
  ll last[n + 1];
  ll ans[q];
  clr(ans);
  ll qq = 0;
  for (i = 0; i <= n; i++) {
    last[i] = -1;
  }
  for (i = 0; i < n; i++) {
    if (last[a[i]] != -1) {
      increment(1, last[a[i]] + 1, last[a[i]] + 1, -1);
    }
    last[a[i]] = i;
    increment(1, i + 1, i + 1, 1);
    while (qq < q && op[qq][0] == (i)) {
      ans[op[qq][2]] = rmq(1, op[qq][1] + 1, op[qq][0] + 1);
      qq++;
    }
  }
  for (i = 0; i < q; i++) {
    cout << ans[i] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);

  ll t = 1;

  // cin>>t;

  while (t--) {
    solve();
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vll vector<long long>
#define pll pair<long long, long long>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1e18

typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_tree;

const int N = 6e5 + 6;
ll lo[4 * N + 1], hi[4 * N + 1], delta[4 * N + 1], mini[4 * N + 1];

// change update and rmq according to question

void buildSeg(ll node, ll l, ll r) {
  lo[node] = l;
  hi[node] = r;
  if (l == r) {
    return;
  }
  ll mid = l + (r - l) / 2;
  buildSeg(2 * node + 1, l, mid);
  buildSeg(2 * node + 2, mid + 1, r);
}

void prop(ll i) {
  delta[2 * i + 1] += delta[i];
  delta[2 * i + 2] += delta[i];
  delta[i] = 0;
}

void update(ll i) {
  mini[i] =
      mini[2 * i + 1] + delta[2 * i + 1] + mini[2 * i + 2] + delta[2 * i + 2];
}

void increment(ll i, ll l, ll r, ll val) {
  if (r < lo[i] || l > hi[i]) {
    return;
  }
  if (l <= lo[i] && r >= hi[i]) {
    delta[i] += val;
    return;
  }
  prop(i);

  increment(2 * i + 1, l, r, val);
  increment(2 * i + 2, l, r, val);
  update(i);
}

ll rmq(ll i, ll l, ll r) {
  if (r < lo[i] || l > hi[i]) {
    return 0;
  }
  if (l <= lo[i] && r >= hi[i]) {
    return (mini[i] + delta[i]);
  }
  prop(i);
  ll lefmin = rmq(2 * i + 1, l, r);
  ll rigmin = rmq(2 * i + 2, l, r);
  update(i);
  return lefmin + rigmin;
}

void solve() {
  ll n, q;
  cin >> n >> q;
  ll i, j;
  ll a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  array<ll, 3> op[q];
  for (i = 0; i < q; i++) {
    cin >> op[i][1] >> op[i][0];
    op[i][1]--;
    op[i][0]--;
    op[i][2] = i;
  }
  sort(op, op + q);
  buildSeg(1, 1, n + 5);
  ll last[n + 1];
  ll ans[q];
  clr(ans);
  ll qq = 0;
  for (i = 0; i <= n; i++) {
    last[i] = -1;
  }
  for (i = 0; i < n; i++) {
    if (last[a[i]] != -1) {
      increment(1, last[a[i]] + 1, last[a[i]] + 1, -1);
    }
    last[a[i]] = i;
    increment(1, i + 1, i + 1, 1);
    while (qq < q && op[qq][0] == (i)) {
      ans[op[qq][2]] = rmq(1, op[qq][1] + 1, op[qq][0] + 1);
      qq++;
    }
  }
  for (i = 0; i < q; i++) {
    cout << ans[i] << "\n";
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);

  ll t = 1;

  // cin>>t;

  while (t--) {
    solve();
  }

  return 0;
}