Problem p02557 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <float.h>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;

using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

ll ll_gcd(ll a, ll b) {
  if (a < b)
    return ll_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

struct UnionFind {
  vector<ll> par;
  vector<ll> siz;
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return siz[root(x)]; }
};

long long modpow(long long a, long long n, long long mod) {
  if (n < 0)
    return 0;
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

ll merge_cnt(vector<ll> &a) {
  int n = a.size();
  if (n <= 1) {
    return 0;
  }

  ll cnt = 0;
  vector<ll> b(a.begin(), a.begin() + n / 2);
  vector<ll> c(a.begin() + n / 2, a.end());

  cnt += merge_cnt(b);
  cnt += merge_cnt(c);

  int ai = 0, bi = 0, ci = 0;
  while (ai < n) {
    if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
      a[ai++] = b[bi++];
    } else {
      cnt += n / 2 - bi;
      a[ai++] = c[ci++];
    }
  }
  return cnt;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> z(n);
  vector<ll> x(n);
  vector<ll> a(n + 1);
  vector<ll> b(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> z[i];
    a[z[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    b[x[i]]++;
  }
  ll p = 0;
  ll q = n + 1;
  ll f = 0;
  ll g = 0;
  for (int i = 0; i < n + 1; i++) {
    f += a[i] + b[i];
    if (f > n)
      break;
    p++;
  }
  for (int i = n; i >= 0; i--) {
    g += a[i] + b[i];
    if (g > n)
      break;
    q--;
  }
  vector<pair<int, int>> s(3);
  s[0].second = 0;
  s[1].second = 1;
  s[2].second = 2;

  for (int i = 0; i < p; i++) {
    s[0].first += a[i];
  }
  for (int i = p; i < q; i++) {
    s[1].first += a[i];
  }
  for (int i = q; i < n + 1; i++) {
    s[2].first += a[i];
  }
  ll ff = s[0].first;
  ll gg = ff + s[1].first;
  vector<pair<int, int>> t(3);
  t[0].second = 0;
  t[1].second = 1;
  t[2].second = 2;
  for (int i = 0; i < p; i++) {
    t[0].first += b[i];
  }
  for (int i = p; i < q; i++) {
    t[1].first += b[i];
  }
  for (int i = q; i < n + 1; i++) {
    t[2].first += b[i];
  }
  ll fff = t[0].first;
  ll ggg = fff + t[1].first;
  if (t[1].first + s[1].first > n)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    vector<ll> ans(n);
    ll fi = 0;
    ll se = 0;
    if (false) {
      fi = t[2].second;
      se = (t[2].second + 1) % 3;
    } else {
      vector<int> fs = {1, 2, 3};
      while (true) {
        fi = fs[0] - 1;
        se = fs[1] - 1;

        if (t[fi].first >= s[se].first && s[fi].first >= t[fs[2] - 1].first)
          break;
        next_permutation(fs.begin(), fs.end());
      }
    }
    ll th = 3 - fi - se;
    vector<ll> d = {0, fff, ggg, n};
    vector<ll> t = {0, ff, gg, n};

    for (int i = d[fi]; i < d[fi + 1]; i++) {
      ans[i - d[fi]] = x[i];
    }
    for (int i = d[se]; i < d[se + 1]; i++) {
      ans[i - d[se] + d[fi + 1] - d[fi]] = x[i];
    }
    for (int i = d[th]; i < d[th + 1]; i++) {
      ans[i - d[th] + d[se + 1] - d[se] + d[fi + 1] - d[fi]] = x[i];
    }
    vector<ll> anss(n);
    d = t;
    for (int i = d[se]; i < d[se + 1]; i++) {
      anss[i] = ans[i - d[se]];
    }
    for (int i = d[th]; i < d[th + 1]; i++) {
      anss[i] = ans[i - d[th] + d[se + 1] - d[se]];
    }
    for (int i = d[fi]; i < d[fi + 1]; i++) {
      anss[i] = ans[i - d[fi] + d[th + 1] - d[th] + d[se + 1] - d[se]];
    }
    for (int i = 0; i < n; i++) {
      cout << anss[i] << " ";
    }
    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <deque>
#include <float.h>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;

using ll = long long;
unsigned euclidean_gcd(unsigned a, unsigned b) {
  if (a < b)
    return euclidean_gcd(b, a);
  unsigned r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

ll ll_gcd(ll a, ll b) {
  if (a < b)
    return ll_gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

struct UnionFind {
  vector<ll> par;
  vector<ll> siz;
  UnionFind(ll sz_) : par(sz_), siz(sz_, 1LL) {
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  void init(ll sz_) {
    par.resize(sz_);
    siz.assign(sz_, 1LL);
    for (ll i = 0; i < sz_; ++i)
      par[i] = i;
  }
  ll root(ll x) {
    while (par[x] != x) {
      x = par[x] = par[par[x]];
    }
    return x;
  }
  bool merge(ll x, ll y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (siz[x] < siz[y])
      swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(ll x, ll y) { return root(x) == root(y); }

  ll size(ll x) { return siz[root(x)]; }
};

long long modpow(long long a, long long n, long long mod) {
  if (n < 0)
    return 0;
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }

ll merge_cnt(vector<ll> &a) {
  int n = a.size();
  if (n <= 1) {
    return 0;
  }

  ll cnt = 0;
  vector<ll> b(a.begin(), a.begin() + n / 2);
  vector<ll> c(a.begin() + n / 2, a.end());

  cnt += merge_cnt(b);
  cnt += merge_cnt(c);

  int ai = 0, bi = 0, ci = 0;
  while (ai < n) {
    if (bi < b.size() && (ci == c.size() || b[bi] <= c[ci])) {
      a[ai++] = b[bi++];
    } else {
      cnt += n / 2 - bi;
      a[ai++] = c[ci++];
    }
  }
  return cnt;
}

int main() {
  ll n;
  cin >> n;
  vector<ll> z(n);
  vector<ll> x(n);
  vector<ll> a(n + 1);
  vector<ll> b(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> z[i];
    a[z[i]]++;
  }
  for (int i = 0; i < n; i++) {
    cin >> x[i];
    b[x[i]]++;
  }
  ll p = 0;
  ll q = n + 1;
  ll f = 0;
  ll g = 0;
  for (int i = 0; i < n + 1; i++) {
    f += a[i] + b[i];
    if (f > n)
      break;
    p++;
  }
  for (int i = n; i >= 0; i--) {
    g += a[i] + b[i];
    if (g > n)
      break;
    q--;
  }
  vector<pair<int, int>> s(3);
  s[0].second = 0;
  s[1].second = 1;
  s[2].second = 2;

  for (int i = 0; i < p; i++) {
    s[0].first += a[i];
  }
  for (int i = p; i < q; i++) {
    s[1].first += a[i];
  }
  for (int i = q; i < n + 1; i++) {
    s[2].first += a[i];
  }
  ll ff = s[0].first;
  ll gg = ff + s[1].first;
  vector<pair<int, int>> t(3);
  t[0].second = 0;
  t[1].second = 1;
  t[2].second = 2;
  for (int i = 0; i < p; i++) {
    t[0].first += b[i];
  }
  for (int i = p; i < q; i++) {
    t[1].first += b[i];
  }
  for (int i = q; i < n + 1; i++) {
    t[2].first += b[i];
  }
  ll fff = t[0].first;
  ll ggg = fff + t[1].first;
  if (t[1].first + s[1].first > n)
    cout << "No" << endl;
  else {
    cout << "Yes" << endl;
    vector<ll> ans(n);
    ll fi = 0;
    ll se = 0;
    if (false) {
      fi = t[2].second;
      se = (t[2].second + 1) % 3;
    } else {
      vector<int> fs = {1, 2, 3};
      while (true) {
        fi = fs[0] - 1;
        se = fs[1] - 1;

        if (t[fi].first >= s[se].first && s[fi].first >= t[fs[2] - 1].first)
          break;
      }
      next_permutation(fs.begin(), fs.end());
    }
    ll th = 3 - fi - se;
    vector<ll> d = {0, fff, ggg, n};
    vector<ll> t = {0, ff, gg, n};

    for (int i = d[fi]; i < d[fi + 1]; i++) {
      ans[i - d[fi]] = x[i];
    }
    for (int i = d[se]; i < d[se + 1]; i++) {
      ans[i - d[se] + d[fi + 1] - d[fi]] = x[i];
    }
    for (int i = d[th]; i < d[th + 1]; i++) {
      ans[i - d[th] + d[se + 1] - d[se] + d[fi + 1] - d[fi]] = x[i];
    }
    vector<ll> anss(n);
    d = t;
    for (int i = d[se]; i < d[se + 1]; i++) {
      anss[i] = ans[i - d[se]];
    }
    for (int i = d[th]; i < d[th + 1]; i++) {
      anss[i] = ans[i - d[th] + d[se + 1] - d[se]];
    }
    for (int i = d[fi]; i < d[fi + 1]; i++) {
      anss[i] = ans[i - d[fi] + d[th + 1] - d[th] + d[se + 1] - d[se]];
    }
    for (int i = 0; i < n; i++) {
      cout << anss[i] << " ";
    }
    cout << endl;
  }
}