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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define REP(i, x, n) for (ll i = x; i < n; i++)
#define SZ(v) (ll) v.size()
#define endl "\\n"
#define ff first
#define ss second
#define PQ_MIN priority_queue<ll, vector<ll>, greater<ll>>
#define PQ priority_queue<int>
#define pbds                                                                   \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define setbit(x) __builtin_popcount(x)
#define prec(n) fixed << setprecision(n)
#define pb(n) push_back(n)
#define mp(a, b) make_pair(a, b)
#define clr(n) memset(n, 0, sizeof(n))
#define reset(n) memset(n, -1, sizeof(n))
#define ii pair<ll, ll>
#define vll vector<ll>
using namespace std;
using namespace __gnu_pbds;
const ll INF = 1e16;
const ll nxm = 5e5 + 5;
ll a[nxm];
ll n, q;
ll ans[nxm];
ll fre[nxm];
const ll rnxm = 800;
ll cnt = 0;

struct node {
  ll l, r, ind;
};
node query[nxm];

bool comp(node a, node b) {

  if (a.l / rnxm == b.l / rnxm)
    return a.r <= b.r;
  else
    return a.l / rnxm <= b.l / rnxm;
}

void add(ll val) {
  fre[val]++;
  if (fre[val] == 1)
    cnt++;
}
void remo(ll val) {
  fre[val]--;
  if (fre[val] == 0)
    cnt--;
}

void solve() {

  cin >> n >> q;

  REP(i, 0, n) cin >> a[i];

  REP(i, 0, q) {
    cin >> query[i].l >> query[i].r;
    query[i].l--;
    query[i].r--;
    query[i].ind = i;
  }

  sort(query, query + q, comp);

  ll prevl = 0, prevr = -1;

  REP(i, 0, q) {

    ll l = query[i].l, r = query[i].r;

    while (l < prevl)
      prevl--, add(a[prevl]);

    while (r > prevr)
      prevr++, add(a[prevr]);

    while (prevl < l)
      remo(a[prevl]), prevl++;

    while (prevr > r)
      remo(a[prevr]), prevr--;

    ans[query[i].ind] = cnt;
  }

  REP(i, 0, q) cout << ans[i] << endl;
}
int main() {
  //    freopen("timber_input", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  //	cin>>t;
  REP(i, 1, t + 1) {
    //    cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long int
#define REP(i, x, n) for (ll i = x; i < n; i++)
#define SZ(v) (ll) v.size()
#define endl "\\n"
#define ff first
#define ss second
#define PQ_MIN priority_queue<ll, vector<ll>, greater<ll>>
#define PQ priority_queue<int>
#define pbds                                                                   \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define setbit(x) __builtin_popcount(x)
#define prec(n) fixed << setprecision(n)
#define pb(n) push_back(n)
#define mp(a, b) make_pair(a, b)
#define clr(n) memset(n, 0, sizeof(n))
#define reset(n) memset(n, -1, sizeof(n))
#define ii pair<ll, ll>
#define vll vector<ll>
#define rnxm 555
using namespace std;
using namespace __gnu_pbds;

struct node {
  ll l, r, ind;
};

const ll INF = 1e16;
const ll nxm = 1e6 + 2;
ll a[30000 + 3];
ll n, q;
ll ans[200000 + 2];
ll fre[nxm];
ll cnt = 0;

node query[200000 + 2];

bool comp(node a, node b) {

  if (a.l / rnxm == b.l / rnxm)
    return a.r < b.r;
  else
    return a.l / rnxm < b.l / rnxm;
}

void add(ll val) {
  fre[val]++;
  if (fre[val] == 1)
    cnt++;
}
void remo(ll val) {
  fre[val]--;
  if (fre[val] == 0)
    cnt--;
}

void solve() {

  cin >> n;
  cin >> q;

  REP(i, 0, n) cin >> a[i];

  REP(i, 0, q) {

    cin >> query[i].l >> query[i].r;
    query[i].l--;
    query[i].r--;
    query[i].ind = i;
  }

  sort(query, query + q, comp);

  ll prevl = 0, prevr = -1;

  REP(i, 0, q) {

    ll l = query[i].l, r = query[i].r;

    while (l < prevl)
      prevl--, add(a[prevl]);

    while (r > prevr)
      prevr++, add(a[prevr]);

    while (prevl < l)
      remo(a[prevl]), prevl++;

    while (prevr > r)
      remo(a[prevr]), prevr--;

    ans[query[i].ind] = cnt;
  }

  REP(i, 0, q) cout << ans[i] << endl;
}
int main() {
  //    freopen("timber_input", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll t = 1;
  //	cin>>t;
  REP(i, 1, t + 1) {
    //    cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}
//  bitset<64>(x).to_ullong() // in my com  _ulong()
//  cout<<bitset<64>(ans).to_string().substr(64-m)<<endl;
/*

char s[100];

fgets(s,100,stdin);

stringstream stram(s);

string abc;

bool ok1=0,ok2=0;

while(stram>>abc)
*/