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

#include <bits/stdc++.h>
using namespace std;
#define BS 500
#define MAX 214514
int up[500], fr[500], to[500], ou[500];
int dn[MAX];
void eval(int p) {
  if (!up[p])
    return;
  for (int j = BS * p; j < BS * (p + 1); j++) {
    if (dn[j] == fr[p])
      dn[j] = to[p];
    else
      dn[j] = -1;
  }
  up[p] = 0;
}
signed main() {
  int n, k, t;
  cin >> n >> k >> t;
  memset(up, 0, sizeof(up));
  memset(fr, 0, sizeof(fr));
  memset(to, 0, sizeof(to));
  memset(ou, 0, sizeof(ou));
  memset(dn, 0, sizeof(dn));
  int l[t], r[t], x[t];
  for (int i = 0; i < t; i++)
    cin >> l[i] >> r[i] >> x[i];
  for (int i = 0; i < t; i++) {
    l[i]--;
    int p = 0;
    while (BS * p <= l[i])
      p++;
    eval(p - 1);
    for (int j = l[i]; j < min(r[i], BS * p); j++) {
      if (ou[j / BS])
        break;
      if (dn[j] == x[i] - 1)
        dn[j] = x[i];
      else
        dn[j] = -1;
    }
    for (; BS * (p + 1) <= r[i]; p++) {
      if (ou[p])
        continue;
      if (up[p]) {
        if (to[p] == x[i] - 1)
          to[p] = x[i];
        else
          ou[p] = 1;
      } else {
        up[p] = 1;
        fr[p] = x[i] - 1;
        to[p] = x[i];
      }
    }
    eval(p);
    for (int j = BS * p; j < r[i]; j++) {
      if (ou[j / BS])
        break;
      if (dn[j] == x[i] - 1)
        dn[j] = x[i];
      else
        dn[j] = -1;
    }
  }
  int p = 0;
  while (BS * p < n)
    eval(p++);
  int ans = 0;
  for (int j = 0; j < n; j++) {
    if (ou[j / BS])
      continue;
    ans += dn[j] == k;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
struct Chien {
  int n;
  vector<map<int, int>> dat;
  vector<vector<int>> laz;
  vector<int> law;
  map<int, int> def;
  Chien() {}
  Chien(int n_) { init(n_); }
  void init(int n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.clear();
    dat.resize(2 * n - 1);
    laz.clear();
    laz.resize(2 * n - 1);
    law.clear();
    law.resize(2 * n - 1, 0);
    construct(n_);
  }
  void construct(int n_) {
    for (int i = 0; i < n_; i++)
      dat[i + n - 1][0] = 1;
    for (int i = n - 2; i >= 0; i--) {
      for (P p : dat[i * 2 + 1])
        dat[i][p.first] += p.second;
      for (P p : dat[i * 2 + 2])
        dat[i][p.first] += p.second;
    }
    // for(int i=0;i<n*2-1;i++) cout<<i<<":"<<dat[i][0]<<endl;
  }
  inline void eval(int k) {
    if (dat[k].empty() || laz[k].empty())
      return;
    if (k * 2 + 1 < n * 2 - 1) {
      for (int i : laz[k]) {
        if (!dat[k * 2 + 1].empty())
          laz[k * 2 + 1].push_back(i);
        if (!dat[k * 2 + 2].empty())
          laz[k * 2 + 2].push_back(i);
      }
    }
    bool flg = 1;
    for (int i = 0; i < (int)laz[k].size() - 1; i++)
      flg &= laz[k][i] + 1 == laz[k][i + 1];
    int tmp = dat[k][laz[k][0] - 1];
    dat[k].clear();
    if (!flg || !tmp) {
      laz[k].clear();
      return;
    }
    dat[k][laz[k].back()] = tmp;
    laz[k].clear();
  }
  void rmv(map<int, int> m, int k) {
    for (P p : m) {
      if (dat[k].count(p.first)) {
        dat[k][p.first] -= p.second;
        if (!dat[k][p.first])
          dat[k].erase(p.first);
      }
    }
    if (k)
      rmv(m, (k - 1) / 2);
  }
  void add(map<int, int> m, int k) {
    for (P p : m)
      dat[k][p.first] += p.second;
    if (k)
      add(m, (k - 1) / 2);
  }
  void update(int a, int b, int x, int k, int l, int r) {
    eval(k);
    if (r <= a || b <= l)
      return;
    if (a <= l && r <= b) {
      if (dat[k].empty())
        return;
      laz[k].push_back(x);
      if (k) {
        rmv(dat[k], (k - 1) / 2);
        eval(k);
        add(dat[k], (k - 1) / 2);
      }
      return;
    }
    update(a, b, x, k * 2 + 1, l, (l + r) / 2);
    update(a, b, x, k * 2 + 2, (l + r) / 2, r);
  }
  void update(int a, int b, int x) { update(a, b, x, 0, 0, n); }
};
signed main() {
  int n, k, t;
  cin >> n >> k >> t;
  Chien ch(n);
  for (int i = 0; i < t; i++) {
    int l, r, x;
    cin >> l >> r >> x;
    ch.update(l - 1, r, x);
    // for(P p:ch.dat[0]) cout<<p.first<<":"<<p.second<<endl;
  }
  ch.eval(0);
  // for(P p:ch.dat[0]) cout<<p.first<<":"<<p.second<<endl;
  cout << ch.dat[0][k] << endl;
}