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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef pair<int, pi> pii;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define fax(i, a) for (int i = 0; i < (a); i++)
#define f0x(i, a, b) for (int i = (a); i < (b); i++)
#define faxd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define f0xd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define memeset memset

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 500005;
const ld PI = 4 * atan((ld)1);
ll tree[4 * MX];
ll in[MX];
void build(int v, int l, int r) {
  if (l == r) {
    tree[v] = 0;
  } else {
    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m + 1, r);
    tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
  }
}

void update(int v, int l, int r, int i, int x) {
  if (i > r || i < l) {
    return;
  } else if (l == r) {
    tree[v] += x;
  } else {
    int m = (l + r) / 2;
    update(2 * v + 1, l, m, i, x);
    update(2 * v + 2, m + 1, r, i, x);
    tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
  }
}

ll query(int v, int l, int r, int ql, int qr) {
  if (l > qr || r < ql) {
    return 0;
  } else if (l >= ql && r <= qr) {
    return tree[v];
  } else {
    int m = (l + r) / 2;
    ll a = query(2 * v + 1, l, m, ql, qr);
    ll b = query(2 * v + 2, m + 1, r, ql, qr);
    return a + b;
  }
}

template <class T> void ckmin(T &a, T b) { a = min(a, b); }
template <class T> void ckmax(T &a, T b) { a = max(a, b); }
map<int, int> compress;
int cnt = 0;
set<int> vals;
vector<pi> queries[MX];
int pre[MX], ret[MX];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  fax(i, n) {
    cin >> in[i];
    vals.insert(in[i]);
  }
  trav(curr, vals) {
    compress.insert({curr, cnt});
    cnt++;
  }
  fax(i, q) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    queries[b].pb({a, i});
  }
  build(0, 0, n - 1);
  memeset(pre, -1, sizeof(pre));
  fax(i, n) {
    int comp = compress[in[i]];
    // cout << i << " " << pre[comp] << "\\n";
    if (pre[comp] == -1) {
      update(0, 0, n - 1, i, 1);
      pre[comp] = i;
    } else {
      update(0, 0, n - 1, pre[comp], -1);
      update(0, 0, n - 1, i, 1);
      pre[comp] = i;
    }
    for (pi val : queries[i]) {
      // cout << val << " ";
      ret[val.s] = query(0, 0, n - 1, val.f, i);
    }
    // cout << endl;
  }
  fax(i, q) { cout << ret[i] << "\\n"; }
}

Error: 0

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ld, ld> pd;
typedef pair<int, pi> pii;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

#define fax(i, a) for (int i = 0; i < (a); i++)
#define f0x(i, a, b) for (int i = (a); i < (b); i++)
#define faxd(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define f0xd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)
#define memeset memset

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 200005;
const ld PI = 4 * atan((ld)1);
ll tree[4 * MX];
ll in[MX];
void build(int v, int l, int r) {
  if (l == r) {
    tree[v] = 0;
  } else {
    int m = (l + r) / 2;
    build(2 * v + 1, l, m);
    build(2 * v + 2, m + 1, r);
    tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
  }
}

void update(int v, int l, int r, int i, int x) {
  if (i > r || i < l) {
    return;
  } else if (l == r) {
    tree[v] += x;
  } else {
    int m = (l + r) / 2;
    update(2 * v + 1, l, m, i, x);
    update(2 * v + 2, m + 1, r, i, x);
    tree[v] = tree[2 * v + 1] + tree[2 * v + 2];
  }
}

ll query(int v, int l, int r, int ql, int qr) {
  if (l > qr || r < ql) {
    return 0;
  } else if (l >= ql && r <= qr) {
    return tree[v];
  } else {
    int m = (l + r) / 2;
    ll a = query(2 * v + 1, l, m, ql, qr);
    ll b = query(2 * v + 2, m + 1, r, ql, qr);
    return a + b;
  }
}

template <class T> void ckmin(T &a, T b) { a = min(a, b); }
template <class T> void ckmax(T &a, T b) { a = max(a, b); }
map<int, int> compress;
int cnt = 0;
set<int> vals;
vector<pi> queries[MX];
int pre[MX], ret[MX];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  fax(i, n) {
    cin >> in[i];
    vals.insert(in[i]);
  }
  trav(curr, vals) {
    compress.insert({curr, cnt});
    cnt++;
  }
  fax(i, q) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    queries[b].pb({a, i});
  }
  build(0, 0, n - 1);
  memeset(pre, -1, sizeof(pre));
  fax(i, n) {
    int comp = compress[in[i]];
    // cout << i << " " << pre[comp] << "\\n";
    if (pre[comp] == -1) {
      update(0, 0, n - 1, i, 1);
      pre[comp] = i;
    } else {
      update(0, 0, n - 1, pre[comp], -1);
      update(0, 0, n - 1, i, 1);
      pre[comp] = i;
    }
    for (pi val : queries[i]) {
      // cout << val << " ";
      ret[val.s] = query(0, 0, n - 1, val.f, i);
    }
    // cout << endl;
  }
  fax(i, q) { cout << ret[i] << "\\n"; }
}