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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct node {
  int l, r, s, d;
} t[2000005];

void pushup(int x) {
  t[x].s = t[x << 1].s + t[(x << 1) + 1].s;
  return;
}

void pushdown(int x) {
  int mid = (t[x].l + t[x].r) >> 1;
  int d = t[x].d;
  t[x << 1].d += d;
  t[(x << 1) + 1].d += d;
  t[x << 1].s += (mid - t[x].l + 1) * d;
  t[(x << 1) + 1].s += (t[x].r - mid) * d;
  return;
}

void build(int i, int l, int r) {
  t[i].l = l;
  t[i].r = r;
  t[i].d = 0;
  if (l == r) {
    t[i].s = 0;
    return;
  }

  int mid = l + r >> 1;

  build(i << 1, l, mid);
  build((i << 1) + 1, mid + 1, r);

  pushup(i);
  return;
}

int getsum(int i, int l, int r) {
  int mid = t[i].l + t[i].r >> 1;
  if (l <= t[i].l && t[i].r <= r) {
    return t[i].s;
  }
  int d = t[i].d;
  if (!d)
    pushdown(i);
  int ret = 0;
  if (mid >= l)
    ret += getsum(i << 1, l, r);
  if (mid < r)
    ret += getsum((i << 1) + 1, l, r);

  return ret;
}

void update(int i, int l, int r, int del) {
  if (t[i].l == t[i].r) {
    t[i].d += del;
    t[i].s += (t[i].l - t[i].r + 1) * del;
    return;
  }

  int mid = t[i].l + t[i].r >> 1;
  if (!t[i].d)
    pushdown(i);
  if (mid >= l)
    update(i << 1, l, r, del);
  if (mid < r)
    update((i << 1) + 1, l, r, del);
  pushup(i);
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> vc(n + 1, 0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &vc[i + 1]);
  }
  vector<pair<pair<int, int>, int>> vp;
  vector<int> res(q + 1, 0);
  for (int i = 0; i < q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    vp.push_back(make_pair(make_pair(r, l), i + 1));
  }
  sort(vp.begin(), vp.end());
  build(1, 1, n);
  vector<int> last(n + 1, -1);
  int id = 1;
  for (int i = 0; i < q; i++) {
    int l = vp[i].first.second, r = vp[i].first.first;
    int index = vp[i].second;
    while (id <= r) {
      int c = vc[id];
      if (last[c] == -1) {
        last[c] = id;
        // update_tree(um, id, 1, 1, n);
        update(1, id, id, 1);
      } else {
        // update_tree(um, last[c], -1, 1, n);
        // update_tree(um, id, 1, 1, n);
        update(1, last[c], last[c], -1);
        update(1, id, id, 1);
        last[c] = id;
      }
      id++;
    }
    // res[index] = query_tree(um, l, r, 1, n);
    res[index] = getsum(1, l, r);
  }
  for (int i = 1; i <= q; i++) {
    printf("%d\\n", res[i]);
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
struct node {
  int l, r, s, d;
} t[500005];

void pushup(int x) {
  t[x].s = t[x << 1].s + t[(x << 1) + 1].s;
  return;
}

void pushdown(int x) {
  int mid = (t[x].l + t[x].r) >> 1;
  int d = t[x].d;
  t[x << 1].d += d;
  t[(x << 1) + 1].d += d;
  t[x << 1].s += (mid - t[x].l + 1) * d;
  t[(x << 1) + 1].s += (t[x].r - mid) * d;
  return;
}

void build(int i, int l, int r) {
  t[i].l = l;
  t[i].r = r;
  t[i].d = 0;
  if (l == r) {
    t[i].s = 0;
    return;
  }

  int mid = l + r >> 1;

  build(i << 1, l, mid);
  build((i << 1) + 1, mid + 1, r);

  pushup(i);
  return;
}

int getsum(int i, int l, int r) {
  int mid = t[i].l + t[i].r >> 1;
  if (l <= t[i].l && t[i].r <= r) {
    return t[i].s;
  }
  int d = t[i].d;
  if (!d)
    pushdown(i);
  int ret = 0;
  if (mid >= l)
    ret += getsum(i << 1, l, r);
  if (mid < r)
    ret += getsum((i << 1) + 1, l, r);

  return ret;
}

void update(int i, int l, int r, int del) {
  if (t[i].l == t[i].r) {
    t[i].d += del;
    t[i].s += (t[i].l - t[i].r + 1) * del;
    return;
  }

  int mid = t[i].l + t[i].r >> 1;
  if (!t[i].d)
    pushdown(i);
  if (mid >= l)
    update(i << 1, l, r, del);
  if (mid < r)
    update((i << 1) + 1, l, r, del);
  pushup(i);
}

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> vc(n + 1, 0);
  for (int i = 0; i < n; i++) {
    scanf("%d", &vc[i + 1]);
  }
  vector<pair<pair<int, int>, int>> vp;
  vector<int> res(q + 1, 0);
  for (int i = 0; i < q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    vp.push_back(make_pair(make_pair(r, l), i + 1));
  }
  sort(vp.begin(), vp.end());
  build(1, 1, n);
  vector<int> last(n + 1, -1);
  int id = 1;
  for (int i = 0; i < q; i++) {
    int l = vp[i].first.second, r = vp[i].first.first;
    int index = vp[i].second;
    while (id <= r) {
      int c = vc[id];
      if (last[c] == -1) {
        last[c] = id;
        // update_tree(um, id, 1, 1, n);
        update(1, id, id, 1);
      } else {
        // update_tree(um, last[c], -1, 1, n);
        // update_tree(um, id, 1, 1, n);
        update(1, last[c], last[c], -1);
        update(1, id, id, 1);
        last[c] = id;
      }
      id++;
    }
    // res[index] = query_tree(um, l, r, 1, n);
    res[index] = getsum(1, l, r);
  }
  for (int i = 1; i <= q; i++) {
    printf("%d\\n", res[i]);
  }
}