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

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

inline char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  if (p1 == p2) {
    p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
    if (p1 == p2)
      return EOF;
  }
  return *p1++;
}

inline void read(int &x) {
  char c = nc(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = nc())
    if (c == '-')
      b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= b;
}

const int N = 200005;

struct abcd {
  int l, r;
  abcd(int l = -1, int r = -1) : l(l), r(r) {}
  bool operator==(const abcd &B) const { return l == B.l && r == B.r; }
} T[N << 2];

bool Jud(abcd L, abcd R) { return L.r + 1 == R.l; }

inline void Push(int x, abcd u) {
  if (T[x] == abcd(-1, -1)) {
    T[x] = u;
    return;
  }
  if (T[x] == abcd(-2, -2))
    return;
  if (u == abcd(-2, -2)) {
    T[x] = u;
    return;
  }
  if (Jud(T[x], u) || T[x] == abcd(-1, -1))
    T[x].r = u.r;
  else
    T[x] = abcd(-2, -2);
}

inline void Modify(int x, int l, int r, int ql, int qr, abcd u) {
  int mid = (l + r) >> 1;
  if (!(T[x] == abcd(-1, -1)) && l != r)
    Push(x << 1, T[x]), Push(x << 1 | 1, T[x]), T[x] = abcd(-1, -1);
  if (ql <= l && r <= qr) {
    Push(x, u);
    return;
  }
  if (ql <= mid)
    Modify(x << 1, l, mid, ql, qr, u);
  if (mid < qr)
    Modify(x << 1 | 1, mid + 1, r, ql, qr, u);
}

int n, K;
int cnt = 0;

inline void Query(int x, int l, int r) {
  if (l == r) {
    if (T[x] == abcd(0, K))
      cnt++;
    return;
  }
  if (!(T[x] == abcd(-1, -1)) && l != r)
    Push(x << 1, T[x]), Push(x << 1 | 1, T[x]), T[x] = abcd(-1, -1);
  int mid = (l + r) >> 1;
  Query(x << 1, l, mid);
  Query(x << 1 | 1, mid + 1, r);
}

int main() {
  int Q;
  int l, r, c;
  read(n);
  read(K);
  T[1] = abcd(0, 0);
  read(Q);
  while (Q--) {
    read(l);
    read(r);
    read(c);
    Modify(1, 1, n, l, r, abcd(c, c));
  }
  Query(1, 1, n);
  printf("%d\\n", cnt);
  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstring>
#define N 100010
using namespace std;
struct node {
  int sum, w, p, q;
};
node tree[N * 4];
int n, m, v;

void build(int t, int l, int r) {
  tree[t].q = 0;
  tree[t].p = 0;
  tree[t].w = 0;
  tree[t].sum = 0;
  if (l == r)
    return;
  int mid = (l + r) / 2;
  build(t * 2, l, mid);
  build(t * 2 + 1, mid + 1, r);
}

void clear(int t, int l, int r) {
  if (tree[t].q == -1)
    return;
  int mid = (l + r) / 2;
  tree[t * 2].p = tree[t].p;
  tree[t * 2].w = tree[t].w;
  if (tree[t * 2].w == 1)
    tree[t * 2].sum = mid - l + 1;
  tree[t * 2 + 1].p = tree[t].p;
  tree[t * 2 + 1].w = tree[t].w;
  if (tree[t * 2 + 1].w == 1)
    tree[t * 2 + 1].sum = r - mid;
  tree[t].q = -1;
}

void updata(int t, int l, int r, int left, int right, int k) {
  int mid = (l + r) / 2;
  if (left <= l && r <= right) {
    if (tree[t].w == -1)
      return;
    if (tree[t].q != -1) {
      if (tree[t].p == k - 1) {
        tree[t].p = k;
        if (tree[t].p == v) {
          tree[t].w = 1;
          tree[t].sum = r - l + 1;
        }
        return;
      } else {
        tree[t].w = -1;
        tree[t].sum = 0;
        return;
      }
    }
  }
  clear(t, l, r);
  if (left <= mid)
    updata(t * 2, l, mid, left, right, k);
  if (right > mid)
    updata(t * 2 + 1, mid + 1, r, left, right, k);

  if (tree[t * 2].w == 1 && tree[t * 2 + 1].w == 1) {
    tree[t].w = 1;
  }
  tree[t].sum = tree[t * 2].sum + tree[t * 2 + 1].sum;
  if (tree[t * 2].w == -1 && tree[t * 2 + 1].w == -1)
    tree[t].w = -1;
  if (tree[t * 2].p == tree[t * 2 + 1].p)
    tree[t].p = tree[t * 2].p;
}

int main() {
  freopen("d.in", "r", stdin);
  freopen("d.out", "w", stdout);
  scanf("%d%d", &n, &v);
  scanf("%d", &m);
  build(1, 1, n);
  for (int i = 1; i <= m; i++) {
    int l, r, k;
    scanf("%d%d%d", &l, &r, &k);
    updata(1, 1, n, l, r, k);
  }
  printf("%d\\n", tree[1].sum);
  return 0;
}