Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;

const int DAT_SIZE = 1 << 18;
const int MAX_N = 500050;
int N;

vll dat1(DAT_SIZE), dat2(DAT_SIZE);
vll v(MAX_N);

//[a,b)にxを加算
// 0-indexであることに注意
void add(int a, int b, ll x, int k = 0, int l = 0, int r = N) {
  if (a <= l && r <= b)
    dat1[k] += x;
  else if (l < b && a < r) {
    dat2[k] += (ll)(min(b, r) - max(a, l)) * x;
    add(a, b, x, 2 * k + 1, l, (l + r) / 2);
    add(a, b, x, 2 * k + 2, (l + r) / 2, r);
  }
}

// 初期化
void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  // rep(i,n)add(i,i+1,v[i],0,0,n);
}

//[a,b)の和
// 0-indexであることに注意
ll getsum(int a, int b, int k = 0, int l = 0, int r = N) {
  if (b <= l || r <= a)
    return 0;
  else if (a <= l && r <= b)
    return dat1[k] * (ll)(r - l) + dat2[k];
  else {
    ll res = (ll)(min(b, r) - max(a, l)) * dat1[k];
    res += getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    res += getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return res;
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vin c(n);
  rep(i, n) cin >> c[i];
  vector<tuple<int, int, int>> query(q);
  int l, r;
  rep(i, q) {
    cin >> l >> r;
    l--;
    r--;
    query[i] = make_tuple(r, l, i);
  }
  sort(all(query));
  init(n);
  int x, y, z;
  int tmp = 0;
  vin prev(n + 1, -1);
  vin ans(q);
  rep(i, q) {
    y = get<0>(query[i]);
    x = get<1>(query[i]);
    z = get<2>(query[i]);
    rep2(j, tmp, y + 1) {
      add(prev[c[j]] + 1, j + 1, 1);
      prev[c[j]] = j;
    }
    tmp = y;
    ans[z] = getsum(x, x + 1);
  }
  rep(i, q) cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vin = vector<int>;
using vll = vector<long long>;
using vvin = vector<vector<int>>;
using vvll = vector<vector<long long>>;
using vstr = vector<string>;
using vvstr = vector<vector<string>>;
using vch = vector<char>;
using vvch = vector<vector<char>>;
using vbo = vector<bool>;
using vvbo = vector<vector<bool>>;
using vpii = vector<pair<int, int>>;
using pqsin = priority_queue<int, vector<int>, greater<int>>;
#define mp make_pair
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define decp(n) cout << fixed << setprecision((int)n)
const int inf = 1e9 + 7;
const ll INF = 1e18;

const int DAT_SIZE = 1 << 22;
const int MAX_N = 500050;
int N;

vll dat1(DAT_SIZE), dat2(DAT_SIZE);
vll v(MAX_N);

//[a,b)にxを加算
// 0-indexであることに注意
void add(int a, int b, ll x, int k = 0, int l = 0, int r = N) {
  if (a <= l && r <= b)
    dat1[k] += x;
  else if (l < b && a < r) {
    dat2[k] += (ll)(min(b, r) - max(a, l)) * x;
    add(a, b, x, 2 * k + 1, l, (l + r) / 2);
    add(a, b, x, 2 * k + 2, (l + r) / 2, r);
  }
}

// 初期化
void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  // rep(i,n)add(i,i+1,v[i],0,0,n);
}

//[a,b)の和
// 0-indexであることに注意
ll getsum(int a, int b, int k = 0, int l = 0, int r = N) {
  if (b <= l || r <= a)
    return 0;
  else if (a <= l && r <= b)
    return dat1[k] * (ll)(r - l) + dat2[k];
  else {
    ll res = (ll)(min(b, r) - max(a, l)) * dat1[k];
    res += getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    res += getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return res;
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  vin c(n);
  rep(i, n) cin >> c[i];
  vector<tuple<int, int, int>> query(q);
  int l, r;
  rep(i, q) {
    cin >> l >> r;
    l--;
    r--;
    query[i] = make_tuple(r, l, i);
  }
  sort(all(query));
  init(n);
  int x, y, z;
  int tmp = 0;
  vin prev(n + 1, -1);
  vin ans(q);
  rep(i, q) {
    y = get<0>(query[i]);
    x = get<1>(query[i]);
    z = get<2>(query[i]);
    rep2(j, tmp, y + 1) {
      add(prev[c[j]] + 1, j + 1, 1);
      prev[c[j]] = j;
    }
    tmp = y;
    ans[z] = getsum(x, x + 1);
  }
  rep(i, q) cout << ans[i] << endl;
}