prithivMLmods/Coder-Stat huggingface dataset:
Problem p03640 (Python)

Buggy submission (status: Runtime Error):
import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7


def resolve():
    H, W = map(int, input().split())
    n = int(input())
    A = list(map(int, input().split()))

    grid = [[0 for _ in range(W)] for _ in range(W)]
    h, w = 0, 0
    for i in range(n):
        cnt = A[i]
        while cnt:
            grid[h][w] = i + 1
            cnt -= 1
            if w + 1 < W:
                w += 1
            else:
                h += 1
                w = 0

    for h in range(H):
        print(*grid[h]) if h % 2 == 0 else print(*grid[h][::-1])


if __name__ == "__main__":
    resolve()


Fixed submission:
import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7


def resolve():
    H, W = map(int, input().split())
    n = int(input())
    A = list(map(int, input().split()))

    grid = [[0 for _ in range(W)] for _ in range(H)]
    h, w = 0, 0
    for i in range(n):
        cnt = A[i]
        while cnt:
            grid[h][w] = i + 1
            cnt -= 1
            if w + 1 < W:
                w += 1
            else:
                h += 1
                w = 0

    for h in range(H):
        print(*grid[h]) if h % 2 == 0 else print(*grid[h][::-1])


if __name__ == "__main__":
    resolve()
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 123;
int ans[MAXN][MAXN];
int a[MAXN];

int main() {
  int H, W, N;
  while (~scanf("%d %d", &H, &W)) {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++)
      scanf("%d", &a[i]);
    int curidx = 0;
    int dir = 1;
    for (int i = 1; i <= N; i++) {
      while (a[i]) {
        int x = curidx / W + 1;
        int y = curidx % W + 1;
        ans[x][y] = i;
        a[i]--;
        if (curidx % W == W - 1 && dir == 1) {
          dir = -1;
          curidx += W;
        } else if (curidx % W == 0 && dir == -1) {
          dir = 1;
          curidx += W;
        } else
          curidx += dir;
      }
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        printf("%d ", ans[i][j]);
      }
      puts("");
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int MAXN = 123;
int ans[MAXN][MAXN];
int a[MAXN * MAXN];

int main() {
  int H, W, N;
  while (~scanf("%d %d", &H, &W)) {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++)
      scanf("%d", &a[i]);
    int curidx = 0;
    int dir = 1;
    for (int i = 1; i <= N; i++) {
      while (a[i]) {
        int x = curidx / W + 1;
        int y = curidx % W + 1;
        ans[x][y] = i;
        a[i]--;
        if (curidx % W == W - 1 && dir == 1) {
          dir = -1;
          curidx += W;
        } else if (curidx % W == 0 && dir == -1) {
          dir = 1;
          curidx += W;
        } else
          curidx += dir;
      }
    }
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++) {
        printf("%d ", ans[i][j]);
      }
      puts("");
    }
  }
  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int N, H, W, a[109], c[109][109];
int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
    for (int j = a[i]; j < a[i + 1]; j++) {
      int x = j / W, y = j % W;
      if (x % 2 == 1)
        y = W - y - 1;
      c[x][y] = i + 1;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << c[i][j] << ' ';
    }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int N, H, W, a[10009], c[109][109];
int main() {
  cin >> H >> W >> N;
  for (int i = 0; i < N; i++) {
    cin >> a[i + 1];
    a[i + 1] += a[i];
    for (int j = a[i]; j < a[i + 1]; j++) {
      int x = j / W, y = j % W;
      if (x % 2 == 1)
        y = W - y - 1;
      c[x][y] = i + 1;
    }
  }
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << c[i][j] << ' ';
    }
    cout << endl;
  }
  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> istream &operator>>(istream &s, vector<T> &v) {
  for (T &x : v) {
    s >> x;
  }
  return s;
}
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int SIZE(string s) { return (int)s.size(); }

int main() {
  cout << setprecision(16);
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<int>> ans(w, vector<int>(h));
  int x = 0;
  rep(i, h) {
    if (i % 2 == 0)
      rep(j, w) {
        if (a[x] == 0)
          x++;
        ans[i][j] = x + 1;
        a[x]--;
      }
    else {
      for (int j = w - 1; j >= 0; j--) {
        if (a[x] == 0)
          x++;
        ans[i][j] = x + 1;
        a[x]--;
      }
    }
  }
  rep(i, h) {
    rep(j, w) cout << ans[i][j] << " ";
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
template <class T> istream &operator>>(istream &s, vector<T> &v) {
  for (T &x : v) {
    s >> x;
  }
  return s;
}
void yes() { cout << "Yes" << endl; }
void no() { cout << "No" << endl; }
int SIZE(string s) { return (int)s.size(); }

int main() {
  cout << setprecision(16);
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<vector<int>> ans(h, vector<int>(w));
  int x = 0;
  rep(i, h) {
    if (i % 2 == 0)
      rep(j, w) {
        if (a[x] == 0)
          x++;
        ans[i][j] = x + 1;
        a[x]--;
      }
    else {
      for (int j = w - 1; j >= 0; j--) {
        if (a[x] == 0)
          x++;
        ans[i][j] = x + 1;
        a[x]--;
      }
    }
  }
  rep(i, h) {
    rep(j, w) cout << ans[i][j] << " ";
    cout << endl;
  }
  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

int h, w;
int n;
int a;
int grid[105][105];

int main() {
  scanf("%d%d%d", &h, &w, &n);
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    scanf("%d", a);
    for (int j = 0; j < a; j++)
      q.push(i);
  }
  for (int i = 1; i <= h; i++) {
    if (i % 2 == 1) {
      for (int j = 1; j <= w; j++) {
        grid[i][j] = q.front();
        q.pop();
      }
    } else {
      for (int j = w; j >= 1; j--) {
        grid[i][j] = q.front();
        q.pop();
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      printf("%d ", grid[i][j]);
    }
    printf("\n");
  }
}

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define db double
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define PII pair<int, int>

int h, w;
int n;
int a;
int grid[105][105];

int main() {
  scanf("%d%d%d", &h, &w, &n);
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a);
    for (int j = 0; j < a; j++)
      q.push(i);
  }
  for (int i = 1; i <= h; i++) {
    if (i % 2 == 1) {
      for (int j = 1; j <= w; j++) {
        grid[i][j] = q.front();
        q.pop();
      }
    } else {
      for (int j = w; j >= 1; j--) {
        grid[i][j] = q.front();
        q.pop();
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      printf("%d ", grid[i][j]);
    }
    printf("\n");
  }
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = (n); i < (int)N; i++)
#define RREP(i, n, N) for (int i = N - 1; i >= (int)n; i--)
#define p(s) cout << (s) << endl
#define DEBUG(x, y) cout << #x << ": " << x << " , " << #y << ": " << y << endl;
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf = 1e9;

int field[110][110];
int a[110];

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  REP(i, 0, N) { cin >> a[i]; }
  int nw = 0, nh = 0;
  int inc = 1;
  REP(i, 0, N) {
    while (a[i] > 0) {
      field[nh][nw] = i + 1;
      if ((0 < nw && inc == -1) || (inc == 1 && nw < W - 1)) {
        nw += inc;
      } else {
        nh++;
        inc = -inc;
      }
      a[i]--;
    }
  }
  REP(i, 0, H) {
    REP(j, 0, W) {
      cout << field[i][j];
      if (j < W - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n, N) for (int i = (n); i < (int)N; i++)
#define RREP(i, n, N) for (int i = N - 1; i >= (int)n; i--)
#define p(s) cout << (s) << endl
#define DEBUG(x, y) cout << #x << ": " << x << " , " << #y << ": " << y << endl;
#define CK(n, a, b) ((a) <= (n) && (n) < (b))
#define F first
#define S second
typedef long long ll;
using namespace std;
const int inf = 1e9;

int field[110][110];
int a[11000];

int main() {
  int H, W, N;
  cin >> H >> W >> N;
  REP(i, 0, N) { cin >> a[i]; }
  int nw = 0, nh = 0;
  int inc = 1;
  REP(i, 0, N) {
    while (a[i] > 0) {
      field[nh][nw] = i + 1;
      if ((0 < nw && inc == -1) || (inc == 1 && nw < W - 1)) {
        nw += inc;
      } else {
        nh++;
        inc = -inc;
      }
      a[i]--;
    }
  }
  REP(i, 0, H) {
    REP(j, 0, W) {
      cout << field[i][j];
      if (j < W - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p03640 (C++)

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

int h, w, n, mat[110][110], a[110];

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int i = 1, j = 1;
  for (int p = 1; p <= n; p++) {
    while (a[p]--) {
      // printf ("asdfsdf %d %d\n",i,j);
      mat[i][j] = p;
      if ((h == i) && (j % 2))
        j++;
      else if ((i == 1) && (!(j % 2)))
        j++;
      else if (j % 2)
        i++;
      else
        i--;
    }
  }
  // printf ("%d %d\n",h,w);
  for (int p = 1; p <= h; p++) {
    for (int q = 1; q <= w; q++)
      printf("%d ", mat[p][q]);

    printf("\n");
  }
  return 0;
}


Fixed submission:
#include <cstdio>

int h, w, n, mat[110][110], a[10010];

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int i = 1, j = 1;
  for (int p = 1; p <= n; p++) {
    while (a[p]--) {
      // printf ("asdfsdf %d %d\n",i,j);
      mat[i][j] = p;
      if ((h == i) && (j % 2))
        j++;
      else if ((i == 1) && (!(j % 2)))
        j++;
      else if (j % 2)
        i++;
      else
        i--;
    }
  }
  // printf ("%d %d\n",h,w);
  for (int p = 1; p <= h; p++) {
    for (int q = 1; q <= w; q++)
      printf("%d ", mat[p][q]);

    printf("\n");
  }
  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10;
int a[MAXN];
int col[MAXN][MAXN];
int main() {
  int H, W;
  int N;
  scanf("%d%d%d", &H, &W, &N);
  int now = 1;
  for (int i = 1; i <= N; i++)
    scanf("%d", a + i);
  for (int i = 1; i <= H; i++) {
    for (int j = (i % 2 == 1) ? 1 : W; (i % 2 == 1) ? j <= W : j >= 1;
         (i % 2 == 1) ? j++ : j--) {
      while (a[now] == 0)
        now++;
      col[i][j] = now;
      a[now]--;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      printf("%d", col[i][j]);
      if (j != W)
        printf(" ");
    }
    printf("\n");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10;
int a[MAXN * MAXN];
int col[MAXN][MAXN];
int main() {
  int H, W;
  int N;
  scanf("%d%d%d", &H, &W, &N);
  int now = 1;
  for (int i = 1; i <= N; i++)
    scanf("%d", a + i);
  for (int i = 1; i <= H; i++) {
    for (int j = (i % 2 == 1) ? 1 : W; (i % 2 == 1) ? j <= W : j >= 1;
         (i % 2 == 1) ? j++ : j--) {
      while (a[now] == 0)
        now++;
      col[i][j] = now;
      a[now]--;
    }
  }
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      printf("%d", col[i][j]);
      if (j != W)
        printf(" ");
    }
    printf("\n");
  }
  return 0;
}
Problem p03640 (C++)

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

int h, w;
int n;
int ma[110][110];
int a[1100];

int cmp(int i, int j) { return i > j; }

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  int i = 0, j = 0;
  int k = 1;
  int res = 1;
  while (k <= n) {
    while (a[k]) {
      if (j == w) {
        i++;
        j = w - 1;
        res = -1;
      } else if (j == -1) {
        i++;
        j = 0;
        res = 1;
      }
      ma[i][j] = k;
      j += res;
      a[k]--;
    }
    k++;
  }
  for (int p = 0; p < h; p++) {
    for (int q = 0; q < w; q++) {
      printf("%d", ma[p][q]);
      if (q == w - 1)
        printf("\n");
      else
        printf(" ");
    }
  }
}


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

int h, w;
int n;
int ma[110][110];
int a[11000];

int cmp(int i, int j) { return i > j; }

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  int i = 0, j = 0;
  int k = 1;
  int res = 1;
  while (k <= n) {
    while (a[k]) {
      if (j == w) {
        i++;
        j = w - 1;
        res = -1;
      } else if (j == -1) {
        i++;
        j = 0;
        res = 1;
      }
      ma[i][j] = k;
      j += res;
      a[k]--;
    }
    k++;
  }
  for (int p = 0; p < h; p++) {
    for (int q = 0; q < w; q++) {
      printf("%d", ma[p][q]);
      if (q == w - 1)
        printf("\n");
      else
        printf(" ");
    }
  }
}
Problem p03640 (C++)

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

int main() {
  int H, W, N;

  cin >> H >> W >> N;
  vector<long> a(N);

  for (int i = 0; i < N; i++)
    cin >> a[i];
  vector<vector<long>> ans(N, vector<long>(N));

  int nh = 0, nw = 0;
  for (int i = 0; i < N; i++) {
    int now = 0;
    while (now < a[i]) {
      if (nh % 2 == 0) {
        ans[nh][nw] = i + 1;
      } else {
        ans[nh][W - nw - 1] = i + 1;
      }
      nw++;
      if (nw >= W)
        nh++, nw = 0;
      now++;
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << ans[i][j] << " ";
    }
    cout << endl;
  }
}


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

int main() {
  int H, W, N;

  cin >> H >> W >> N;
  vector<long> a(N);

  for (int i = 0; i < N; i++)
    cin >> a[i];
  vector<vector<long>> ans(H, vector<long>(W));

  int nh = 0, nw = 0;
  for (int i = 0; i < N; i++) {
    int now = 0;
    while (now < a[i]) {
      if (nh % 2 == 0) {
        ans[nh][nw] = i + 1;
      } else {
        ans[nh][W - nw - 1] = i + 1;
      }
      nw++;
      if (nw >= W)
        nh++, nw = 0;
      now++;
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << ans[i][j] << " ";
    }
    cout << endl;
  }
}
Problem p03640 (C++)

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

using namespace std;
using vi = vector<int>;

int main(void) {
  int h, w, n;

  while (scanf("%d%d%d", &h, &w, &n) == 3) {
    vi a(n);
    vi f(n);

    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < a[i]; j++) {
        if (y & 1) {
          f[x--] = i + 1;
        } else {
          f[x++] = i + 1;
        }
        if (x < 0 || w <= x) {
          for (int k = 0; k + 1 < w; k++) {
            printf("%d ", f[k]);
          }
          printf("%d\n", f[w - 1]);

          if (x < 0)
            x = 0;
          if (x >= w)
            x = w - 1;
          y++;
        }
      }
    }
  }

  return 0;
}


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

using namespace std;
using vi = vector<int>;

int main(void) {
  int h, w, n;

  while (scanf("%d%d%d", &h, &w, &n) == 3) {
    vi a(n);
    vi f(w);

    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    int x = 0, y = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < a[i]; j++) {
        if (y & 1) {
          f[x--] = i + 1;
        } else {
          f[x++] = i + 1;
        }
        if (x < 0 || w <= x) {
          for (int k = 0; k + 1 < w; k++) {
            printf("%d ", f[k]);
          }
          printf("%d\n", f[w - 1]);

          if (x < 0)
            x = 0;
          if (x >= w)
            x = w - 1;
          y++;
        }
      }
    }
  }

  return 0;
}
Problem p03640 (C++)

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

int a[MAXN], arr[MAXN][MAXN], r = 1, c = 1, dir = 1;

int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    while (a[i]) {
      arr[r][c] = i + 1;
      a[i]--;
      if (dir == 1) {
        if (c == w) {
          r++;
          dir = -1;
        } else {
          c++;
        }
      } else {
        if (c == 1) {
          r++;
          dir = 1;
        } else {
          c--;
        }
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cout << arr[i][j] << " ";
    }
    cout << endl;
  }
}


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

int a[MAXN * 1000], arr[MAXN][MAXN], r = 1, c = 1, dir = 1;

int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    while (a[i]) {
      arr[r][c] = i + 1;
      a[i]--;
      if (dir == 1) {
        if (c == w) {
          r++;
          dir = -1;
        } else {
          c++;
        }
      } else {
        if (c == 1) {
          r++;
          dir = 1;
        } else {
          c--;
        }
      }
    }
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      cout << arr[i][j] << " ";
    }
    cout << endl;
  }
}
Problem p03640 (C++)

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

int a[12345];
int grid[123][123];

int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int c = 1;
  for (int i = 0; i < n; ++i) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; ++j) {
        grid[i][j] = c;
        --a[c];
        if (a[c] == 0) {
          ++c;
        }
      }
    } else {
      for (int j = w - 1; j >= 0; --j) {
        grid[i][j] = c;
        --a[c];
        if (a[c] == 0) {
          ++c;
        }
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << grid[i][j] << ' ';
    }
    cout << '\n';
  }
  return 0;
}


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

int a[12345];
int grid[123][123];

int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int c = 1;
  for (int i = 0; i < h; ++i) {
    if (i % 2 == 0) {
      for (int j = 0; j < w; ++j) {
        grid[i][j] = c;
        --a[c];
        if (a[c] == 0) {
          ++c;
        }
      }
    } else {
      for (int j = w - 1; j >= 0; --j) {
        grid[i][j] = c;
        --a[c];
        if (a[c] == 0) {
          ++c;
        }
      }
    }
  }
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      cout << grid[i][j] << ' ';
    }
    cout << '\n';
  }
  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e2 + 20;
int a[maxn][maxn];
int t[maxn], id[maxn];
bool cmp(int a, int b) { return t[a] < t[b]; }
void work() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> t[i];
    id[i] = i;
  }
  sort(id + 1, id + 1 + n, cmp);
  sort(t + 1, t + 1 + n);
  int r = 1, c = 1;
  int flag = 1;
  for (int i = n; i >= 1; --i) {
    while (t[i]--) {
      a[r][c] = id[i];
      c += flag;
      if (c > w) {
        r++;
        c = w;
        flag = -1;
      }
      if (c < 1) {
        r++;
        c = 1;
        flag = 1;
      }
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e2 + 20;
int a[maxn][maxn];
int t[maxn * maxn], id[maxn * maxn];
bool cmp(int a, int b) { return t[a] < t[b]; }
void work() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> t[i];
    id[i] = i;
  }
  sort(id + 1, id + 1 + n, cmp);
  sort(t + 1, t + 1 + n);
  int r = 1, c = 1;
  int flag = 1;
  for (int i = n; i >= 1; --i) {
    while (t[i]--) {
      a[r][c] = id[i];
      c += flag;
      if (c > w) {
        r++;
        c = w;
        flag = -1;
      }
      if (c < 1) {
        r++;
        c = 1;
        flag = 1;
      }
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      cout << a[i][j] << " ";
    }
    cout << endl;
  }
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}
Problem p03640 (C++)

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

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

  int h = 0, w = 0;
  int c[N][N];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < a[i]; j++) {
      if (h % 2 == 0) {
        c[h][w] = i + 1;
        w++;
      } else {
        c[h][w] = i + 1;
        w--;
      }
      if (w == -1) {
        w = 0;
        h++;
      } else if (w == W) {
        w = W - 1;
        h++;
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << c[i][j];
      if (j == W - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}

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

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

  int h = 0, w = 0;
  int c[H][W];
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < a[i]; j++) {
      if (h % 2 == 0) {
        c[h][w] = i + 1;
        w++;
      } else {
        c[h][w] = i + 1;
        w--;
      }
      if (w == -1) {
        w = 0;
        h++;
      } else if (w == W) {
        w = W - 1;
        h++;
      }
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cout << c[i][j];
      if (j == W - 1)
        cout << endl;
      else
        cout << " ";
    }
  }
}
Problem p03640 (C++)

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

using namespace std;
int arr[1005];
int res[1005][1005];
int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int c = 0;
  for (int i = 0; i < h; i++) {
    if (i % 2 == 0)
      for (int e = 0; e < w; e++) {
        if (arr[c] <= 0)
          c++;
        res[i][e] = c;
        arr[c]--;
      }
    else
      for (int e = w - 1; e >= 0; e--) {
        if (arr[c] <= 0)
          c++;
        res[i][e] = c;
        arr[c]--;
      }
  }

  for (int i = 0; i < h; i++) {
    for (int e = 0; e < w; e++) {
      cout << res[i][e] + 1 << " ";
    }
    cout << endl;
  }
  return 0;
}


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

using namespace std;
int arr[10005];
int res[1005][1005];
int main() {
  int h, w;
  cin >> h >> w;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  int c = 0;
  for (int i = 0; i < h; i++) {
    if (i % 2 == 0)
      for (int e = 0; e < w; e++) {
        if (arr[c] <= 0)
          c++;
        res[i][e] = c;
        arr[c]--;
      }
    else
      for (int e = w - 1; e >= 0; e--) {
        if (arr[c] <= 0)
          c++;
        res[i][e] = c;
        arr[c]--;
      }
  }

  for (int i = 0; i < h; i++) {
    for (int e = 0; e < w; e++) {
      cout << res[i][e] + 1 << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p03640 (C++)

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

using namespace std;

#define MOD 1000000007
#define ll long long int
#define ld long double
#define pb push_back
#define mkp make_pair
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define fi first
#define sc second
#define deb 0

int A[1001][1001], B[1001];

int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);

  int n, m, i, j, k, x, y, z;

  cin >> n >> m >> k;
  for (i = 1; i <= k; ++i)
    cin >> B[i];

  int dir = 0;
  x = 1;
  y = 1;

  i = 0;
  z = 0;
  for (i = 1; i <= k; ++i) {
    z = B[i];
    if (z == 0)
      continue;
    while (z--) {
      A[x][y] = i;
      if (dir == 0) {
        y += 1;
      } else {
        y -= 1;
      }
      if (y == m + 1) {
        y = m;
        x += 1;
        dir = 1;
      }
      if (y == 0) {
        y = 1;
        x += 1;
        dir = 0;
      }
    }
  }

  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= m; ++j) {
      cout << A[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}

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

using namespace std;

#define MOD 1000000007
#define ll long long int
#define ld long double
#define pb push_back
#define mkp make_pair
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%lld", &x)
#define fi first
#define sc second
#define deb 0

int A[1001][1001], B[100001];

int main() {
  ios_base::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);

  int n, m, i, j, k, x, y, z;

  cin >> n >> m >> k;
  for (i = 1; i <= k; ++i)
    cin >> B[i];

  int dir = 0;
  x = 1;
  y = 1;

  i = 0;
  z = 0;
  for (i = 1; i <= k; ++i) {
    z = B[i];
    if (z == 0)
      continue;
    while (z--) {
      A[x][y] = i;
      if (dir == 0) {
        y += 1;
      } else {
        y -= 1;
      }
      if (y == m + 1) {
        y = m;
        x += 1;
        dir = 1;
      }
      if (y == 0) {
        y = 1;
        x += 1;
        dir = 0;
      }
    }
  }

  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= m; ++j) {
      cout << A[i][j] << " ";
    }
    cout << endl;
  }

  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int A[20003];
int B[10003][10003];

int main() {
  int n, m;
  int colornum;
  cin >> n >> m;
  cin >> colornum;
  int e;
  int i = 0;
  memset(A, 0, sizeof(A));
  memset(B, 0, sizeof(B));
  int c = 0;
  for (int ii = 1; ii <= colornum; ii++) {
    cin >> A[ii];
  }
  int d = 0;
  // cout<<A[2]<<"WWWWWWWWWWWWWW";
  int iv = 0;
  int ji = 1;
  int o = 0;
  for (int i = 1; i <= colornum; i++) {
    int h = A[i];
    while (h) {
      if (ji)
        for (; iv < m;) {
          B[d][iv] = i;
          h--;
          if (iv == m - 1) {
            d++;
            ji = 0;
            o = 1;
          } else
            iv++;
          if (o)
            break;
          if (h == 0)
            break;
        }
      if (h == 0)
        break;
      if (o) { // cout<<d<<' '<<iv<<"WWWWWWWWWWWWW"<<endl;
        for (; iv >= 0;) {
          B[d][iv] = i;
          h--;
          if (iv == 0) {
            d++;
            ji = 1;
            o = 0;
          } else
            iv--;
          if (ji)
            break;
          if (h == 0)
            break;
        }
      }
      if (h == 0)
        break;
    }
  }
  // cout<<B[1][0]<<"AAAAAAAAA";
  for (int i = 0; i < n; i++)
    for (int s = 0; s < m; s++) {
      cout << B[i][s];
      if (s == m - 1)
        cout << endl;
      else
        cout << ' ';
    }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
int A[10003];
int B[103][103];

int main() {
  int n, m;
  int colornum;
  cin >> n >> m;
  cin >> colornum;
  int e;
  int i = 0;
  memset(A, 0, sizeof(A));
  memset(B, 0, sizeof(B));
  int c = 0;
  for (int ii = 1; ii <= colornum; ii++) {
    cin >> A[ii];
  }
  int d = 0;
  // cout<<A[2]<<"WWWWWWWWWWWWWW";
  int iv = 0;
  int ji = 1;
  int o = 0;
  for (int i = 1; i <= colornum; i++) {
    int h = A[i];
    while (h) {
      if (ji)
        for (; iv < m;) {
          B[d][iv] = i;
          h--;
          if (iv == m - 1) {
            d++;
            ji = 0;
            o = 1;
          } else
            iv++;
          if (o)
            break;
          if (h == 0)
            break;
        }
      if (h == 0)
        break;
      if (o) { // cout<<d<<' '<<iv<<"WWWWWWWWWWWWW"<<endl;
        for (; iv >= 0;) {
          B[d][iv] = i;
          h--;
          if (iv == 0) {
            d++;
            ji = 1;
            o = 0;
          } else
            iv--;
          if (ji)
            break;
          if (h == 0)
            break;
        }
      }
      if (h == 0)
        break;
    }
  }
  // cout<<B[1][0]<<"AAAAAAAAA";
  for (int i = 0; i < n; i++)
    for (int s = 0; s < m; s++) {
      cout << B[i][s];
      if (s == m - 1)
        cout << endl;
      else
        cout << ' ';
    }
  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
const int N = 105;
int h, w, n, a[N], pos, now, ans[N][N];
int main() {
  //	freopen("ex.in","r",stdin);
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  pos = 1;
  now = 0;
  for (int i = 1; i <= h; ++i) {
    if (now == 0)
      for (int j = 1; j <= w; ++j) {
        while (a[pos] == 0)
          pos++;
        a[pos]--;
        ans[i][j] = pos;
      }
    else
      for (int j = w; j; --j) {
        while (a[pos] == 0)
          pos++;
        a[pos]--;
        ans[i][j] = pos;
      }
    now ^= 1;
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j)
      printf("%d ", ans[i][j]);
    putchar('\n');
  }
  return 0;
}

Fixed submission:
#include <cstdio>
const int N = 105;
int h, w, n, a[N * N], pos, now, ans[N][N];
int main() {
  //	freopen("ex.in","r",stdin);
  scanf("%d%d%d", &h, &w, &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  pos = 1;
  now = 0;
  for (int i = 1; i <= h; ++i) {
    if (now == 0)
      for (int j = 1; j <= w; ++j) {
        while (a[pos] == 0)
          pos++;
        a[pos]--;
        ans[i][j] = pos;
      }
    else
      for (int j = w; j; --j) {
        while (a[pos] == 0)
          pos++;
        a[pos]--;
        ans[i][j] = pos;
      }
    now ^= 1;
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j)
      printf("%d ", ans[i][j]);
    putchar('\n');
  }
  return 0;
}
Problem p03640 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int main() {
  int h, w, n, A[200000], b = 0;
  cin >> h >> w >> n;
  rep(i, n) cin >> A[i];
  vector<int> B;
  rep(i, n) rep(j, A[i]) B.push_back(i + 1);
  rep(i, n / 2)
      reverse(B.begin() + (2 * i + 1) * w, B.begin() + 2 * (i + 1) * w);
  rep(i, h) {
    rep(j, w) cout << B[b] << " ", b++;
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
int main() {
  int h, w, n, A[200000], b = 0;
  cin >> h >> w >> n;
  rep(i, n) cin >> A[i];
  vector<int> B;
  rep(i, n) rep(j, A[i]) B.push_back(i + 1);
  rep(i, h / 2)
      reverse(B.begin() + (2 * i + 1) * w, B.begin() + 2 * (i + 1) * w);
  rep(i, h) {
    rep(j, w) cout << B[b] << " ", b++;
    cout << endl;
  }
}
Problem p03641 (C++)

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

typedef long long ll;

ll N;
const ll L = 3; // 20;
typedef pair<ll, ll> Data;
vector<vector<Data>> Min;
typedef tuple<Data, ll, ll> Entry;
priority_queue<Entry, vector<Entry>, greater<Entry>> Que;

Data min_range(ll i, ll j) {
  assert(i < j);
  assert((j - i) % 2 == 0);
  ll l = 0;
  while ((1 << l) * 2 * 2 < j - i)
    ++l;
  ll j2 = j - (1 << l) * 2;
  assert(i <= j2);
  return min(Min[l][i], Min[l][j2]);
}
void push(ll i, ll j) {
  if (i < j)
    Que.push(Entry(min_range(i, j), i, j));
}
int main() {
  cin >> N;
  Min.resize(L);
  for (auto &min : Min)
    min.resize(N + 2);
  for (ll i = 0; i < N; ++i) {
    ll p;
    cin >> p;
    Min[0][i] = Data(p, i);
  }
  Min[0][N] = Min[0][N + 1] = Data(1ll << 60, -1);
  for (ll l = 1; l < L; ++l)
    for (ll i = 0; i < N + 2; ++i) {
      Min[l][i] = min(Min[l - 1][i],
                      Min[l - 1][i + min(1ll << l, (N + 1 - i) / 2 * 2)]);
    }
  push(0, N);
  bool first = true;
  while (!Que.empty()) {
    Data pk;
    ll i, j, p, k;
    tie(pk, i, j) = Que.top();
    tie(p, k) = pk;
    Que.pop();
    ll q, l;
    tie(q, l) = min_range(k + 1, j + 1);
    cout << (first ? "" : " ") << p << " " << q;
    first = false;
    push(k + 1, l);
    push(i, k);
    push(l + 1, j);
  }
  cout << endl;
  return 0;
}


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

typedef long long ll;

ll N;
const ll L = 20;
typedef pair<ll, ll> Data;
vector<vector<Data>> Min;
typedef tuple<Data, ll, ll> Entry;
priority_queue<Entry, vector<Entry>, greater<Entry>> Que;

Data min_range(ll i, ll j) {
  assert(i < j);
  assert((j - i) % 2 == 0);
  ll l = 0;
  while ((1 << l) * 2 * 2 < j - i)
    ++l;
  ll j2 = j - (1 << l) * 2;
  assert(i <= j2);
  return min(Min[l][i], Min[l][j2]);
}
void push(ll i, ll j) {
  if (i < j)
    Que.push(Entry(min_range(i, j), i, j));
}
int main() {
  cin >> N;
  Min.resize(L);
  for (auto &min : Min)
    min.resize(N + 2);
  for (ll i = 0; i < N; ++i) {
    ll p;
    cin >> p;
    Min[0][i] = Data(p, i);
  }
  Min[0][N] = Min[0][N + 1] = Data(1ll << 60, -1);
  for (ll l = 1; l < L; ++l)
    for (ll i = 0; i < N + 2; ++i) {
      Min[l][i] = min(Min[l - 1][i],
                      Min[l - 1][i + min(1ll << l, (N + 1 - i) / 2 * 2)]);
    }
  push(0, N);
  bool first = true;
  while (!Que.empty()) {
    Data pk;
    ll i, j, p, k;
    tie(pk, i, j) = Que.top();
    tie(p, k) = pk;
    Que.pop();
    ll q, l;
    tie(q, l) = min_range(k + 1, j + 1);
    cout << (first ? "" : " ") << p << " " << q;
    first = false;
    push(k + 1, l);
    push(i, k);
    push(l + 1, j);
  }
  cout << endl;
  return 0;
}
Problem p03641 (C++)

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

using namespace std;
const int N = 1e5, INF = 0x3f3f3f3f3f;

int a[2][N + 5], pos[N + 5], d[2][N][32];

void RMQ_init(int n, int v) {
  for (int i = 1; i <= n; i++)
    d[v][i][0] = a[v][i];
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      d[v][i][j] = min(d[v][i][j - 1], d[v][i + (1 << (j - 1))][j - 1]);
}

int RMQ(int a, int b, int v) {
  int k = 0;
  while ((1 << (k + 1)) <= b - a + 1)
    k++;
  return min(d[v][a][k], d[v][b - (1 << k) + 1][k]);
}

struct node {
  int x, L, R;
  bool operator<(const node &A) const {
    return x > A.x; // 注意是 > 号
  }
};

int main() {
  int n, x;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    a[i & 1][i] = x;
    a[(i & 1) ^ 1][i] = INF;
    pos[x] = i;
  }
  RMQ_init(n, 0);
  RMQ_init(n, 1);
  priority_queue<node> Q;
  x = RMQ(1, n, 1);
  Q.push({x, 1, n});
  while (!Q.empty()) {
    node x = Q.top();
    Q.pop();
    int v1 = x.x, v2 = RMQ(pos[x.x] + 1, x.R, (pos[x.x] & 1) ^ 1);
    printf("%d %d ", v1, v2);
    v1 = pos[v1], v2 = pos[v2];
    if (x.L != v1)
      Q.push({RMQ(x.L, v1 - 1, x.L & 1), x.L, v1 - 1});
    if (v1 + 1 != v2)
      Q.push({RMQ(v1 + 1, v2 - 1, (v1 + 1) & 1), v1 + 1, v2 - 1});
    if (v2 != x.R)
      Q.push({RMQ(v2 + 1, x.R, (v2 + 1) & 1), v2 + 1, x.R});
  }
  printf("\n");
  return 0;
}

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

using namespace std;
const int N = 2e5, INF = 0x3f3f3f3f3f;

int a[2][N + 5], pos[N + 5], d[2][N][32];

void RMQ_init(int n, int v) {
  for (int i = 1; i <= n; i++)
    d[v][i][0] = a[v][i];
  for (int j = 1; (1 << j) <= n; j++)
    for (int i = 1; i + (1 << j) - 1 <= n; i++)
      d[v][i][j] = min(d[v][i][j - 1], d[v][i + (1 << (j - 1))][j - 1]);
}

int RMQ(int a, int b, int v) {
  int k = 0;
  while ((1 << (k + 1)) <= b - a + 1)
    k++;
  return min(d[v][a][k], d[v][b - (1 << k) + 1][k]);
}

struct node {
  int x, L, R;
  bool operator<(const node &A) const {
    return x > A.x; // 注意是 > 号
  }
};

int main() {
  int n, x;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    a[i & 1][i] = x;
    a[(i & 1) ^ 1][i] = INF;
    pos[x] = i;
  }
  RMQ_init(n, 0);
  RMQ_init(n, 1);
  priority_queue<node> Q;
  x = RMQ(1, n, 1);
  Q.push({x, 1, n});
  while (!Q.empty()) {
    node x = Q.top();
    Q.pop();
    int v1 = x.x, v2 = RMQ(pos[x.x] + 1, x.R, (pos[x.x] & 1) ^ 1);
    printf("%d %d ", v1, v2);
    v1 = pos[v1], v2 = pos[v2];
    if (x.L != v1)
      Q.push({RMQ(x.L, v1 - 1, x.L & 1), x.L, v1 - 1});
    if (v1 + 1 != v2)
      Q.push({RMQ(v1 + 1, v2 - 1, (v1 + 1) & 1), v1 + 1, v2 - 1});
    if (v2 != x.R)
      Q.push({RMQ(v2 + 1, x.R, (v2 + 1) & 1), v2 + 1, x.R});
  }
  printf("\n");
  return 0;
}
Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, a[200005], w[2][19][200005];
struct O {
  int x, y;
};
vector<O> f;
vector<int> e[100005];
struct cmp {
  bool operator()(int a, int b) { return f[a].x > f[b].x; }
};
priority_queue<int, vector<int>, cmp> d;
int Min(int x, int y) { return a[x] < a[y] ? x : y; }
int get(int o, int l, int r) {
  int i = log2(r - l + 1);
  return Min(w[o][i][r], w[o][i][l - 1 + (1 << i)]);
}
int dfs(int o, int l, int r) {
  if (l > r)
    return 0;
  int p = get(o, l, r), q = get(o ^ 1, p + 1, r), x = f.size(), y;
  f.push_back({a[p], a[q]});
  y = dfs(o, l, p - 1);
  if (y)
    e[x].push_back(y);
  y = dfs(o ^ 1, p + 1, q - 1);
  if (y)
    e[x].push_back(y);
  y = dfs(o, q + 1, r);
  if (y)
    e[x].push_back(y);
  return x;
}
int main() {
  scanf("%d", &n);
  a[0] = n + 3;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i)
    w[0][0][i] = i & 1 ? 0 : i, w[1][0][i] = i & 1 ? i : 0;
  for (int k = 0; k < 2; ++k)
    for (int i = 1; i < 17; ++i)
      for (int j = 1 << i; j <= n; ++j)
        w[k][i][j] = Min(w[k][i - 1][j - (1 << i - 1)], w[k][i - 1][j]);
  d.push(dfs(1, 1, n));
  while (!d.empty()) {
    n = d.top();
    d.pop();
    printf("%d %d ", f[n].x, f[n].y);
    for (int i = 0; i < e[n].size(); ++i)
      d.push(e[n][i]);
  }
  puts("");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, a[200005], w[2][19][200005];
struct O {
  int x, y;
};
vector<O> f;
vector<int> e[100005];
struct cmp {
  bool operator()(int a, int b) { return f[a].x > f[b].x; }
};
priority_queue<int, vector<int>, cmp> d;
int Min(int x, int y) { return a[x] < a[y] ? x : y; }
int get(int o, int l, int r) {
  int i = log2(r - l + 1);
  return Min(w[o][i][r], w[o][i][l - 1 + (1 << i)]);
}
int dfs(int o, int l, int r) {
  if (l > r)
    return 0;
  int p = get(o, l, r), q = get(o ^ 1, p + 1, r), x = f.size(), y;
  f.push_back({a[p], a[q]});
  y = dfs(o, l, p - 1);
  if (y)
    e[x].push_back(y);
  y = dfs(o ^ 1, p + 1, q - 1);
  if (y)
    e[x].push_back(y);
  y = dfs(o, q + 1, r);
  if (y)
    e[x].push_back(y);
  return x;
}
int main() {
  scanf("%d", &n);
  a[0] = n + 3;
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);
  for (int i = 1; i <= n; ++i)
    w[0][0][i] = i & 1 ? 0 : i, w[1][0][i] = i & 1 ? i : 0;
  for (int k = 0; k < 2; ++k)
    for (int i = 1; i < 18; ++i)
      for (int j = 1 << i; j <= n; ++j)
        w[k][i][j] = Min(w[k][i - 1][j - (1 << i - 1)], w[k][i - 1][j]);
  d.push(dfs(1, 1, n));
  while (!d.empty()) {
    n = d.top();
    d.pop();
    printf("%d %d ", f[n].x, f[n].y);
    for (int i = 0; i < e[n].size(); ++i)
      d.push(e[n][i]);
  }
  puts("");
  return 0;
}
Problem p03641 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <queue>
using namespace std;
inline int read() {
  int x;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    ;
  for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';)
    x = x * 10 + c - '0';
  return x;
}
#define N 262144
#define INF 0x3FFFFFFF
#define mp make_pair
#define tp pq.top()
#define F first
#define S second
int t[N * 2 + 5], t1[N * 2 + 5], t2[N * 2 + 5], p[N];
priority_queue<pair<pair<int, int>, pair<int, int>>> pq;
void build(int *t) {
  for (int i = N; --i;)
    t[i] = min(t[i << 1], t[i << 1 | 1]);
}
int query(int *t, int l, int r) {
  int res = INF;
  for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (~l & 1)
      res = min(res, t[l + 1]);
    if (r & 1)
      res = min(res, t[r - 1]);
  }
  return res;
}
void push(int l, int r) {
  if (l > r)
    return;
  int x = query(l & 1 ? t1 : t2, l, r);
  pq.push(mp(mp(-x, query(1 & 1 ? t2 : t1, p[x] + 1, r)), mp(l, r)));
}
int main() {
  int n = read(), i, x, y, l, r;
  for (i = 1; i <= n; ++i)
    p[x = read()] = i, t[i + N] = x, t1[i + N] = i & 1 ? x : INF,
          t2[i + N] = i & 1 ? INF : x;
  build(t);
  build(t1);
  build(t2);
  for (push(1, n); !pq.empty();) {
    x = -tp.F.F;
    y = tp.F.S;
    l = tp.S.F;
    r = tp.S.S;
    printf("%d %d ", x, y);
    pq.pop();
    push(l, p[x] - 1);
    push(p[x] + 1, p[y] - 1);
    push(p[y] + 1, r);
  }
}

Fixed submission:
#include <cstdio>
#include <queue>
using namespace std;
inline int read() {
  int x;
  char c;
  while ((c = getchar()) < '0' || c > '9')
    ;
  for (x = c - '0'; (c = getchar()) >= '0' && c <= '9';)
    x = x * 10 + c - '0';
  return x;
}
#define N 262144
#define INF 0x3FFFFFFF
#define mp make_pair
#define tp pq.top()
#define F first
#define S second
int t[N * 2 + 5], t1[N * 2 + 5], t2[N * 2 + 5], p[N];
priority_queue<pair<pair<int, int>, pair<int, int>>> pq;
void build(int *t) {
  for (int i = N; --i;)
    t[i] = min(t[i << 1], t[i << 1 | 1]);
}
int query(int *t, int l, int r) {
  int res = INF;
  for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
    if (~l & 1)
      res = min(res, t[l + 1]);
    if (r & 1)
      res = min(res, t[r - 1]);
  }
  return res;
}
void push(int l, int r) {
  if (l > r)
    return;
  int x = query(l & 1 ? t1 : t2, l, r);
  pq.push(mp(mp(-x, query(l & 1 ? t2 : t1, p[x] + 1, r)), mp(l, r)));
}
int main() {
  int n = read(), i, x, y, l, r;
  for (i = 1; i <= n; ++i)
    p[x = read()] = i, t[i + N] = x, t1[i + N] = i & 1 ? x : INF,
          t2[i + N] = i & 1 ? INF : x;
  build(t);
  build(t1);
  build(t2);
  for (push(1, n); !pq.empty();) {
    x = -tp.F.F;
    y = tp.F.S;
    l = tp.S.F;
    r = tp.S.S;
    printf("%d %d ", x, y);
    pq.pop();
    push(l, p[x] - 1);
    push(p[x] + 1, p[y] - 1);
    push(p[y] + 1, r);
  }
}
Problem p03642 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
int a[205], fst[205], match[205], p[1000000], tot = 0;
bool u[10000005], vis[205];
struct Edge {
  int to, nxt;
} e[10005];
void addedge(int x, int y) {
  e[++tot].to = y;
  e[tot].nxt = fst[x];
  fst[x] = tot;
}
void init(int n) {
  for (int i = 2; i <= n; i++) {
    if (!u[i])
      p[++p[0]] = i;
    for (int j = 1; j <= p[0] && i * p[j] <= n; j++) {
      u[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
  u[0] = u[1] = u[2] = 1;
}
bool dfs(int x) {
  vis[x] = 1;
  for (int i = fst[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    if (!match[y] || dfs(match[y])) {
      match[y] = x;
      return 1;
    }
  }
  return 0;
}
int main() {
  int n, m = 0;
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (a[m] == x)
      m--;
    else
      a[++m] = x;
    a[++m] = x + 1;
  }
  init(a[m] - a[1]);
  for (int i = 1; i <= m; i++)
    if (a[i] & 1)
      for (int j = 1; j <= m; j++)
        if (!u[abs(a[i] - a[j])])
          addedge(i, j);
  int ans = 0, cnt = 0;
  for (int i = 1; i <= m; i++)
    if (a[i] & 1) {
      memset(vis, 0, sizeof vis);
      if (dfs(i))
        ans++;
      else
        cnt++;
    }
  printf("%d\n", m - ans + (cnt & 1));
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
int a[205], fst[205], match[205], p[1000000], tot = 0;
bool u[10000005], vis[205];
struct Edge {
  int to, nxt;
} e[10005];
void addedge(int x, int y) {
  e[++tot].to = y;
  e[tot].nxt = fst[x];
  fst[x] = tot;
}
void init(int n) {
  for (int i = 2; i <= n; i++) {
    if (!u[i])
      p[++p[0]] = i;
    for (int j = 1; j <= p[0] && i * p[j] <= n; j++) {
      u[i * p[j]] = 1;
      if (i % p[j] == 0)
        break;
    }
  }
  u[0] = u[1] = u[2] = 1;
}
bool dfs(int x) {
  if (vis[x])
    return 0;
  vis[x] = 1;
  for (int i = fst[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    if (!match[y] || dfs(match[y])) {
      match[y] = x;
      return 1;
    }
  }
  return 0;
}
int main() {
  int n, m = 0;
  scanf("%d\n", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (a[m] == x)
      m--;
    else
      a[++m] = x;
    a[++m] = x + 1;
  }
  init(a[m] - a[1]);
  for (int i = 1; i <= m; i++)
    if (a[i] & 1)
      for (int j = 1; j <= m; j++)
        if (!u[abs(a[i] - a[j])])
          addedge(i, j);
  int ans = 0, cnt = 0;
  for (int i = 1; i <= m; i++)
    if (a[i] & 1) {
      memset(vis, 0, sizeof vis);
      if (dfs(i))
        ans++;
      else
        cnt++;
    }
  printf("%d\n", m - ans + (cnt & 1));
}