結果

問題 No.686 Uncertain LIS
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-03-31 09:50:08
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 3,975 bytes
コンパイル時間 2,790 ms
コンパイル使用メモリ 154,292 KB
実行使用メモリ 9,888 KB
最終ジャッジ日時 2023-09-04 06:57:50
合計ジャッジ時間 7,418 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 209 ms
9,404 KB
testcase_10 AC 195 ms
8,412 KB
testcase_11 AC 124 ms
5,852 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 63 ms
4,376 KB
testcase_14 AC 100 ms
4,500 KB
testcase_15 AC 66 ms
4,380 KB
testcase_16 AC 174 ms
7,124 KB
testcase_17 AC 246 ms
9,148 KB
testcase_18 AC 248 ms
8,696 KB
testcase_19 AC 247 ms
8,900 KB
testcase_20 AC 232 ms
8,900 KB
testcase_21 AC 232 ms
7,644 KB
testcase_22 AC 119 ms
4,572 KB
testcase_23 AC 118 ms
4,520 KB
testcase_24 AC 152 ms
4,580 KB
testcase_25 AC 152 ms
4,540 KB
testcase_26 AC 117 ms
9,108 KB
testcase_27 AC 157 ms
4,508 KB
testcase_28 AC 157 ms
4,608 KB
testcase_29 AC 156 ms
4,544 KB
testcase_30 AC 157 ms
4,612 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 141 ms
9,888 KB
testcase_33 AC 116 ms
9,456 KB
testcase_34 AC 244 ms
9,176 KB
testcase_35 AC 246 ms
8,624 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)); }

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, ad;
  this(int val) {
    l = r = null;
    size = 1;
    this.val = val;
    this.ad = 0;
  }
  void propagate() {
    if (l) l.ad += ad;
    if (r) r.ad += ad;
    val += ad;
    ad = 0;
  }
  Tree change(Tree l, Tree r) {
    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;
  a.propagate();
  b.propagate();
  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) splitGE(Tree a, int key) {
  if (!a) return tuple!(Tree, Tree)(null, null);
  a.propagate();
  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);
  a.propagate();
  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) %s>", toStr(a.l), a.val, a.ad, 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;
        }
        debug {
          writeln("  ", sp0[0].toSeq, " ", sp1[0].toSeq, " ", sp1[1].toSeq);
        }
        t = sp0[0].merge(new Tree(L[i])).merge(sp1[0]).merge(sp1[1]);
        debug {
          writeln(L[i], " ", R[i], " ", t.toSeq, " ", t.toStr);
        }
      }
      writeln(t.size);
    }
  } catch (EOFException e) {
  }
}
0