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

#include <bits/stdc++.h>

using namespace std;
using pii = tuple<int, int, int>;

map<int, int> first_met;

int main() {
  // freopen("103.in", "r", stdin);
  ios::sync_with_stdio(0);
  cin.tie(0);

  int n, q;
  cin >> n >> q;

  vector<int> arr(n);
  int t;
  for (int i = 0; i < n; i++) {
    cin >> t;
    if (first_met.count(t) == 0) {
      first_met[t] = first_met.size();
    }
    arr[i] = first_met[t];
  }

  vector<pii> queries(q);
  int a, b;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    queries[i] = {a - 1, b, i};
  }

  int k = sqrt(n);
  sort(queries.begin(), queries.end(), [&](const pii &q1, const pii &q2) {
    int a1k = get<0>(q1) / k;
    int a2k = get<0>(q2) / k;
    if (a1k < a2k) {
      return true;
    }
    if (a2k < a1k) {
      return false;
    }
    return get<1>(q1) < get<1>(q2);
  });

  vector<int> met(first_met.size());
  vector<int> res(q);
  int l = 0, r = 0, cur = 1;
  met[arr[0]]++;
  for (auto &[start, end, indx] : queries) {
    while (l < start) {
      met[arr[l]]--;
      if (met[arr[l]] == 0) {
        cur--;
      }
      l++;
    }
    while (l > start) {
      l--;
      met[arr[l]]++;
      if (met[arr[l]] == 1) {
        cur++;
      }
    }
    while (r >= end) {
      met[arr[r]]--;
      if (met[arr[r]] == 0) {
        cur--;
      }
      r--;
    }
    while (r < end - 1) {
      r++;
      met[arr[r]]++;
      if (met[arr[r]] == 1) {
        cur++;
      }
    }
    res[indx] = cur;
  }

  for (auto &r : res) {
    cout << r << "\\n";
  }
}

Error: 0
Time : 51.417ms


Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long int ll;
typedef unsigned long long int sll;
typedef long double ld;
#define A 1000000007
#define D 100000000000000ll
#define B 998244353ll
#define C 1000000000000000000ll
#define M 4000
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define oset                                                                   \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define ve vector
#define br break
#define PI acos(-1)
#define subt                                                                   \\
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC       \\
       << "ms\\n"

const ll MAXN = 200005;
ll n, t[4 * MAXN], las[MAXN];

void builds(ll v, ll tl, ll tr) {
  if (tl == tr)
    t[v] = 0;
  else {
    ll tm = (tl + tr) / 2;
    builds(v * 2, tl, tm);
    builds(v * 2 + 1, tm + 1, tr);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}

ll sum(ll v, ll tl, ll tr, ll l, ll r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr)
    return t[v];
  ll tm = (tl + tr) / 2;
  return sum(v * 2, tl, tm, l, min(r, tm)) +
         sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}

void updates(ll v, ll tl, ll tr, ll pos, ll new_val) {
  if (tl == tr)
    t[v] += new_val;
  else {
    ll tm = (tl + tr) / 2;
    if (pos <= tm)
      updates(v * 2, tl, tm, pos, new_val);
    else
      updates(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}

int main() {
  FAST;
  ll n, q;
  cin >> n >> q;
  ll a[n];
  set<ll> b;
  map<ll, ll> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    b.insert(a[i]);
    las[i] = -1;
  }
  ll co = 0;
  for (auto i = b.begin(); i != b.end(); i++)
    m[*i] = co++;
  for (int i = 0; i < n; i++)
    a[i] = m[a[i]];
  builds(1, 0, n - 1);
  ve<pair<ll, pair<ll, ll>>> v;
  co = 0;
  ll ans[q];
  for (int i = 0; i < q; i++) {
    ll x, y;
    cin >> x >> y;
    ans[co] = 0;
    v.pb({y - 1, {x - 1, co++}});
  }
  sort(v.begin(), v.end());
  co = 0;
  for (int i = 0; i < n; i++) {
    if (las[a[i]] != -1)
      updates(1, 0, n - 1, las[a[i]], -1);
    las[a[i]] = i;
    updates(1, 0, n - 1, las[a[i]], 1);
    while (v[co].first == i) {
      ans[(v[co].second).second] =
          sum(1, 0, n - 1, (v[co].second).first, v[co].first);
      co++;
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << '\\n';
  subt;
  return 0;
}