結果

問題 No.1698 Face to Face
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-12-28 02:49:38
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 716 ms / 5,000 ms
コード長 4,898 bytes
コンパイル時間 5,629 ms
コンパイル使用メモリ 107,360 KB
実行使用メモリ 55,496 KB
最終ジャッジ日時 2023-09-04 15:36:38
合計ジャッジ時間 22,676 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 353 ms
48,888 KB
testcase_04 AC 387 ms
53,640 KB
testcase_05 AC 545 ms
52,144 KB
testcase_06 AC 530 ms
52,908 KB
testcase_07 AC 505 ms
54,656 KB
testcase_08 AC 511 ms
54,704 KB
testcase_09 AC 519 ms
54,852 KB
testcase_10 AC 397 ms
54,288 KB
testcase_11 AC 626 ms
53,128 KB
testcase_12 AC 716 ms
52,896 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 663 ms
48,636 KB
testcase_16 AC 640 ms
48,764 KB
testcase_17 AC 655 ms
48,168 KB
testcase_18 AC 567 ms
47,588 KB
testcase_19 AC 533 ms
44,196 KB
testcase_20 AC 513 ms
31,940 KB
testcase_21 AC 242 ms
29,924 KB
testcase_22 AC 181 ms
16,364 KB
testcase_23 AC 40 ms
7,648 KB
testcase_24 AC 286 ms
31,452 KB
testcase_25 AC 565 ms
48,684 KB
testcase_26 AC 23 ms
7,184 KB
testcase_27 AC 121 ms
16,164 KB
testcase_28 AC 137 ms
16,036 KB
testcase_29 AC 95 ms
14,776 KB
testcase_30 AC 342 ms
31,148 KB
testcase_31 AC 250 ms
26,592 KB
testcase_32 AC 449 ms
30,876 KB
testcase_33 AC 453 ms
29,996 KB
testcase_34 AC 626 ms
49,204 KB
testcase_35 AC 1 ms
4,376 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
testcase_40 AC 1 ms
4,376 KB
testcase_41 AC 1 ms
4,376 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 1 ms
4,380 KB
testcase_44 AC 2 ms
4,380 KB
testcase_45 AC 451 ms
52,520 KB
testcase_46 AC 450 ms
50,824 KB
testcase_47 AC 572 ms
55,496 KB
権限があれば一括ダウンロードができます

ソースコード

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