Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <cstdlib>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// #define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>,
// rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<pair<ll, ll>> vpll;
#define Boost()                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fo(i, n) for (ll i = 1; i <= n; i++)
#define fore(i, l, r) for (ll i = l; i <= r; i++)
#define rev(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(c) c.begin(), c.end()
#define go(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define back(c, it) for (auto it = (c).rbegin(); it != (c).rend(); it++)
#define fi first
#define se second
#define MOD 998244353
#define MAX 1000000007
ll power(ll a, ll b) {
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fact[500009];
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll nCrMod(ll n, ll r) {
  ll p = fact[n];
  ll q = (fact[r] * fact[n - r]) % MOD;
  return (p * fermat_inv(q)) % MOD;
}
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
const ld PI = acos(-1.0);
vector<int> g[200009];
struct query {
  int l, r, ind;
};
bool cmp(query x, query y) { return x.r <= y.r; }
ll BIT[200009] = {0};
void update(ll x, ll y, ll n) {
  for (; x <= n; x += x & -x)
    BIT[x] += y;
}
ll sum(ll x, ll n) {
  ll s = 0;
  for (; x > 0; x -= x & -x)
    s += BIT[x];
  return s;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.cpp", "r", stdin);
  freopen("output.cpp", "w", stdout);
#endif
  Boost();
  int t;
  // cin>>t;
  t = 1;
  int t1 = t;
  while (t--) {
    ll n, q;
    cin >> n >> q;
    ll a[n + 1];
    fo(i, n) cin >> a[i];
    query qu[q + 1];
    fo(i, q) {
      cin >> qu[i].l >> qu[i].r;
      qu[i].ind = i;
    }
    sort(qu + 1, qu + q + 1, cmp);
    ll lst[500007];
    memset(lst, -1, sizeof(lst));
    int c = 1;
    ll ans[q + 1];
    fo(i, n) {
      if (lst[a[i]] != -1)
        update(lst[a[i]], -1, n);
      lst[a[i]] = i;
      update(lst[a[i]], 1, n);
      while (c <= q && qu[c].r == i) {
        ans[qu[c].ind] = sum(qu[c].r, n) - sum(qu[c].l - 1, n);
        c++;
      }
    }
    fo(i, q) cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <cstdlib>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// #define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>,
// rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef long double ld;
typedef vector<pair<ll, ll>> vpll;
#define Boost()                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define pb push_back
#define forn(i, n) for (ll i = 0; i < n; i++)
#define fo(i, n) for (ll i = 1; i <= n; i++)
#define fore(i, l, r) for (ll i = l; i <= r; i++)
#define rev(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(c) c.begin(), c.end()
#define go(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define back(c, it) for (auto it = (c).rbegin(); it != (c).rend(); it++)
#define fi first
#define se second
#define MOD 998244353
#define MAX 1000000007
ll power(ll a, ll b) {
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}
ll fact[500009];
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll nCrMod(ll n, ll r) {
  ll p = fact[n];
  ll q = (fact[r] * fact[n - r]) % MOD;
  return (p * fermat_inv(q)) % MOD;
}
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
const ld PI = acos(-1.0);
vector<int> g[200009];
struct query {
  int l, r, ind;
};
bool cmp(query x, query y) { return x.r <= y.r; }
ll BIT[500009] = {0};
void update(ll x, ll y, ll n) {
  for (; x <= n; x += x & -x)
    BIT[x] += y;
}
ll sum(ll x, ll n) {
  ll s = 0;
  for (; x > 0; x -= x & -x)
    s += BIT[x];
  return s;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.cpp", "r", stdin);
  freopen("output.cpp", "w", stdout);
#endif
  Boost();
  int t;
  // cin>>t;
  t = 1;
  int t1 = t;
  while (t--) {
    ll n, q;
    cin >> n >> q;
    ll a[n + 1];
    fo(i, n) cin >> a[i];
    query qu[q + 1];
    fo(i, q) {
      cin >> qu[i].l >> qu[i].r;
      qu[i].ind = i;
    }
    sort(qu + 1, qu + q + 1, cmp);
    ll lst[500007];
    memset(lst, -1, sizeof(lst));
    int c = 1;
    ll ans[q + 1];
    fo(i, n) {
      if (lst[a[i]] != -1)
        update(lst[a[i]], -1, n);
      lst[a[i]] = i;
      update(lst[a[i]], 1, n);
      while (c <= q && qu[c].r == i) {
        ans[qu[c].ind] = sum(qu[c].r, n) - sum(qu[c].l - 1, n);
        c++;
      }
    }
    fo(i, q) cout << ans[i] << endl;
  }
  return 0;
}