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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v[n];
  vector<vector<int>> dist(n, vector<int>(n, inf));

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].emplace_back(y);
  }

  rep(i, n) {
    dist[i][i] = 0;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      for (auto to : v[p]) {
        if (dist[i][to] > dist[i][p] + 1) {
          dist[i][to] = dist[i][p] + 1;
          q.push(to);
        }
      }
    }
  }

  int st = -1, gl = -1, c = inf;
  rep(i, n) rep(j, i) {
    if (dist[i][j] + dist[j][i] < c && dist[j][i] == 1) {
      st = i;
      gl = j;
      c = dist[i][j] + dist[j][i];
    }
  }
  if (c == inf) {
    cout << -1 << ln;
    return 0;
  }
  int pos = st;
  vector<int> a;
  a.emplace_back(pos);
  while (pos != gl) {
    for (auto to : v[pos]) {
      if (dist[pos][gl] == dist[to][gl] - 1 &&
          dist[st][pos] + 1 == dist[st][to]) {
        pos = to;
        a.emplace_back(pos);
        break;
      }
    }
  }

  cout << a.size() << ln;
  for (auto ans : a) {
    cout << ans + 1 << ln;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v[n];
  vector<vector<int>> dist(n, vector<int>(n, inf));

  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].emplace_back(y);
  }

  rep(i, n) {
    dist[i][i] = 0;
    queue<int> q;
    q.push(i);
    while (!q.empty()) {
      auto p = q.front();
      q.pop();
      for (auto to : v[p]) {
        if (dist[i][to] > dist[i][p] + 1) {
          dist[i][to] = dist[i][p] + 1;
          q.push(to);
        }
      }
    }
  }

  int st = -1, gl = -1, c = inf;
  rep(i, n) rep(j, i) {
    if (dist[i][j] + dist[j][i] < c && dist[j][i] == 1) {
      st = i;
      gl = j;
      c = dist[i][j] + dist[j][i];
    }
  }
  if (c == inf) {
    cout << -1 << ln;
    return 0;
  }
  int pos = st;
  vector<int> a;
  a.emplace_back(pos);
  while (pos != gl) {
    for (auto to : v[pos]) {
      if (dist[pos][gl] - 1 == dist[to][gl] &&
          dist[st][pos] + 1 == dist[st][to]) {
        pos = to;
        a.emplace_back(pos);
        break;
      }
    }
  }

  cout << a.size() << ln;
  for (auto ans : a) {
    cout << ans + 1 << ln;
  }
  return 0;
}
Problem p02902 (C++)

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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

#define INF 1e12

ll N, M;
vector<ll> abv[1010];
ll i, j, k;
vector<ll> ans;
ll ansck;
ll mk[1010], mk2[1010];

bool dfs(ll a, ll p) {
  mk2[a] = 1;
  ll i = 0, f = 0;
  fornum(i, 0, abv[a].size()) {
    ll b = abv[a][i];
    if (b < p)
      continue;
    if (b == p) {
      f++;
    } else if (mk2[b]) {
      f += 99999;
    }
    mk[b]++;
  }
  // printf("<%lld>", f);
  if (f == 1) {
    ans.push_back(a);
    return true;
  } else if (f == 0) {
    fornum(i, 0, abv[a].size()) {
      ll b = abv[a][i];
      if (b < p)
        continue;
      if (mk[b] < 2 && dfs(b, p)) {
        ans.push_back(a);
        return true;
      }
    }
  }
  fornum(i, 0, abv[a].size()) {
    ll b = abv[a][i];
    mk[b]--;
  }
  mk2[a] = 0;
  // printf("\n");
  return false;
}

int main() {
  scanf("%lld%lld", &N, &M);
  fornum(i, 0, M) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    abv[a].push_back(b);
  }
  fornum(i, 0, N) {
    fornum(j, 0, N) { mk[j + 1] = 0; }
    mk[i + 1] = 1;
    // printf("%lld\n", i + 1);
    if (dfs(i + 1, i + 1)) {
      printf("%d\n", ans.size());
      fornum(i, 0, ans.size()) { printf("%lld\n", ans[i]); }
      return 0;
    }
  }
  printf("-1");
  return 0;
}


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

#define ll long long
#define fornum(A, B, C) for (A = B; A < C; A++)
#define mp make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

/////////////////////////////////////////////////////

#define INF 1e12

ll N, M;
vector<ll> abv[1010];
ll i, j, k;
vector<ll> ans;
ll ansck;
ll mk[1010], mk2[1010];

bool dfs(ll a, ll p) {
  mk2[a] = 1;
  ll i = 0, f = 0;
  fornum(i, 0, abv[a].size()) {
    ll b = abv[a][i];
    if (b < p)
      continue;
    if (b == p) {
      f++;
    } else if (mk2[b]) {
      f += 99999;
    }
    mk[b]++;
  }
  // printf("<%lld>", f);
  if (f == 1) {
    ans.push_back(a);
    return true;
  } else if (f == 0) {
    fornum(i, 0, abv[a].size()) {
      ll b = abv[a][i];
      if (b < p)
        continue;
      if (mk[b] < 2 && dfs(b, p)) {
        ans.push_back(a);
        return true;
      }
    }
  }
  fornum(i, 0, abv[a].size()) {
    ll b = abv[a][i];
    if (b < p)
      mk[b]--;
  }
  mk2[a] = 0;
  // printf("\n");
  return false;
}

int main() {
  scanf("%lld%lld", &N, &M);
  fornum(i, 0, M) {
    ll a, b;
    scanf("%lld%lld", &a, &b);
    abv[a].push_back(b);
  }
  fornum(i, 0, N) {
    fornum(j, 0, N) { mk[j + 1] = 0; }
    mk[i + 1] = 1;
    // printf("%lld\n", i + 1);
    if (dfs(i + 1, i + 1)) {
      printf("%d\n", ans.size());
      fornum(i, 0, ans.size()) { printf("%lld\n", ans[i]); }
      return 0;
    }
  }
  printf("-1");
  return 0;
}
Problem p02903 (C++)

Buggy submission (status: Runtime Error):
#include <bits/extc++.h>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 3e5 + 7;
const int maxm = 1e7 + 7;
const int ALP = 26;
const long long mod = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;

int H, W, A, B;

int cnt[2];
int ans[107][107];

bool check() {
  for (int i = 1; i <= H; i++) {
    memset(cnt, 0, sizeof(cnt));
    for (int j = 1; j <= W; j++)
      cnt[ans[i][j]]++;
    if (min(cnt[0], cnt[1]) != A) {
      return false;
    }
  }
  for (int j = 1; j <= W; j++) {
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= H; i++)
      cnt[ans[i][j]]++;
    if (min(cnt[0], cnt[1]) != B)
      return false;
  }
  return true;
}

int main() {
  // freopen("1.in","r",stdin);
  scanf("%d%d%d%d", &H, &W, &A, &B);

  for (int i = 1; i <= B; i++) {
    for (int j = 1; j <= W; j++)
      ans[i][j] = (j > A);
  }
  for (int i = B + 1; i <= H; i++) {
    for (int j = 1; j <= W; j++)
      ans[i][j] = (j <= A);
  }
  if (check()) {
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++)
        printf("%d", ans[i][j]);
      printf("\n");
    }
  } else {
    printf("-1\n");
  }

  return 0;
}


Fixed submission:
#include <bits/extc++.h>
#include <bits/stdc++.h>
using namespace __gnu_pbds;
using namespace std;
const long long llINF = 9223372036854775807;
const int INF = 2147483647;
const int maxn = 3e5 + 7;
const int maxm = 1e7 + 7;
const int ALP = 26;
const long long mod = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;

int H, W, A, B;

int cnt[2];
int ans[1007][1007];

bool check() {
  for (int i = 1; i <= H; i++) {
    memset(cnt, 0, sizeof(cnt));
    for (int j = 1; j <= W; j++)
      cnt[ans[i][j]]++;
    if (min(cnt[0], cnt[1]) != A) {
      return false;
    }
  }
  for (int j = 1; j <= W; j++) {
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= H; i++)
      cnt[ans[i][j]]++;
    if (min(cnt[0], cnt[1]) != B)
      return false;
  }
  return true;
}

int main() {
  // freopen("1.in","r",stdin);
  scanf("%d%d%d%d", &H, &W, &A, &B);

  for (int i = 1; i <= B; i++) {
    for (int j = 1; j <= W; j++)
      ans[i][j] = (j > A);
  }
  for (int i = B + 1; i <= H; i++) {
    for (int j = 1; j <= W; j++)
      ans[i][j] = (j <= A);
  }
  if (check()) {
    for (int i = 1; i <= H; i++) {
      for (int j = 1; j <= W; j++)
        printf("%d", ans[i][j]);
      printf("\n");
    }
  } else {
    printf("-1\n");
  }

  return 0;
}
Problem p02903 (Python)

Buggy submission (status: Runtime Error):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

h, w, a, b = map(int, readline().split())
x_cnt = [0] * h
y_cnt = [0] * w
ans = [""] * h
if h == 1:
    print("1" * a + "0" * (w - a))
elif w == 1:
    for i in range(b):
        print("1")
    for i in range(h - b):
        print("0")
else:
    for i in range(h):
        for j in range(w):
            print(ans)
            if y_cnt[j] < b and x_cnt[i] < a:
                y_cnt[j] += 1
                x_cnt[i] += 1
                ans[i] += "1"
            else:
                ans[i] += "0"
        if a != 0:
            if x_cnt[i] != a:
                print("No")
                exit()
    if b != 0:
        for i in range(w):
            if y_cnt[i] != b:
                print("No")
                exit()
    for i in range(h):
        print(ans[i])


Fixed submission:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)

h, w, a, b = map(int, readline().split())
ans_1 = "1" * a + "0" * (w - a)
ans_2 = "0" * a + "1" * (w - a)
for i in range(b):
    print(ans_1)
for i in range(h - b):
    print(ans_2)
Problem p02903 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;
ll solver(ll h, ll w, ll a, ll b) {
  rep(i, 0, b) {
    rep(j, 0, a) cout << '0';
    rep(j, a, w) cout << '1';
    cout << '\n';
  }
  rep(i, b, h) {
    rep(j, 0, a) cout << '1';
    rep(j, a, w) cout << '0';
    cout << '\n';
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  solver(h, w, a, b);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;
void solver(ll h, ll w, ll a, ll b) {
  rep(i, 0, b) {
    rep(j, 0, a) cout << '0';
    rep(j, a, w) cout << '1';
    cout << '\n';
  }
  rep(i, b, h) {
    rep(j, 0, a) cout << '1';
    rep(j, a, w) cout << '0';
    cout << '\n';
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  solver(h, w, a, b);
}
Problem p02903 (C++)

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

int main() {
  int H, W, A, B;
  cin >> H >> W >> A >> B;
  vector<vector<int>> ans(H, vector<int>(W));
  for (int i = 0; i < A; i++) {
    for (int j = 0; j < B; j++)
      ans[j][i] = 1;
  }
  for (int i = A; i < H; i++) {
    for (int j = B; j < W; j++)
      ans[j][i] = 1;
  }
  for (vector<int> &i : ans) {
    for (int &j : i)
      cout << j;
    cout << endl;
  }
}

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

int main() {
  int H, W, A, B;
  cin >> H >> W >> A >> B;
  vector<vector<int>> ans(H, vector<int>(W));
  for (int i = 0; i < A; i++) {
    for (int j = 0; j < B; j++)
      ans[j][i] = 1;
  }
  for (int i = A; i < W; i++) {
    for (int j = B; j < H; j++)
      ans[j][i] = 1;
  }
  for (vector<int> &i : ans) {
    for (int &j : i)
      cout << j;
    cout << endl;
  }
}
Problem p02903 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[1000][1000];
int sumb[1000];
int main() {
  int h, w, A, b;
  cin >> h >> w >> A >> b;
  for (int j = 1; j <= A; j++)
    for (int i = 1; i <= b; i++)
      a[i][j] = 1;
  for (int j = A + 1; j <= w; j++) {
    for (int i = 1; i <= b; i++)
      a[i][j] = 0;
  }
  for (int i = 1; i <= A; i++)
    for (int j = b + 1; j <= h; j++)
      a[j][i] = 0;
  for (int i = A + 1; i <= w; i++) {
    for (int j = b + 1; j <= h; j++)
      a[j][i] = 1;
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      cout << a[i][j];
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[1006][1006];
int sumb[1006];
int main() {
  int h, w, A, b;
  cin >> h >> w >> A >> b;
  for (int j = 1; j <= A; j++)
    for (int i = 1; i <= b; i++)
      a[i][j] = 1;
  for (int j = A + 1; j <= w; j++) {
    for (int i = 1; i <= b; i++)
      a[i][j] = 0;
  }
  for (int i = 1; i <= A; i++)
    for (int j = b + 1; j <= h; j++)
      a[j][i] = 0;
  for (int i = A + 1; i <= w; i++) {
    for (int j = b + 1; j <= h; j++)
      a[j][i] = 1;
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      cout << a[i][j];
    cout << endl;
  }
}
Problem p02903 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define vi vector<int>
#define INF (1 << 30) - 1
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  vector<vi> ans(h, vi(w));

  if (b == 0) {
    rep(i, h) rep(j, a) ans[i][j] = 1;
  } else {
    int start = -a;
    rep(i, h) {
      if (i % b == 0)
        start += a;
      rep(j, a) { ans[i][j + start] = 1; }
    }
  }

  bool res = true;
  rep(i, w) {
    int cnt = 0;
    rep(j, h) cnt += ans[j][i];
    if (b != min(cnt, h - cnt))
      res = false;
  }
  rep(i, h) {
    int cnt = 0;
    rep(j, w) cnt += ans[i][j];
    if (a != min(cnt, w - cnt))
      res = false;
  }
  if (res) {
    rep(i, h) {
      rep(j, w) cout << ans[i][j];
      cout << endl;
    }
  } else
    cout << "No\n";
  // cout << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define vi vector<int>
#define INF (1 << 30) - 1
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  vector<vi> ans(h, vi(w));

  rep(i, b) rep(j, w - a) ans[i][j + a] = 1;
  rep(i, h - b) rep(j, a) ans[i + b][j] = 1;

  bool res = true;
  rep(i, w) {
    int cnt = 0;
    rep(j, h) cnt += ans[j][i];
    if (b != min(cnt, h - cnt))
      res = false;
  }
  rep(i, h) {
    int cnt = 0;
    rep(j, w) cnt += ans[i][j];
    if (a != min(cnt, w - cnt))
      res = false;
  }
  if (res) {
    rep(i, h) {
      rep(j, w) cout << ans[i][j];
      cout << endl;
    }
  } else
    cout << "No\n";
  // cout << "\n";
  return 0;
}
Problem p02903 (C++)

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

using namespace std;
typedef long long int ll;

int main() {

  ll H, W, A, B;
  cin >> H >> W >> A >> B;

  ll n_rows = H;
  ll n_cols = W;
  ll value = 0;
  vector<vector<ll>> vec(W, vector<ll>(H, 0));
  ;

  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      if (i < B && j < A) {
        vec[i][j] = 0;
      } else if (i >= B && j < A) {
        vec[i][j] = 1;
      } else if (i < B && j >= A) {
        vec[i][j] = 1;
      } else if (i >= B && j >= A) {
        vec[i][j] = 0;
      }
    }
  }

  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      cout << vec[i][j];
    }
    cout << endl;
  }
}

Fixed submission:
#include "bits/stdc++.h"

using namespace std;
typedef long long int ll;

int main() {

  ll H, W, A, B;
  cin >> H >> W >> A >> B;

  ll n_rows = H;
  ll n_cols = W;
  ll value = 0;
  vector<vector<ll>> vec(H, vector<ll>(W, 0));
  ;

  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      if (i < B && j < A) {
        vec[i][j] = 0;
      } else if (i >= B && j < A) {
        vec[i][j] = 1;
      } else if (i < B && j >= A) {
        vec[i][j] = 1;
      } else if (i >= B && j >= A) {
        vec[i][j] = 0;
      }
    }
  }

  for (ll i = 0; i < H; i++) {
    for (ll j = 0; j < W; j++) {
      cout << vec[i][j];
    }
    cout << endl;
  }
}
Problem p02903 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
int main() {
  int H, W, A, B, i, j;
  cin >> H >> W >> A >> B;
  int h = H - 1;
  int w = W - 1;
  int matrix[h][w];

  for (i = 0; i <= B - 1; i++) {
    for (j = 0; j <= A - 1; j++) {
      matrix[i][j] = 0;
    }
    for (j = A; j <= w; j++) {
      matrix[i][j] = 1;
    }
  }

  for (i = B; i <= h; i++) {
    for (j = 0; j <= A - 1; j++) {
      matrix[i][j] = 1;
    }
    for (j = A; j <= w; j++) {
      matrix[i][j] = 0;
    }
  }

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

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <string.h>

using namespace std;
int main() {
  int H, W, A, B, i, j;
  cin >> H >> W >> A >> B;
  int h = H - 1;
  int w = W - 1;
  int matrix[H][W];

  for (i = 0; i <= B - 1; i++) {
    for (j = 0; j <= A - 1; j++) {
      matrix[i][j] = 0;
    }
    for (j = A; j <= w; j++) {
      matrix[i][j] = 1;
    }
  }

  for (i = B; i <= h; i++) {
    for (j = 0; j <= A - 1; j++) {
      matrix[i][j] = 1;
    }
    for (j = A; j <= w; j++) {
      matrix[i][j] = 0;
    }
  }

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

  return 0;
}
Problem p02903 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);                                                             \
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  Init();
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  string zo = string(max(a, b), '0') + string(w - max(a, b), '1');
  string oz = string(max(a, b), '1') + string(w - max(a, b), '0');
  rep(i, h) cout << (i < a ? zo : oz) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);                                                             \
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  Init();
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  string zo = string(a, '0') + string(w - a, '1');
  string oz = string(a, '1') + string(w - a, '0');
  rep(i, h) cout << (i < b ? zo : oz) << endl;
}
Problem p02903 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int H, W;
  cin >> H >> W;
  int A, B;
  cin >> A >> B;
  vector<vector<int>> ans(H, vector<int>(W, 0));
  for (int i = 0; i < H - B; i++) {
    for (int j = 0; j < W - A; j++) {
      ans[i][j] = 1;
    }
  }
  for (int i = 0; i < A; i++) {
    for (int j = 0; j < B; j++) {
      ans[H - 1 - i][W - 1 - j] = 1;
    }
  }

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


Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int H, W;
  cin >> H >> W;
  int A, B;
  cin >> A >> B;
  vector<vector<int>> ans(H, vector<int>(W, 0));
  for (int i = 0; i < H - B; i++) {
    for (int j = 0; j < W - A; j++) {
      ans[i][j] = 1;
    }
  }
  for (int i = 0; i < B; i++) {
    for (int j = 0; j < A; j++) {
      ans[H - 1 - i][W - 1 - j] = 1;
    }
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = __int64_t;
using lll = __int128_t;
typedef pair<ll, ll> P;
#define REP(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define rep0(i, n) REP(i, 0ll, n)
#define rep1(i, n) REP(i, 1ll, n + 1)
const ll longinf = 1ll << 60;
const int inf = (1 << 30) - 1;
const ll mod = 1e9 + 7;

int main(void) {
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  int f[h][w];
  rep0(i, h) rep0(j, w) f[i][j] = 0;
  if (h == 1) {
    rep0(i, h) rep0(j, w) {
      if (j < a)
        f[i][j] = 1;
    }
  } else if (w == 1) {
    rep0(i, h) rep0(j, w) {
      if (i < b)
        f[i][j] = 1;
    }
  } else if (h % a == 0 && w % b == 0 && h / a == w / b) {
    int d = h / a;
    rep0(p, d) {
      rep0(j, b) rep0(k, a) { f[p * b + j][p * a + k] = 1; }
    }
  } else {
    cout << "No" << endl;
    return 0;
  }
  rep0(i, h) {
    rep0(j, w) { cout << f[i][j]; }
    cout << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
using ll = __int64_t;
using lll = __int128_t;
typedef pair<ll, ll> P;
#define REP(i, b, e) for (ll i = (ll)b; i < (ll)e; i++)
#define rep0(i, n) REP(i, 0ll, n)
#define rep1(i, n) REP(i, 1ll, n + 1)
const ll longinf = 1ll << 60;
const int inf = (1 << 30) - 1;
const ll mod = 1e9 + 7;

int main(void) {
  int h, w, a, b;
  cin >> h >> w >> a >> b;
  int f[h][w];
  rep0(i, h) rep0(j, w) {
    if (i < b && j < a)
      f[i][j] = 0;
    else if (i < b)
      f[i][j] = 1;
    else if (j < a)
      f[i][j] = 1;
    else
      f[i][j] = 0;
  }
  rep0(i, h) {
    rep0(j, w) { cout << f[i][j]; }
    cout << endl;
  }
  return 0;
}
Problem p02903 (C++)

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

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int main() {
  int h, w, a, b;
  scanf("%d%d%d%d", &h, &w, &a, &b);
  int x[101][101] = {};
  rep(i, 0, b) rep(j, 0, a) x[i][j] = 1;
  rep(i, b, h) rep(j, a, w) x[i][j] = 1;
  rep(i, 0, h) {
    rep(j, 0, w) printf("%d", x[i][j]);
    puts("");
  }
  return 0;
}

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

// template
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  fill((T *)array, (T *)(array + N), val);
}
const int inf = INT_MAX / 2;
const ll INF = LLONG_MAX / 2;
// template end

int main() {
  int h, w, a, b;
  scanf("%d%d%d%d", &h, &w, &a, &b);
  int x[1010][1010] = {};
  rep(i, 0, b) rep(j, 0, a) x[i][j] = 1;
  rep(i, b, h) rep(j, a, w) x[i][j] = 1;
  rep(i, 0, h) {
    rep(j, 0, w) printf("%d", x[i][j]);
    puts("");
  }
  return 0;
}
Problem p02903 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  ll ans[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i + 1 <= b)
        ans[i][j] = 1;
      else
        ans[i][j] = 0;
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < h; j++) {
      if (ans[j][i] == 1)
        ans[i][j] = 0;
      else
        ans[j][i] = 1;
    }
  }
  rep(i, h) {
    rep(j, w) cout << ans[i][j];
    cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
  ll h, w, a, b;
  cin >> h >> w >> a >> b;
  ll ans[h][w];
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (i + 1 <= b)
        ans[i][j] = 1;
      else
        ans[i][j] = 0;
    }
  }
  for (int i = 0; i < a; i++) {
    for (int j = 0; j < h; j++) {
      if (ans[j][i] == 1)
        ans[j][i] = 0;
      else
        ans[j][i] = 1;
    }
  }
  rep(i, h) {
    rep(j, w) cout << ans[i][j];
    cout << endl;
  }
}
Problem p02903 (C++)

Buggy submission (status: Time Limit Exceeded):
// A.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

typedef long long LL;

int main(int argc, char *argv[]) {
  while (true) {
    LL h, w, a, b;
    cin >> h >> w >> a >> b;
    if (h <= 0 || w <= 0)
      break;
    for (int i = 0; i < h; ++i) {
      string s;
      if (i < b) {
        s = string(a, '0') + string(w - a, '1');
      } else {
        s = string(a, '1') + string(w - a, '0');
      }
      cout << s << endl;
    }
  }
  return 0;
}


Fixed submission:
// A.

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <sstream>

using namespace std;

typedef long long LL;

int main(int argc, char *argv[]) {
  while (true) {
    LL h = -1, w = -1, a, b;
    cin >> h >> w >> a >> b;
    if (h <= 0 || w <= 0)
      break;
    for (int i = 0; i < h; ++i) {
      string s;
      if (i < b) {
        s = string(a, '0') + string(w - a, '1');
      } else {
        s = string(a, '1') + string(w - a, '0');
      }
      cout << s << endl;
    }
  }
  return 0;
}
Problem p02904 (C++)

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

const int N = 2e5 + 5;

int n, K;
int a[N], Q[N];
boolean ismx[N], ismi[N];

int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  int st = 1, ed = 0;
  for (int i = n; i; i--) {
    while (st <= ed && Q[st] >= i + K)
      st++;
    while (st <= ed && a[Q[ed]] > a[i])
      ed--;
    Q[++ed] = i;
    ismi[i] = (st == ed);
  }
  st = 1, ed = 0;
  for (int i = 1; i <= n; i++) {
    while (st <= ed && Q[st] <= i - K)
      st++;
    while (st <= ed && a[Q[ed]] < a[i])
      ed--;
    Q[++ed] = i;
    ismx[i] = (st == ed);
  }
  int qaq = 0;
  for (int i = 1, j; i <= n; i = j) {
    ++j;
    while (a[j] > a[j - 1])
      j++;
    qaq += (j - i >= K);
  }
  int ans = n - K + 1 - max(qaq - 1, 0);
  for (int i = K + 1; i <= n; i++) {
    ans -= ismi[i - K] && ismx[i];
  }
  printf("%d\n", ans);
  return 0;
}


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

const int N = 2e5 + 5;

int n, K;
int a[N], Q[N];
boolean ismx[N], ismi[N];

int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  int st = 1, ed = 0;
  for (int i = n; i; i--) {
    while (st <= ed && Q[st] >= i + K)
      st++;
    while (st <= ed && a[Q[ed]] > a[i])
      ed--;
    Q[++ed] = i;
    ismi[i] = (st == ed);
  }
  st = 1, ed = 0;
  for (int i = 1; i <= n; i++) {
    while (st <= ed && Q[st] <= i - K)
      st++;
    while (st <= ed && a[Q[ed]] < a[i])
      ed--;
    Q[++ed] = i;
    ismx[i] = (st == ed);
  }
  int qaq = 0;
  for (int i = 1, j = 1; i <= n; i = j) {
    ++j;
    while (a[j] > a[j - 1])
      j++;
    qaq += (j - i >= K);
  }
  int ans = n - K + 1 - max(qaq - 1, 0);
  for (int i = K + 1; i <= n; i++) {
    ans -= ismi[i - K] && ismx[i];
  }
  printf("%d\n", ans);
  return 0;
}
Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, k;
  cin >> n >> k;
  int ans = n - k + 1, v = 0, cnt = 1;
  vector<int> p(n, 0), l(n - k + 1, 0);
  rep(i, n) cin >> p[i];
  set<pp> s;
  rep(i, n - 1) {
    if (p[i] < p[i + 1])
      cnt++;
    else
      cnt = 1;
    if (cnt >= k) {
      l[i - k + 1] = 1;
      if (v)
        ans--;
      v = 1;
    }
  }
  rep(i, k) s.insert(mk(p[i], i));
  for (int i = 1; i < n - k + 1; i++) {
    if (l[i] == 0) {
      if ((*begin(s)).second == i - 1 && (*rbegin(s)).first < p[i + k - 1]) {
        ans--;
      }
    }
    s.insert(mk(p[i + k - 1], i + k - 1));
    s.erase(mk(p[i - 1], i - 1));
  }
  cout << ans << endl;
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  int n, k;
  cin >> n >> k;
  int ans = n - k + 1, v = 0, cnt = 1;
  vector<int> p(n, 0), l(n - k + 1, 0);
  rep(i, n) cin >> p[i];
  set<pp> s;
  rep(i, n - 1) {
    if (p[i] < p[i + 1])
      cnt++;
    else
      cnt = 1;
    if (cnt >= k) {
      l[i - k + 2] = 1;
      if (v)
        ans--;
      v = 1;
    }
  }
  rep(i, k) s.insert(mk(p[i], i));
  for (int i = 1; i < n - k + 1; i++) {
    if (l[i] == 0) {
      if ((*begin(s)).second == i - 1 && (*rbegin(s)).first < p[i + k - 1]) {
        ans--;
      }
    }
    s.insert(mk(p[i + k - 1], i + k - 1));
    s.erase(mk(p[i - 1], i - 1));
  }
  cout << ans << endl;
}
Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 1e5;

deque<pii> qac, qde;

int p[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
  }

  int len = 1;
  int cnt2 = 0;
  for (int i = 1; i < n; i++) {
    if (p[i] > p[i - 1])
      len++;
    else
      len = 1;
    if (len == k)
      cnt2++;
  }

  for (int i = 0; i < k; i++) {
    while (!qac.empty() && qac.back().first > p[i])
      qac.pop_back();
    qac.push_back({p[i], i});

    while (!qde.empty() && qde.back().first < p[i])
      qde.pop_back();
    qde.push_back({p[i], i});
  }
  int cnt = 0;
  for (int i = k; i < n; i++) {
    // cout << qac.front().first << " ";
    // cout << qde.front().first << endl;
    bool f1 = qac.front().first == p[i - k];
    if (qac.front().second <= i - k)
      qac.pop_front();
    if (qde.front().second <= i - k)
      qde.pop_front();

    while (!qac.empty() && qac.back().first > p[i])
      qac.pop_back();
    qac.push_back({p[i], i});
    while (!qde.empty() && qde.back().first < p[i])
      qde.pop_back();
    qde.push_back({p[i], i});
    bool f2 = qde.front().first == p[i];
    if (f1 && f2) {
      // cout << "same" << endl;
      cnt++;
    }
  }
  cout << n - k + 1 - cnt - max(0, cnt2 - 1);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 10;

deque<pii> qac, qde;

int p[N];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    scanf("%d", &p[i]);
  }

  int len = 1;
  int cnt2 = 0;
  for (int i = 1; i < n; i++) {
    if (p[i] > p[i - 1])
      len++;
    else
      len = 1;
    if (len == k)
      cnt2++;
  }

  for (int i = 0; i < k; i++) {
    while (!qac.empty() && qac.back().first > p[i])
      qac.pop_back();
    qac.push_back({p[i], i});

    while (!qde.empty() && qde.back().first < p[i])
      qde.pop_back();
    qde.push_back({p[i], i});
  }
  int cnt = 0;
  for (int i = k; i < n; i++) {
    // cout << qac.front().first << " ";
    // cout << qde.front().first << endl;
    bool f1 = qac.front().first == p[i - k];
    if (qac.front().second <= i - k)
      qac.pop_front();
    if (qde.front().second <= i - k)
      qde.pop_front();

    while (!qac.empty() && qac.back().first > p[i])
      qac.pop_back();
    qac.push_back({p[i], i});
    while (!qde.empty() && qde.back().first < p[i])
      qde.pop_back();
    qde.push_back({p[i], i});
    bool f2 = qde.front().first == p[i];
    if (f1 && f2) {
      // cout << "same" << endl;
      cnt++;
    }
  }
  cout << n - k + 1 - cnt - max(0, cnt2 - 1);
}
Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
typedef long long i64;
int n, k, p[N], t[N], ans, cnt;
i64 s;
void add(int x, int d) {
  for (; x <= n; x += x & -x)
    t[x] += d;
}
int ask(int x) {
  int y = 0;
  for (; x; x ^= x & -x)
    y += t[x];
  return y;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", p + i), ++p[i];
  for (int i = k; i; --i)
    s += ask(p[i]), add(p[i], 1);
  ans = s > 0, cnt = s == 0;
  for (int i = k + 1, w = 0, v; i <= n; ++i) {
    w = (v = ask(p[i - k] - 1)) > 0, add(p[i - k], -1), s -= v,
    w |= (v = k - 1 - ask(p[i])) > 0, add(p[i], 1), s += v, ans += w && s > 0,
    cnt += s == 0;
  }
  printf("%d\n", ans + (cnt > 0));
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
typedef long long i64;
int n, k, p[N], t[N], ans, cnt;
i64 s;
void add(int x, int d) {
  for (; x <= n; x += x & -x)
    t[x] += d;
}
int ask(int x) {
  int y = 0;
  for (; x; x ^= x & -x)
    y += t[x];
  return y;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", p + i), ++p[i];
  for (int i = k; i; --i)
    s += ask(p[i]), add(p[i], 1);
  ans = s > 0, cnt = s == 0;
  for (int i = k + 1, w = 0, v; i <= n; ++i) {
    w = (v = ask(p[i - k] - 1)) > 0, add(p[i - k], -1), s -= v,
    w |= (v = k - 1 - ask(p[i])) > 0, add(p[i], 1), s += v, ans += w && s > 0,
    cnt += s == 0;
  }
  printf("%d\n", ans + (cnt > 0));
  return 0;
}
Problem p02904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, k;
const int maxn = 200005;
int a[maxn];
int ans;
set<int> st;
int b[maxn];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int now = 1;
  int cnt = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1])
      now = i;
    if (i - now + 1 >= k)
      b[i] = 1, cnt = 1;
  }
  if (!b[k])
    ans = 1;
  for (int i = 1; i <= n; i++)
    for (int i = 1; i <= k; i++) {
      st.insert(a[i]);
    }
  for (int i = k + 1; i <= n; i++) {
    ans++;
    if (b[i] || (st.lower_bound(a[i]) == st.end() &&
                 st.lower_bound(a[i - k]) == st.begin())) {
      ans--;
    }
    st.erase(a[i - k]);
    st.insert(a[i]);
  }
  cout << ans + cnt << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, k;
const int maxn = 200005;
int a[maxn];
int ans;
set<int> st;
int b[maxn];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  int now = 1;
  int cnt = 0;
  for (int i = 2; i <= n; i++) {
    if (a[i] < a[i - 1])
      now = i;
    if (i - now + 1 >= k)
      b[i] = 1, cnt = 1;
  }
  if (!b[k])
    ans = 1;
  for (int i = 1; i <= k; i++) {
    st.insert(a[i]);
  }
  for (int i = k + 1; i <= n; i++) {
    ans++;
    if (b[i] || (st.lower_bound(a[i]) == st.end() &&
                 st.lower_bound(a[i - k]) == st.begin())) {
      ans--;
    }
    st.erase(a[i - k]);
    st.insert(a[i]);
  }
  cout << ans + cnt << endl;
}
Problem p02904 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <iostream>
#include <set>
using namespace std;

int n, k, c, cnt, ans = 1, a[200005];
set<int> st;

int main() {
  int i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1])
      c++;
    else
      c = 0;
    if (c == k - 1)
      cnt++;
  }
  for (i = 0; i < k; i++)
    st.insert(a[i]);
  while (i < n) {
    st.erase(a[i - k]);
    if (a[i - k] > *st.begin() || a[i] < *st.rbegin())
      ans++;
    st.insert(a[i]);
  }
  cout << ans - max(0, cnt - 1);
  return 0;
}


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

int n, k, c, cnt, ans = 1, a[200005];
set<int> st;

int main() {
  int i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (i = 0; i < n - 1; i++) {
    if (a[i] < a[i + 1])
      c++;
    else
      c = 0;
    if (c == k - 1)
      cnt++;
  }
  for (i = 0; i < k; i++)
    st.insert(a[i]);
  while (i < n) {
    st.erase(a[i - k]);
    if (a[i - k] > *st.begin() || a[i] < *st.rbegin())
      ans++;
    st.insert(a[i]);
    i++;
  }
  cout << ans - max(0, cnt - 1);
  return 0;
}
Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define inf 998244353
#define ll long long
using namespace std;
ll leng[114514], used[114514], mi[114514], ma[114514];
signed main() {
  int n, k;
  cin >> n >> k;
  int ans = n - k + 1;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.pb(x);
  }
  set<int> s;
  for (int i = 0; i < k; i++) {
    s.insert(a[i]);
  }
  leng[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] < a[i + 1])
      leng[i] = leng[i + 1] + 1;
    else
      leng[i] = 1;
  }
  for (int i = 1; i <= n + 1 - k; i++) {
    s.erase(a[i - 1]);
    s.insert(a[i + k - 2]);
    set<int>::iterator it = s.begin();
    mi[i] = *it;
    it = s.end();
    it--;
    ma[i] = *it;
  }
  for (int i = 0; i < n - k; i++) {
    int cmi = mi[i + 1];
    int cma = ma[i + 1];
    // cout<<a[i]<<" "<<cmi<<" "<<cma<<" "<<a[i+k]<<endl;
    if (a[i] < cmi && cma < a[i + k]) {
      ans--;
      used[i] = 1;
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    // cout<<leng[i]<<endl;
    if (leng[i] >= k && used[i] == 0)
      cnt++;
  }
  if (cnt >= 2)
    ans -= cnt - 1;
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
#define inf 998244353
#define ll long long
using namespace std;
ll leng[214514], used[214514], mi[214514], ma[214514];
signed main() {
  int n, k;
  cin >> n >> k;
  int ans = n - k + 1;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    a.pb(x);
  }
  set<int> s;
  for (int i = 0; i < k; i++) {
    s.insert(a[i]);
  }
  leng[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (a[i] < a[i + 1])
      leng[i] = leng[i + 1] + 1;
    else
      leng[i] = 1;
  }
  for (int i = 1; i <= n + 1 - k; i++) {
    s.erase(a[i - 1]);
    s.insert(a[i + k - 2]);
    set<int>::iterator it = s.begin();
    mi[i] = *it;
    it = s.end();
    it--;
    ma[i] = *it;
  }
  for (int i = 0; i < n - k; i++) {
    int cmi = mi[i + 1];
    int cma = ma[i + 1];
    // cout<<a[i]<<" "<<cmi<<" "<<cma<<" "<<a[i+k]<<endl;
    if (a[i] < cmi && cma < a[i + k]) {
      ans--;
      used[i] = 1;
    }
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    // cout<<leng[i]<<endl;
    if (leng[i] >= k && used[i] == 0)
      cnt++;
  }
  if (cnt >= 2)
    ans -= cnt - 1;
  cout << ans << endl;
  return 0;
}
Problem p02904 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string.h>
#define maxn 100005
using namespace std;
int n, k;
int p[maxn];
set<int> s1, s2;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  p[0] = -1;
  int cnt = 0, flag = 1;
  for (int i = 1; i <= k; i++) {
    s1.insert(p[i]);
    s2.insert(-p[i]);
    if (p[i] > p[i - 1])
      cnt++;
    else
      cnt = 1;
  }
  if (cnt == k)
    flag = 0;
  int ans = 1;
  for (int i = k + 1; i <= n; i++) {
    if (p[i] > p[i - 1])
      cnt++;
    else
      cnt = 1;
    s1.erase(p[i - k]);
    s2.erase(-p[i - k]);
    int minn = (*s1.begin());
    int maxx = (-(*s2.begin()));
    if (maxx < p[i] && minn > p[i - k])
      1;
    else {
      if (cnt >= k) {
        if (flag == 1) {
          flag = 0;
          ans++;
        } else {
          flag = 0;
        }
      } else
        ans++;
    }
    s1.insert(p[i]);
    s2.insert(-p[i]);
  }
  printf("%d\n", ans);
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <string.h>
#define maxn 200005
using namespace std;
int n, k;
int p[maxn];
set<int> s1, s2;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++)
    scanf("%d", &p[i]);
  p[0] = -1;
  int cnt = 0, flag = 1;
  for (int i = 1; i <= k; i++) {
    s1.insert(p[i]);
    s2.insert(-p[i]);
    if (p[i] > p[i - 1])
      cnt++;
    else
      cnt = 1;
  }
  if (cnt == k)
    flag = 0;
  int ans = 1;
  for (int i = k + 1; i <= n; i++) {
    if (p[i] > p[i - 1])
      cnt++;
    else
      cnt = 1;
    s1.erase(p[i - k]);
    s2.erase(-p[i - k]);
    int minn = (*s1.begin());
    int maxx = (-(*s2.begin()));
    if (maxx < p[i] && minn > p[i - k])
      1;
    else {
      if (cnt >= k) {
        if (flag == 1) {
          flag = 0;
          ans++;
        } else {
          flag = 0;
        }
      } else
        ans++;
    }
    s1.insert(p[i]);
    s2.insert(-p[i]);
  }
  printf("%d\n", ans);
}
Problem p02904 (C++)

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

const int maxn = 1e5 + 10;
int p[maxn];

set<int> st;

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  int inc = 1;
  int tmp = 0;
  for (int i = 0; i < k; i++) {
    st.insert(p[i]);
    if (i > 0)
      inc = p[i] > p[i - 1] ? inc + 1 : 1;
  }
  int ans = inc >= k ? 0 : 1;
  tmp = inc >= k ? 1 : tmp;
  for (int i = k; i < n; i++) {
    inc = p[i] > p[i - 1] ? inc + 1 : 1;

    tmp = inc >= k ? 1 : tmp;
    int j = i - k;
    st.erase(p[j]);
    if (p[i] > *st.rbegin() && p[j] < *st.begin()) {
    } else
      ans += inc >= k ? 0 : 1;
    st.insert(p[i]);
  }
  cout << ans + tmp << endl;
}

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

const int maxn = 2e5 + 10;
int p[maxn];

set<int> st;

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  int inc = 1;
  int tmp = 0;
  for (int i = 0; i < k; i++) {
    st.insert(p[i]);
    if (i > 0)
      inc = p[i] > p[i - 1] ? inc + 1 : 1;
  }
  int ans = inc >= k ? 0 : 1;
  tmp = inc >= k ? 1 : tmp;
  for (int i = k; i < n; i++) {
    inc = p[i] > p[i - 1] ? inc + 1 : 1;

    tmp = inc >= k ? 1 : tmp;
    int j = i - k;
    st.erase(p[j]);
    if (p[i] > *st.rbegin() && p[j] < *st.begin()) {
    } else
      ans += inc >= k ? 0 : 1;
    st.insert(p[i]);
  }
  cout << ans + tmp << endl;
}