1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| Reg: Type reg: (l: Int) -> (r: Int) -> Reg
sep: Reg -> Reg -> Bool sep(a, b) = a.r <= a.l || b.r <= b.l || a.r <= b.l || b.r <= a.l
has: Reg -> Reg -> Bool has(large, small) = large.l <= small.l && small.r <= large.r
mid: Reg -> Int mid(reg(l, r)) = l + (r - l) / 2
left: Reg -> Reg left(x@reg(l, r)) = reg(l, x.mid)
right: Reg -> Reg right(x@reg(l, r)) = reg(x.mid, r)
BinaryOp: Type BinaryOp(T) = T -> T -> T
Seg: Type seg: (~: BinaryOp(Int, Int)) -> (n: Int) -> (tree: [Int, Int]) -> Seg
build: BinaryOp (Int, Int), Int, (Int, Int) -> Seg build(~, n, base) = seg ~ n [base for 4n]
push: Seg -> Int -> Seg push(s@seg(~, n, tree))(p) = s mut _.tree[2p] ~= tree[p] mut _.tree[2p + 1] ~= tree[p]
pull: Seg -> Int -> Seg pull(s@seg(~, n, tree))(p) = s mut _.tree[p] = tree[2p] ~ tree[2p + 1]
setF: Seg -> (Int, Int), Reg, Reg, Int -> Seg setF(s@seg(~, n, tree))(x, query, focus, p) = if query.sep(focus) then s else if query.has(focus) then s mut _.tree[p] ~= value else s.push(p).setF(x, query, focus.left).setF(x, query, focus.right).pull(p)
set: Seg -> (Int, Int), Reg -> Seg set(s@seg(_, n, _))(x, query) = setF(s)(x, query, reg(0, n), 1) getF: Seg -> Reg, Reg, Int -> Int, Int getF(s@seg(~, n, tree))(query, focus, p) = if query.sep(focus) then 0, -1 else if query.has(focus) then tree[p] else let sp = s.push(p) in sp.getF(query, focus.left) ~ sp.getF(query, focus.right)
get: Seg -> Reg -> (Int, Int) get(s@seg(_, n, _))(query) = getF(s)(query, reg(0, n), 1)
main: Int main = // make discrete let n, m = input, input let regs, ids, rem = [[] for n], [], m rec if rem == 0 then nope else let i, l, r = input, input, input + 1 (regs mut _[i - 1] ::= l, r), ids :: l :: r, rem - 1 mut regs, ids.sort(<).unique, 0 mut regs.map(pair => pair.map(ids.find)), ids, 0 // dp on seg tree let prev, seg, curr = [], build(max, ids.length, (0, -1)), 0 rec if curr == n then nope else let res = regs[curr].fold((0, -1), (reg => res => max(res, seg.get(reg)))) let new_res = reg(res[0] + 1, curr) let new_seg = regs[curr].fold(seg, (reg => seg => seg.set(new_res, reg))) prev :: res[1], new_seg, curr + 1 // output result let res = seg.get(reg(0, seg.n)) let vis, curr = [0 for n], res[1] rec if curr == -1 then nope else vis mut _[curr] = 1, prev[curr] run print(n - res[0]) run for i in n if vis[i] then null else print(i + 1) 0
|