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

// Patwari26
// #include template<t> as mine
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define endl '\\n'
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define INF (1ll << 60)
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int N = 500005;
// We will represent each query as three numbers: L, R, idx. Idx is
// the position (in original order) of this query.
pair<pair<int, int>, int> queries[500500];
int BLOCK_SIZE;
int arr[500005];
long long cnt[500005];
// Essential part of Mo's algorithm: comparator, which we will
// use with std::sort. It is a function, which must return True
// if query x must come earlier than query y, and False otherwise.
inline bool mo_cmp(const pair<pair<int, int>, int> &x,
                   const pair<pair<int, int>, int> &y) {
  int block_x = x.first.first / BLOCK_SIZE;
  int block_y = y.first.first / BLOCK_SIZE;
  if (block_x != block_y)
    return block_x < block_y;
  return x.first.second < y.first.second;
}
ll val[N];
void solve() {
  ll n, q;
  cin >> n;
  cin >> q;
  BLOCK_SIZE = static_cast<int>(sqrt(n));
  rep(i, 0, n - 1) cin >> arr[i];
  rep(i, 0, q - 1) {
    cin >> queries[i].first.first >> queries[i].first.second;
    queries[i].second = i;
  }
  sort(queries, queries + q, mo_cmp);
  int mo_left = 0, mo_right = -1;
  ll cc = 0;
  ll ans[q];
  for (int i = 0; i < q; i++) {
    // [left, right] is what query we must answer now.
    int left = queries[i].first.first - 1;   // -1 for zero based indexing
    int right = queries[i].first.second - 1; // -1 for zero based indexing

    // Usual part of applying Mo's algorithm: moving mo_left
    // and mo_right.
    while (mo_right < right) {
      mo_right++;
      cnt[arr[mo_right]]++;
      if (cnt[arr[mo_right]] == 1)
        cc++;
    }
    while (mo_right > right) {
      if (cnt[arr[mo_right]] == 1)
        cc--;
      cnt[arr[mo_right]]--;
      mo_right--;
    }

    while (mo_left < left) {
      if (cnt[arr[mo_left]] == 1)
        cc--;
      cnt[arr[mo_left]]--;
      mo_left++;
    }
    while (mo_left > left) {
      mo_left--;
      cnt[arr[mo_left]]++;
      if (cnt[arr[mo_left]] == 1)
        cc++;
    }
    ans[queries[i].second] = cc;
  }
  rep(i, 0, q - 1) { cout << ans[i] << endl; }
}
signed main() {
  ios int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  // time
  return 0;
}

Error: 0

Fixed submission:

// Patwari26
// #include template<t> as mine
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define endl '\\n'
#define mii map<ll, ll>
#define pii pair<ll, ll>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define INF (1ll << 60)
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define rrep(i, a, b) for (ll i = a; i >= b; i--)
#define ios                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define time                                                                   \\
  cerr << "\\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\\n";
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll lcm(ll a, ll b) { return a * (b / gcd(a, b)); }
const int N = 500005;
// We will represent each query as three numbers: L, R, idx. Idx is
// the position (in original order) of this query.
pair<pair<int, int>, int> queries[100500];
int BLOCK_SIZE;
int arr[500005];
long long cnt[500005];
// Essential part of Mo's algorithm: comparator, which we will
// use with std::sort. It is a function, which must return True
// if query x must come earlier than query y, and False otherwise.
inline bool mo_cmp(const pair<pair<int, int>, int> &x,
                   const pair<pair<int, int>, int> &y) {
  int block_x = x.first.first / BLOCK_SIZE;
  int block_y = y.first.first / BLOCK_SIZE;
  if (block_x != block_y)
    return block_x < block_y;
  return x.first.second < y.first.second;
}
ll val[N];
void solve() {
  ll n, q;
  cin >> n;
  cin >> q;
  BLOCK_SIZE = static_cast<int>(sqrt(n));
  rep(i, 0, n - 1) cin >> arr[i];
  rep(i, 0, q - 1) {
    cin >> queries[i].first.first >> queries[i].first.second;
    queries[i].second = i;
  }
  sort(queries, queries + q, mo_cmp);
  int mo_left = 0, mo_right = -1;
  ll cc = 0;
  ll ans[q];
  for (int i = 0; i < q; i++) {
    // [left, right] is what query we must answer now.
    int left = queries[i].first.first - 1;   // -1 for zero based indexing
    int right = queries[i].first.second - 1; // -1 for zero based indexing

    // Usual part of applying Mo's algorithm: moving mo_left
    // and mo_right.
    while (mo_right < right) {
      mo_right++;
      cnt[arr[mo_right]]++;
      if (cnt[arr[mo_right]] == 1)
        cc++;
    }
    while (mo_right > right) {
      if (cnt[arr[mo_right]] == 1)
        cc--;
      cnt[arr[mo_right]]--;
      mo_right--;
    }

    while (mo_left < left) {
      if (cnt[arr[mo_left]] == 1)
        cc--;
      cnt[arr[mo_left]]--;
      mo_left++;
    }
    while (mo_left > left) {
      mo_left--;
      cnt[arr[mo_left]]++;
      if (cnt[arr[mo_left]] == 1)
        cc++;
    }
    ans[queries[i].second] = cc;
  }
  rep(i, 0, q - 1) { cout << ans[i] << endl; }
}
signed main() {
  ios int TESTS = 1;
  // cin>>TESTS;
  while (TESTS--) {
    solve();
  }
  // time
  return 0;
}