Problem p02867 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
inline ll read() {
  ll x = 0;
  char ch = getchar();
  bool f = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = 1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return f ? -x : x;
}
void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(ll x) {
  write(x);
  puts("");
}
void writep(ll x) {
  write(x);
  putchar(' ');
}
const int N = 1e5 + 5;
struct node {
  int id, val;
  bool operator<(const node &b) const {
    if (val == b.val)
      return id < b.id;
    return val < b.val;
  }
} b[N], d[N];
int n, p[N], tot, tr[N];
void upt(int x) {
  while (x <= n)
    tr[x]++, x += (x & -x);
}
int query(int x) {
  int res = 0;
  while (x)
    res += tr[x], x -= (x & -x);
  return res;
}
ll calc(int *a) {
  memset(tr, 0, sizeof(tr));
  ll ans = 0;
  for (int i = 1; i <= n; ++i)
    b[i].id = i, b[i].val = a[i];
  sort(b + 1, b + n + 1);
  tot = 1;
  p[b[1].id] = 1;
  for (int i = 2; i <= n; ++i)
    if (b[i].val == b[i - 1].val)
      p[b[i].id] = tot;
    else
      p[b[i].id] = ++tot;
  for (int i = n; i >= 1; --i) {
    ans += query(p[i] - 1);
    upt(p[i]);
  }
  return ans;
}
int a[N], c[N], pos[N];
signed main() {
  freopen("in06.txt", "r", stdin);
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), b[i].id = i, b[i].val = a[i];
  for (int i = 1; i <= n; ++i)
    c[i] = read(), d[i].id = i, d[i].val = c[i];
  sort(b + 1, b + n + 1);
  sort(d + 1, d + n + 1);
  for (int i = 1; i <= n; ++i)
    if (b[i].val > d[i].val)
      return puts("No"), 0;
  //	for(int i=1;i<=n;++i,putchar(' '))
  //		cout<<b[i].id;puts("");
  //	for(int i=1;i<=n;++i,putchar(' '))
  //		cout<<d[i].id;puts("");
  for (int i = 1; i <= n; ++i)
    p[b[i].id] = d[i].id;
  //	for(int i=1;i<=n;++i)
  //		cout<<p[i]<<" ";cout<<endl;
  int tot = 0;
  for (int i = 1; i <= n; ++i)
    pos[p[i]] = i;
  for (int i = n; i >= 1; --i) {
    if (p[i] != i) {
      int tmp = pos[i];
      // cout<<p[pos[i]]<<" "<<p[i]<<endl;
      swap(p[pos[i]], p[i]);
      pos[p[pos[i]]] = tmp;
      ++tot;
    }
  }
  //	cout<<tot<<endl;
  if (tot <= n - 2)
    puts("Yes");
  else {
    for (int i = 1; i < n; ++i)
      if (b[i + 1].val <= d[i].val)
        return puts("Yes"), 0;
    puts("No");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
inline ll read() {
  ll x = 0;
  char ch = getchar();
  bool f = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = 1;
  for (; isdigit(ch); ch = getchar())
    x = x * 10 + ch - '0';
  return f ? -x : x;
}
void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(ll x) {
  write(x);
  puts("");
}
void writep(ll x) {
  write(x);
  putchar(' ');
}
const int N = 1e5 + 5;
struct node {
  int id, val;
  bool operator<(const node &b) const {
    if (val == b.val)
      return id < b.id;
    return val < b.val;
  }
} b[N], d[N];
int n, p[N], tot, tr[N];
void upt(int x) {
  while (x <= n)
    tr[x]++, x += (x & -x);
}
int query(int x) {
  int res = 0;
  while (x)
    res += tr[x], x -= (x & -x);
  return res;
}
ll calc(int *a) {
  memset(tr, 0, sizeof(tr));
  ll ans = 0;
  for (int i = 1; i <= n; ++i)
    b[i].id = i, b[i].val = a[i];
  sort(b + 1, b + n + 1);
  tot = 1;
  p[b[1].id] = 1;
  for (int i = 2; i <= n; ++i)
    if (b[i].val == b[i - 1].val)
      p[b[i].id] = tot;
    else
      p[b[i].id] = ++tot;
  for (int i = n; i >= 1; --i) {
    ans += query(p[i] - 1);
    upt(p[i]);
  }
  return ans;
}
int a[N], c[N], pos[N];
signed main() {
  //	freopen("in06.txt","r",stdin);
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read(), b[i].id = i, b[i].val = a[i];
  for (int i = 1; i <= n; ++i)
    c[i] = read(), d[i].id = i, d[i].val = c[i];
  sort(b + 1, b + n + 1);
  sort(d + 1, d + n + 1);
  for (int i = 1; i <= n; ++i)
    if (b[i].val > d[i].val)
      return puts("No"), 0;
  //	for(int i=1;i<=n;++i,putchar(' '))
  //		cout<<b[i].id;puts("");
  //	for(int i=1;i<=n;++i,putchar(' '))
  //		cout<<d[i].id;puts("");
  for (int i = 1; i <= n; ++i)
    p[b[i].id] = d[i].id;
  //	for(int i=1;i<=n;++i)
  //		cout<<p[i]<<" ";cout<<endl;
  int tot = 0;
  for (int i = 1; i <= n; ++i)
    pos[p[i]] = i;
  for (int i = n; i >= 1; --i) {
    if (p[i] != i) {
      int tmp = pos[i];
      // cout<<p[pos[i]]<<" "<<p[i]<<endl;
      swap(p[pos[i]], p[i]);
      pos[p[pos[i]]] = tmp;
      ++tot;
    }
  }
  //	cout<<tot<<endl;
  if (tot <= n - 2)
    puts("Yes");
  else {
    for (int i = 1; i < n; ++i)
      if (b[i + 1].val <= d[i].val)
        return puts("Yes"), 0;
    puts("No");
  }
  return 0;
}