Problem p03336 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 2020000;
int n, m, k, a[N], b[N], sta[N], top;
int main() {
  freopen("1.in", "r", stdin);
  read(n);
  read(m);
  read(k);
  static char S[N];
  scanf("%s", S + 1);
  rep(i, 1, n) a[i] = S[n - i + 1] - '0';
  scanf("%s", S + 1);
  rep(i, 1, m) b[i] = S[m - i + 1] - '0';
  sta[0] = 1e9; // sta[]: 01&10 in [pos+1..inf]
  per(i, max(n, m), 1) if (a[i] && b[i]) {
    int now = k, pos = i;
    Vi tmp;
    while (now) {
      if (!a[pos + 1] && !b[pos + 1]) {
        int mn = min(now, sta[top] - pos - 1);
        a[pos] = b[pos] = 0;
        a[pos + mn] = b[pos + mn] = 1;
        pos += mn;
        now -= mn;
      } else {
        int mx = 0;
        now--;
        for (int i = pos;; i++) {
          if (a[i])
            a[i] = 0;
          else {
            a[i] = 1;
            umax(mx, i);
            break;
          }
        }
        for (int i = pos;; i++) {
          if (b[i])
            b[i] = 0;
          else {
            b[i] = 1;
            umax(mx, i);
            break;
          }
        }
        if (a[mx] && b[mx]) {
          rep(i, pos, mx) if (a[i] + b[i] == 1) tmp.pb(i);
          while (top && sta[top] <= mx)
            top--;
          pos = mx;
        } else {
          rep(i, pos, mx) if (a[i] + b[i] == 1) tmp.pb(i);
          while (top && sta[top] <= mx)
            top--;
          break;
        }
      }
    }
    per(i, SZ(tmp) - 1, 0) sta[++top] = tmp[i];
  }
  else if (a[i] || b[i]) sta[++top] = i;
  per(i, n + k + 2, 1) if (a[i]) {
    n = i;
    break;
  }
  per(i, m + k + 2, 1) if (b[i]) {
    m = i;
    break;
  }
  per(i, n, 1) printf("%d", a[i]);
  puts("");
  per(i, m, 1) printf("%d", b[i]);
  puts("");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define SZ(x) ((int)x.size())
#define L(i, u) for (register int i = head[u]; i; i = nxt[i])
#define rep(i, a, b) for (register int i = (a); i <= (b); i++)
#define per(i, a, b) for (register int i = (a); i >= (b); i--)
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> Pii;
typedef vector<int> Vi;
template <class T> inline void read(T &x) {
  x = 0;
  char c = getchar();
  int f = 1;
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= f;
}
template <class T> inline void umin(T &x, T y) { x = x < y ? x : y; }
template <class T> inline void umax(T &x, T y) { x = x > y ? x : y; }
inline ui R() {
  static ui seed = 416;
  return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 2020000;
int n, m, k, a[N], b[N], sta[N], top;
int main() {
  read(n);
  read(m);
  read(k);
  static char S[N];
  scanf("%s", S + 1);
  rep(i, 1, n) a[i] = S[n - i + 1] - '0';
  scanf("%s", S + 1);
  rep(i, 1, m) b[i] = S[m - i + 1] - '0';
  sta[0] = 1e9; // sta[]: 01&10 in [pos+1..inf]
  per(i, max(n, m), 1) if (a[i] && b[i]) {
    int now = k, pos = i;
    Vi tmp;
    while (now) {
      if (!a[pos + 1] && !b[pos + 1]) {
        int mn = min(now, sta[top] - pos - 1);
        a[pos] = b[pos] = 0;
        a[pos + mn] = b[pos + mn] = 1;
        pos += mn;
        now -= mn;
      } else {
        int mx = 0;
        now--;
        for (int i = pos;; i++) {
          if (a[i])
            a[i] = 0;
          else {
            a[i] = 1;
            umax(mx, i);
            break;
          }
        }
        for (int i = pos;; i++) {
          if (b[i])
            b[i] = 0;
          else {
            b[i] = 1;
            umax(mx, i);
            break;
          }
        }
        if (a[mx] && b[mx]) {
          rep(i, pos, mx) if (a[i] + b[i] == 1) tmp.pb(i);
          while (top && sta[top] <= mx)
            top--;
          pos = mx;
        } else {
          rep(i, pos, mx) if (a[i] + b[i] == 1) tmp.pb(i);
          while (top && sta[top] <= mx)
            top--;
          break;
        }
      }
    }
    per(i, SZ(tmp) - 1, 0) sta[++top] = tmp[i];
  }
  else if (a[i] || b[i]) sta[++top] = i;
  per(i, n + k + 2, 1) if (a[i]) {
    n = i;
    break;
  }
  per(i, m + k + 2, 1) if (b[i]) {
    m = i;
    break;
  }
  per(i, n, 1) printf("%d", a[i]);
  puts("");
  per(i, m, 1) printf("%d", b[i]);
  puts("");
  return 0;
}