Problem p03689 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef vector<pii> vii;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()

void solve(int n, int m, int h, int w) {
  if (n % h == 0 && m % w == 0) {
    cout << "No" << endl;
    return;
  }

  bool sw = false;
  if (n % h == 0) {
    assert(m % w != 0);
    swap(n, m);
    swap(h, w);
    sw = true;
  }

  vvi grid(n, vi(m));
  const int X = (int)1e8;

  forn(i, n) {
    forn(j, m) if (j) grid[i][j] = -1;

    if (i % h == 0)
      grid[i][0] = X;
    else if (i % h == h - 1)
      grid[i][0] = -X - 1;
    else
      grid[i][0] = -1;
  }

  if (sw) {
    vvi ngrid(m, vi(n));
    forn(i, n) forn(j, m) ngrid[j][i] = grid[i][j];
    grid = ngrid;
    swap(n, m);
    swap(h, w);
  }

  ll sum = 0;
  forn(i, n) forn(j, m) sum += grid[i][j];
  assert(sum > 0);

  forn(x, n - h + 1) forn(y, m - w + 1) {
    ll csum = 0;
    forn(i, h) forn(j, w) csum += grid[x + i][y + j];
    assert(csum < 0);
  }

  cout << "Yes" << endl;
  forn(i, n) { forn(j, m) cout << grid[i][j] << " \n"[j + 1 == m]; }
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m, h, w;
  while (cin >> n >> m >> h >> w)
    //    forn (n, 10) if (n)
    //    forn (m, 10) if (m)
    //    forn (h, n + 1) if (h)
    //    forn (w, m + 1) if (w)
    solve(n, m, h, w);
}


Fixed submission:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<char> vc;
typedef pair<int, int> pii;
typedef vector<pii> vii;
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()

void solve(int n, int m, int h, int w) {
  if (n % h == 0 && m % w == 0) {
    cout << "No" << endl;
    return;
  }

  bool sw = false;
  if (n % h == 0) {
    assert(m % w != 0);
    swap(n, m);
    swap(h, w);
    sw = true;
  }

  vvi grid(n, vi(m));
  const int X = (int)1e8;

  forn(i, n) {
    forn(j, m) if (j) grid[i][j] = -1;

    if (i % h == 0)
      grid[i][0] = X;
    else if (i % h == h - 1)
      grid[i][0] = -X - 1;
    else
      grid[i][0] = -1;
  }

  if (sw) {
    vvi ngrid(m, vi(n));
    forn(i, n) forn(j, m) ngrid[j][i] = grid[i][j];
    grid = ngrid;
    swap(n, m);
    swap(h, w);
  }

  //    ll sum = 0;
  //    forn (i, n) forn (j, m)
  //        sum += grid[i][j];
  //    assert(sum > 0);
  //
  //    forn (x, n - h + 1) forn (y, m - w + 1)
  //    {
  //        ll csum = 0;
  //        forn (i, h) forn (j, w)
  //            csum += grid[x + i][y + j];
  //        assert(csum < 0);
  //    }

  cout << "Yes" << endl;
  forn(i, n) { forn(j, m) cout << grid[i][j] << " \n"[j + 1 == m]; }
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, m, h, w;
  while (cin >> n >> m >> h >> w)
    //    forn (n, 10) if (n)
    //    forn (m, 10) if (m)
    //    forn (h, n + 1) if (h)
    //    forn (w, m + 1) if (w)
    solve(n, m, h, w);
}