Problem p04007 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define getchar gc
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline char gg() {
  char ch = getchar();
  while (ch != '.' && ch != '?')
    ch = getchar();
  return ch;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return rand() % x; }
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 505;
int h, w, tot;
char ch[N];
char f[N][N], g[N][N];
char ans[N * N * 2];
int main() {
  h = read();
  w = read();
  for (int i = 1; i <= h; i++) {
    f[i][1] = 1;
    g[i][w] = 1;
  }
  for (int i = 1; i <= h; i++) {
    if (i & 1)
      for (int j = 2; j < w; j++)
        f[i][j] = 1;
    else
      for (int j = 2; j < w; j++)
        g[i][j] = 1;
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      if (gg() == '#')
        f[i][j] = g[i][j] = 1;
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      ans[tot++] = f[i][j] ? '#' : '.';
    ans[tot++] = '\n';
  }
  ans[tot++] = '\n';
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      ans[tot++] = g[i][j] ? '#' : '.';
    ans[tot++] = '\n';
  }
  puts(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define getchar gc
inline int read() {
  int x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline char gg() {
  char ch = getchar();
  while (ch != '.' && ch != '#')
    ch = getchar();
  return ch;
}
inline void write(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(int a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  write(a);
  puts("");
}
inline int rnd(int x) { return rand() % x; }
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 505;
int h, w, tot;
char ch[N];
char f[N][N], g[N][N];
char ans[N * N * 2];
int main() {
  h = read();
  w = read();
  for (int i = 1; i <= h; i++) {
    f[i][1] = 1;
    g[i][w] = 1;
  }
  for (int i = 1; i <= h; i++) {
    if (i & 1)
      for (int j = 2; j < w; j++)
        f[i][j] = 1;
    else
      for (int j = 2; j < w; j++)
        g[i][j] = 1;
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      if (gg() == '#')
        f[i][j] = g[i][j] = 1;
  }
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      ans[tot++] = f[i][j] ? '#' : '.';
    ans[tot++] = '\n';
  }
  ans[tot++] = '\n';
  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++)
      ans[tot++] = g[i][j] ? '#' : '.';
    ans[tot++] = '\n';
  }
  puts(ans);
}