結果

問題 No.2242 Cities and Teleporters
ユーザー 👑 hos.lyrichos.lyric
提出日時 2023-03-10 22:23:10
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 567 ms / 3,000 ms
コード長 3,194 bytes
コンパイル時間 2,324 ms
コンパイル使用メモリ 113,936 KB
実行使用メモリ 28,916 KB
最終ジャッジ日時 2023-09-04 20:00:27
合計ジャッジ時間 15,195 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 459 ms
27,484 KB
testcase_06 AC 434 ms
26,740 KB
testcase_07 AC 464 ms
26,672 KB
testcase_08 AC 567 ms
26,600 KB
testcase_09 AC 451 ms
26,616 KB
testcase_10 AC 369 ms
27,276 KB
testcase_11 AC 410 ms
27,980 KB
testcase_12 AC 421 ms
26,908 KB
testcase_13 AC 410 ms
26,628 KB
testcase_14 AC 435 ms
26,660 KB
testcase_15 AC 410 ms
26,744 KB
testcase_16 AC 448 ms
26,680 KB
testcase_17 AC 503 ms
26,692 KB
testcase_18 AC 402 ms
27,584 KB
testcase_19 AC 501 ms
28,916 KB
testcase_20 AC 403 ms
25,984 KB
testcase_21 AC 401 ms
25,852 KB
testcase_22 AC 485 ms
26,432 KB
testcase_23 AC 464 ms
26,724 KB
testcase_24 AC 417 ms
26,708 KB
testcase_25 AC 372 ms
26,688 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; }

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)); }


enum E = 18;

void main() {
  try {
    for (; ; ) {
      const N = readInt;
      auto H = new int[N]; foreach (i; 0 .. N) H[i] = readInt;
      auto T = new int[N]; foreach (i; 0 .. N) T[i] = readInt;
      const Q = readInt;
      auto A = new int[Q];
      auto B = new int[Q];
      foreach (q; 0 .. Q) {
        A[q] = readInt - 1;
        B[q] = readInt - 1;
      }
      
      auto js = iota(N).array;
      js.sort!((i, j) => (H[i] < H[j]));
      H = iota(N).map!(u => H[js[u]]).array;
      T = iota(N).map!(u => T[js[u]]).array;
      auto tr = new int[N];
      foreach (u; 0 .. N) {
        tr[js[u]] = u;
      }
      foreach (q; 0 .. Q) {
        A[q] = tr[A[q]];
        B[q] = tr[B[q]];
      }
      debug {
        writeln("H = ", H);
        writeln("T = ", T);
      }
      
      auto once = new int[N];
      foreach (u; 0 .. N) {
        once[u] = H.upperBound(T[u]) - 1;
      }
      auto mxs = once.dup;
      foreach (u; 1 .. N) {
        chmax(mxs[u], mxs[u - 1]);
      }
      debug {
        writeln("once = ", once);
        writeln("mxs = ", mxs);
      }
      
      auto f = new int[][](E, N);
      f[0][] = mxs[];
      foreach (e; 0 .. E - 1) {
        foreach (u; 0 .. N) {
          f[e + 1][u] = (~f[e][u]) ? f[e][f[e][u]] : -1;
        }
      }
      
      foreach (q; 0 .. Q) {
        int ans = 0;
        int u = A[q];
        if (u != B[q]) {
          ans += 1;
          u = once[u];
          // [0, u]
          if (~u) {
            if (u < B[q]) {
              foreach_reverse (e; 0 .. E) {
                if (~u && f[e][u] < B[q]) {
                  ans += (1 << e);
                  u = f[e][u];
                }
              }
              if (~u) {
                ans += 1;
                u = f[0][u];
                if (u < B[q]) {
                  ans = -1;
                }
              } else {
                ans = -1;
              }
            }
          } else {
            ans = -1;
          }
        }
        writeln(ans);
      }
    }
  } catch (EOFException e) {
  }
}
0