prithivMLmods/Coder-Stat huggingface dataset:
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int seg[200005];
int n, q;
void add(int a, int b) {
  a += n - 1;
  seg[a] += b;
  while (a > 0) {
    a = (a - 1) / 2;
    seg[a] += b;
  }
}
int sum(int a, int b, int l, int m, int k) {
  if (a <= l && m <= b)
    return seg[k];

  if (m < a || b < l)
    return 0;

  return sum(a, b, l, (l + m - 1) / 2, k * 2 + 1) +
         sum(a, b, (l + m + 1) / 2, m, k * 2 + 2);
}
int main(void) {
  cin >> n >> q;
  int n1 = 1;
  for (; n1 < n; n1 *= 2)
    ;
  n = n1;
  for (int i = 0; i < q; i++) {
    int c, x, y;
    cin >> c >> x >> y;
    x--;
    if (c == 0) {
      add(x, y);
    } else {
      y--;
      cout << sum(x, y, 0, n - 1, 0) << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int seg[20000000];
int n, q;
void add(int a, int b) {
  a += n - 1;
  seg[a] += b;
  while (a > 0) {
    a = (a - 1) / 2;
    seg[a] += b;
  }
}
int sum(int a, int b, int l, int m, int k) {
  if (a <= l && m <= b)
    return seg[k];

  if (m < a || b < l)
    return 0;

  return sum(a, b, l, (l + m - 1) / 2, k * 2 + 1) +
         sum(a, b, (l + m + 1) / 2, m, k * 2 + 2);
}
int main(void) {
  cin >> n >> q;
  int n1 = 1;
  for (; n1 < n; n1 *= 2)
    ;
  n = n1;
  for (int i = 0; i < q; i++) {
    int c, x, y;
    cin >> c >> x >> y;
    x--;
    if (c == 0) {
      add(x, y);
    } else {
      y--;
      cout << sum(x, y, 0, n - 1, 0) << endl;
    }
  }
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, q, bit[100001];
void add(int a, int b) {
  bit[a] += b;
  while (a <= n) {
    a += a & -a;
    bit[a] += b;
  }
}
int sum(int i) {
  if (i == 0)
    return 0;

  return bit[i] + sum(i - (i & -i));
}
int main(void) {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      add(x, y);
    } else {
      cout << sum(y) - sum(x - 1) << endl;
    }
  }
}

Fixed submission:
#include <iostream>
using namespace std;
int n, q, bit[10000001];
void add(int a, int b) {
  bit[a] += b;
  while (a <= n) {
    a += a & -a;
    bit[a] += b;
  }
}
int sum(int i) {
  if (i == 0)
    return 0;

  return bit[i] + sum(i - (i & -i));
}
int main(void) {
  cin >> n >> q;
  for (int i = 0; i < q; i++) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      add(x, y);
    } else {
      cout << sum(y) - sum(x - 1) << endl;
    }
  }
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#define MAX 250000
using namespace std;
int D[MAX];
int n = 1;

void initRSQ(int n_) {
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    D[i] = 0;
}

void add(int k, int a) {
  k += n - 1;
  D[k] += a;

  while (k > 0) {
    k = (k - 1) / 2;
    D[k] = D[2 * k + 1] + D[2 * k + 2];
  }
}

int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return D[k];

  int vleft, vright;
  vleft = query(a, b, 2 * k + 1, l, (l + r) / 2);
  vright = query(a, b, 2 * k + 2, (l + r) / 2, r);
  return vleft + vright;
}

int getSum(int a, int b) { return query(a, b, 0, 0, n); }

int main() {
  int q, com, x, y, n_, min;
  cin >> n_ >> q;
  initRSQ(n_);
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    x--;
    if (com == 0)
      add(x, y);
    else
      cout << getSum(x, y) << endl;
  }
  return 0;
}


Fixed submission:
#include <iostream>
#define MAX 300000
using namespace std;
int D[MAX];
int n = 1;

void initRSQ(int n_) {
  while (n < n_)
    n *= 2;
  for (int i = 0; i < 2 * n - 1; i++)
    D[i] = 0;
}

void add(int k, int a) {
  k += n - 1;
  D[k] += a;

  while (k > 0) {
    k = (k - 1) / 2;
    D[k] = D[2 * k + 1] + D[2 * k + 2];
  }
}

int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return D[k];

  int vleft, vright;
  vleft = query(a, b, 2 * k + 1, l, (l + r) / 2);
  vright = query(a, b, 2 * k + 2, (l + r) / 2, r);
  return vleft + vright;
}

int getSum(int a, int b) { return query(a, b, 0, 0, n); }

int main() {
  int q, com, x, y, n_, min;
  cin >> n_ >> q;
  initRSQ(n_);
  for (int i = 0; i < q; i++) {
    cin >> com >> x >> y;
    x--;
    if (com == 0)
      add(x, y);
    else
      cout << getSum(x, y) << endl;
  }
  return 0;
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

template <typename T, int MAX_N> struct segment_tree_sum {
  T arr[MAX_N << 1];
  int N;

  segment_tree_sum(int n) {
    N = 1;
    while (N < n)
      N *= 2;

    for (int i = 0; i < 2 * N - 1; ++i) {
      arr[i] = (T)0;
    }
  }

  void add(int i, T v) {
    int j = i + N - 1;
    arr[j] += v;
    while (j > 0) {
      j = (j - 1) / 2;
      arr[j] += v;
    }
  }

  T query(int a, int b) { return query(a, b, 0, 0, N); }

  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return arr[k];

    int m = (l + r) / 2;
    return query(a, b, 2 * k + 1, l, m) + query(a, b, 2 * k + 2, m, r);
  }
};

int N, Q;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  segment_tree_sum<int, 101010> segt(N + 1);
  for (int j = 0; j < Q; ++j) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      segt.add(x, y);
    } else {
      cout << segt.query(x, y + 1) << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

template <typename T, int MAX_N> struct segment_tree_sum {
  T arr[MAX_N << 1];
  int N;

  segment_tree_sum(int n) {
    N = 1;
    while (N < n)
      N *= 2;

    for (int i = 0; i < 2 * N - 1; ++i) {
      arr[i] = (T)0;
    }
  }

  void add(int i, T v) {
    int j = i + N - 1;
    arr[j] += v;
    while (j > 0) {
      j = (j - 1) / 2;
      arr[j] += v;
    }
  }

  T query(int a, int b) { return query(a, b, 0, 0, N); }

  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return arr[k];

    int m = (l + r) / 2;
    return query(a, b, 2 * k + 1, l, m) + query(a, b, 2 * k + 2, m, r);
  }
};

int N, Q;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> Q;
  segment_tree_sum<int, 201010> segt(N + 1);
  for (int j = 0; j < Q; ++j) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      segt.add(x, y);
    } else {
      cout << segt.query(x, y + 1) << endl;
    }
  }
  return 0;
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 10005;
int bit[maxn];
int n;

int get_sum(int i) {
  int ret = 0;
  while (i > 0) {
    ret += bit[i];
    i -= i & -i;
  }
  return ret;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main(void) {
  int op, q, a, b;
  string str;
  ios::sync_with_stdio(false);
  cin >> n >> q;
  while (q--) {
    cin >> op >> a >> b;
    if (op == 0)
      add(a, b);
    else
      cout << get_sum(b) - get_sum(a - 1) << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
const int maxn = 100005;
int bit[maxn];
int n;

int get_sum(int i) {
  int ret = 0;
  while (i > 0) {
    ret += bit[i];
    i -= i & -i;
  }
  return ret;
}

void add(int i, int x) {
  while (i <= n) {
    bit[i] += x;
    i += i & -i;
  }
}

int main(void) {
  int op, q, a, b;
  string str;
  ios::sync_with_stdio(false);
  cin >> n >> q;
  while (q--) {
    cin >> op >> a >> b;
    if (op == 0)
      add(a, b);
    else
      cout << get_sum(b) - get_sum(a - 1) << endl;
  }

  return 0;
}
Problem p02346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

struct SegTree {
  vector<int> node;
  int n;
  SegTree(int sz) {
    n = 1;
    while (sz > n)
      n *= 2;
    node.resize(2 * n - 1, 0);
  }
  void add(int p, int x) {
    p += n - 1;
    node[p] += x;
    do {
      p = (p - 1) / 2;
      node[p] += x;
    } while (p);
  }
  int get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      return node[k];
    }
    return get(a, b, k * 2 + 1, l, (l + r) / 2) +
           get(a, b, k * 2 + 2, (r + l) / 2, r);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree s(n);
  if (n == 1) {
    int s = 0;
    while (q--) {
      int c, x, y;
      cin >> c >> x >> y;
      if (c == 0) {
        s += y;
      } else {
        cout << s << endl;
      }
    }
  }
  while (q--) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      s.add(x - 1, y);
    } else {
      cout << s.get(x - 1, y) << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

struct SegTree {
  vector<int> node;
  int n;
  SegTree(int sz) {
    n = 1;
    while (sz > n)
      n *= 2;
    node.resize(2 * n - 1, 0);
  }
  void add(int p, int x) {
    p += n - 1;
    node[p] += x;
    do {
      p = (p - 1) / 2;
      node[p] += x;
    } while (p);
  }
  int get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l || r <= a)
      return 0;
    if (a <= l && r <= b) {
      return node[k];
    }
    return get(a, b, k * 2 + 1, l, (l + r) / 2) +
           get(a, b, k * 2 + 2, (r + l) / 2, r);
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  SegTree s(n);
  if (n == 1) {
    int s = 0;
    while (q--) {
      int c, x, y;
      cin >> c >> x >> y;
      if (c == 0) {
        s += y;
      } else {
        cout << s << endl;
      }
    }
    return 0;
  }
  while (q--) {
    int c, x, y;
    cin >> c >> x >> y;
    if (c == 0) {
      s.add(x - 1, y);
    } else {
      cout << s.get(x - 1, y) << endl;
    }
  }
}
Problem p02346 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX 100001
int n, seg[MAX * 2 - 1];
void init(int size) {
  n = 1;
  while (n < size)
    n *= 2;
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return seg[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl + vr;
}
void update(int i, int x) {
  i += n - 1;
  seg[i] += x;
  while (i > 0) {
    i = (i - 1) / 2;
    seg[i] = seg[2 * i + 1] + seg[2 * i + 2];
  }
}
int main() {
  int size, q;
  scanf("%d %d", &size, &q);
  init(size);
  while (q--) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0)
      update(x - 1, y);
    else
      printf("%d\n", query(x - 1, y, 0, 0, n));
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
using namespace std;
#define MAX (1 << 17)
int n, seg[MAX * 2 - 1];
void init(int size) {
  n = 1;
  while (n < size)
    n *= 2;
}
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return seg[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl + vr;
}
void update(int i, int x) {
  i += n - 1;
  seg[i] += x;
  while (i > 0) {
    i = (i - 1) / 2;
    seg[i] = seg[2 * i + 1] + seg[2 * i + 2];
  }
}
int main() {
  int size, q;
  scanf("%d %d", &size, &q);
  init(size);
  while (q--) {
    int com, x, y;
    scanf("%d %d %d", &com, &x, &y);
    if (com == 0)
      update(x - 1, y);
    else
      printf("%d\n", query(x - 1, y, 0, 0, n));
  }
}
Problem p02346 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

template <typename T> struct Add {
  T operator()(T x, T y) { return x + y; }
};

template <class T, class Functor> struct SegmentTree {
  size_t n;
  T e;
  vector<T> tree;
  Functor F;
  SegmentTree(size_t m, T e = T()) : n(1), e(e) {
    while (n < m)
      n <<= 1;
    tree.assign(n << 1, e);
  }
  T range_query(size_t a, size_t b, size_t i = 1, size_t l = 0, size_t r = -1) {
    if (!~r)
      r = n;
    if (a <= l && r <= b)
      return tree[i];

    T vl = e, vr = e;
    size_t c = (l + r) >> 1;

    if (a < c)
      vl = F(e, range_query(a, b, i << 1 | 0, l, c));
    if (c < b)
      vr = F(e, range_query(a, b, i << 1 | 1, c, r));

    return F(vl, vr);
  }
  void update(size_t i, T x) {
    i += n;
    tree[i] = x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
  void add(size_t i, T x) {
    i += n;
    tree[i] += x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
};

int main() {
  size_t n, q;
  scanf("%zu %zu", &n, &q);

  SegmentTree<int, Add<int>> tree(n);
  for (size_t i = 0; i < q; ++i) {
    int com;
    scanf("%d", &com);
    if (com) {
      size_t s, t;
      scanf("%zu %zu", &s, &t);
      printf("%d\n", tree.range_query(s, t + 1));
    } else {
      size_t i;
      int x;
      scanf("%zu %d", &i, &x);
      tree.add(i, x);
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

template <typename T> struct Add {
  T operator()(T x, T y) { return x + y; }
};

template <class T, class Functor> struct SegmentTree {
  size_t n;
  T e;
  vector<T> tree;
  Functor F;
  SegmentTree(size_t m, T e = T()) : n(1), e(e) {
    while (n <= m)
      n <<= 1;
    tree.assign(n << 1, e);
  }
  T range_query(size_t a, size_t b, size_t i = 1, size_t l = 0, size_t r = -1) {
    if (!~r)
      r = n;
    if (a <= l && r <= b)
      return tree[i];

    T vl = e, vr = e;
    size_t c = (l + r) >> 1;

    if (a < c)
      vl = F(e, range_query(a, b, i << 1 | 0, l, c));
    if (c < b)
      vr = F(e, range_query(a, b, i << 1 | 1, c, r));

    return F(vl, vr);
  }
  void update(size_t i, T x) {
    i += n;
    tree[i] = x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
  void add(size_t i, T x) {
    i += n;
    tree[i] += x;
    while (i >>= 1)
      tree[i] = F(tree[i << 1 | 0], tree[i << 1 | 1]);
  }
};

int main() {
  size_t n, q;
  scanf("%zu %zu", &n, &q);

  SegmentTree<int, Add<int>> tree(n);
  for (size_t i = 0; i < q; ++i) {
    int com;
    scanf("%d", &com);
    if (com) {
      size_t s, t;
      scanf("%zu %zu", &s, &t);
      printf("%d\n", tree.range_query(s, t + 1));
    } else {
      size_t i;
      int x;
      scanf("%zu %d", &i, &x);
      tree.add(i, x);
    }
  }

  return 0;
}
Problem p02348 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[200010];
  void build(int now, int l, int r) {
    if (l == r) {
      map[now] = INF;
      return;
    }
    map[now] = -1;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] = n;
      return;
    }
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      map[now] = -1;
    }
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      int temp = map[now];
      map[now] = -1;
      return temp;
    }
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 0, n - 1);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 0, n - 1, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\n", arr.find(0, 0, n - 1, k));
    }
  }
}

Fixed submission:
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[2000010];
  void build(int now, int l, int r) {
    if (l == r) {
      map[now] = INF;
      return;
    }
    map[now] = -1;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] = n;
      return;
    }
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      map[now] = -1;
    }
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    if (map[now] != -1) {
      map[now * 2 + 1] = map[now * 2 + 2] = map[now];
      int temp = map[now];
      map[now] = -1;
      return temp;
    }
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 0, n - 1);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 0, n - 1, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\n", arr.find(0, 0, n - 1, k));
    }
  }
}
Problem p02349 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
int n, z;

pair<int, int> st[4 * 100000];

int op(int iz, int der) { return st[iz].first + st[der].first; }
void push(int v, int l, int r) {
  int m = (r + l) / 2;
  st[2 * v].second += st[v].second;
  st[2 * v].first += st[v].second * (m - l);
  st[2 * v + 1].second += st[v].second;
  st[2 * v + 1].first += st[v].second * (r - m);
  st[v].second = 0;
}

void u(int v, int l, int r, int i, int j, int x) {
  push(v, l, r);
  if (j < l || i >= r) {
    return;
  } else if (i <= l && r - 1 <= j) {
    st[v].second += x;
    st[v].first += (r - l) * x;
  } else {
    int m = (r + l) / 2;
    u(2 * v, l, m, i, j, x);
    u(2 * v + 1, m, r, i, j, x);
    st[v].first = op(2 * v, 2 * v + 1);
  }
}

int q(int v, int l, int r, int x) {
  push(v, l, r);
  if (x < l || x >= r) {
    return 0;
  } else {
    if (r - l == 1) {
      return st[v].first;
    } else {
      int m = (r + l) / 2;
      return max(q(2 * v, l, m, x), q(2 * v + 1, m, r, x));
    }
  }
}

int main() {
  cin >> n >> z;
  int a, b, c, d;
  while (z--) {
    cin >> a;
    if (a == 0) {
      cin >> b >> c >> d;
      u(1, 1, n + 1, min(b, c), max(b, c), d);
    } else {
      cin >> b;
      cout << q(1, 1, n + 1, b) << '\n';
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
int n, z;

pair<int, int> st[4 * 100000];

int op(int iz, int der) { return st[iz].first + st[der].first; }
void push(int v, int l, int r) {
  if (r - l == 1)
    return;
  int m = (r + l) / 2;
  st[2 * v].second += st[v].second;
  st[2 * v].first += st[v].second * (m - l);
  st[2 * v + 1].second += st[v].second;
  st[2 * v + 1].first += st[v].second * (r - m);
  st[v].second = 0;
}

void u(int v, int l, int r, int i, int j, int x) {
  push(v, l, r);
  if (j < l || i >= r) {
    return;
  } else if (i <= l && r - 1 <= j) {
    st[v].second += x;
    st[v].first += (r - l) * x;
  } else {
    int m = (r + l) / 2;
    u(2 * v, l, m, i, j, x);
    u(2 * v + 1, m, r, i, j, x);
    st[v].first = op(2 * v, 2 * v + 1);
  }
}

int q(int v, int l, int r, int x) {
  push(v, l, r);
  if (x < l || x >= r) {
    return 0;
  } else {
    if (r - l == 1) {
      return st[v].first;
    } else {
      int m = (r + l) / 2;
      return max(q(2 * v, l, m, x), q(2 * v + 1, m, r, x));
    }
  }
}

int main() {
  cin >> n >> z;
  int a, b, c, d;
  while (z--) {
    cin >> a;
    if (a == 0) {
      cin >> b >> c >> d;
      u(1, 1, n + 1, min(b, c), max(b, c), d);
    } else {
      cin >> b;
      cout << q(1, 1, n + 1, b) << '\n';
    }
  }
  return 0;
}
Problem p02349 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[200010];
  void build(int now, int l, int r) {
    map[now] = 0;
    if (l == r)
      return;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] += n;
      return;
    }
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 1, n);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 1, n, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\n", arr.find(0, 1, n, k));
    }
  }
}

Fixed submission:
#include <stdio.h>
const int INF = 2147483647;
struct segment_tree {
  int map[2000010];
  void build(int now, int l, int r) {
    map[now] = 0;
    if (l == r)
      return;
    int mid = (l + r) / 2;
    build(now * 2 + 1, l, mid);
    build(now * 2 + 2, mid + 1, r);
    return;
  }
  void change(int n, int now, int L, int R, int l, int r) {
    if (R < l || L > r)
      return;
    if (L >= l && R <= r) {
      map[now] += n;
      return;
    }
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    change(n, now * 2 + 1, L, mid, l, r);
    change(n, now * 2 + 2, mid + 1, R, l, r);
    return;
  }
  int find(int now, int L, int R, int n) {
    if (L == R)
      return map[now];
    map[now * 2 + 1] += map[now];
    map[now * 2 + 2] += map[now];
    map[now] = 0;
    int mid = (L + R) / 2;
    if (mid >= n)
      return find(now * 2 + 1, L, mid, n);
    else
      return find(now * 2 + 2, mid + 1, R, n);
  }
};
int main() {
  int n, m, k, l, r;
  segment_tree arr;
  scanf("%d%d", &n, &m);
  arr.build(0, 1, n);
  while (m--) {
    scanf("%d", &k);
    if (k == 0) {
      scanf("%d%d%d", &l, &r, &k);
      arr.change(k, 0, 1, n, l, r);
    } else {
      scanf("%d", &k);
      printf("%d\n", arr.find(0, 1, n, k));
    }
  }
}
Problem p02350 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int n, q;

struct Node {
  int min = 0x7fffffff, lazy = -1;
} seg[400005];

void update(int u, int l, int r, int ql, int qr, int v) {
  if (seg[u].lazy != -1) {
    seg[u << 1].lazy = seg[u << 1 | 1].lazy = seg[u].lazy;
    seg[u].lazy = -1;
  }

  if (l == ql && r == qr) {
    seg[u].lazy = v;
    return;
  }

  int m = (l + r) >> 1;

  if (qr <= m)
    update(u << 1, l, m, ql, qr, v);
  else if (ql > m)
    update(u << 1 | 1, m + 1, r, ql, qr, v);
  else
    update(u << 1, l, m, ql, m, v), update(u << 1 | 1, m + 1, r, m + 1, qr, v);

  int lm = seg[u << 1].lazy != -1 ? seg[u << 1].lazy : seg[u << 1].min;
  int rm =
      seg[u << 1 | 1].lazy != -1 ? seg[u << 1 | 1].lazy : seg[u << 1 | 1].min;

  seg[u].min = min(lm, rm);
}

int query(int u, int l, int r, int ql, int qr) {
  if (seg[u].lazy != -1)
    return seg[u].lazy;

  if (l == ql && r == qr)
    return seg[u].min;

  int m = (l + r) >> 1;
  if (qr <= m)
    return query(u << 1, l, m, ql, qr);
  else if (ql > m)
    return query(u << 1 | 1, m + 1, r, ql, qr);
  else
    return min(query(u << 1, l, m, ql, m),
               query(u << 1 | 1, m + 1, r, m + 1, qr));
}

int main() {
  cin >> n >> q;

  for (int i = 0; i < q; i++) {
    int o;
    cin >> o;
    if (o == 0) {
      // update
      int l, r, v;
      cin >> l >> r >> v;
      update(1, 0, n - 1, l, r, v);
    } else {
      int l, r;
      cin >> l >> r;
      cout << query(1, 0, n - 1, l, r) << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int n, q;

struct Node {
  int min = 0x7fffffff, lazy = -1;
} seg[800005];

void update(int u, int l, int r, int ql, int qr, int v) {
  if (seg[u].lazy != -1) {
    seg[u << 1].lazy = seg[u << 1 | 1].lazy = seg[u].lazy;
    seg[u].lazy = -1;
  }

  if (l == ql && r == qr) {
    seg[u].lazy = v;
    return;
  }

  int m = (l + r) >> 1;

  if (qr <= m)
    update(u << 1, l, m, ql, qr, v);
  else if (ql > m)
    update(u << 1 | 1, m + 1, r, ql, qr, v);
  else
    update(u << 1, l, m, ql, m, v), update(u << 1 | 1, m + 1, r, m + 1, qr, v);

  int lm = seg[u << 1].lazy != -1 ? seg[u << 1].lazy : seg[u << 1].min;
  int rm =
      seg[u << 1 | 1].lazy != -1 ? seg[u << 1 | 1].lazy : seg[u << 1 | 1].min;

  seg[u].min = min(lm, rm);
}

int query(int u, int l, int r, int ql, int qr) {
  if (seg[u].lazy != -1)
    return seg[u].lazy;

  if (l == ql && r == qr)
    return seg[u].min;

  int m = (l + r) >> 1;
  if (qr <= m)
    return query(u << 1, l, m, ql, qr);
  else if (ql > m)
    return query(u << 1 | 1, m + 1, r, ql, qr);
  else
    return min(query(u << 1, l, m, ql, m),
               query(u << 1 | 1, m + 1, r, m + 1, qr));
}

int main() {
  cin >> n >> q;

  for (int i = 0; i < q; i++) {
    int o;
    cin >> o;
    if (o == 0) {
      // update
      int l, r, v;
      cin >> l >> r >> v;
      update(1, 0, n - 1, l, r, v);
    } else {
      int l, r;
      cin >> l >> r;
      cout << query(1, 0, n - 1, l, r) << endl;
    }
  }
}
Problem p02350 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

constexpr int MAX_N = 100000, INF = 2147483647;

int n, q;

int seg[MAX_N * 4], lazy[MAX_N * 4];

bool flag[MAX_N * 4];

void init(int size) {

  n = 1;

  while (n < size)
    n *= 2;

  for (int i = 0; i < n * 2 - 1; ++i) {
    seg[i] = INF;
    lazy[i] = INF;
  }
}

void eval(int k, int l, int r) {

  if (flag[k]) {

    seg[k] = lazy[k];

    if (r - l > 1) {
      lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
      flag[k * 2 + 1] = flag[k * 2 + 2] = true;
    }
  }
  flag[k] = false;
}

void update(int a, int b, int v, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return;

  if (a <= l && r <= b) {

    lazy[k] = v;

    flag[k] = true;

    eval(k, l, r);

    return;
  }

  update(a, b, v, k * 2 + 1, l, (l + r) / 2);

  update(a, b, v, k * 2 + 2, (l + r) / 2, r);

  seg[k] = std::min(seg[k * 2 + 1], seg[k * 2 + 2]);
}

int query(int a, int b, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return INF;

  if (a <= l && r <= b)
    return seg[k];

  return std::min(query(a, b, k * 2 + 1, l, (l + r) / 2),
                  query(a, b, k * 2 + 2, (l + r) / 2, r));
}

int main() {

  std::cin >> n >> q;

  init(n);

  int com, s, t, x;

  for (int i = 0; i < q; ++i) {

    std::cin >> com >> s >> t;

    if (com == 0) {
      std::cin >> x;
      update(s, t + 1, x, 0, 0, n);
    } else
      std::cout << query(s, t + 1, 0, 0, n) << std::endl;
  }

  system("pause");

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>

constexpr int MAX_N = 100000, INF = 2147483647;

int n, q;

int seg[MAX_N * 4], lazy[MAX_N * 4];

bool flag[MAX_N * 4];

void init(int size) {

  n = 1;

  while (n < size)
    n *= 2;

  for (int i = 0; i < n * 2 - 1; ++i) {
    seg[i] = INF;
    lazy[i] = INF;
  }
}

void eval(int k, int l, int r) {

  if (flag[k]) {

    seg[k] = lazy[k];

    if (r - l > 1) {
      lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
      flag[k * 2 + 1] = flag[k * 2 + 2] = true;
    }
  }
  flag[k] = false;
}

void update(int a, int b, int v, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return;

  if (a <= l && r <= b) {

    lazy[k] = v;

    flag[k] = true;

    eval(k, l, r);

    return;
  }

  update(a, b, v, k * 2 + 1, l, (l + r) / 2);

  update(a, b, v, k * 2 + 2, (l + r) / 2, r);

  seg[k] = std::min(seg[k * 2 + 1], seg[k * 2 + 2]);
}

int query(int a, int b, int k, int l, int r) {

  eval(k, l, r);

  if (r <= a || b <= l)
    return INF;

  if (a <= l && r <= b)
    return seg[k];

  return std::min(query(a, b, k * 2 + 1, l, (l + r) / 2),
                  query(a, b, k * 2 + 2, (l + r) / 2, r));
}

int main() {

  std::cin >> n >> q;

  init(n);

  int com, s, t, x;

  for (int i = 0; i < q; ++i) {

    std::cin >> com >> s >> t;

    if (com == 0) {
      std::cin >> x;
      update(s, t + 1, x, 0, 0, n);
    } else
      std::cout << query(s, t + 1, 0, 0, n) << std::endl;
  }

  return 0;
}
Problem p02355 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> counts(k + 1, 0);
  vector<int> v(n);

  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  int total = 0;

  int ans = 1000000000;
  for (int l = 0, r = 0; r < n; r++) {
    if (v[r] > k) {
      continue;
    }
    counts[v[r]]++;
    if (counts[v[r]] == 1) {
      total++;
    }
    while (l < r && (v[l] > k || (total == k && counts[v[l]] > 1))) {
      counts[v[l]]--;
      l++;
    }
    if (total == k) {
      ans = min(ans, r - l + 1);
    }
  }

  cout << (total == k ? ans : 0) << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> counts(k + 1, 0);
  vector<int> v(n);

  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  int total = 0;

  int ans = 1000000000;
  for (int l = 0, r = 0; r < n; r++) {
    if (v[r] > k) {
      continue;
    }
    counts[v[r]]++;
    if (counts[v[r]] == 1) {
      total++;
    }
    while (l < r && (v[l] > k || (total == k && counts[v[l]] > 1))) {
      if (v[l] <= k) {
        counts[v[l]]--;
      }
      l++;
    }
    if (total == k) {
      ans = min(ans, r - l + 1);
    }
  }

  cout << (total == k ? ans : 0) << endl;

  return 0;
}
Problem p02356 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
using namespace std;
long long p[100005], N, Q, A;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    scanf("%lld", p[i]);
  for (int i = 0; i < Q; i++) {
    long long sum = 0, R = 0;
    cin >> A;
    long long cnt = 0;
    for (int i = 0; i < N; i++) {
      while (R < N) {
        if (sum + p[R] > A)
          break;
        sum += p[R];
        R++;
      }
      if (sum > 0) {
        sum -= p[i];
      }
      cnt += R - i;
      if (R < i + 1)
        R = i + 1;
    }
    cout << cnt << endl;
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
using namespace std;
long long p[100005], N, Q, A;
int main() {
  cin >> N >> Q;
  for (int i = 0; i < N; i++)
    scanf("%lld", &p[i]);
  for (int i = 0; i < Q; i++) {
    long long sum = 0, R = 0;
    cin >> A;
    long long cnt = 0;
    for (int i = 0; i < N; i++) {
      while (R < N) {
        if (sum + p[R] > A)
          break;
        sum += p[R];
        R++;
      }
      if (sum > 0) {
        sum -= p[i];
      }
      cnt += R - i;
      if (R < i + 1)
        R = i + 1;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p02357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, l, a[111111];
  cin >> n >> l;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  priority_queue<P> q;
  for (int i = 0; i < n; i++) {
    q.push(P(-a[i], i));
    if (i < l - 1)
      continue;
    while (q.top().second <= i - l)
      q.pop();
    cout << -q.top().first;
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int main() {
  int n, l, a[1111111];
  cin >> n >> l;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  priority_queue<P> q;
  for (int i = 0; i < n; i++) {
    q.push(P(-a[i], i));
    if (i < l - 1)
      continue;
    while (q.top().second <= i - l)
      q.pop();
    cout << -q.top().first;
    if (i == n - 1)
      cout << endl;
    else
      cout << " ";
  }
  return 0;
}
Problem p02357 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int a[400000];
int main() {
  int n, l;
  scanf("%d%d", &n, &l);
  rep(i, n) scanf("%d", &a[i]);
  multiset<int> se;
  rep(i, l) se.insert(a[i]);
  printf("%d", *se.begin());
  for (int i = l; i < n; i++) {
    se.insert(a[i]);
    se.erase(se.find(a[i - l]));
    printf(" %d", *se.begin());
  }
  puts("");
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int a[1000000];
int main() {
  int n, l;
  scanf("%d%d", &n, &l);
  rep(i, n) scanf("%d", &a[i]);
  multiset<int> se;
  rep(i, l) se.insert(a[i]);
  printf("%d", *se.begin());
  for (int i = l; i < n; i++) {
    se.insert(a[i]);
    se.erase(se.find(a[i - l]));
    printf(" %d", *se.begin());
  }
  puts("");
}
Problem p02357 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>

using namespace std;

int main() {

  int N, L, a[1000000];

  cin >> N >> L;

  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  // solve
  multiset<int> ms;

  for (int i = 0; i < L; i++) {
    ms.insert(a[i]);
  }

  cout << *ms.begin();

  for (int i = L; i < N; i++) {
    int c = ms.erase(a[i - L]);
    for (int j = 0; j < c - 1; j++) {
      ms.insert(a[i - L]);
    }
    ms.insert(a[i]);
    cout << ' ' << *ms.begin();
  }

  cout << endl;
}


Fixed submission:
#include <iostream>
#include <set>

using namespace std;

int main() {

  int N, L, a[1000000];

  cin >> N >> L;

  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }

  // solve
  multiset<int> ms;

  for (int i = 0; i < L; i++) {
    ms.insert(a[i]);
  }

  cout << *ms.begin();

  for (int i = L; i < N; i++) {
    ms.erase(ms.find(a[i - L]));
    ms.insert(a[i]);
    cout << ' ' << *ms.begin();
  }

  cout << endl;
}
Problem p02357 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<long long int, long long int> P;
long long int INF = 1e18;

long long int a[110000], deq[110000];

int main() {

  int n, L;
  cin >> n >> L;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int s = 0, t = 0; // 先頭と末尾

  for (int i = 0; i < n; i++) {
    while (s < t && a[deq[t - 1]] >= a[i]) {
      t--;
    }
    deq[t] = i;
    t++;
    if (i - L + 1 >= 0) {
      // cout << deq[s] << " " << i - L + 1 << endl;
      printf("%d%c", a[deq[s]], i == n - 1 ? '\n' : ' ');
      if (deq[s] == i - L + 1) {
        s++;
      }
    } else {
      continue;
    }
  }

  return 0;
}


Fixed submission:
#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef pair<long long int, long long int> P;
long long int INF = 1e18;

long long int a[1100000], deq[1100000];

int main() {

  int n, L;
  cin >> n >> L;

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  int s = 0, t = 0; // 先頭と末尾

  for (int i = 0; i < n; i++) {
    while (s < t && a[deq[t - 1]] >= a[i]) {
      t--;
    }
    deq[t] = i;
    t++;
    if (i - L + 1 >= 0) {
      // cout << deq[s] << " " << i - L + 1 << endl;
      printf("%d%c", a[deq[s]], i == n - 1 ? '\n' : ' ');
      if (deq[s] == i - L + 1) {
        s++;
      }
    } else {
      continue;
    }
  }

  return 0;
}
Problem p02358 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 2005;

vector<int> X, Y, X1, Y1, X2, Y2;
bool flag[MAX_N][MAX_N];
int n;

int main() {
  int p, q, r, s;
  scanf("%d", &n);
  rep(i, n) {
    scanf("%d%d%d%d", &p, &q, &r, &s);
    X.push_back(p), X.push_back(r);
    Y.push_back(q), Y.push_back(s);
    X1.push_back(p), X2.push_back(r);
    Y1.push_back(q), Y2.push_back(s);
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  rep(i, n) {
    X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
    Y1[i] = lower_bound(Y.begin(), Y.end(), Y1[i]) - Y.begin();
    X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
    Y2[i] = lower_bound(Y.begin(), Y.end(), Y2[i]) - Y.begin();
  }
  rep(i, n) {
    for (int j = Y1[i]; j < Y2[i]; j++) {
      for (int k = X1[i]; k < X2[i]; k++) {
        flag[j][k] = true;
      }
    }
  }
  ll ans = 0;
  rep(i, Y.size() - 1) {
    rep(j, X.size() - 1) {
      if (flag[i][j]) {
        ans += (ll)(X[j + 1] - X[j]) * (Y[i + 1] - Y[i]);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i, n) for (int i = 0; i < n; ++i)

using namespace std;

typedef pair<int, int> P;

const int MAX_N = 2005;

vector<int> X, Y, X1, Y1, X2, Y2;
bool flag[MAX_N * 2][MAX_N * 2];
int n;

int main() {
  int p, q, r, s;
  scanf("%d", &n);
  rep(i, n) {
    scanf("%d%d%d%d", &p, &q, &r, &s);
    X.push_back(p), X.push_back(r);
    Y.push_back(q), Y.push_back(s);
    X1.push_back(p), X2.push_back(r);
    Y1.push_back(q), Y2.push_back(s);
  }
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  rep(i, n) {
    X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
    Y1[i] = lower_bound(Y.begin(), Y.end(), Y1[i]) - Y.begin();
    X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
    Y2[i] = lower_bound(Y.begin(), Y.end(), Y2[i]) - Y.begin();
  }
  rep(i, n) {
    for (int j = Y1[i]; j < Y2[i]; j++) {
      for (int k = X1[i]; k < X2[i]; k++) {
        flag[j][k] = true;
      }
    }
  }
  ll ans = 0;
  rep(i, Y.size() - 1) {
    rep(j, X.size() - 1) {
      if (flag[i][j]) {
        ans += (ll)(X[j + 1] - X[j]) * (Y[i + 1] - Y[i]);
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p02358 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)

ll N;
vector<ll> x, y;
ll x1[2000], y1[2000], x2[2000], y2[2000];

ll c[2020][2020];

int main() {
  cin >> N;
  rep(i, N) {
    cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; //[x1,x2) [y1,y2)

    x.push_back(x1[i]);
    x.push_back(x2[i]);
    y.push_back(y1[i]);
    y.push_back(y2[i]);
  }

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());

  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  rep(i, N) {
    ll ix1 = lower_bound(x.begin(), x.end(), x1[i]) - x.begin();
    ll ix2 = lower_bound(x.begin(), x.end(), x2[i]) - x.begin();
    ll iy1 = lower_bound(y.begin(), y.end(), y1[i]) - y.begin();
    ll iy2 = lower_bound(y.begin(), y.end(), y2[i]) - y.begin();

    c[iy1][ix1]++;
    c[iy1][ix2]--;
    c[iy2][ix1]--;
    c[iy2][ix2]++;
  }

  rep(i, y.size()) {
    rep(j, x.size() - 1) { c[i][j + 1] += c[i][j]; }
  }

  rep(j, x.size()) {
    rep(i, y.size() - 1) { c[i + 1][j] += c[i][j]; }
  }

  ll ans = 0;
  rep(i, y.size() - 1) {
    rep(j, x.size() - 1) {
      if (c[i][j] > 0) {
        ans += (x[j + 1] - x[j]) * (y[i + 1] - y[i]);
      }
    }
  }

  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

typedef long long ll;

#define rep(i, n) for (int i = 0; i < (n); i++)

ll N;
vector<ll> x, y;
ll x1[2000], y1[2000], x2[2000], y2[2000];

ll c[4020][4020]; // x1,x2があるのでNの倍必要

int main() {
  cin >> N;
  rep(i, N) {
    cin >> x1[i] >> y1[i] >> x2[i] >> y2[i]; //[x1,x2) [y1,y2)

    x.push_back(x1[i]);
    x.push_back(x2[i]);
    y.push_back(y1[i]);
    y.push_back(y2[i]);
  }

  sort(x.begin(), x.end());
  sort(y.begin(), y.end());

  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());

  rep(i, N) {
    ll ix1 = lower_bound(x.begin(), x.end(), x1[i]) - x.begin();
    ll ix2 = lower_bound(x.begin(), x.end(), x2[i]) - x.begin();
    ll iy1 = lower_bound(y.begin(), y.end(), y1[i]) - y.begin();
    ll iy2 = lower_bound(y.begin(), y.end(), y2[i]) - y.begin();

    c[iy1][ix1]++;
    c[iy1][ix2]--;
    c[iy2][ix1]--;
    c[iy2][ix2]++;
  }

  rep(i, y.size()) {
    rep(j, x.size() - 1) { c[i][j + 1] += c[i][j]; }
  }

  rep(j, x.size()) {
    rep(i, y.size() - 1) { c[i + 1][j] += c[i][j]; }
  }

  ll ans = 0;
  rep(i, y.size() - 1) {
    rep(j, x.size() - 1) {
      if (c[i][j] > 0) {
        ans += (x[j + 1] - x[j]) * (y[i + 1] - y[i]);
      }
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02358 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
//
int n;
ll px[2000], py[2000], qx[2000], qy[2000];
int dp[8000][8000];
vector<ll> compress(ll *x1, ll *x2) {
  vector<ll> xs;
  for (int i = 0; i < n; i++) {
    for (int j = -1; j <= 1; j++) {
      xs.push_back(x1[i] + j);
      xs.push_back(x2[i] + j);
    }
  }
  xs.push_back(1e18 + 7);
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < n; i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  };
  return xs;
}
//
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> px[i] >> py[i] >> qx[i] >> qy[i];
  }
  vector<ll> X = compress(px, qx);
  vector<ll> Y = compress(py, qy);
  for (int i = 0; i < n; i++) {
    dp[px[i]][py[i]]++;
    dp[qx[i]][qy[i]]++;
    dp[px[i]][qy[i]]--;
    dp[qx[i]][py[i]]--;
  }
  for (int i = 0; i < X.size() - 1; i++) {
    for (int j = 1; j < Y.size() - 1; j++) {
      dp[i][j] += dp[i][j - 1];
    }
  }
  for (int i = 0; i < Y.size() - 1; i++) {
    for (int j = 1; j < X.size() - 1; j++) {
      dp[j][i] += dp[j - 1][i];
    }
  }
  ll ans = 0;
  for (int i = 0; i < X.size() - 1; i++) {
    for (int j = 0; j < Y.size() - 1; j++) {
      if (dp[i][j] > 0) {
        ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
      }
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define ll long long
using namespace std;
//
int n;
ll px[2000], py[2000], qx[2000], qy[2000];
int dp[8000][8000];
vector<ll> compress(ll *x1, ll *x2) {
  vector<ll> xs;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= 1; j++) {
      xs.push_back(x1[i] + j);
      xs.push_back(x2[i] + j);
    }
  }
  xs.push_back(1e18 + 7);
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  for (int i = 0; i < n; i++) {
    x1[i] = lower_bound(xs.begin(), xs.end(), x1[i]) - xs.begin();
    x2[i] = lower_bound(xs.begin(), xs.end(), x2[i]) - xs.begin();
  };
  return xs;
}
//
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> px[i] >> py[i] >> qx[i] >> qy[i];
  }
  vector<ll> X = compress(px, qx);
  vector<ll> Y = compress(py, qy);
  for (int i = 0; i < n; i++) {
    dp[px[i]][py[i]]++;
    dp[qx[i]][qy[i]]++;
    dp[px[i]][qy[i]]--;
    dp[qx[i]][py[i]]--;
  }
  for (int i = 0; i < X.size() - 1; i++) {
    for (int j = 1; j < Y.size() - 1; j++) {
      dp[i][j] += dp[i][j - 1];
    }
  }
  for (int i = 0; i < Y.size() - 1; i++) {
    for (int j = 1; j < X.size() - 1; j++) {
      dp[j][i] += dp[j - 1][i];
    }
  }
  ll ans = 0;
  for (int i = 0; i < X.size() - 1; i++) {
    for (int j = 0; j < Y.size() - 1; j++) {
      if (dp[i][j] > 0) {
        ans += (X[i + 1] - X[i]) * (Y[j + 1] - Y[j]);
      }
    }
  }
  cout << ans << endl;
}
Problem p02358 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n;
int X1[2000], Y1[2000], X2[2000], Y2[2000];
int fld[2000][2000];
int compress(int *X1, int *X2, vector<int> &X) {
  rep(i, n) {
    X.push_back(X1[i]);
    X.push_back(X2[i]);
  }
  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  rep(i, n) {
    X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
    X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
  }
  return X.size();
}
int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
  vector<int> a, b;
  int w = compress(X1, X2, a), h = compress(Y1, Y2, b);
  rep(i, n) {
    for (int j = X1[i]; j < X2[i]; j++)
      for (int k = Y1[i]; k < Y2[i]; k++)
        fld[j][k] = true;
  }
  ll ans = 0;
  rep(i, w - 1) rep(j, h - 1) {
    if (fld[i][j])
      ans += ((ll)a[i + 1] - a[i]) * (b[j + 1] - b[j]);
  }
  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n;
int X1[2000], Y1[2000], X2[2000], Y2[2000];
int fld[4000][4000];
int compress(int *X1, int *X2, vector<int> &X) {
  rep(i, n) {
    X.push_back(X1[i]);
    X.push_back(X2[i]);
  }
  sort(X.begin(), X.end());
  X.erase(unique(X.begin(), X.end()), X.end());
  rep(i, n) {
    X1[i] = lower_bound(X.begin(), X.end(), X1[i]) - X.begin();
    X2[i] = lower_bound(X.begin(), X.end(), X2[i]) - X.begin();
  }
  return X.size();
}
int main() {
  scanf("%d", &n);
  rep(i, n) scanf("%d%d%d%d", &X1[i], &Y1[i], &X2[i], &Y2[i]);
  vector<int> a, b;
  int w = compress(X1, X2, a), h = compress(Y1, Y2, b);
  rep(i, n) {
    for (int j = X1[i]; j < X2[i]; j++)
      for (int k = Y1[i]; k < Y2[i]; k++)
        fld[j][k] = true;
  }
  ll ans = 0;
  rep(i, w - 1) rep(j, h - 1) {
    if (fld[i][j])
      ans += ((ll)a[i + 1] - a[i]) * (b[j + 1] - b[j]);
  }
  printf("%lld\n", ans);
}