Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios                                                                    \
  ;                                                                            \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
const int M = 2e6 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20], vis[N * 20];
int n, q, k;
int id;
void build(int &o, int l, int r) {
  o = ++id;
  sum[o] = 0;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  build(ls[o], l, m);
  build(rs[o], m + 1, r);
}

void update(int &o, int l, int r, int last, int pos, int val) {
  o = ++id;
  ls[o] = ls[last];
  rs[o] = rs[last];
  sum[o] = sum[last] + val;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (pos <= m)
    update(ls[o], l, m, ls[last], pos, val);
  else
    update(rs[o], m + 1, r, rs[last], pos, val);
}
int query(int pos, int root, int l, int r) {
  if (l == r)
    return sum[root];
  int m = (l + r) >> 1;
  if (m >= pos)
    return sum[rs[root]] + query(pos, ls[root], l, m);
  else
    return query(pos, rs[root], m + 1, r);
}
void solve() {
  int l, r;
  scanf("%d%d", &l, &r);
  printf("%d\n", query(l, rt[r], 1, n));
}

int main() {
  memset(vis, 0, sizeof(vis));
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  id = 0;
  build(rt[0], 1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      update(rt[i], 1, n, rt[i - 1], i, 1);
    else {
      int pos = vis[a[i]];
      int temp;
      update(temp, 1, n, rt[i - 1], pos, -1);
      update(rt[i], 1, n, temp, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--)
    solve();
  return 0;
}
/*
a b a+b+c*2
a a+b*2+c*4 a+b+c*2
a+b+c*4

*/
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG


Fixed submission:
#include <bits/stdc++.h>
#define rep(a, b, c) for (int a = b; a <= c; a++)
#define per(a, b, c) for (int a = b; a >= c; a--)
#define ios                                                                    \
  ;                                                                            \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define mem(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define ls(x) (x << 1)
#define rs(x) (ls(x) | 1)
#define eps 1e-11
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> VI;
typedef double db;
mt19937 rnd(random_device{}());
const int mod = 1e9 + 7;
const int N = 2e6 + 5;
const int M = 2e6 + 5;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
inline ll ksm(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = res * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return res;
}
int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20], vis[N * 20];
int n, q, k;
int id;
void build(int &o, int l, int r) {
  o = ++id;
  sum[o] = 0;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  build(ls[o], l, m);
  build(rs[o], m + 1, r);
}

void update(int &o, int l, int r, int last, int pos, int val) {
  o = ++id;
  ls[o] = ls[last];
  rs[o] = rs[last];
  sum[o] = sum[last] + val;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (pos <= m)
    update(ls[o], l, m, ls[last], pos, val);
  else
    update(rs[o], m + 1, r, rs[last], pos, val);
}
int query(int pos, int root, int l, int r) {
  if (l == r)
    return sum[root];
  int m = (l + r) >> 1;
  if (m >= pos)
    return sum[rs[root]] + query(pos, ls[root], l, m);
  else
    return query(pos, rs[root], m + 1, r);
}
void solve() {
  int l, r;
  scanf("%d%d", &l, &r);
  printf("%d\n", query(l, rt[r], 1, n));
}

int main() {
  memset(vis, 0, sizeof(vis));
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  id = 0;
  build(rt[0], 1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      update(rt[i], 1, n, rt[i - 1], i, 1);
    else {
      int pos = vis[a[i]];
      int temp;
      update(temp, 1, n, rt[i - 1], pos, -1);
      update(rt[i], 1, n, temp, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--)
    solve();
  return 0;
}
/*
a b a+b+c*2
a a+b*2+c*4 a+b+c*2
a+b+c*4

*/
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG