結果

問題 No.1698 Face to Face
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-12-28 02:49:38
言語 D
(dmd 2.106.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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 303 ms
47,936 KB
testcase_04 AC 343 ms
52,964 KB
testcase_05 AC 453 ms
51,168 KB
testcase_06 AC 449 ms
51,532 KB
testcase_07 AC 434 ms
53,944 KB
testcase_08 AC 448 ms
54,028 KB
testcase_09 AC 435 ms
54,160 KB
testcase_10 AC 347 ms
53,636 KB
testcase_11 AC 530 ms
52,188 KB
testcase_12 AC 595 ms
51,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 548 ms
47,556 KB
testcase_16 AC 536 ms
47,884 KB
testcase_17 AC 541 ms
47,704 KB
testcase_18 AC 467 ms
47,448 KB
testcase_19 AC 641 ms
42,952 KB
testcase_20 AC 427 ms
31,592 KB
testcase_21 AC 208 ms
29,312 KB
testcase_22 AC 156 ms
15,344 KB
testcase_23 AC 33 ms
6,940 KB
testcase_24 AC 243 ms
29,124 KB
testcase_25 AC 483 ms
48,112 KB
testcase_26 AC 19 ms
6,944 KB
testcase_27 AC 103 ms
15,036 KB
testcase_28 AC 120 ms
15,096 KB
testcase_29 AC 86 ms
14,224 KB
testcase_30 AC 284 ms
29,844 KB
testcase_31 AC 208 ms
25,984 KB
testcase_32 AC 372 ms
29,104 KB
testcase_33 AC 374 ms
29,028 KB
testcase_34 AC 500 ms
47,824 KB
testcase_35 AC 1 ms
6,940 KB
testcase_36 AC 1 ms
6,944 KB
testcase_37 AC 1 ms
6,940 KB
testcase_38 AC 1 ms
6,940 KB
testcase_39 AC 1 ms
6,940 KB
testcase_40 AC 1 ms
6,944 KB
testcase_41 AC 1 ms
6,940 KB
testcase_42 AC 1 ms
6,940 KB
testcase_43 AC 1 ms
6,940 KB
testcase_44 AC 1 ms
6,940 KB
testcase_45 AC 376 ms
50,952 KB
testcase_46 AC 373 ms
51,876 KB
testcase_47 AC 457 ms
53,604 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