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

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

#define NMAX 500010

struct QUARR {
  int l;
  int r;
  int index;
  int type;
  int ans;
  bool operator<(QUARR q) const {
    if (type == 1) {
      if (q.r == r)
        return q.l > l;
      return q.r > r;
    } else {
      return q.index > index;
    }
  }
};

int ft_bit[NMAX];
void ft_add(int a, int w, int N) {
  for (int x = a; x <= N; x += x & -x) {
    ft_bit[x] += w;
  }
}

int ft_sum(int a) {
  int ret = 0;
  for (int x = a; x > 0; x -= x & -x) {
    ret += ft_bit[x];
  }
  return ret;
}

signed main() {
  QUARR qu[NMAX];
  int last[NMAX];
  int c[NMAX];
  int N, Q;

  cin >> N >> Q;
  for (int i = 1; i <= N; i++) {
    cin >> c[i];
    last[i] = -1;
  }

  for (int i = 1; i <= Q; i++) {
    int l, r;
    cin >> l >> r;
    qu[i].l = l;
    qu[i].r = r;
    qu[i].index = i;
    qu[i].type = 1;
    qu[i].ans = 0;
  }

  sort(qu + 1, qu + Q + 1);

  for (int i = 1; i <= qu[1].r; i++) {
    if (last[c[i]] == -1) {
      ft_add(i, 1, N);
      last[c[i]] = i;
    } else {
      ft_add(last[c[i]], -1, N);
      ft_add(i, 1, N);
      last[c[i]] = i;
    }
  }

  qu[1].ans = ft_sum(qu[1].r) - ft_sum(qu[1].l - 1);
  qu[1].type = 2;
  for (int i = 2; i <= Q; i++) {
    for (int j = qu[i - 1].r + 1; j <= qu[i].r; j++) {
      if (last[c[j]] == -1) {
        ft_add(j, 1, N);
        last[c[j]] = j;
      } else {
        ft_add(last[c[j]], -1, N);
        ft_add(j, 1, N);
        last[c[j]] = j;
      }
    }
    qu[i].ans = ft_sum(qu[i].r) - ft_sum(qu[i].l - 1);
    qu[i].type = 2;
  }

  sort(qu + 1, qu + Q + 1);

  for (int i = 1; i <= Q; i++) {
    cout << qu[i].ans << endl;
  }
}


Error: 0

Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

/*
#include <boost/multiprecision/cpp_dec_float.hpp>
#include<boost/multiprecision/cpp_int.hpp>
#include <boost/rational.hpp>
namespace mp = boost::multiprecision;
using Real = mp::number<mp::cpp_dec_float<1024>>;
using Bint = mp::cpp_int;
*/

using namespace std;

#define int long long
#define REP(i, s, e) for ((i) = (s); (i) < (e); (i)++)
#define RREP(i, s, e) for ((i) = ((s)-1); (i) >= (e); (i)--)
#define FOR(i, n) for ((i) = (0); (i) < (n); (i)++)
#define RFOR(i, n) for ((i) = ((n)-1); (i) >= (0); (i)--)
#define MOD 1000000007

#define NMAX 5000

struct QUARR {
  int l;
  int r;
  int index;
  int type;
  int ans;
  bool operator<(QUARR q) const {
    if (type == 1) {
      if (q.r == r)
        return q.l > l;
      return q.r > r;
    } else {
      return q.index > index;
    }
  }
};

int ft_bit[NMAX];
void ft_add(int a, int w, int N) {
  for (int x = a; x <= N; x += x & -x) {
    ft_bit[x] += w;
  }
}

int ft_sum(int a) {
  int ret = 0;
  for (int x = a; x > 0; x -= x & -x) {
    ret += ft_bit[x];
  }
  return ret;
}

signed main() {
  QUARR qu[NMAX];
  int last[NMAX];
  int c[NMAX];
  int N, Q;

  cin >> N >> Q;
  for (int i = 1; i <= N; i++) {
    cin >> c[i];
    last[i] = -1;
  }

  for (int i = 1; i <= Q; i++) {
    int l, r;
    cin >> l >> r;
    qu[i].l = l;
    qu[i].r = r;
    qu[i].index = i;
    qu[i].type = 1;
    qu[i].ans = 0;
  }

  sort(qu + 1, qu + Q + 1);

  for (int i = qu[1].l; i <= qu[1].r; i++) {
    if (last[c[i]] == -1) {
      ft_add(i, 1, N);
      last[c[i]] = i;
    } else {
      ft_add(last[c[i]], -1, N);
      ft_add(i, 1, N);
      last[c[i]] = i;
    }
  }

  qu[1].ans = ft_sum(qu[1].r) - ft_sum(qu[1].l - 1);
  qu[1].type = 2;
  for (int i = 2; i <= Q; i++) {
    for (int j = qu[i - 1].r + 1; j <= qu[i].r; j++) {
      if (last[c[j]] == -1) {
        ft_add(j, 1, N);
        last[c[j]] = j;
      } else {
        ft_add(last[c[j]], -1, N);
        ft_add(i, 1, N);
        last[c[j]] = j;
      }
    }
    qu[i].ans = ft_sum(qu[i].r) - ft_sum(qu[i].l - 1);
    qu[i].type = 2;
  }

  sort(qu + 1, qu + Q + 1);

  for (int i = 1; i <= Q; i++) {
    cout << qu[i].ans << endl;
  }
}