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

#include <iostream>
#include <vector>

#define int long long

using namespace std;

struct node {
  int i, j;
  node *left, *right;
  int val, lazy;
};

vector<int> a;

node *build(int i, int j) {
  if (i == j) {
    return new node{i, j, nullptr, nullptr, a[i], 0};
  }
  node *left = build(i, (i + j) / 2);
  node *right = build((i + j) / 2 + 1, j);
  return new node{i, j, left, right, left->val + right->val, 0};
}

void propagate(node *cur) {
  if (cur->lazy != 0) {
    cur->val += (cur->j - cur->i + 1) * cur->lazy;
    if (cur->left != nullptr) {
      cur->left->lazy += cur->lazy;
      cur->right->lazy += cur->lazy;
    }
    cur->lazy = 0;
  }
}

int query(node *cur, int i, int j) {
  propagate(cur);
  if (cur->i > j || cur->j < i) {
    return 0;
  }
  if (cur->i >= i && cur->j <= j) {
    return cur->val;
  }
  return query(cur->left, i, j) + query(cur->right, i, j);
}

void update(node *cur, int i, int j, int newVal) {
  // cout << i << ' ' << j  << endl;
  // cout << cur->i << ' ' << cur->j << endl;
  propagate(cur);
  if (cur->i > j || cur->j < i) {
    return;
  }
  if (cur->i >= i && cur->j <= j) {
    cur->lazy += newVal;
    propagate(cur);
    return;
  }
  update(cur->left, i, j, newVal);
  update(cur->right, i, j, newVal);
  cur->val = cur->left->val + cur->right->val;
  return;
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  // cout << n << q << endl;
  a = vector<int>(n, 0);
  node *root = build(0, n - 1);
  // cout << root->val << endl;
  while (q--) {
    int c;
    int s, t, x;
    cin >> c >> s >> t;
    // cout << s << t << endl;
    if (c == 0) {
      cin >> x;
      update(root, s - 1, t - 1, x);
    } else {
      cout << query(root, s - 1, t - 1) << '\\n';
    }
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

struct node {
  int i, j;
  node *left, *right;
  int val, lazy;
};

vector<int> a;

node *build(int i, int j) {
  if (i == j) {
    return new node{i, j, nullptr, nullptr, a[i], 0};
  }
  node *left = build(i, (i + j) / 2);
  node *right = build((i + j) / 2 + 1, j);
  return new node{i, j, left, right, left->val + right->val, 0};
}

void propagate(node *cur) {
  if (cur->lazy != 0) {
    cur->val += (cur->j - cur->i + 1) * cur->lazy;
    if (cur->left != nullptr) {
      cur->left->lazy += cur->lazy;
      cur->right->lazy += cur->lazy;
    }
    cur->lazy = 0;
  }
}

int query(node *cur, int i, int j) {
  propagate(cur);
  if (cur->i > j || cur->j < i) {
    return 0;
  }
  if (cur->i >= i && cur->j <= j) {
    return cur->val;
  }
  return query(cur->left, i, j) + query(cur->right, i, j);
}

void update(node *cur, int i, int j, int newVal) {
  propagate(cur);
  if (cur->i > j || cur->j < i) {
    return;
  }
  if (cur->i >= i && cur->j <= j) {
    cur->lazy += newVal;
    propagate(cur);
  }
  update(cur->left, i, j, newVal);
  update(cur->right, i, j, newVal);
  cur->val = cur->left->val + cur->right->val;
  return;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  a = vector<int>(n, 0);
  node *root = build(0, n - 1);
  while (q--) {
    int c;
    int s, t, x;
    cin >> c >> s >> t;
    if (c == 0) {
      cin >> x;
      update(root, s - 1, t - 1, x);
    } else {
      cout << query(root, s - 1, t - 1) << '\\n';
    }
  }
  return 0;
}