結果

問題 No.686 Uncertain LIS
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-03-31 09:30:08
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 4,127 bytes
コンパイル時間 1,526 ms
コンパイル使用メモリ 159,140 KB
実行使用メモリ 9,796 KB
最終ジャッジ日時 2024-06-22 06:18:11
合計ジャッジ時間 7,023 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 5 ms
6,944 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 132 ms
6,940 KB
testcase_25 AC 131 ms
6,940 KB
testcase_26 AC 102 ms
9,568 KB
testcase_27 AC 135 ms
6,940 KB
testcase_28 AC 134 ms
6,940 KB
testcase_29 AC 135 ms
6,940 KB
testcase_30 AC 136 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 122 ms
9,736 KB
testcase_33 AC 100 ms
9,764 KB
testcase_34 WA -
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

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

uint xrand() {
  static uint x = 314159265, y = 358979323, z = 846264338, w = 327950288;
  uint t = x ^ x << 11; x = y; y = z; z = w; return w = w ^ w >> 19 ^ t ^ t >> 8;
}

class Tree {
  Tree l, r;
  int size;
  int val, mn, ad;
  this(int val) {
    l = r = null;
    size = 1;
    this.val = val;
    this.ad = 0;
  }
  Tree change(Tree l, Tree r) {
    if (this.l) this.l.ad += ad;
    if (this.r) this.r.ad += ad;
    val += ad;
    ad = 0;
    this.l = l;
    this.r = r;
    size = (l ? l.size : 0) + 1 + (r ? r.size : 0);
    return this;
  }
}
Tree merge(Tree a, Tree b) {
  if (!a) return b;
  if (!b) return a;
  if (xrand() % (a.size + b.size) < a.size) {
    return a.change(a.l, merge(a.r, b));
  } else {
    return b.change(merge(a, b.l), b.r);
  }
}
Tuple!(Tree, Tree) splitAt(Tree a, int size) {
  if (!a) return tuple!(Tree, Tree)(null, null);
  if (size <= (a.l ? a.l.size : 0)) {
    auto l = splitAt(a.l, size);
    return tuple(l[0], a.change(l[1], a.r));
  } else {
    auto r = splitAt(a.r, size - (a.l ? a.l.size : 0) - 1);
    return tuple(a.change(a.l, r[0]), r[1]);
  }
}
Tuple!(Tree, Tree) splitGE(Tree a, int key) {
  if (!a) return tuple!(Tree, Tree)(null, null);
  key -= a.ad;
  if (key <= a.val) {
    auto l = splitGE(a.l, key);
    return tuple(l[0], a.change(l[1], a.r));
  } else {
    auto r = splitGE(a.r, key);
    return tuple(a.change(a.l, r[0]), r[1]);
  }
}
Tuple!(Tree, Tree) splitHead(Tree a) {
  assert(a);
  if (a.l) {
    auto l = splitHead(a.l);
    return tuple(l[0], a.change(l[1], a.r));
  } else {
    return tuple(a, a.r);
  }
}
string toStr(Tree a) {
  if (!a) return "";
  return format("<%s %s %s>", toStr(a.l), a.val, toStr(a.r));
}
int[] toSeq(Tree a, int ad = 0) {
  if (!a) return [];
  ad += a.ad;
  return toSeq(a.l, ad) ~ (a.val + ad) ~ toSeq(a.r, ad);
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto L = new int[N];
      auto R = new int[N];
      foreach (i; 0 .. N) {
        L[i] = readInt();
        R[i] = readInt();
      }
      
      debug {
        // O~(\sum_i (R[i] - L[i] + 1))
        int[] lis;
        foreach (i; 0 .. N) {
          foreach_reverse (x; L[i] .. R[i] + 1) {
            const pos = lis.lowerBound(x);
            if (pos == lis.length) {
              lis ~= x;
            } else {
              lis[pos] = x;
            }
          }
          writeln(L[i], " ", R[i], " ", lis);
        }
        writeln("brute: ", lis.length);
      }
      
      Tree t = null;
      foreach (i; 0 .. N) {
        auto sp0 = t.splitGE(L[i]);
        auto sp1 = sp0[1].splitGE(R[i]);
        if (sp1[1]) {
          sp1[1] = sp1[1].splitHead[1];
        }
        if (sp1[0]) {
          sp1[0].ad += 1;
        }
        t = sp0[0].merge(new Tree(L[i])).merge(sp1[0]).merge(sp1[1]);
        debug {
          writeln(L[i], " ", R[i], " ", t.toSeq);
        }
      }
      writeln(t.size);
    }
  } catch (EOFException e) {
  }
}
0