結果
問題 | No.875 Range Mindex Query |
ユーザー | guricerin |
提出日時 | 2020-01-30 00:06:46 |
言語 | F# (F# 4.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,672 bytes |
コンパイル時間 | 9,087 ms |
コンパイル使用メモリ | 205,560 KB |
実行使用メモリ | 80,912 KB |
最終ジャッジ日時 | 2024-09-16 03:09:57 |
合計ジャッジ時間 | 24,512 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 351 ms
35,620 KB |
testcase_01 | WA | - |
testcase_02 | AC | 359 ms
36,856 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 354 ms
36,128 KB |
testcase_06 | WA | - |
testcase_07 | AC | 356 ms
36,568 KB |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.fsproj を復元しました (473 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) /home/judge/data/code/Main.fs(107,13): warning FS0025: この式のパターン マッチが不完全です たとえば、値 '[|_; _; _; _|]' はパターンに含まれないケースを示す可能性があります。 [/home/judge/data/code/main.fsproj] /home/judge/data/code/Main.fs(95,9): warning FS0025: この式のパターン マッチが不完全です たとえば、値 '[|_; _; _|]' はパターンに含まれないケースを示す可能性があります。 [/home/judge/data/code/main.fsproj] main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
open System open System.Collections.Generic [<AutoOpen>] module Cin = let read f = stdin.ReadLine() |> f let reada f = stdin.ReadLine().Split() |> Array.map f let readChars() = read string |> Seq.toArray let readInts() = readChars() |> Array.map (fun x -> Convert.ToInt32(x.ToString())) [<AutoOpen>] module Cout = let writer = new IO.StreamWriter(new IO.BufferedStream(Console.OpenStandardOutput())) let print (s: string) = writer.Write s let println (s: string) = writer.WriteLine s let inline puts (s: ^a) = string s |> println type SegmentTree<'Monoid> = { /// 実データの要素数(葉ノードの数) size: int height: int /// モノイドの単位元 unity: 'Monoid nodes: 'Monoid array /// 二項演算 merge: Merge<'Monoid> } and Merge<'a> = 'a -> 'a -> 'a [<RequireQualifiedAccess>] [<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>] module SegmentTree = let internal sizeAndHeight n = let rec loop sAcc hAcc = if sAcc < n then loop (sAcc <<< 1) (hAcc + 1) else (sAcc, hAcc) loop 1 0 let internal parent i = (i - 1) >>> 1 let internal leftChild i = (i <<< 1) + 1 let internal rightChild i = (i <<< 1) + 2 let internal leafIdx tree k = k + tree.size - 1 let init (n: int) (f: Merge<'Monoid>) (unity: 'Monoid) = let size, height = sizeAndHeight n let nodes = Array.init (size * 2 - 1) (fun _ -> unity) { SegmentTree.size = size height = height unity = unity nodes = nodes merge = f } let build (sq: 'Monoid seq) tree: unit = let sq = Seq.indexed sq let nodes = tree.nodes for (i, s) in sq do let li = leafIdx tree i nodes.[li] <- s let len = Seq.length sq for i in len - 2 .. -1 .. 0 do let lc, rc = leftChild i, rightChild i nodes.[i] <- tree.merge nodes.[lc] nodes.[rc] /// 一点更新 let update k x tree: unit = let k = leafIdx tree k let nodes = tree.nodes nodes.[k] <- x // 子から親に伝搬 let mutable p = k while p > 0 do p <- parent p let lc, rc = leftChild p, rightChild p nodes.[p] <- tree.merge nodes.[lc] nodes.[rc] let rec internal queryCore (a: int) (b: int) (k: int) (l: int) (r: int) tree: 'Monoid = if r <= a || b <= l then tree.unity elif a <= l && r <= b then tree.nodes.[k] else let lc, rc, mid = leftChild k, rightChild k, (l + r) >>> 1 let lv = queryCore a b lc l mid tree let rv = queryCore a b rc mid r tree tree.merge lv rv let get k tree = let k = leafIdx tree k tree.nodes.[k] let query a b tree: 'Monoid = queryCore a b 0 0 tree.size tree let main() = let [| N; Q |] = reada int let A = reada int |> Array.indexed let f x y = let xi, xv = x let yi, yv = y if xv <= yv then x else y let e = -1, Int32.MaxValue let seg = SegmentTree.init N f e SegmentTree.build A seg for i in 0 .. Q - 1 do let [| q; l; r |] = reada int let l, r = l - 1, r - 1 match q with | 1 -> let li, lv = seg |> SegmentTree.get l let ri, rv = seg |> SegmentTree.get r seg |> SegmentTree.update l (l, rv) seg |> SegmentTree.update r (r, lv) | _ -> let i, v = seg |> SegmentTree.query l (r + 1) printfn "%d" (i + 1) () main() writer.Close()