Problem p02557 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
#include <algorithm>
#include <assert.h>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
using lld = long long int;
using pll = pair<lld, lld>;
using lf = double;
using pff = pair<lf, lf>;
using pif = pair<int, lf>;
using pii = pair<int, int>;
using pli = pair<lld, int>;
using pil = pair<int, lld>;
using piii = pair<int, pii>;
#define M 1000000007
int n;
int a[200009];
int s[200009];
int b[200009];
pii c[200009];
int r[200009];
#define I 262144
struct A {
  int m1;
  int m2;
  int i1;
  int i2;
  int type;
  A() {
    m2 = m1 = i2 = i1 = I;
    type = 0;
  }
  A operator+(const A &p) {
    A res;
    res.m1 = m1;
    res.m2 = m2;
    res.i1 = i1;
    res.i2 = i2;
    if (res.m2 > p.m1) {
      res.m2 = p.m1;
      res.i2 = p.i1;
      if (res.m1 > res.m2) {
        swap(res.m1, res.m2);
        swap(res.i1, res.i2);
      }
    }
    if (res.m2 > p.m2) {
      res.m2 = p.m2;
      res.i2 = p.i2;
    }
    return res;
  }
};
A ma[2 * I];
A mb[2 * I];
void updatema(int i) {
  i += I;
  if (a[i - I] > 0) {
    ma[i].m1 = n - a[i - I] - b[i - I];
    ma[i].i1 = i - I;
  } else {
    ma[i] = A();
  }
  if (b[i - I] > 0) {
    mb[i].m1 = n - a[i - I] - b[i - I];
    mb[i].i1 = i - I;
  } else {
    mb[i] = A();
  }
  i >>= 1;
  while (i > 0) {
    ma[i] = ma[i * 2] + ma[i * 2 + 1];
    mb[i] = mb[i * 2] + mb[i * 2 + 1];
    i >>= 1;
  }
}
int main() {
  int i, j, k, l;
  int t = 1, tv = 0;
  // scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      scanf("%d", &j);
      a[j - 1]++;
    }
    for (i = 0; i < n; i++) {
      s[i + 1] = s[i] + a[i];
    }
    for (i = 0; i < n; i++) {
      scanf("%d", &j);
      b[j - 1]++;
    }
    bool valid = true;
    for (i = 0; i < n; i++) {
      if (a[i] > n - b[i]) {
        valid = false;
        break;
      }
    }
    if (!valid) {
      printf("No\\n");
    } else {
      printf("Yes\\n");
      for (i = 0; i < n; i++) {
        if (a[i] > 0) {
          ma[i + I].m1 = n - a[i] - b[i];
          ma[i + I].i1 = i;
        } else {
          ma[i + I] = A();
        }
        if (b[i] > 0) {
          mb[i + I].m1 = n - a[i] - b[i];
          mb[i + I].i1 = i;
        } else {
          mb[i + I] = A();
        }
      }
      for (; i < I; i++) {
        ma[i + I] = A();
        mb[i + I] = A();
      }
      for (i = I - 1; i > 0; i--) {
        ma[i] = ma[i * 2] + ma[i * 2 + 1];
        mb[i] = mb[i * 2] + mb[i * 2 + 1];
      }
      int ccnt = n;
      queue<int> que;
      while (ccnt--) {
        int mi = ma[1].i1;
        int ei = mb[1].i1;
        if (mi == ei) {
          ei = mb[1].i2;
        }
        // printf("%d %d  %d %d  %d %d\\n", mi, ei, a[mi], b[mi], a[ei], b[ei]);
        r[s[mi]++] = ei;
        a[mi]--;
        b[ei]--;
        updatema(mi);
        updatema(ei);
      }
      for (i = 0; i < n; i++) {
        printf("%d ", r[i] + 1);
      }
      printf("\\n");
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2")
#include <algorithm>
#include <assert.h>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unordered_map>
#include <vector>
using namespace std;
using lld = long long int;
using pll = pair<lld, lld>;
using lf = double;
using pff = pair<lf, lf>;
using pif = pair<int, lf>;
using pii = pair<int, int>;
using pli = pair<lld, int>;
using pil = pair<int, lld>;
using piii = pair<int, pii>;
#define M 1000000007
int n;
int a[200009];
int s[200009];
int b[200009];
pii c[200009];
int r[200009];
int main() {
  int i, j, k, l;
  int t = 1, tv = 0;
  // scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      scanf("%d", &j);
      a[j - 1]++;
    }
    for (i = 0; i < n; i++) {
      s[i + 1] = s[i] + a[i];
    }
    for (i = 0; i < n; i++) {
      scanf("%d", &j);
      b[j - 1]++;
    }
    bool valid = true;
    for (i = 0; i < n; i++) {
      if (a[i] > n - b[i]) {
        valid = false;
        break;
      }
    }
    if (!valid) {
      printf("No\\n");
    } else {
      printf("Yes\\n");
      for (i = 0; i < n; i++) {
        c[i] = (pii(n - b[i] - a[i], i));
      }
      sort(c, c + n);
      int zi = 0;
      int zj = 1;
      for (i = 0; i < n; i++) {
        int ci = c[i].second;
        if (zj == i)
          zj = i + 1;
        while (a[ci] > 0) {
          a[ci]--;
          if (zi == i) {
            for (; b[c[zj].second] == 0; zj++)
              ;
            b[c[zj].second]--;
            r[s[ci]++] = c[zj].second;
          } else {
            for (; b[c[zi].second] == 0; zi++)
              ;
            b[c[zi].second]--;
            r[s[ci]++] = c[zi].second;
          }
        }
      }
      for (i = 0; i < n; i++) {
        printf("%d ", r[i] + 1);
      }
      printf("\\n");
    }
  }
  return 0;
}