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

#include <bits/stdc++.h>

using namespace std;

const int M = 200005;

struct node {
  int x, y;
  long long s0, s1;
};

node A[4 * M];
int S[M];
bool rev[4 * M];
int n, q;

node merge(node A, node B) {
  return (node){A.x + B.x, A.y + B.y, A.s0 + B.s0 + (long long)A.y * B.x,
                A.s1 + B.s1 + (long long)A.x * B.y};
}

void push_up(int p) { A[p] = merge(A[p << 1], A[p << 1 | 1]); }

void push_down(int p) {
  if (rev[p]) {
    rev[p << 1] ^= true;
    swap(A[p << 1].x, A[p << 1].y);
    swap(A[p << 1].s0, A[p << 1].s1);
    rev[p << 1 | 1] ^= true;
    swap(A[p << 1 | 1].x, A[p << 1 | 1].y);
    swap(A[p << 1 | 1].s0, A[p << 1 | 1].s1);
    rev[p] = false;
  }
}

void build(int p, int l, int r) {
  if (l == r) {
    if (S[l] == 1)
      A[p] = (node){0, 1, 0, 0};
    else
      A[p] = (node){1, 0, 0, 0};
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  push_up(p);
}

void modify(int p, int l, int r, int a, int b) {
  if (l == a && r == b) {
    rev[p] ^= true;
    swap(A[p].x, A[p].y);
    swap(A[p].s0, A[p].s1);
    return;
  }
  push_down(p);
  int mid = (l + r) >> 1;
  if (mid < a)
    modify(p << 1 | 1, mid + 1, r, a, b);
  else if (b <= mid)
    modify(p << 1, l, mid, a, b);
  else {
    modify(p << 1, l, mid, a, mid);
    modify(p << 1 | 1, mid + 1, r, mid + 1, b);
  }
  push_up(p);
}

node query(int p, int l, int r, int a, int b) {
  if (l == a && r == b)
    return A[p];
  push_down(p);
  int mid = (l + r) >> 1;
  if (mid < a)
    return query(p << 1 | 1, mid + 1, r, a, b);
  if (b <= mid)
    return query(p << 1, l, mid, a, b);
  return merge(query(p << 1, l, mid, a, mid),
               query(p << 1 | 1, mid + 1, r, mid + 1, b));
}

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &S[i]);
  build(1, 1, n);
  while (q-- != 0) {
    int op, l, r;
    scanf("%d%d%d", &op, &l, &r);
    if (op == 1)
      modify(1, 1, n, l, r);
    if (op == 2)
      printf("%lld\\n", query(1, 1, n, l, r).s0);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int M = 500005;

struct node {
  int x, y;
  long long s0, s1;
};

node A[M];
int S[M];
bool rev[M];
int n, q;

node merge(node A, node B) {
  return (node){A.x + B.x, A.y + B.y, A.s0 + B.s0 + (long long)A.y * B.x,
                A.s1 + B.s1 + (long long)A.x * B.y};
}

void push_up(int p) { A[p] = merge(A[p << 1], A[p << 1 | 1]); }

void push_down(int p) {
  if (rev[p]) {
    rev[p << 1] ^= true;
    swap(A[p << 1].x, A[p << 1].y);
    swap(A[p << 1].s0, A[p << 1].s1);
    rev[p << 1 | 1] ^= true;
    swap(A[p << 1 | 1].x, A[p << 1 | 1].y);
    swap(A[p << 1 | 1].s0, A[p << 1 | 1].s1);
    rev[p] = false;
  }
}

void build(int p, int l, int r) {
  if (l == r) {
    if (S[l] == 1)
      A[p] = (node){0, 1, 0, 0};
    else
      A[p] = (node){1, 0, 0, 0};
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  push_up(p);
}

void modify(int p, int l, int r, int a, int b) {
  if (l == a && r == b) {
    rev[p] ^= true;
    swap(A[p].x, A[p].y);
    swap(A[p].s0, A[p].s1);
    return;
  }
  push_down(p);
  int mid = (l + r) >> 1;
  if (mid < a)
    modify(p << 1 | 1, mid + 1, r, a, b);
  else if (b <= mid)
    modify(p << 1, l, mid, a, b);
  else {
    modify(p << 1, l, mid, a, mid);
    modify(p << 1 | 1, mid + 1, r, mid + 1, b);
  }
  push_up(p);
}

node query(int p, int l, int r, int a, int b) {
  if (l == a && r == b)
    return A[p];
  push_down(p);
  int mid = (l + r) >> 1;
  if (mid < a)
    return query(p << 1 | 1, mid + 1, r, a, b);
  if (b <= mid)
    return query(p << 1, l, mid, a, b);
  return merge(query(p << 1, l, mid, a, mid),
               query(p << 1 | 1, mid + 1, r, mid + 1, b));
}

int main() {
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &S[i]);
  build(1, 1, n);
  while (q-- != 0) {
    int op, l, r;
    scanf("%d%d%d", &op, &l, &r);
    if (op == 1)
      modify(1, 1, n, l, r);
    if (op == 2)
      printf("%lld\\n", query(1, 1, n, l, r).s0);
  }
  return 0;
}