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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vl;
typedef vector<vector<ll>> vll;
typedef vector<bool> vb;
typedef unordered_set<int> si;
typedef queue<int> qi;
typedef stack<int> sti;
typedef priority_queue<int> pqi;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
// #define __builtin_popcountll bitCountLL
// #define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
#define maxn 500050
#define Mod 1000000007
const int INF = 2e9;
const ll ML = 4e18;
int dat[maxn * 2];
int n, q;
void init() {
  memset(dat, 0, sizeof(dat));
  // n++;
}
int lowbit(int x) { return x & -x; }
void update(int i, int x) {
  while (i <= n) {
    dat[i] += x;
    i += lowbit(i);
  }
}
int getSum(int x) {
  int res = 0, i = x;
  while (i > 0) {
    res += dat[i];
    i -= lowbit(i);
  }
  return res;
}
int query(int l, int r) { return getSum(r) - getSum(l - 1); }
void solve() {
  cin >> n >> q;
  vi a(n + 1), ans(q + 1);
  fori(i, 0, n) cin >> a[i + 1];
  vector<vector<pii>> vp(n + 1);
  fori(i, 1, q + 1) {
    int x, y;
    cin >> x >> y;
    vp[y].push_back({x, i});
  }
  // sort(all(p));
  vi c(n + 1, -1);
  init();
  fori(i, 1, n + 1) {
    if (c[a[i]] != -1)
      update(c[a[i]], -1);
    update(i, 1);
    c[a[i]] = i;
    for (pii m : vp[i]) {
      int x = m.first, id = m.second;
      ans[id] = query(x, i);
    }
  }
  fori(i, 1, q + 1) cout << ans[i] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  // freopen("D:\\\\data_in.txt", "r", stdin);

  solve();

  /*
  clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vector<int>> vii;
typedef vector<long long> vl;
typedef vector<vector<ll>> vll;
typedef vector<bool> vb;
typedef unordered_set<int> si;
typedef queue<int> qi;
typedef stack<int> sti;
typedef priority_queue<int> pqi;
#define fori(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, b, a) for (int i = (b); i >= (a); --i)
#define all(a) (a).begin(), (a).end()
// #define __builtin_popcountll bitCountLL
// #define __builtin_popcount bitCount
//__builtin_ctz 末尾0的个数, __builtin_clz 前导0的个数
//__builtin_ffs 最后一个1的位置
#define maxn 500050
#define Mod 1000000007
const int INF = 2e9;
const ll ML = 4e18;
int dat[maxn * 2];
int n, q;
void init() {
  memset(dat, 0, sizeof(dat));
  // n++;
}
int lowbit(int x) { return x & -x; }
void update(int i, int x) {
  while (i <= n) {
    dat[i] += x;
    i += lowbit(i);
  }
}
int getSum(int x) {
  int res = 0, i = x;
  while (i > 0) {
    res += dat[i];
    i -= lowbit(i);
  }
  return res;
}
int query(int l, int r) { return getSum(r) - getSum(l - 1); }
void solve() {
  cin >> n >> q;
  vi a(n + 1), ans(n + 1);
  fori(i, 0, n) cin >> a[i + 1];
  vector<vector<pii>> vp(n + 1);
  fori(i, 1, q + 1) {
    int x, y;
    cin >> x >> y;
    vp[y].push_back({x, i});
  }
  // sort(all(p));
  vi c(n + 1, -1);
  init();
  fori(i, 1, n + 1) {
    if (c[a[i]] != -1)
      update(c[a[i]], -1);
    update(i, 1);
    c[a[i]] = i;
    for (pii m : vp[i]) {
      int x = m.first, id = m.second;
      ans[id] = query(x, i);
    }
  }
  fori(i, 1, q + 1) cout << ans[i] << "\\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // cout.precision(30); cout << fixed;
  //	freopen("D:\\\\data_in.txt", "r", stdin);

  solve();

  /*
  clock_t start,finish;
  start = clock();

  finish = clock();
  cout << "\\n" << (double)(finish - start) / (double)(CLOCKS_PER_SEC) * 1000 <<
  "ms" << endl;
  */
  return 0;
}

/***************************************************************
 question:
 idea:
 tag:
 record:
 ***************************************************************/