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

#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
const int MX = 5e5 + 7;
const int mod = 998244353;
const double pi = 3.1415926535897932384;
double isp = 1e-13;
using namespace std;
ll qpow(ll a, ll b, ll MOD = mod) {
  for (ll ans = 1;; a = a * a % MOD, b >>= 1) {
    if (b & 1)
      ans = ans * a % MOD;
    if (!b)
      return ans;
  }
}
ll inv(ll a, ll MOD = mod) { return qpow(a, MOD - 2, MOD); } // 要求MOD为质数
ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll ret = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return ret;
}
ll getInv(int a, int mod) {
  ll x, y;
  ll d = exgcd(a, mod, x, y);
  return d == 1 ? (x % mod + mod) % mod : -1;
} // 求a在mod下的逆元，不存在逆元返回-1，不要求MOD为质数
int n, c[MX], a[MX], ans[MX];
void upd(int pos, int val) {
  for (int x = pos; x <= n; x += x & -x)
    c[x] += val;
}
int Sum(int pos) /// 从0位置到pos位置的区间和
{
  int ans = 0;
  for (int x = pos; x; x -= x & -x)
    ans += c[x];
  return ans;
}
unordered_map<int, int> vis;
vector<pair<int, int>> mp[MX];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    mp[r].push_back(make_pair(l, i));
  }
  for (int i = 1; i <= n; i++) {
    // cout<<vis[a[i]]<<endl;
    if (vis[a[i]])
      upd(vis[a[i]], -1);
    upd(i, 1);
    vis[a[i]] = i;
    for (auto x : mp[i]) {
      int l = x.first;
      ans[x.second] = Sum(i) - Sum(l - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define endl '\\n'
#define rep(i, l, r) for (int i = l; i <= r; i++)
#define per(i, r, l) for (int i = r; i >= l; i--)
const int MX = 2e5 + 7;
const int mod = 998244353;
const double pi = 3.1415926535897932384;
double isp = 1e-13;
using namespace std;
ll qpow(ll a, ll b, ll MOD = mod) {
  for (ll ans = 1;; a = a * a % MOD, b >>= 1) {
    if (b & 1)
      ans = ans * a % MOD;
    if (!b)
      return ans;
  }
}
ll inv(ll a, ll MOD = mod) { return qpow(a, MOD - 2, MOD); } // 要求MOD为质数
ll exgcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1, y = 0;
    return a;
  }
  ll ret = exgcd(b, a % b, y, x);
  y -= a / b * x;
  return ret;
}
ll getInv(int a, int mod) {
  ll x, y;
  ll d = exgcd(a, mod, x, y);
  return d == 1 ? (x % mod + mod) % mod : -1;
} // 求a在mod下的逆元，不存在逆元返回-1，不要求MOD为质数
int n, c[MX], a[MX], ans[MX];
void upd(int pos, int val) {
  for (int x = pos; x <= n; x += x & -x)
    c[x] += val;
}
int Sum(int pos) /// 从0位置到pos位置的区间和
{
  int ans = 0;
  for (int x = pos; x; x -= x & -x)
    ans += c[x];
  return ans;
}
unordered_map<int, int> vis;
vector<pair<int, int>> mp[MX];
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    mp[r].push_back(make_pair(l, i));
  }
  for (int i = 1; i <= n; i++) {
    // cout<<vis[a[i]]<<endl;
    if (vis[a[i]])
      upd(vis[a[i]], -1);
    upd(i, 1);
    vis[a[i]] = i;
    for (auto x : mp[i]) {
      int l = x.first;
      ans[x.second] = Sum(i) - Sum(l - 1);
    }
  }
  for (int i = 1; i <= q; i++) {
    cout << ans[i] << endl;
  }
}