Problem p03336 (C++)

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

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

// const int MD = 2 * TEN(6) + 100;
const int MD = 30;
using P = pair<int, int>; // time, value

int K;
int d[MD];

V<P> res;
V<int> st;
void dfs(int l, int r, int p, P nw) {
  while (st.size() && st.back() <= p)
    st.pop_back();
  if (p == r) {
    res.push_back(nw);
    return;
  }
  int nx = d[p] & nw.second;
  d[p] ^= nw.second;
  if (nx) {
    dfs(l, r, p + 1, P(nw.first, nx));
  }
  if (d[p] == 3 && nw.first != K - 1) {
    int np = (st.size() ? st.back() : r) - p;
    int nt = K - nw.first - 1;
    int mi = min(np, nt);
    //        cout << "MI: " << np << " " << nt << " " << mi << endl;
    assert(mi > 0);
    //        int mi = 1;
    d[p] = 0;
    dfs(l, r, p + mi, P(nw.first + mi, 3));

    // d[p] = 0;
    // dfs(l, r, p+1, P(nw.first+1, 3));
  }
  if (d[p])
    st.push_back(p);
}

V<P> single(int l, int r, const V<P> &v) {
  for (int i = r - 1; i >= l; i--) {
    if (d[i])
      st.push_back(i);
  }
  res.clear();
  for (P nw : v) {
    dfs(l, r, l, nw);
  }
  return res;
}

V<P> solve(int l, int r) {
  if (r - l == 1) {
    if (d[l] == 3) {
      d[l] = 0;
      return {P(0, 3)};
    }
    return {};
  }
  int md = (r + l) / 2;
  auto v1 = solve(l, md);
  auto v2 = solve(md, r);
  auto v3 = single(md, r, v1);
  for (auto p : v3)
    v2.push_back(p);

  /*    cout << l << " " << r << " DEBUG: ";
      for (auto p: v2) {
          cout << "(" << p.first << ", " << p.second << "), ";
      } cout << endl;*/

  return v2;
}

string norm(string s) {
  while (s.back() == '0')
    s.pop_back();
  reverse(begin(s), end(s));
  return s;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m >> K;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    d[n - 1 - i] |= 1;
  }
  cin >> s;
  for (int i = 0; i < m; i++) {
    if (s[i] == '0')
      continue;
    d[m - 1 - i] |= 2;
  }
  solve(0, MD);
  {
    string s, t;
    for (int i = 0; i < MD; i++) {
      if (d[i] & 1)
        s += "1";
      else
        s += "0";
    }
    for (int i = 0; i < MD; i++) {
      if (d[i] & 2)
        t += "1";
      else
        t += "0";
    }
    cout << norm(s) << endl << norm(t) << endl;
  }
  return 0;
}


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

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const int MD = 2 * TEN(6) + 100;
// const int MD = 30;
using P = pair<int, int>; // time, value

int K;
int d[MD];

V<P> res;
V<int> st;
void dfs(int l, int r, int p, P nw) {
  while (st.size() && st.back() <= p)
    st.pop_back();
  if (p == r) {
    res.push_back(nw);
    return;
  }
  int nx = d[p] & nw.second;
  d[p] ^= nw.second;
  if (nx) {
    dfs(l, r, p + 1, P(nw.first, nx));
  }
  if (d[p] == 3 && nw.first != K - 1) {
    int np = (st.size() ? st.back() : r) - p;
    int nt = K - nw.first - 1;
    int mi = min(np, nt);
    //        cout << "MI: " << np << " " << nt << " " << mi << endl;
    assert(mi > 0);
    //        int mi = 1;
    d[p] = 0;
    dfs(l, r, p + mi, P(nw.first + mi, 3));

    // d[p] = 0;
    // dfs(l, r, p+1, P(nw.first+1, 3));
  }
  if (d[p])
    st.push_back(p);
}

V<P> single(int l, int r, const V<P> &v) {
  for (int i = r - 1; i >= l; i--) {
    if (d[i])
      st.push_back(i);
  }
  res.clear();
  for (P nw : v) {
    dfs(l, r, l, nw);
  }
  return res;
}

V<P> solve(int l, int r) {
  if (r - l == 1) {
    if (d[l] == 3) {
      d[l] = 0;
      return {P(0, 3)};
    }
    return {};
  }
  int md = (r + l) / 2;
  auto v1 = solve(l, md);
  auto v2 = solve(md, r);
  auto v3 = single(md, r, v1);
  for (auto p : v3)
    v2.push_back(p);

  /*    cout << l << " " << r << " DEBUG: ";
      for (auto p: v2) {
          cout << "(" << p.first << ", " << p.second << "), ";
      } cout << endl;*/

  return v2;
}

string norm(string s) {
  while (s.back() == '0')
    s.pop_back();
  reverse(begin(s), end(s));
  return s;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m >> K;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0')
      continue;
    d[n - 1 - i] |= 1;
  }
  cin >> s;
  for (int i = 0; i < m; i++) {
    if (s[i] == '0')
      continue;
    d[m - 1 - i] |= 2;
  }
  solve(0, MD);
  {
    string s, t;
    for (int i = 0; i < MD; i++) {
      if (d[i] & 1)
        s += "1";
      else
        s += "0";
    }
    for (int i = 0; i < MD; i++) {
      if (d[i] & 2)
        t += "1";
      else
        t += "0";
    }
    cout << norm(s) << endl << norm(t) << endl;
  }
  return 0;
}