結果

問題 No.1698 Face to Face
ユーザー 👑 hos.lyric
提出日時 2021-12-28 02:49:38
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 641 ms / 5,000 ms
コード長 4,898 bytes
コンパイル時間 967 ms
コンパイル使用メモリ 122,952 KB
実行使用メモリ 54,160 KB
最終ジャッジ日時 2024-06-22 13:57:11
合計ジャッジ時間 16,216 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}


void main() {
  try {
    for (; ; ) {
      const N = readInt;
      auto A = new int[N]; foreach (i; 0 .. N) A[i] = readInt - 1;
      auto B = new int[N]; foreach (i; 0 .. N) B[i] = readInt - 1;
      auto Z = new int[N]; foreach (i; 0 .. N) Z[i] = readInt - 1;
      
      auto posZA = new int[N];
      auto posB = new int[N];
      foreach (u; 0 .. N) {
        posZA[Z[A[u]]] = u;
        posB[B[u]] = u;
      }
      
      // x can meet at meets[x] when numbers <= his[x] are merged
      auto los = new int[N];
      auto his = new int[N];
      los[] = -1;
      his[] = N - 1;
      auto meets = new int[N];
      meets[] = 0;
      {
        auto essA = new int[][N];
        auto essB = new int[][N];
        foreach (u; 0 .. N - 1) {
          essA[max(A[u], A[u + 1])] ~= u;
          essB[max(B[u], B[u + 1])] ~= u;
        }
        auto xss = new int[][N];
        for (; ; ) {
          bool cont;
          foreach (x; 0 .. N) {
            if (los[x] + 1 < his[x]) {
              cont = true;
              const mid = (los[x] + his[x]) / 2;
              xss[mid] ~= x;
            }
          }
          if (!cont) {
            break;
          }
          class Info {
            int[] uf;
            int[] mns, mxs;
            this() {
              uf = new int[N];
              uf[] = -1;
              mns = iota(N).array;
              mxs = iota(N).array;
            }
            void connect(int u, int v) {
              u = uf.root(u);
              v = uf.root(v);
              if (uf[u] > uf[v]) {
                swap(u, v);
              }
              uf[u] += uf[v];
              uf[v] = u;
              chmin(mns[u], mns[v]);
              chmax(mxs[u], mxs[v]);
            }
          }
          auto infoA = new Info;
          auto infoB = new Info;
          foreach (k; 0 .. N) {
            foreach (e; essA[k]) infoA.connect(e, e + 1);
            foreach (e; essB[k]) infoB.connect(e, e + 1);
            foreach (x; xss[k]) {
              const rA = infoA.uf.root(posZA[x]);
              const rB = infoB.uf.root(posB[x]);
              const mn = max(infoA.mns[rA], infoB.mns[rB]);
              const mx = min(infoA.mxs[rA], infoB.mxs[rB]);
              if (mn <= mx) {
                his[x] = k;
                meets[x] = mn;
              } else {
                los[x] = k;
              }
            }
            xss[k] = [];
          }
        }
      }
      debug {
        writeln("his = ", his);
      }
      
      auto uf = new int[N];
      uf[] = -1;
      auto cs = new int[N];
      int ans;
      void connect(int u, int v) {
        u = uf.root(u);
        v = uf.root(v);
        assert(u != v);
        if (uf[u] > uf[v]) {
          swap(u, v);
        }
        ans -= min(cs[u], -uf[u]);
        ans -= min(cs[v], -uf[v]);
        uf[u] += uf[v];
        uf[v] = u;
        cs[u] += cs[v];
        ans += min(cs[u], -uf[u]);
      }
      
      auto ess = new int[][N];
      foreach (u; 0 .. N - 1) {
        ess[max(A[u], A[u + 1], B[u], B[u + 1])] ~= u;
      }
      auto xss = new int[][N];
      foreach (x; 0 .. N) {
        xss[his[x]] ~= x;
      }
      foreach (k; 0 .. N) {
        foreach (e; ess[k]) {
          connect(e, e + 1);
        }
        foreach (x; xss[k]) {
          const r = uf.root(meets[x]);
          ans -= min(cs[r], -uf[r]);
          ++cs[r];
          ans += min(cs[r], -uf[r]);
        }
        debug {
          writeln("uf = ", uf);
          writeln("cs = ", cs);
        }
        writeln(ans);
      }
      debug {
        writeln("========");
      }
    }
  } catch (EOFException e) {
  }
}
0