Problem p03336 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC optimize("O3", "Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int k, n, m;

string s;
string t;
set<int> s1;
set<int> t1;

int add(int v, int id) {
  if (id == 0) {
    for (int j = v;; j++) {
      if (s1.find(j) == s1.end()) {
        s1.insert(j);
        return j;
      } else {
        s1.erase(j);
      }
    }
    if (s1.find(v) == s1.end()) {
      s1.insert(v);
      return v;
    }
    s1.erase(v);
    return add(v + 1, 0);
  }
  if (id == 1) {
    for (int j = v;; j++) {
      if (t1.find(j) == t1.end()) {
        t1.insert(j);
        return j;
      } else {
        t1.erase(j);
      }
    }
    if (t1.find(v) == t1.end()) {
      t1.insert(v);
      return v;
    }
    t1.erase(v);
    return add(v + 1, 0);
  }
}

void process(int v, int k) {
  if (k == 0)
    return;
  if (s1.find(v) == s1.end() || t1.find(v) == t1.end())
    return;
  int next1 = *s1.upper_bound(v);
  int next2 = *t1.upper_bound(v);
  int r = min(k, min(next1, next2) - v - 1);
  if (r > 0) {
    s1.erase(v);
    t1.erase(v);
    s1.insert(v + r);
    t1.insert(v + r);
    process(v + r, k - r);
    return;
  }
  if (k == 0)
    return;
  // if(next1 == v+1 && next2 == v+1) assert(false);
  if (next1 == v + 1) {
    int d = add(v, 0);
    add(v, 1);
    process(d, k - 1);
  } else {
    add(v, 0);
    int d = add(v, 1);
    process(d, k - 1);
  }
}
void pr(set<int> s) {
  string g;
  int last = 0;
  for (int x : s) {
    for (int j = last; j < x; j++) {
      g += '0';
    }
    g += '1';
    last = x + 1;
  }
  reverse(g.begin(), g.end());
  cout << g << '\n';
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  cin >> s >> t;
  reverse(s.begin(), s.end());
  reverse(t.begin(), t.end());
  s.resize(1000000, '0');
  t.resize(1000000, '0');
  s1.insert(11000000);
  t1.insert(11000000);
  for (int v = 1000000; v >= 0; v--) {
    if (s[v] == '0' && t[v] == '0')
      continue;
    if (s[v] == '1' && t[v] == '0')
      s1.insert(v);
    if (s[v] == '0' && t[v] == '1')
      t1.insert(v);
    if (s[v] == '1' && t[v] == '1') {
      s1.insert(v);
      t1.insert(v);
      process(v, k);
    }
  }
  s1.erase(11000000);
  t1.erase(11000000);
  pr(s1);
  pr(t1);
}


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int k, n, m;

string s;
string t;
set<int> s1;
set<int> t1;

int add(int v, int id) {
  if (id == 0) {
    for (int j = v;; j++) {
      if (s1.find(j) == s1.end()) {
        s1.insert(j);
        return j;
      } else {
        s1.erase(j);
      }
    }
    if (s1.find(v) == s1.end()) {
      s1.insert(v);
      return v;
    }
    s1.erase(v);
    return add(v + 1, 0);
  }
  if (id == 1) {
    for (int j = v;; j++) {
      if (t1.find(j) == t1.end()) {
        t1.insert(j);
        return j;
      } else {
        t1.erase(j);
      }
    }
    if (t1.find(v) == t1.end()) {
      t1.insert(v);
      return v;
    }
    t1.erase(v);
    return add(v + 1, 0);
  }
}

void process(int v, int k) {
  if (k == 0)
    return;
  if (s1.find(v) == s1.end() || t1.find(v) == t1.end())
    return;
  int next1 = *s1.upper_bound(v);
  int next2 = *t1.upper_bound(v);
  int r = min(k, min(next1, next2) - v - 1);
  if (r > 0) {
    s1.erase(v);
    t1.erase(v);
    s1.insert(v + r);
    t1.insert(v + r);
    process(v + r, k - r);
    return;
  }
  if (k == 0)
    return;
  // if(next1 == v+1 && next2 == v+1) assert(false);
  if (next1 == v + 1) {
    int d = add(v, 0);
    add(v, 1);
    process(d, k - 1);
  } else {
    add(v, 0);
    int d = add(v, 1);
    process(d, k - 1);
  }
}
void pr(set<int> s) {
  string g;
  int last = 0;
  for (int x : s) {
    for (int j = last; j < x; j++) {
      g += '0';
    }
    g += '1';
    last = x + 1;
  }
  reverse(g.begin(), g.end());
  cout << g << '\n';
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> k;
  cin >> s >> t;
  reverse(s.begin(), s.end());
  reverse(t.begin(), t.end());
  s.resize(1000000, '0');
  t.resize(1000000, '0');
  s1.insert(11000000);
  t1.insert(11000000);
  for (int v = 1000000; v >= 0; v--) {
    if (s[v] == '0' && t[v] == '0')
      continue;
    if (s[v] == '1' && t[v] == '0')
      s1.insert(v);
    if (s[v] == '0' && t[v] == '1')
      t1.insert(v);
    if (s[v] == '1' && t[v] == '1') {
      s1.insert(v);
      t1.insert(v);
      process(v, k);
    }
  }
  s1.erase(11000000);
  t1.erase(11000000);
  pr(s1);
  pr(t1);
}