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

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define f1 first
#define s2 second

#define fastio                                                                 \\
  ios ::sync_with_stdio(0);                                                    \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(x...) cerr << "[" << #x << "]: " << x << "\\n";

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pl;

ld const PI = 4 * atan((ld)1);

int const N = 2e5 + 7;

int a[N], b[N];
ii range[N] = {};

int bit[N] = {};
void add(int x, int val) {
  while (x < N)
    bit[x] += val, x += (x & -x);
}

int get(int x) {
  int res = 0;
  while (x)
    res += bit[x], x -= (x & -x);
  return res;
}

int main() {
  fastio;

  int n;
  cin >> n;

  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 1; i <= n; ++i)
    cin >> b[i];

  bool valid = true;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i])
      valid = false;
  }

  if (valid) {
    cout << "Yes\\n";
    cout << b[1];
    for (int i = 2; i <= n; ++i)
      cout << " " << b[i];
    cout << '\\n';

    return 0;
  }

  range[a[1]] = {1, 1};
  for (int i = 2; i <= n; ++i) {
    if (a[i] == a[i - 1])
      range[a[i]].s2 = i;
    else
      range[a[i]] = {i, i};
  }

  // cout << range[1].f1 << " " << range[1].s2 << '\\n';
  // cout << range[2].f1 << " " << range[2].s2 << '\\n';
  // cout << range[3].f1 << " " << range[3].s2 << '\\n';

  for (int i = 1; i <= n; ++i) {
    if (range[b[i]].f1 == 0) {
      continue;
    } else {
      int lt = range[b[i]].f1, rt = range[b[i]].s2;
      if (lt <= i && i <= rt) {
        add(1, 1);
        add(max(1, rt - i + 1), -1);

        add(max(1, n - i + lt), 1);
      } else if (rt <= i) {
        add(max(1, n - i + lt), 1);
        add(max(1, n - i + rt + 1), -1);
      } else {
        add(max(1, lt - i), 1);
        add(max(1, rt - i + 1), -1);
      }
    }
  }

  int shift = -1;
  for (int i = 1; i < n; ++i) {
    if (get(i) == 0) {
      shift = i;
      break;
    }
  }

  debug(shift);

  if (shift == -1)
    cout << "No\\n";
  else {
    cout << "Yes\\n";
    for (int i = 1; i <= n; ++i) {
      int idx = (i + n - shift) % n;
      if (idx == 0)
        idx = n;

      if (i > 1)
        cout << " ";
      cout << b[idx];
    }
    cout << '\\n';
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define f1 first
#define s2 second

#define fastio                                                                 \\
  ios ::sync_with_stdio(0);                                                    \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug(x...) cerr << "[" << #x << "]: " << x << "\\n";

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pl;

ld const PI = 4 * atan((ld)1);

int const N = 2e5 + 7;

int fqA[N] = {}, fqB[N] = {};

int ar[N];
int res[N];
queue<int> used;
queue<int> unused;

int main() {
  fastio;

  int n;
  cin >> n;

  set<int> st;
  for (int i = 1; i <= n; ++i)
    cin >> ar[i], st.insert(ar[i]), fqA[ar[i]]++;

  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    fqB[x]++;
    if (st.count(x))
      used.push(x);
    else
      unused.push(x);
  }

  for (int i = 1; i <= n; ++i) {
    if (fqA[i] + fqB[i] > n) {
      cout << "No\\n";
      return 0;
    }
  }

  int lt = 1;
  while (lt <= n) {
    if (used.empty()) {
      res[lt++] = unused.front();
      unused.pop();
    } else {
      if (used.front() < ar[lt]) {
        unused.push(used.front());
        used.pop();
      } else if (used.front() == ar[lt]) {
        int number = used.front();
        int fq = 0;
        while (!used.empty() && used.front() == number)
          fq++, used.pop();

        while (lt <= n && ar[lt] == number) {
          if (!used.empty())
            res[lt++] = used.front(), used.pop();
          else if (!unused.empty())
            res[lt++] = unused.front(), unused.pop();
        }

        for (int i = 0; i < fq; ++i)
          unused.push(number);
      } else {
        res[lt++] = used.front();
        used.pop();
      }
    }
  }

  cout << "Yes\\n";
  for (int i = 1; i <= n; ++i) {
    if (i > 1)
      cout << " ";
    cout << res[i];
  }
  cout << '\\n';

  return 0;
}