|
| 1 | +let ( |> ) v f = f v |
| 2 | +let ( <| ) f v = f v |
| 3 | +let foi = float_of_int |
| 4 | +let iof = int_of_float |
| 5 | + |
| 6 | +let next_int () = Scanf.scanf "%d%c" (fun x _ -> x) |
| 7 | +let next_flt () = Scanf.scanf "%f%c" (fun x _ -> x) |
| 8 | + |
| 9 | +let rec show_flt_list a = |
| 10 | + match a with |
| 11 | + [] -> print_newline () |
| 12 | + | x::xs -> print_float x; print_string " "; show_flt_list xs |
| 13 | + |
| 14 | + |
| 15 | +let rec loop n f acc = |
| 16 | + if n = 0 then List.rev acc |
| 17 | + else loop (n - 1) f ( (f n) :: acc) |
| 18 | + |
| 19 | + |
| 20 | +type data = {ma : float; lv : float; rv : float; sum : float} |
| 21 | +type range = {l: int; r : int} |
| 22 | + |
| 23 | +type seg_tree = |
| 24 | + Leaf of data |
| 25 | +| Node of int * int * data * seg_tree * seg_tree |
| 26 | + |
| 27 | + |
| 28 | +let get_data tr = |
| 29 | + match tr with |
| 30 | + Leaf d |
| 31 | + | Node (_, _, d, _, _) -> d |
| 32 | + |
| 33 | + |
| 34 | +let update dlc drc = |
| 35 | + let s = dlc.sum +. drc.sum in |
| 36 | + let l = max dlc.lv (dlc.sum +. drc.lv) in |
| 37 | + let r = max drc.rv (drc.sum +. dlc.rv) in |
| 38 | + let m = max (max dlc.ma drc.ma) (dlc.rv +. drc.lv) in |
| 39 | + {ma = m; lv = l; rv = r; sum = s;} |
| 40 | +;; |
| 41 | + |
| 42 | +let n = next_int () |
| 43 | +let m = next_int () |
| 44 | +let c = next_flt () |
| 45 | + |
| 46 | +let d = loop n (fun _ -> next_flt ()) [] |
| 47 | +let p = loop (n - 1) (fun _ -> ((next_flt ()) /. 100.0)) [] |
| 48 | + |
| 49 | +let v = List.map (fun (a, b) -> a -. b) (List.combine (List.tl d) (List.rev (List.tl (List.rev d)))) |
| 50 | +let a = Array.of_list <| List.map (fun (a, b) -> a /. 2.0 -. b *. c) (List.combine v p) |
| 51 | + |
| 52 | + |
| 53 | +let rec build x y = |
| 54 | + if x = y then Leaf ({ma = max (foi 0) a.(x); lv = a.(x); rv = a.(x); sum = a.(x)}) |
| 55 | + else begin |
| 56 | + let mid = (x + y) / 2 in |
| 57 | + let lc = build x mid in |
| 58 | + let rc = build (mid+1) y in |
| 59 | + Node (x, y, (update (get_data lc) (get_data rc)), lc, rc) |
| 60 | + end |
| 61 | + |
| 62 | +let rec query tr x y = |
| 63 | + match tr with |
| 64 | + Leaf d -> d |
| 65 | + | Node (l, r, d, lc, rc) -> begin |
| 66 | + if x <= l && y >= r then d |
| 67 | + else |
| 68 | + let mid = (l + r) / 2 in |
| 69 | + let snt = {ma = -1e20; lv = -1e20; rv = -1e20; sum = -1e20} in |
| 70 | + let qlc = if x <= mid then query lc x y else snt in |
| 71 | + let qrc = if y > mid then query rc x y else snt in |
| 72 | + update qlc qrc |
| 73 | + end |
| 74 | + |
| 75 | +let tr = build 0 (n - 2) |
| 76 | + |
| 77 | +let () = |
| 78 | + loop m (fun _ -> (let a = next_int() in |
| 79 | + let b = next_int() in |
| 80 | + query tr (a - 1) (b - 2)).ma) [] |
| 81 | + |> List.fold_left (+.) (foi 0) |
| 82 | + |> print_float |
| 83 | + |
| 84 | + |
| 85 | + |
0 commit comments