You are given array consisting of n integers. Your task is to find the maximum length of an increasing subarray of the given array.A subarray is the sequence of consecutive elements of the array. Subarray is called increasing if each element of this subarray strictly greater than previous.

The first line contains single positive integer n (1 ≤ n ≤ 105) — the number of integers. The second line contains n positive integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Print the maximum length of an increasing subarray of the given array.

let read_nums () =
    let s = read_line () in
    let nums = List.map int_of_string (Str.split (Str.regexp " ") s) in
    nums

;;

let rec solve lst cnt ans prev =
    match lst with
    | [] -> max cnt ans
    | hd :: tl -> if hd > prev then
                    solve tl (cnt+1) (max cnt ans) hd
                  else
                    solve tl 1  (max cnt ans) hd
;;

let  main () =
    let _ = read_int () in
    let l = read_nums () in
    let n = solve l 1 1 1000000007 in
    let () = print_int n in
    print_string "\n"

;;

main ()

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b
let long x = Int64.of_int x
let short x = Int64.to_int x

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_long () = bscanf Scanning.stdib " %Ld " (fun x -> x)

let () =
        let n = read_int() in
        let a = Array.make n 0 in
        for i = 0 to n - 1 do
                a.(i) <- read_int()
        done;
        let res = ref 1 in
        let cur = ref 1 in
        for i = 1 to n - 1 do
                if a.(i) > a.(i - 1) then
                        cur := !cur + 1
                else
                        cur := 1;
                if res < cur then
                        res := !cur
        done;
        printf "%d\n" !res

open Scanf
open Printf

let n = Scanf.scanf "%d " ( fun x -> x )

let a = Array.make (n+1) 0

let b = Array.make (n+1) 0

for i = 1 to n do
  let j = Scanf.scanf "%d " ( fun x -> x ) in
  a.(i)<-j
done

let cnt = ref 0

for i = 1 to n do
  begin
  if a.(i) > a.(i-1) then
    cnt := !cnt + 1
  else
    cnt := 1
  end; b.(i) <- !cnt
done

(*test
let () = Array.iter (fun x -> printf "%d " x) b
*)

let ff = Array.make (n+1) (-10)
let rec f m =
  if m = 1 then
    1
  else
    if ff.(m-1) < 0 then
      begin
      ff.(m-1) <- f (m-1);
      max ff.(m-1) b.(m)
      end
  else
    max ff.(m-1) b.(m)

let rslt = f n in
printf "%d\n" rslt

open Printf
let input_int_array n =
	Array.init n (fun _ -> Scanf.scanf " %d" (fun v -> v))

let get_int () = Scanf.scanf " %d" (fun v -> v)
let get_2_ints () = Scanf.scanf " %d %d" (fun u v -> u,v)
let get_3_ints () = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w)
let get_4_ints () = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x)
let get_5_ints () = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)

let print_int_endline n = n |> string_of_int |> print_endline

let () =
  let n = get_int ()
  in let a = input_int_array n
  in let v,len,lenmax = ref a.(0),ref 1,ref 1
  in
  for i=1 to (n-1) do
    if !v < a.(i) then (
      len := !len+1;
      if !len > !lenmax then lenmax := !len;
    ) else (
      len := 1;
    );
    v := a.(i)
  done;
  !lenmax |> print_int_endline

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let a = Array.make n 0 in
  let _ =
    for i = 0 to n - 1 do
      a.(i) <- Scanf.bscanf sb "%d " (fun s -> s)
    done;
  in
  let res = ref 1 in
  let p = ref a.(0) in
  let len = ref 1 in
    for i = 1 to n - 1 do
      if a.(i) > !p then (
	p := a.(i);
	incr len;
	res := max !res !len;
      ) else (
	p := a.(i);
	len := 1;
      )
    done;
    Printf.printf "%d\n" !res

let n = int_of_string (input_line stdin) ;;

let rec solve last acc best count =
    if count == 0 then max best acc
    else let num = Scanf.scanf "%d " (fun a -> a) in
        if num > last then solve num (acc + 1) best (count - 1)
        else solve num 1 (max best acc) (count - 1) ;;

let ans = solve 0 0 0 n in
    Printf.printf "%d\n" ans ;;

let n = int_of_string (input_line stdin) ;;

let rec solve last acc best count =
    Printf.printf "solve %d %d %d %d\n" last acc best count ;
    if count == 0 then max best acc
    else let num = Scanf.scanf "%d " (fun a -> a) in
        if num > last then solve num (acc + 1) best (count - 1)
        else solve num 1 (max best acc) (count - 1) ;;

let ans = solve 0 0 0 n in
    Printf.printf "%d\n" ans ;;


You are given an array of $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$. After you watched the amazing film "Everything Everywhere All At Once", you came up with the following operation.In one operation, you choose $$$n-1$$$ elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array $$$[1, 2, 3, 1]$$$ we can get the array $$$[2, 2, 2, 1]$$$, if we choose the first three elements, or we can get the array $$$[\frac{4}{3}, \frac{4}{3}, 3, \frac{4}{3}]$$$, if we choose all elements except the third.Is it possible to make all elements of the array equal by performing a finite number of such operations?

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 200$$$)  — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 \le n \le 50$$$)  — the number of integers. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 100$$$).

For each test case, if it is possible to make all elements equal after some number of operations, output $$$\texttt{YES}$$$. Otherwise, output $$$\texttt{NO}$$$. You can output $$$\texttt{YES}$$$ and $$$\texttt{NO}$$$ in any case (for example, strings $$$\texttt{yEs}$$$, $$$\texttt{yes}$$$, $$$\texttt{Yes}$$$ will be recognized as a positive response).

NoteIn the first test case, all elements are already equal.In the second test case, you can choose all elements except the third, their average is $$$\frac{1 + 2 + 4 + 5}{4} = 3$$$, so the array will become $$$[3, 3, 3, 3, 3]$$$.It's possible to show that it's impossible to make all elements equal in the third and fourth test cases.

open Printf
open Scanf

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0
let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let () =
  let cases = read_int() in
  for case = 1 to cases do
    let n = read_int() in
    let a = Array.init n read_int in
    let s = sum 0 (n-1) (fun i -> a.(i)) in
    if exists 0 (n-1) (fun i -> a.(i) * n = s)
    then printf "Yes\n" else printf "No\n"
  done


Dreamoon likes to play with sets, integers and .  is defined as the largest positive integer that divides both a and b.Let S be a set of exactly four distinct integers greater than 0. Define S to be of rank k if and only if for all pairs of distinct elements si, sj from S, .Given k and n, Dreamoon wants to make up n sets of rank k using integers from 1 to m such that no integer is used in two different sets (of course you can leave some integers without use). Calculate the minimum m that makes it possible and print one possible solution.

The single line of the input contains two space separated integers n, k (1 ≤ n ≤ 10 000, 1 ≤ k ≤ 100).

On the first line print a single integer — the minimal possible m.  On each of the next n lines print four space separated integers representing the i-th set. Neither the order of the sets nor the order of integers within a set is important. If there are multiple possible solutions with minimal m, print any one of them.

NoteFor the first example it's easy to see that set {1, 2, 3, 4} isn't a valid set of rank 1 since .

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let () =
  let n = read_int () in
  let k = read_int () in

  let m = 6*n-1 in

  printf "%d\n" (k*m);

  let rec printit i = if i=m+1 then () else (
    printf "%d %d %d %d\n" (k*(i+1)) (k*(i+2)) (k*(i+3)) (k*(i+5));
    printit (i+6)
  ) in

  printit 0


On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v1, v2, v3 such that the angle  (where v2 is the vertex of the angle, and v1 and v3 lie on its sides) is as close as possible to a. In other words, the value  should be minimum possible.If there are many optimal solutions, Mister B should be satisfied with any of them.

First and only line contains two space-separated integers n and a (3 ≤ n ≤ 105, 1 ≤ a ≤ 180) — the number of vertices in the polygon and the needed angle, in degrees.

Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.

NoteIn first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| < |90 - 67|. Other correct answers are: "3 1 2", "3 2 4", "4 2 3", "4 3 1", "1 3 4", "1 4 2", "2 4 1", "4 1 3", "3 1 4", "3 4 2", "2 4 3", "2 3 1", "1 3 2", "1 2 4", "4 2 1".In third sample test, on the contrary, the angle of 90 degrees was chosen, since |90 - 68| < |45 - 68|. Other correct answers are: "2 1 4", "3 2 1", "1 2 3", "4 3 2", "2 3 4", "1 4 3", "3 4 1".

open Printf
open Scanf

let pi = 4.0 *. (atan 1.0)

let read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let (n,a) = read_pair () in
  let a = (float a) *. (pi /. 180.0) in
  let alpha = pi /. (float n) in

  let lo = int_of_float (floor (a /. alpha)) in
  let hi = int_of_float (ceil (a /. alpha)) in

  let score k =
    let k = float k in
    abs_float (k *. alpha -. a)
  in

  let best = if score lo < score hi then lo else hi in

  let best = min best (n-2) in
  let best = max best 1 in

  printf "%d %d %d\n" (best+1) n 1


This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: https://codeforces.com/blog/entry/45307.The jury guessed some array $$$a$$$ consisting of $$$6$$$ integers. There are $$$6$$$ special numbers — $$$4$$$, $$$8$$$, $$$15$$$, $$$16$$$, $$$23$$$, $$$42$$$ — and each of these numbers occurs in $$$a$$$ exactly once (so, $$$a$$$ is some permutation of these numbers).You don't know anything about their order, but you are allowed to ask up to $$$4$$$ queries. In each query, you may choose two indices $$$i$$$ and $$$j$$$ ($$$1 \le i, j \le 6$$$, $$$i$$$ and $$$j$$$ are not necessarily distinct), and you will get the value of $$$a_i \cdot a_j$$$ in return.Can you guess the array $$$a$$$?The array $$$a$$$ is fixed beforehand in each test, the interaction program doesn't try to adapt to your queries.

NoteIf you want to submit a hack for this problem, your test should contain exactly six space-separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_6$$$. Each of $$$6$$$ special numbers should occur exactly once in the test. The test should be ended with a line break character.

open Printf
open Scanf

let read_int () = int_of_string (read_line())
let output s = printf "%s\n%!" s
let output2 i j = output (sprintf "? %d %d" i j)
let output6 a b c d e f = output (sprintf "! %d %d %d %d %d %d" a b c d e f)

let () =
  output2 1 2;
  let r1 = read_int () in
  output2 3 4;
  let r2 = read_int () in
  output2 1 3;
  let r3 = read_int () in
  output2 3 5;
  let r4 = read_int () in

  let a = [|4;8;16;15;23;42|] in

  let swap i j =
    let (ai,aj) = (a.(i), a.(j)) in
    a.(i) <- aj;
    a.(j) <- ai
  in

  let b = Array.make 6 0 in

  let check () =
    a.(0) * a.(1) = r1 && a.(2) * a.(3) = r2 && a.(0) * a.(2) = r3 && a.(2) * a.(4) = r4
  in
  let rec perm i = if i=6 then
      if check() then
	for i=0 to 5 do
	  b.(i) <- a.(i)
	done
      else ()
    else (
      let rec loop j = if j = 6 then () else (
	swap i j;
	perm (i+1);
	swap i j;
	loop (j+1)
      ) in
      loop i
    )
  in

  perm 0;

  output6 b.(0) b.(1) b.(2) b.(3) b.(4) b.(5)

open Printf
open Scanf

let read_int () = int_of_string (read_line())
let output s = printf "%s\n%!" s
let output2 i j = output (sprintf "? %d %d" i j)
let output6 a b c d e f = output (sprintf "%d %d %d %d %d %d" a b c d e f)

let () =
  output2 1 2;
  let r1 = read_int () in
  output2 3 4;
  let r2 = read_int () in
  output2 1 3;
  let r3 = read_int () in
  output2 3 5;
  let r4 = read_int () in

  let a = [|4;8;16;15;23;42|] in

  let swap i j =
    let (ai,aj) = (a.(i), a.(j)) in
    a.(i) <- aj;
    a.(j) <- ai
  in

  let b = Array.make 6 0 in

  let check () =
    a.(0) * a.(1) = r1 && a.(2) * a.(3) = r2 && a.(0) * a.(2) = r3 && a.(2) * a.(4) = r4
  in
  let rec perm i = if i=6 then
      if check() then
	for i=0 to 5 do
	  b.(i) <- a.(i)
	done
      else ()
    else (
      let rec loop j = if j = 6 then () else (
	swap i j;
	perm (i+1);
	swap i j;
	loop (j+1)
      ) in
      loop i
    )
  in

  perm 0;

  output6 b.(0) b.(1) b.(2) b.(3) b.(4) b.(5)


Andryusha goes through a park each day. The squares and paths between them look boring to Andryusha, so he decided to decorate them.The park consists of n squares connected with (n - 1) bidirectional paths in such a way that any square is reachable from any other using these paths. Andryusha decided to hang a colored balloon at each of the squares. The baloons' colors are described by positive integers, starting from 1. In order to make the park varicolored, Andryusha wants to choose the colors in a special way. More precisely, he wants to use such colors that if a, b and c are distinct squares that a and b have a direct path between them, and b and c have a direct path between them, then balloon colors on these three squares are distinct.Andryusha wants to use as little different colors as possible. Help him to choose the colors!

The first line contains single integer n (3 ≤ n ≤ 2·105) — the number of squares in the park. Each of the next (n - 1) lines contains two integers x and y (1 ≤ x, y ≤ n) — the indices of two squares directly connected by a path. It is guaranteed that any square is reachable from any other using the paths.

In the first line print single integer k — the minimum number of colors Andryusha has to use. In the second line print n integers, the i-th of them should be equal to the balloon color on the i-th square. Each of these numbers should be within range from 1 to k.

NoteIn the first sample the park consists of three squares: 1 → 3 → 2. Thus, the balloon colors have to be distinct.    Illustration for the first sample. In the second example there are following triples of consequently connected squares:   1 → 3 → 2  1 → 3 → 4  1 → 3 → 5  2 → 3 → 4  2 → 3 → 5  4 → 3 → 5  We can see that each pair of squares is encountered in some triple, so all colors have to be distinct.    Illustration for the second sample. In the third example there are following triples:   1 → 2 → 3  2 → 3 → 4  3 → 4 → 5  We can see that one or two colors is not enough, but there is an answer that uses three colors only.    Illustration for the third sample.

let color_tree n g =
  let colors = Array.make (n+1) 0 in
  let k_max = ref 1 in
  let rec dfs v p =
    let parent_c = colors.(p) in
    let self_c = colors.(v) in
    let children = List.filter (fun x -> x != p) g.(v) in
    let k = ref 0 in
    List.iter (fun child ->
        incr k;
        while !k = parent_c || !k = self_c do incr k done;
        colors.(child) <- !k;
        dfs child v
    ) children;
    k_max := max !k_max !k
  in
  colors.(1) <- 1;
  dfs 1 0;
  (!k_max, colors)

let () =
  let n = Scanf.scanf "%d" (fun x -> x) in
  let out = Array.make (n + 1) [] in
  for i = 2 to n do
    let (a, b) = Scanf.scanf " %d %d" (fun x y -> (x, y)) in
    out.(a) <- b :: out.(a);
    out.(b) <- a :: out.(b)
  done;
  let (k, colors) = color_tree n out in
  Printf.printf "%d\n%d" k colors.(1);
  for i = 2 to n do
    Printf.printf " %d" colors.(i)
  done;
  Printf.printf "\n"

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdib " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x-1,y-1))

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)

let () =
  let n = read_int () in

  let tree = Array.make n [] in

  let add_edge (i,j) =
    tree.(i) <- j::tree.(i);
    tree.(j) <- i::tree.(j)
  in

  for i=1 to n-1 do add_edge (read_pair ()) done;

  let color = Array.make n 0 in

  let nextcolor trycolor p v =
    let pcolor = if p < 0 then 0 else color.(p) in
    let vcolor = if v < 0 then 0 else color.(v) in
    if trycolor <> pcolor && trycolor <> vcolor then trycolor
    else
      let trycolor = trycolor + 1 in
      if trycolor <> pcolor && trycolor <> vcolor then trycolor
      else trycolor + 1
  in

  let rec dfs p v =
    let _ = List.fold_left (fun trycolor w -> if w = p then trycolor else (
      color.(w) <- nextcolor trycolor p v;
      dfs v w;
      color.(w) + 1
    )
    ) 1 tree.(v)
    in ()
  in

  color.(0) <- 1;
  dfs (-1) 0;

  let mcolor = maxf 0 (n-1) (fun i -> color.(i)) in

  printf "%d\n" mcolor;

  for i=0 to n-1 do
    printf "%d " color.(i)
  done;

  print_newline()

let color_tree n g =
  let colors = Array.make (n+1) 0 in
  let k_max = ref 1 in
  let rec dfs v p =
    let parent_c = colors.(p) in
    let self_c = colors.(v) in
    let children = List.filter (fun x -> x != p) g.(v) in
    let k = ref 0 in
    List.iter (fun child ->
        incr k;
        if !k = parent_c then incr k;
        if !k = self_c then incr k;
        colors.(child) <- !k;
        dfs child v
    ) children;
    k_max := max !k_max !k
  in
  colors.(1) <- 1;
  dfs 1 0;
  (!k_max, colors)

let () =
  let n = Scanf.scanf "%d" (fun x -> x) in
  let out = Array.make (n + 1) [] in
  for i = 2 to n do
    let (a, b) = Scanf.scanf " %d %d" (fun x y -> (x, y)) in
    out.(a) <- b :: out.(a);
    out.(b) <- a :: out.(b)
  done;
  let (k, colors) = color_tree n out in
  Printf.printf "%d\n%d" k colors.(1);
  for i = 2 to n do
    Printf.printf " %d" colors.(i)
  done;
  Printf.printf "\n"


This problem is interactive.We have hidden an array $$$a$$$ of $$$n$$$ pairwise different numbers (this means that no two numbers are equal). You can get some information about this array using a new device you just ordered on Amazon. This device can answer queries of the following form: in response to the positions of $$$k$$$ different elements of the array, it will return the position and value of the $$$m$$$-th among them in the ascending order.Unfortunately, the instruction for the device was lost during delivery. However, you remember $$$k$$$, but don't remember $$$m$$$. Your task is to find $$$m$$$ using queries to this device. You can ask not more than $$$n$$$ queries.Note that the array $$$a$$$ and number $$$m$$$ are fixed before the start of the interaction and don't depend on your queries. In other words, interactor is not adaptive.Note that you don't have to minimize the number of queries, and you don't need to guess array $$$a$$$. You just have to guess $$$m$$$.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1\le k < n \le 500$$$) — the length of the array and the number of the elements in the query. It is guaranteed that number $$$m$$$ satisfies $$$1\le m \le k$$$, elements $$$a_1, a_2, \dots, a_n$$$ of the array satisfy $$$0\le a_i \le 10^9$$$, and all of them are different.

open Printf
open String

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let maxf i j f = fold (i+1) j (fun k a -> max (f k) a) (f i)
let minf i j f = fold (i+1) j (fun k a -> min (f k) a) (f i)
let sum i j f = fold i j (fun i a -> (f i) + a) 0

let split_string s =
  let s = trim s in

  let n = String.length s in
  let li = fold 0 (n-1)
    (fun i ac -> if s.[i] = ' ' then i::ac else ac) []
  in

  let rec chop prev li ac =
    match li with [] -> List.rev ac
      | i::tail ->
	chop i tail ((sub s (prev+1) (i-prev-1))::ac)
  in

  chop (-1) (List.rev (n::li)) []

let read_two () =
  let c = split_string (read_line()) in
  let n = int_of_string(List.nth c 0) in
  let k = int_of_string(List.nth c 1) in
  (n,k)

let () =
  let (n,k) = read_two() in

  let query i =
    (* leave a hole at position i *)
    printf "?";
    for j=1 to k+1 do
      if j <> i then printf " %d" j
    done;
    printf "\n%!";
    read_two()
  in

  let mth = Array.make (k+2) 0 in

  for i=1 to k+1 do
    let (_,b) = query i in
    mth.(i) <- b
  done;

(*  let mmin = minf 1 (k+1) (fun i -> mth.(i)) in *)
  let mmax = maxf 1 (k+1) (fun i -> mth.(i)) in

  let nmaxes = sum 1 (k+1) (fun i -> if mmax = mth.(i) then 1 else 0) in

  printf "! %d\n%!" nmaxes

open Printf
open String

let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let split_string s =
  let s = trim s in

  let n = String.length s in
  let li = fold 0 (n-1)
    (fun i ac -> if s.[i] = ' ' then i::ac else ac) []
  in

  let rec chop prev li ac =
    match li with [] -> List.rev ac
      | i::tail ->
	chop i tail ((sub s (prev+1) (i-prev-1))::ac)
  in

  chop (-1) (List.rev (n::li)) []

let read_two () =
  let c = split_string (read_line()) in
  let n = int_of_string(List.nth c 0) in
  let k = int_of_string(List.nth c 1) in
  (n,k)

let () =
  let (n,k) = read_two() in

  let query i =
    (* leave a hole at position i *)
    printf "?";
    for j=1 to k+1 do
      if j <> i then printf " %d" j
    done;
    printf "\n%!";
    read_two()
  in

  let rec find_change i prev_pos =
    let (pos,_) = query i in
    if pos <> prev_pos then i-1 else find_change (i+1) prev_pos
  in

  let m = find_change 2 (fst (query 1)) in

  printf "! %d\n%!" m


The classic programming language of Bitland is Bit++. This language is so peculiar and complicated.The language is that peculiar as it has exactly one variable, called x. Also, there are two operations:  Operation ++ increases the value of variable x by 1.  Operation -- decreases the value of variable x by 1. A statement in language Bit++ is a sequence, consisting of exactly one operation and one variable x. The statement is written without spaces, that is, it can only contain characters "+", "-", "X". Executing a statement means applying the operation it contains.A programme in Bit++ is a sequence of statements, each of them needs to be executed. Executing a programme means executing all the statements it contains.You're given a programme in language Bit++. The initial value of x is 0. Execute the programme and find its final value (the value of the variable when this programme is executed).

The first line contains a single integer n (1 ≤ n ≤ 150) — the number of statements in the programme. Next n lines contain a statement each. Each statement contains exactly one operation (++ or --) and exactly one variable x (denoted as letter «X»). Thus, there are no empty statements. The operation and the variable can be written in any order.

Print a single integer — the final value of x.

open Printf;;
open Scanf;;
let n=read_int();;

let rec solve n x =
  if n=0 then x
  else
    let s=read_line() in
     if ((s="X++") || (s="++X")) then (solve (n-1) (x+1)) else (solve (n-1) (x-1))
;;

printf "%d\n" (solve n 0);;

open Printf;;
open Scanf;;
open List;;
let rec initlist n = if (n=0) then [] else 0::(initlist (n-1)) ;;

			let n=read_int();;
			let r=map (fun x -> read_line()) (initlist n);;
			let ans=fold_left (fun x y -> if ((y="X++") || (y="++X")) then x+1 else x-1) 0 r;;
			  printf "%d\n" ans;;

let number_of_operations = Scanf.scanf "%d " (fun n -> n)

let execute s x = match s with
    |"X++" | "++X" -> x + 1
    |_ -> x - 1

let rec solve n x =
    if n > 0
    then
        let bit_function = Scanf.scanf "%s " (fun s -> s) in
        solve (n - 1) (execute bit_function x)
    else
        Printf.printf "%d\n" x

let () = solve number_of_operations 0

let () =
  let n = Scanf.scanf "%d " (fun a -> a) in
  let a_lst = Array.to_list @@ Array.init n (fun _ -> Scanf.scanf "%s " (fun a -> a)) in
  let l = List.map (fun x -> String.sub x 1 1) a_lst in

  let p = List.filter (fun x -> x = "+") l |>
          List.length in
  let m = n-p in
  Printf.printf "%d\n" @@ p-m

let rec solve (n : int) (ak : int) =
  if n = 0 then
    ak
  else
    let op = read_line () in
    match op with
    | "X++" -> solve (n - 1) (ak + 1)
    | "++X" -> solve (n - 1) (ak + 1)
    | "X--" -> solve (n - 1) (ak - 1)
    | "--X" -> solve (n - 1) (ak - 1)
    | _ -> raise (Failure "Unsupported Operation")

let () =
  let n = read_int () in
  Printf.printf "%d" (solve n 0)

let n=Scanf.scanf "%d" (fun a->a);;
let x=ref 0;;
for i=1 to n do
   x:=!x+(Scanf.scanf " %s" (fun a-> if (String.contains a '+')==true then 1 else -1))
done;;
print_int(!x);;

let rec read_lines n =
  match n with
  | 0 -> []
  | _ -> let res = read_line () in
    res :: read_lines (n - 1)

let () =
  let num = read_int() in
  let res = read_lines num in
  List.fold_left (fun num str ->
    if String.contains str '-'
    then
      num - 1
    else
      num + 1
    ) 0 res
  |> Printf.printf "%d"
;;

let read_int () = Scanf.scanf "%d " (fun x -> x);;
let read_int3 () = Scanf.scanf " %d %d %d " (fun x y z -> (x,y,z) );;

let inc_or_dec = function
  | "X++" -> 1
  | "++X" -> 1
  | "--X" -> -1
  | "X--" -> -1
;;

let read_s () = Scanf.scanf "%s " (fun x -> inc_or_dec x );;

let n = read_int ();;

let a = Array.init n (fun i -> (read_s () ));;

let b = Array.fold_left (+) 0 a in print_int b; print_endline ""

let read_int () = Scanf.scanf "%d " (fun x -> x);;
let read_int3 () = Scanf.scanf " %d %d %d " (fun x y z -> (x,y,z) );;

let inc_or_dec = function
  | "X++" -> 1
  | "++X" -> 1
  | _ -> -1
;;

let read_s () = Scanf.scanf "%s " (fun x -> inc_or_dec x );;

let n = read_int ();;

let a = Array.init n (fun i -> (read_s () ));;

let b = Array.fold_left (+) 0 a in print_int b; print_endline ""

let read_int () = Scanf.scanf "%d" (fun x -> x)
let read_string () = Scanf.scanf " %s" (fun x -> x)

let get_bit n =
  let rec calculate_bit r = function
    | 0 -> r
    | n ->  match read_string () with
              | "X++" | "++X" -> calculate_bit (r + 1) (n - 1)
              | "X--" | "--X" -> calculate_bit (r - 1) (n - 1)
              | q -> Printf.printf "%s\n" q; assert  false
  in calculate_bit 0 n

let () = Printf.printf "%d\n" (get_bit (read_int ()))

let pf = Printf.printf;;
let sf = Scanf.scanf;;

let (|>) x f = f x;;
let (@@) f x = f x;;
let read0() = sf "%d " (fun x -> x)
let read1() = sf "%d\n" (fun x -> x)
let read2() = sf "%d %d\n" (fun x y -> x, y)
let read3() = sf "%d %d %d\n" (fun x y z -> x, y, z)

exception Error of string

let inf = 1000000000;;
let eps = 1e-11;;

let _ =
  let n = int_of_string @@ read_line() in
  let res = ref 0 in
  for i = 1 to n do
    let s = read_line() in
    res := !res + if s.[1] == '+' then 1 else -1
  done;
  pf "%d\n" !res
;;

open Printf

let read_ints() =
	Array.map (int_of_string) (Array.of_list (Str.split (Str.regexp " +") (read_line())))

let main =
    let n = read_int() in
    let ans = ref 0 in
    for i = 1 to n do
        let ins = read_line() in
        if ins.[1] = '+' then
            ans := !ans + 1
        else
            ans := !ans - 1
    done;
    printf "%d\n" !ans

let read_int () = Scanf.bscanf Scanf.Scanning.stdib " %d " (fun x -> x)
let read_string () = Scanf.bscanf Scanf.Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let x = ref 0 in
    for i=1 to n do
      let s = read_string() in
	if s="++X" || s="X++" then x := !x+1
	else if s="--X" || s="X--" then x := !x-1
	else failwith "bad statement"
    done;
    Printf.printf "%d\n" !x


Soon there will be held the world's largest programming contest, but the testing system still has m bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has n students able to perform such work. The students realize that they are the only hope of the organizers, so they don't want to work for free: the i-th student wants to get ci 'passes' in his subjects (regardless of the volume of his work).Bugs, like students, are not the same: every bug is characterized by complexity aj, and every student has the level of his abilities bi. Student i can fix a bug j only if the level of his abilities is not less than the complexity of the bug: bi ≥ aj, and he does it in one day. Otherwise, the bug will have to be fixed by another student. Of course, no student can work on a few bugs in one day. All bugs are not dependent on each other, so they can be corrected in any order, and different students can work simultaneously.The university wants to fix all the bugs as quickly as possible, but giving the students the total of not more than s passes. Determine which students to use for that and come up with the schedule of work saying which student should fix which bug.

The first line contains three space-separated integers: n, m and s (1 ≤ n, m ≤ 105, 0 ≤ s ≤ 109) — the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students. The next line contains m space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — the bugs' complexities. The next line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109) — the levels of the students' abilities. The next line contains n space-separated integers c1, c2, ..., cn (0 ≤ ci ≤ 109) — the numbers of the passes the students want to get for their help.

If the university can't correct all bugs print "NO". Otherwise, on the first line print "YES", and on the next line print m space-separated integers: the i-th of these numbers should equal the number of the student who corrects the i-th bug in the optimal answer. The bugs should be corrected as quickly as possible (you must spend the minimum number of days), and the total given passes mustn't exceed s. If there are multiple optimal answers, you can output any of them.

NoteConsider the first sample.The third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the students can work in parallel).The second student wants 3 passes for his assistance, the third student wants 6 passes. It meets the university's capabilities as it is ready to give at most 9 passes.

(* It's a binary search for the minimum number of days, and for a
   given number of days, it's a greedy algorithm to determine if it's
   possible.

   We work through the bugs from hardest to easiest.  At any point in
   time we maintain a priority queue of all the students who are
   capable of fixing the given bug.  The priority is the number of
   credits needed to make use of the given student.  It's zero if
   we've already used this student (and paid for his credits).
   A student is removed from the queue when he has been used for
   the bound on the number of days.  This forces the algorithm to
   use some other student, and stay within the day bound.
*)

open Printf
open Scanf

module Pset = Set.Make (struct type t = int*int let compare = compare end)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
  let n = read_int () in
  let m = read_int () in
  let s = read_int () in
  let a = Array.init m (fun _ -> read_int()) in (*bug values*)
  let b = Array.init n (fun _ -> read_int()) in (*student values*)
  let c = Array.init n (fun _ -> read_int()) in (*student costs*)

  let perma = Array.init m (fun i -> i) in
  let permb = Array.init n (fun i -> i) in

  Array.sort (fun i j -> compare a.(i) a.(j)) perma;
  Array.sort (fun i j -> compare b.(i) b.(j)) permb;

  let a' = Array.init m (fun j -> a.(perma.(j))) in
  let b' = Array.init n (fun i -> b.(permb.(i))) in

  let use = Array.make n 0 in  (* how much student i has been used *)
  let fixer = Array.make m 0 in (* which student fixed this bug *)

  let possible days =  (* return true if its possible to do it in days or fewer *)
    Array.fill use 0 n 0;
    let rec scan queue i j cred = if j=0 then true else (
      (* j is previous bug, i is previous student (the weakest who can solve bug j) *)

      let j = j-1 in
      let rec go_back (i,queue) =
	if i-1<0 || b'.(i-1) < a'.(j) then (i,queue) else
	  go_back (i-1, Pset.add (c.(permb.(i-1)), i-1) queue)
      in

      let (i, queue) = go_back (i, queue) in

      if Pset.is_empty queue then false else
	let (c0,ii) = Pset.min_elt queue in
	  (* now we know bug j is solved by student ii *)
	let cred = cred - c0 in
	if cred<0 then false else (
	  use.(ii) <- use.(ii) + 1;
	  fixer.(perma.(j)) <- permb.(ii);

	  let queue =
	    if use.(ii) = days then Pset.remove (c0,ii) queue
	    else if c0 = 0 then queue else Pset.add (0,ii) (Pset.remove (c0,ii) queue)
	  in

(*
  (* This is equivalent but does more unnecessary inserts and deletes *)
	  let queue = Pset.remove (c0,ii) queue in
	  let queue = if use.(ii) < days then Pset.add (0,ii) queue else queue in
*)
	  scan queue i j cred
	)
    )
    in
    let startqueue = Pset.singleton (c.(permb.(n-1)), n-1) in
    scan startqueue (n-1) m s
  in

  if a'.(m-1) > b'.(n-1) || (not (possible m)) then printf "NO\n" else
    let rec crude_upper_bound d =
      if possible d then d else crude_upper_bound (8*d)
    in

    let cub = crude_upper_bound 1 in

    let answer =
      if cub=1 then 1 else
	let rec bsearch lo hi =
	  if lo+1 = hi then hi else
	    let m = (lo + hi)/2 in
	    if possible m then bsearch lo m else bsearch m hi
	in
	bsearch (cub/8) cub
    in

    ignore (possible answer);

    printf "YES\n";

    for j=0 to m-1 do
      if j>0 then printf " ";
      printf "%d" (fixer.(j)+1);
    done;
    print_newline();

(* It's a binary search for the minimum number of days, and for a
   given number of days, it's a greedy algorithm to determine if it's
   possible.

   We work through the bugs from hardest to easiest.  At any point in
   time we maintain a priority queue of all the students who are
   capable of fixing the given bug.  The priority is the number of
   credits needed to make use of the given student.  It's zero if
   we've already used this student (and paid for his credits).
   A student is removed from the queue when he has been used for
   the bound on the number of days.  This forces the algorithm to
   use some other student, and stay within the day bound.
*)

open Printf
open Scanf

module Pset = Set.Make (struct type t = int*int let compare = compare end)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
  let n = read_int () in
  let m = read_int () in
  let s = read_int () in
  let a = Array.init m (fun _ -> read_int()) in (*bug values*)
  let b = Array.init n (fun _ -> read_int()) in (*student values*)
  let c = Array.init n (fun _ -> read_int()) in (*student costs*)

  let perma = Array.init m (fun i -> i) in
  let permb = Array.init n (fun i -> i) in

  Array.stable_sort (fun i j -> compare a.(i) a.(j)) perma;
  Array.stable_sort (fun i j -> compare b.(i) b.(j)) permb;

  let a' = Array.init m (fun j -> a.(perma.(j))) in
  let b' = Array.init n (fun i -> b.(permb.(i))) in

  let use = Array.make n 0 in  (* how much student i has been used *)
  let fixer = Array.make m 0 in (* which student fixed this bug *)

  let possible days =  (* return true if its possible to do it in days or fewer *)
    Array.fill use 0 n 0;
    let rec scan queue i j cred = if j=0 then true else (
      (* j is previous bug, i is previous student (the weakest who can solve bug j) *)

      let j = j-1 in
      let rec go_back (i,queue) =
	if i-1<0 || b'.(i-1) < a'.(j) then (i,queue) else
	  go_back (i-1, Pset.add (c.(permb.(i-1)), i-1) queue)
      in

      let (i, queue) = go_back (i, queue) in

      if Pset.is_empty queue then false else
	let (c0,ii) = Pset.min_elt queue in
	  (* now we know bug j is solved by student ii *)
	let cred = cred - c0 in
	if cred<0 then false else (
	  use.(ii) <- use.(ii) + 1;
	  fixer.(perma.(j)) <- permb.(ii);

	  let queue =
	    if use.(ii) = days then Pset.remove (c0,ii) queue
	    else if c0 = 0 then queue else Pset.add (0,ii) (Pset.remove (c0,ii) queue)
	  in

(*
  (* This is equivalent but does more unnecessary inserts and deletes *)
	  let queue = Pset.remove (c0,ii) queue in
	  let queue = if use.(ii) < days then Pset.add (0,ii) queue else queue in
*)
	  scan queue i j cred
	)
    )
    in
    let startqueue = Pset.singleton (c.(permb.(n-1)),permb.(n-1)) in
    scan startqueue (n-1) m s
  in

  if a'.(m-1) > b'.(n-1) || (not (possible m)) then printf "NO\n" else
    let rec crude_upper_bound d =
      if possible d then d else crude_upper_bound (2*d)
    in

    let cub = crude_upper_bound 1 in

    let answer =
      if cub = 1 then 1 else
	let rec bsearch lo hi =
	  if lo+1 = hi then hi else
	    let m = (lo + hi)/2 in
	    if possible m then bsearch lo m else bsearch m hi
	in
	bsearch (cub/2) cub
    in

    ignore (possible answer);

    printf "YES\n";

    for j=0 to m-1 do
      if j>0 then printf " ";
      printf "%d" (fixer.(j)+1);
      if b.(fixer.(j)) < a.(j) then printf " ERROR "
    done;
    print_newline();

(* It's a binary search for the minimum number of days, and for a
   given number of days, it's a greedy algorithm to determine if it's
   possible.

   We work through the bugs from hardest to easiest.  At any point in
   time we maintain a priority queue of all the students who are
   capable of fixing the given bug.  The priority is the number of
   credits needed to make use of the given student.  It's zero if
   we've already used this student (and paid for his credits).
   A student is removed from the queue when he has been used for
   the bound on the number of days.  This forces the algorithm to
   use some other student, and stay within the day bound.
*)

open Printf
open Scanf

module Pset = Set.Make (struct type t = int*int let compare = compare end)

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)

let () =
  let n = read_int () in
  let m = read_int () in
  let s = read_int () in
  let a = Array.init m (fun _ -> read_int()) in (*bug values*)
  let b = Array.init n (fun _ -> read_int()) in (*student values*)
  let c = Array.init n (fun _ -> read_int()) in (*student costs*)

  let perma = Array.init m (fun i -> i) in
  let permb = Array.init n (fun i -> i) in

  Array.sort (fun i j -> compare a.(i) a.(j)) perma;
  Array.sort (fun i j -> compare b.(i) b.(j)) permb;

  let a' = Array.init m (fun j -> a.(perma.(j))) in
  let b' = Array.init n (fun i -> b.(permb.(i))) in

  let use = Array.make n 0 in  (* how much student i has been used *)
  let fixer = Array.make m 0 in (* which student fixed this bug *)

  let possible days =  (* return true if its possible to do it in days or fewer *)
    Array.fill use 0 n 0;
    let rec scan queue i j cred = if j=0 then true else (
      (* j is previous bug, i is previous student (the weakest who can solve bug j) *)

      let j = j-1 in
      let rec go_back (i,queue) =
	if i-1<0 || b'.(i-1) < a'.(j) then (i,queue) else
	  go_back (i-1, Pset.add (c.(permb.(i-1)), i-1) queue)
      in

      let (i, queue) = go_back (i, queue) in

      if Pset.is_empty queue then false else
	let (c0,ii) = Pset.min_elt queue in
	  (* now we know bug j is solved by student ii *)
	let cred = cred - c0 in
	if cred<0 then false else (
	  use.(ii) <- use.(ii) + 1;
	  fixer.(perma.(j)) <- permb.(ii);
	  let queue = Pset.remove (c0,ii) queue in
	  let queue = if use.(ii) < days then Pset.add (0,ii) queue else queue in
	  scan queue i j cred
	)
    )
    in
    let startqueue = Pset.singleton (c.(permb.(n-1)),permb.(n-1)) in
    scan startqueue (n-1) m s
  in

  if a'.(m-1) > b'.(n-1) || (not (possible m)) then printf "NO\n" else
    let rec crude_upper_bound d =
      if possible d then d else crude_upper_bound (2*d)
    in

    let cub = crude_upper_bound 1 in

    let answer =
      if cub = 1 then 1 else
	let rec bsearch lo hi =
	  if lo+1 = hi then hi else
	    let m = (lo + hi)/2 in
	    if possible m then bsearch lo m else bsearch m hi
	in
	bsearch (cub/2) cub
    in

    ignore (possible answer);

    printf "YES\n";

    for j=0 to m-1 do
      if j>0 then printf " ";
      printf "%d" (fixer.(j)+1)
    done;
    print_newline();


Anton likes permutations, especially he likes to permute their elements. Note that a permutation of n elements is a sequence of numbers {a1, a2, ..., an}, in which every number from 1 to n appears exactly once.One day Anton got a new permutation and started to play with it. He does the following operation q times: he takes two elements of the permutation and swaps these elements. After each operation he asks his friend Vanya, how many inversions there are in the new permutation. The number of inversions in a permutation is the number of distinct pairs (i, j) such that 1 ≤ i < j ≤ n and ai > aj.Vanya is tired of answering Anton's silly questions. So he asked you to write a program that would answer these questions instead of him.Initially Anton's permutation was {1, 2, ..., n}, that is ai = i for all i such that 1 ≤ i ≤ n.

The first line of the input contains two integers n and q (1 ≤ n ≤ 200 000, 1 ≤ q ≤ 50 000) — the length of the permutation and the number of operations that Anton does. Each of the following q lines of the input contains two integers li and ri (1 ≤ li, ri ≤ n) — the indices of elements that Anton swaps during the i-th operation. Note that indices of elements that Anton swaps during the i-th operation can coincide. Elements in the permutation are numbered starting with one.

Output q lines. The i-th line of the output is the number of inversions in the Anton's permutation after the i-th operation.

NoteConsider the first sample.After the first Anton's operation the permutation will be {1, 2, 3, 5, 4}. There is only one inversion in it: (4, 5).After the second Anton's operation the permutation will be {1, 5, 3, 2, 4}. There are four inversions: (2, 3), (2, 4), (2, 5) and (3, 4).After the third Anton's operation the permutation will be {1, 4, 3, 2, 5}. There are three inversions: (2, 3), (2, 4) and (3, 4).After the fourth Anton's operation the permutation doesn't change, so there are still three inversions.

(*    O(n log n + q sqrt(n) + q log(n))  *)

open Printf
open Scanf
let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)

let superceil n =
  let rec loop i = if i>=n then i else loop (2*i) in
  loop 1

let ( ++ ) a b = Int64.add a b
let long x = Int64.of_int x

let ceil a b = (a+b-1) / b  (* ceiling of a/b *)
let rec fold i j f init = if i>j then init else fold (i+1) j f (f i init)
let sum i j f = fold i j (fun i a -> (f i) + a) 0

let read_pair _ = bscanf Scanning.stdib " %d %d " (fun x y -> (x,y))
let () =
  let (n,q) = read_pair () in

  let perm = Array.init n (fun i -> i) in
  let iperm = Array.copy perm in

  let swap i j =
    let (pi,pj) = (perm.(i), perm.(j)) in
    perm.(i) <- pj;
    perm.(j) <- pi;
    let (ipi,ipj) = (iperm.(pi),iperm.(pj)) in
    iperm.(pi) <- ipj;
    iperm.(pj) <- ipi
  in

  let sn = int_of_float (sqrt (float n)) in
  let rec find_level l size = if size > n then l else find_level (l+1) (2*size) in
  let levels = find_level 0 sn in

  let rec make_boxes i size ac = if i=levels then List.rev ac else
      let nboxes = ceil n size in
      make_boxes (i+1) (2*size) ((Array.make_matrix nboxes nboxes 0) :: ac)
  in

  let blist = make_boxes 0 sn [] in

  let adjust_counts (x,y) delta =
    let rec loop bsize bl =
      match bl with [] -> ()
	| box_array::tail ->
	  let (x',y') = (x/bsize, y/bsize) in
	  box_array.(x').(y') <- delta + box_array.(x').(y');
	  loop (bsize * 2) tail
    in
    loop sn blist
  in

  for i=0 to n-1 do
    adjust_counts (i, perm.(i)) 1
  done;

  let count_smaller x y =
    let (xbase,ybase) = (sn * (x/sn), sn * (y/sn)) in
    let top_bar  = sum ybase (y-1) (fun y -> if iperm.(y) < x then 1 else 0) in
    let side_bar = sum xbase (x-1) (fun x -> if  perm.(x) < ybase then 1 else 0) in
    fst (List.fold_left (fun (ac, size) box_array ->
      let (xmult, ymult) = (x/size, y/size) in
      let right_col_sum = if xmult mod 2 = 0 then 0 else
	  sum 0 (ymult-1) (fun y -> box_array.(xmult-1).(y))
      in
      let top_row_sum = if ymult mod 2 = 0 then 0 else
	  sum 0 (xmult-1) (fun x -> box_array.(x).(ymult-1))
      in
      let adjust = if ymult mod 2 = 1 && xmult mod 2 = 1 then
	  box_array.(xmult-1).(ymult-1) else 0
      in
      let ac = ac + top_row_sum + right_col_sum - adjust in
      (ac, 2*size)
    ) (top_bar + side_bar, sn) blist
    )
  in

  ignore (fold 1 q (fun _ inversions ->
    let (l,r) = read_pair() in
    if l = r then (
      printf "%Ld\n" inversions;
      inversions
    ) else (
      let (l,r) = (-1 + min l r, -1 + max l r) in
      let (x1,y1) = (l, min perm.(l) perm.(r)) in
      let (x2,y2) = (r, max perm.(l) perm.(r)) in
      let c =
	(count_smaller x2 y2) - (count_smaller (x1+1) y2)
	-(count_smaller x2 (y1+1)) + (count_smaller (x1+1) (y1+1))
      in
      let delta = if perm.(l) < perm.(r) then 2*c + 1 else -2*c-1 in
      adjust_counts (l, perm.(l)) (-1);
      adjust_counts (r, perm.(r)) (-1);
      adjust_counts (l, perm.(r)) 1;
      adjust_counts (r, perm.(l)) 1;
      swap l r;
      printf "%Ld\n" (inversions ++ (long delta));
      inversions ++ (long delta)
    )
  ) 0L
  )


You are given an array consisting of $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ and an integer $$$x$$$. It is guaranteed that for every $$$i$$$, $$$1 \le a_i \le x$$$.Let's denote a function $$$f(l, r)$$$ which erases all values such that $$$l \le a_i \le r$$$ from the array $$$a$$$ and returns the resulting array. For example, if $$$a = [4, 1, 1, 4, 5, 2, 4, 3]$$$, then $$$f(2, 4) = [1, 1, 5]$$$.Your task is to calculate the number of pairs $$$(l, r)$$$ such that $$$1 \le l \le r \le x$$$ and $$$f(l, r)$$$ is sorted in non-descending order. Note that the empty array is also considered sorted.

The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 \le n, x \le 10^6$$$) — the length of array $$$a$$$ and the upper limit for its elements, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, \dots a_n$$$ ($$$1 \le a_i \le x$$$).

Print the number of pairs $$$1 \le l \le r \le x$$$ such that $$$f(l, r)$$$ is sorted in non-descending order.

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let long x = Int64.of_int x
let short x = Int64.to_int x

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)

let () =
  let n = read_int () in
  let x = read_int () in
  let a = Array.init n read_int in

  let inf = int_of_float (1e8) in
  let minia = Array.make (x+1) (inf) in
  let maxia = Array.make (x+1) (-inf) in

  for i=0 to n-1 do
    minia.(a.(i)) <- min i minia.(a.(i));
    maxia.(a.(i)) <- max i maxia.(a.(i));
  done;

  let (miniaS,maxiaS) = (Array.copy minia, Array.copy maxia) in

  for j=x-1 downto 1 do
    miniaS.(j) <- min miniaS.(j) miniaS.(j+1)
  done;

  for j=2 to x do
    maxiaS.(j) <- max maxiaS.(j) maxiaS.(j-1)
  done;

  let rec loopr r = if r <= 1 then 1 else (
    if maxia.(r) > miniaS.(r+1) then r else loopr (r-1)
  ) in
  let min_r = loopr (x-1) in

  let rec loopl l = if l >= x then x else (
    if minia.(l) < maxiaS.(l-1) then l else loopl (l+1)
  ) in
  let max_l = loopl 2 in

  let good l r =
  (* returns true if the interval [l,r] is good,
     meaning that the stuff < l does not cross the stuff > r *)
    l-1 < 1 || r+1 > x || maxiaS.(l-1) < miniaS.(r+1)
  in

  let rec walk l r ac =
    (* invariant is that l <= r.  the ac does not count this r.
       first we advance l to make it as large as possible
       such that interval [l,r] is good. Then add this l to the ac. *)
    let rec walkl l =
      if l=r || l>=max_l || (not (good (l+1) r)) then l else walkl (l+1)
    in
    let l = walkl l in
    (* compute the contribution of this r to the total *)
    let ac = ac ++ (long l) in
    if r = x then ac else walk l (r+1) ac
  in

  let answer = walk 0 min_r 0L in
  printf "%Ld\n" answer

open Printf
open Scanf

let ( ** ) a b = Int64.mul a b
let ( ++ ) a b = Int64.add a b
let ( -- ) a b = Int64.sub a b
let ( // ) a b = Int64.div a b
let ( %% ) a b = Int64.rem a b

let long x = Int64.of_int x
let short x = Int64.to_int x

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)

let () =
  let n = read_int () in
  let x = read_int () in
  let a = Array.init n read_int in

  let push_lo_up lo x =
    match lo with
      | None -> Some x
      | Some y -> Some (max x y)
  in

  let push_hi_down hi x =
    match hi with
      | None -> Some x
      | Some y -> Some (min x y)
  in

  let rec scan i max_so_far lo hi = if i=n then (lo,hi) else (
    if max_so_far > a.(i) then (
      let lo = push_lo_up lo a.(i) in
      let hi = push_hi_down hi max_so_far in
      scan (i+1) max_so_far lo hi
    ) else (
      scan (i+1) a.(i) lo hi
    )
  ) in

  let answer = match scan 1 a.(0) None None with
    | (None,None) ->
      ((long x) ** (long(x+1))) // 2L
    | (Some maxlo, Some minhi) ->
      if minhi <= maxlo then (
	(long minhi) ** (long (x-maxlo+1))
      ) else (
	(long maxlo) ** (long (x-maxlo+1)) ++ (long (minhi - maxlo -1))
      )
    | _ -> failwith "can't happen"
  in

  printf "%Ld\n" answer


As Sherlock Holmes was investigating a crime, he identified n suspects. He knows for sure that exactly one of them committed the crime. To find out which one did it, the detective lines up the suspects and numbered them from 1 to n. After that, he asked each one: "Which one committed the crime?". Suspect number i answered either "The crime was committed by suspect number ai", or "Suspect number ai didn't commit the crime". Also, the suspect could say so about himself (ai = i).Sherlock Holmes understood for sure that exactly m answers were the truth and all other answers were a lie. Now help him understand this: which suspect lied and which one told the truth?

The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ n) — the total number of suspects and the number of suspects who told the truth. Next n lines contain the suspects' answers. The i-th line contains either "+ai" (without the quotes), if the suspect number i says that the crime was committed by suspect number ai, or "-ai" (without the quotes), if the suspect number i says that the suspect number ai didn't commit the crime (ai is an integer, 1 ≤ ai ≤ n). It is guaranteed that at least one suspect exists, such that if he committed the crime, then exactly m people told the truth.

Print n lines. Line number i should contain "Truth" if suspect number i has told the truth for sure. Print "Lie" if the suspect number i lied for sure and print "Not defined" if he could lie and could tell the truth, too, depending on who committed the crime.

NoteThe first sample has the single person and he confesses to the crime, and Sherlock Holmes knows that one person is telling the truth. That means that this person is telling the truth.In the second sample there are three suspects and each one denies his guilt. Sherlock Holmes knows that only two of them are telling the truth. Any one of them can be the criminal, so we don't know for any of them, whether this person is telling the truth or not.In the third sample the second and the fourth suspect defend the first and the third one. But only one is telling the truth, thus, the first or the third one is the criminal. Both of them can be criminals, so the second and the fourth one can either be lying or telling the truth. The first and the third one are lying for sure as they are blaming the second and the fourth one.

let _ =
  let sb = Scanf.Scanning.stdib in
  let n = Scanf.bscanf sb "%d " (fun s -> s) in
  let m = Scanf.bscanf sb "%d " (fun s -> s) in
  let g = Array.make n 0 in
  let ng = Array.make n 0 in
  let gs = Array.make n [] in
  let ngs = Array.make n [] in
  let t = ref 0 in
  let nt = ref 0 in
  let a = Array.make n false in
  let b = Array.make n 0 in
  let res = ref 0 in
    for i = 0 to n - 1 do
      let k = Scanf.bscanf sb "%d " (fun s -> s) in
	b.(i) <- k;
	if k > 0 then (
	  let k = k - 1 in
	    g.(k) <- g.(k) + 1;
	    gs.(k) <- i :: gs.(k);
	    incr t
	) else (
	  let k = -k - 1 in
	    ng.(k) <- ng.(k) + 1;
	    ngs.(k) <- i :: ngs.(k);
	    incr nt
	)
    done;
    (*for i = 0 to n - 1 do
      ng.(k) <- ng.(k) + !t - g.(k)
    done;*)
    for i = 0 to n - 1 do
      if g.(i) + !nt - ng.(i) = m then (
	a.(i) <- true;
	incr res;
      )
    done;
    for i = 0 to n - 1 do
      (*Printf.printf "asd %d %d\n" i b.(i);*)
      if b.(i) < 0 then (
	let k = -b.(i) - 1 in
	  if !res > 1 && a.(k)
	  then Printf.printf "Not defined\n"
	  else if a.(k)
	  then Printf.printf "Lie\n"
	  else Printf.printf "Truth\n"
      ) else (
	let k = b.(i) - 1 in
	if !res > 1 && a.(k)
	then Printf.printf "Not defined\n"
	else if a.(k)
	then Printf.printf "Truth\n"
	else Printf.printf "Lie\n"
      )
    done


Automatic Bakery of Cyberland (ABC) recently bought an n × m rectangle table. To serve the diners, ABC placed seats around the table. The size of each seat is equal to a unit square, so there are 2(n + m) seats in total.ABC placed conveyor belts on each unit square on the table. There are three types of conveyor belts: "^", "<" and ">". A "^" belt can bring things upwards. "<" can bring leftwards and ">" can bring rightwards.Let's number the rows with 1 to n from top to bottom, the columns with 1 to m from left to right. We consider the seats above and below the top of the table are rows 0 and n + 1 respectively. Also we define seats to the left of the table and to the right of the table to be column 0 and m + 1. Due to the conveyor belts direction restriction there are currently no way for a diner sitting in the row n + 1 to be served.Given the initial table, there will be q events in order. There are two types of events:  "A x y" means, a piece of bread will appear at row x and column y (we will denote such position as (x, y)). The bread will follow the conveyor belt, until arriving at a seat of a diner. It is possible that the bread gets stuck in an infinite loop. Your task is to simulate the process, and output the final position of the bread, or determine that there will be an infinite loop.  "C x y c" means that the type of the conveyor belt at (x, y) is changed to c. Queries are performed separately meaning that even if the bread got stuck in an infinite loop, it won't affect further queries.

The first line of input contains three integers n, m and q (1 ≤ n ≤ 105, 1 ≤ m ≤ 10, 1 ≤ q ≤ 105), separated by a space. Next n lines, each line contains m characters, describing the table. The characters can only be one of "<^>". Next q lines, each line describes an event. The format is "C x y c" or "A x y" (Consecutive elements are separated by a space). It's guaranteed that 1 ≤ x ≤ n, 1 ≤ y ≤ m. c is a character from the set "<^>". There are at most 10000 queries of "C" type.

For each event of type "A", output two integers tx, ty in a line, separated by a space, denoting the destination of (x, y) is (tx, ty). If there is an infinite loop, you should output tx = ty =  - 1.

NoteFor the first sample:If the bread goes from (2, 1), it will go out of the table at (1, 3).After changing the conveyor belt of (1, 2) to "<", when the bread goes from (2, 1) again, it will get stuck at "><", so output is ( - 1,  - 1).

type node = {
  mutable id:(int*int);
  mutable l:node;
  mutable r:node;
  mutable p:node
}

let rec null = {id=(0,0); l=null; r=null; p=null}

let isroot n =
  n.p == null || (n.p.l != n && n.p.r != n)

let update p = ()

let discharge p = ()

let rot_right p =
  let q = p.p in
  let r = q.p in
  q.l <- p.r;
  if q.l != null then q.l.p <- q;
  p.r <- q;
  q.p <- p;
  p.p <- r;
  if r != null then (
    if r.l == q then r.l <- p
    else if r.r == q then r.r <- p
  );
  update q

let rot_left p =
  let q = p.p in
  let r = q.p in
  q.r <- p.l;
  if q.r != null then q.r.p <- q;
  p.l <- q;
  q.p <- p;
  p.p <- r;
  if r != null then (
    if r.r == q then r.r <- p
    else if r.l == q then r.l <- p
  );
  update q

let splay p =
  let rec loop p = if not (isroot p) then
      let q = p.p in
      if isroot q then (
	discharge q;
	discharge p;
	if q.l == p then rot_right p else rot_left p
      ) else (
	let r = q.p in
	discharge r;
	discharge q;
	discharge p;
	if r.l == q then (
	  if q.l == p then (rot_right q; rot_right p)
	  else (rot_left p; rot_right p)
	) else (
	  if q.r == p then (rot_left q; rot_left p)
	  else (rot_right p; rot_left p)
	)
      );
      loop p
  in
  loop p;
  discharge p;  (* useful only if p was already the root *)
  update p      (* only useful if p was not already a root *)

let expose q =
  let rec loop r p = if p==null then r else (
    splay p;
    p.l <- r;
    update p;
    loop p p.p
  ) in

  let r = loop null q in
  splay(q);
  r  (* we only use this return value in the LCA operation *)

let link p q =
  ignore(expose p);
  if p.l != null then failwith "p's left child should be null";
  p.p <- q

let lca a b =
  ignore(expose a);
  expose(b)

let cut a =
  ignore(expose a);
  if a.r != null then (
    a.r.p <- null;
    a.r <- null;
    update a
  )

let findroot p =
  ignore(expose p);
  let rec loop p = if p.r == null then p else loop p.r in
  let p = loop p in
  splay p;
  p

(*--------------------------------------------------------------------------*)

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let m = read_int() in
  let q = read_int() in

  let board = Array.make n "" in
  let treenode = Array.make_matrix n m null in
  let nodetype = Array.make_matrix n m (0,0) in

  let on_board (i,j) = i>=0 && i<n && j>=0 && j<m in
  let getnodetype (i,j) = nodetype.(i).(j) in

  let linkcell (i,j) dir =
    (* cell(i,j) is a root.  we want to link it to the cell in direction dir *)
    let (ii,jj) =
      match dir with
        | '<' -> (i,j-1)
        | '>' -> (i,j+1)
        | '^' -> (i-1,j)
        | 'v' -> (i+1,j)
	| _ -> failwith "bad direction"
    in

    nodetype.(i).(j) <- (ii,jj);
    if on_board (ii,jj) then (
      let a = treenode.(i).(j) in (* a is already a root *)
      let b = treenode.(ii).(jj) in
      let broot = findroot b in

      if broot != a then (  (* not a cycle root *)
	nodetype.(i).(j) <- (-1,-1);   (* a is no longer a root *)
	link a b;
      )
    )
  in

  let update_board (i,j) dir =
    let a = treenode.(i).(j) in
    let b = findroot a in
    let (ii,jj) = getnodetype b.id in
    cut a;
    if on_board (ii,jj) then ( (* b is the root of a cycle tree *)
      let c = treenode.(ii).(jj) in
      if a != b && lca a c == a then link b c;
    );
    linkcell (i,j) dir
  in

  for i=0 to n-1 do
    board.(i) <- read_string()
  done;

  for i=0 to n-1 do
    for j=0 to m-1 do
      treenode.(i).(j) <- {null with id=(i,j)}
    done
  done;

  for i=0 to n-1 do
    for j=0 to m-1 do
      linkcell (i,j) board.(i).[j]
    done
  done;

  for query=0 to q-1 do
    let command = read_string() in
    let i = read_int()-1 in
    let j = read_int()-1 in

    match command with
      | "A" ->
	let r = findroot treenode.(i).(j) in
	let (ii,jj) = getnodetype r.id in
	if on_board (ii,jj)
	then printf "-1 -1\n"
	else printf "%d %d\n" (ii+1) (jj+1)
      | "C" ->
	update_board (i,j) (read_string()).[0]
      | _ -> failwith "unsupported command"
  done

(* An implementation of link-cut trees in ocaml *)

type node = {
  mutable id:(int*int);
  mutable l:node;
  mutable r:node;
  mutable p:node
}

let rec null = {id=(0,0); l=null; r=null; p=null}

let isroot n =
  n.p == null || (n.p.l != n && n.p.r != n)

let update p = ()

let discharge p = ()

let rot_right p =
  let q = p.p in
  let r = q.p in
  q.l <- p.r;
  if q.l != null then q.l.p <- q;
  p.r <- q;
  q.p <- p;
  p.p <- r;
  if r != null then (
    if r.l == q then r.l <- p
    else if r.r == q then r.r <- p
  );
  update q

let rot_left p =
  let q = p.p in
  let r = q.p in
  q.r <- p.l;
  if q.r != null then q.r.p <- q;
  p.l <- q;
  q.p <- p;
  p.p <- r;
  if r != null then (
    if r.r == q then r.r <- p
    else if r.l == q then r.l <- p
  );
  update q

let splay p =
  let rec loop p = if not (isroot p) then
      let q = p.p in
      if isroot q then (
	discharge q;
	discharge p;
	if q.l == p then rot_right p else rot_left p
      ) else (
	let r = q.p in
	discharge r;
	discharge q;
	discharge p;
	if r.l == q then (
	  if q.l == p then (rot_right q; rot_right p)
	  else (rot_left p; rot_right p)
	) else (
	  if q.r == p then (rot_left q; rot_left p)
	  else (rot_right p; rot_left p)
	)
      );
      loop p
  in
  loop p;
  discharge p;  (* useful only if p was already the root *)
  update p      (* only useful if p was not already a root *)

let expose q =
  let rec loop r p = if p==null then r else (
    splay p;
    p.l <- r;
    update p;
    loop p p.p
  ) in

  let r = loop null q in
  splay(q);
  r  (* we only use this return value in the LCA operation *)

let link p q =
  ignore(expose p);
  if p.r != null then failwith "p's right child should be null";
  p.p <- q

let lca a b =
  ignore(expose a);
  expose(b)

let cut a =
  ignore(expose a);
  if a.r != null then (
    a.r.p <- null;
    a.r <- null;
    update a
  )

let findroot p =
  ignore(expose p);
  let rec loop p = if p.r == null then p else loop p.r in
  let p = loop p in
  splay p;
  p

(*--------------------------------------------------------------------------*)

open Printf
open Scanf

let read_int () = bscanf Scanning.stdib " %d " (fun x -> x)
let read_string () = bscanf Scanning.stdib " %s " (fun x -> x)

let () =
  let n = read_int() in
  let m = read_int() in
  let q = read_int() in

  let board = Array.make n "" in
  let treenode = Array.make_matrix n m null in
  let nodetype = Array.make_matrix n m (0,0) in

  let on_board (i,j) = i>=0 && i<n && j>=0 && j<m in
  let getnodetype (i,j) = nodetype.(i).(j) in

  let linkcell (i,j) dir =
    (* cell(i,j) is a root.  we want to link it to the cell in direction dir *)
    let (ii,jj) =
      match dir with
        | '<' -> (i,j-1)
        | '>' -> (i,j+1)
        | '^' -> (i-1,j)
        | 'v' -> (i+1,j)
	| _ -> failwith "bad direction"
    in

    nodetype.(i).(j) <- (ii,jj);
    if on_board (ii,jj) then (
      let a = treenode.(i).(j) in (* a is already a root *)
      let b = treenode.(ii).(jj) in
      let broot = findroot b in

      if broot != a then (  (* not a cycle root *)
	nodetype.(i).(j) <- (-1,-1);   (* a is no longer a root *)
	link a b;
      )
    )
  in

  let update_board (i,j) dir =
    let a = treenode.(i).(j) in
    let b = findroot a in
    let (ii,jj) = getnodetype b.id in
    cut a;
    if on_board (ii,jj) then ( (* b is the root of a cycle tree *)
      let c = treenode.(ii).(jj) in
      if a != b && lca a c == a then link b c;
    );
    linkcell (i,j) dir
  in

  for i=0 to n-1 do
    board.(i) <- read_string()
  done;

  for i=0 to n-1 do
    for j=0 to m-1 do
      treenode.(i).(j) <- {null with id=(i,j)}
    done
  done;

  for i=0 to n-1 do
    for j=0 to m-1 do
      linkcell (i,j) board.(i).[j]
    done
  done;

  for query=0 to q-1 do
    let command = read_string() in
    let i = read_int()-1 in
    let j = read_int()-1 in

    match command with
      | "A" ->
	let r = findroot treenode.(i).(j) in
	let (ii,jj) = getnodetype r.id in
	if on_board (ii,jj)
	then printf "-1 -1\n"
	else printf "%d %d\n" (ii+1) (jj+1)
      | "C" ->
	update_board (i,j) (read_string()).[0]
      | _ -> failwith "unsupported command"
  done


Xenia the beginner programmer has a sequence a, consisting of 2n non-negative integers: a1, a2, ..., a2n. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value v for a.Namely, it takes several iterations to calculate value v. At the first iteration, Xenia writes a new sequence a1 or a2, a3 or a4, ..., a2n - 1 or a2n, consisting of 2n - 1 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence a. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is v.Let's consider an example. Suppose that sequence a = (1, 2, 3, 4). Then let's write down all the transformations (1, 2, 3, 4)  →  (1 or 2 = 3, 3 or 4 = 7)  →  (3 xor 7 = 4). The result is v = 4.You are given Xenia's initial sequence. But to calculate value v for a given sequence would be too easy, so you are given additional m queries. Each query is a pair of integers p, b. Query p, b means that you need to perform the assignment ap = b. After each query, you need to print the new value v for the new sequence a.

The first line contains two integers n and m (1 ≤ n ≤ 17, 1 ≤ m ≤ 105). The next line contains 2n integers a1, a2, ..., a2n (0 ≤ ai < 230). Each of the next m lines contains queries. The i-th line contains integers pi, bi (1 ≤ pi ≤ 2n, 0 ≤ bi < 230) — the i-th query.

Print m integers — the i-th integer denotes value v for sequence a after the i-th query.

NoteFor more information on the bit operations, you can follow this link: http://en.wikipedia.org/wiki/Bitwise_operation

let n,m = Scanf.scanf "%d %d\n" (fun x y->x,y);;
let arr = Array.make (1 lsl n) 0;;
let rec read_ints x =
  if x = (1 lsl n) then ()
  else begin
    arr.(x) <- Scanf.scanf "%d " (fun x -> x);
    read_ints (x+1);
  end in
read_ints 0;;

type t = Leaf | Node of t * int * t;;

let oper n l r =
  if (n mod 2) = 0 then (l lxor r)
  else (l lor r);;

let tree =
  let rec make_tree n x =
    if n = 0 then
      Node (Leaf,arr.(x),Leaf)
    else
      match (make_tree (n-1) (2*x)) ,(make_tree (n-1) (2*x+1)) with
      | (Node(_,l,_) as a),(Node(_,r,_) as b) -> Node(a,(oper n l r),b)
  in make_tree n 0;;

let rec modify tree n x p =
  match tree with
  | Node (Leaf,key,Leaf) as org ->
    if (x = p) then Node(Leaf,arr.(p),Leaf) else org
  | Node (left,key,right) as org ->
    let len = (1 lsl n) in
    let a,b = x*len,x*len+len-1 in
    if (p>=a && p<=b) then
      match (modify left (n-1) (x+x) p),(modify right (n-1) (x+x+1) p) with
      | (Node(_,l,_) as a),(Node(_,r,_) as b) -> Node (a,(oper n l r),b)
    else org;;

let rec query tree x =
  if x = m then
    ()
  else begin
    let p,b = Scanf.scanf "%d %d\n" (fun x y -> (x-1),y) in
    arr.(p) <- b;
    let tree = modify tree n 0 p in
    let Node(_,key,_) = tree in
    Printf.printf "%d\n" key;
    query tree (x+1);
  end in query tree 0;;

type tree = Node of int*tree*tree | Leaf
type operator = Or | Xor

let (|>) x f = f x

let flip = function
    Or -> Xor
  | Xor -> Or

let denote_op = function
  |  Or -> (lor)
  | Xor -> (lxor)
;;

let compose_tree seq =
  let rec build_row op seq acc =
    match seq with
      | [] -> List.rev acc
      | (Node (a,tl,tr)) :: (Node (b,tl2,tr2)) :: xs ->
          build_row op xs
            ((Node (denote_op op a b, Node (a, tl, tr), Node (b, tl2, tr2))) :: acc)
  in
  let rec iter_rows op seq =
    match seq with
        [t] -> t
      | xs ->
          iter_rows (flip op) (build_row op seq [])
  in
    iter_rows Or seq

let rec update_leaf leafs p v t =
  match t with
    | Leaf -> Leaf
    | Node (a, Leaf, Leaf) -> Node (v, Leaf, Leaf)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          Node (a,tl, update_leaf (leafs/2) (p - (leafs/2)) v tr)
        else
          Node (a, update_leaf (leafs/2) p v tl,tr)

let rec recalculate_path leafs p t =
  match t with
    | Leaf -> failwith "invariant broken"
    | Node (a, Leaf, Leaf) -> (Node (a, Leaf, Leaf), Or)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          let (Node (v,tl2,tr2), op) =
            recalculate_path (leafs/2) (p - (leafs/2)) tr in
            match tl with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, tl, Node (v,tl2,tr2)), flip op)
        else
          let (Node (v,tl2,tr2),op) = recalculate_path (leafs/2) p tl in
            match tr with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, Node (v,tl2,tr2), tr), flip op)

let rec print_tree = function
    Leaf -> Printf.printf " - "
  | Node (a, tl, tr) ->
      print_tree tl;
      Printf.printf "%d " a;
      print_tree tr

let rec print_root = function
    Leaf -> failwith "error"
  | Node (a, _, _) ->
      Printf.printf "%d\n" a

let main =
  let (n,m) = Scanf.scanf "%d %d\n" (fun a b -> (a,b)) in
  let leafs = 2 lsl (n-1) in
  let rec aux f n acc =
    match n with
        0 -> List.rev acc
      | n ->
          aux f (n-1) ((f ()) :: acc)
  in
  let tree =
    aux (fun _ -> Scanf.scanf "%d " (fun n -> Node (n, Leaf, Leaf))) leafs [] |>
      compose_tree
  in
  let queries = aux (fun _ -> Scanf.scanf "%d %d " (fun p v -> (p,v))) m [] in
    List.fold_left (fun t (p,v) ->
                      let t_new =
                        update_leaf leafs p v t |>
                        recalculate_path leafs p |>
                        fst
                      in print_root t_new; t_new) tree queries

type tree = Node of int*tree*tree | Leaf
type operator = Or | Xor

let (|>) x f = f x

let flip = function
    Or -> Xor
  | Xor -> Or

let denote_op = function
  |  Or -> (lor)
  | Xor -> (lxor)
;;

let compose_tree seq =
  let rec build_row op seq acc =
    match seq with
      | [] -> List.rev acc
      | (Node (a,tl,tr)) :: (Node (b,tl2,tr2)) :: xs ->
          build_row op xs
            ((Node (denote_op op a b, Node (a, tl, tr), Node (b, tl2, tr2))) :: acc)
  in
  let rec iter_rows op seq =
    match seq with
        [t] -> t
      | xs ->
          iter_rows (flip op) (build_row op seq [])
  in
    iter_rows Or seq

let rec update_leaf leafs p v t =
  match t with
    | Leaf -> Leaf
    | Node (a, Leaf, Leaf) -> Node (v, Leaf, Leaf)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          Node (a,tl, update_leaf (leafs/2) (p/2) v tr)
        else
          Node (a, update_leaf (leafs/2) p v tl,tr)

let rec recalculate_path leafs p t =
  match t with
    | Leaf -> failwith "invariant broken"
    | Node (a, Leaf, Leaf) -> (Node (a, Leaf, Leaf), Or)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          let (Node (v,tl2,tr2), op) =
            recalculate_path (leafs/2) (p/2) tr in
            match tl with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, tl, Node (v,tl2,tr2)), flip op)
        else
          let (Node (v,tl2,tr2),op) = recalculate_path (leafs/2) p tl in
            match tr with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, Node (v,tl2,tr2), tr), flip op)

let rec print_tree = function
    Leaf -> Printf.printf " - "
  | Node (a, tl, tr) ->
      print_tree tl;
      Printf.printf "%d " a;
      print_tree tr

let rec print_root = function
    Leaf -> failwith "error"
  | Node (a, _, _) ->
      Printf.printf "%d\n" a

let main =
  let (n,m) = Scanf.scanf "%d %d\n" (fun a b -> (a,b)) in
  let leafs = 2 lsl (n-1) in
  let rec aux f n acc =
    match n with
        0 -> List.rev acc
      | n ->
          aux f (n-1) ((f ()) :: acc)
  in
  let tree =
    aux (fun _ -> Scanf.scanf "%d " (fun n -> Node (n, Leaf, Leaf))) leafs [] |>
      compose_tree
  in
  let queries = aux (fun _ -> Scanf.scanf "%d %d " (fun p v -> (p,v))) m [] in
    List.fold_left (fun t (p,v) ->
                      let t_new =
                        update_leaf leafs p v t |>
                        recalculate_path leafs p |>
                        fst
                      in print_root t_new; t_new) tree queries

type tree = Node of int*tree*tree | Leaf
type operator = Or | Xor

let (|>) x f = f x

let flip = function
    Or -> Xor
  | Xor -> Or

let denote_op = function
  |  Or -> (lor)
  | Xor -> (lxor)
;;

let compose_tree seq =
  let rec build_row op seq acc =
    match seq with
      | [] -> List.rev acc
      | (Node (a,tl,tr)) :: (Node (b,tl2,tr2)) :: xs ->
          build_row op xs
            ((Node (denote_op op a b, Node (a, tl, tr), Node (b, tl2, tr2))) :: acc)
  in
  let rec iter_rows op seq =
    match seq with
        [t] -> t
      | xs ->
          iter_rows (flip op) (build_row op seq [])
  in
    iter_rows Or seq

let rec update_leaf leafs p v t =
  match t with
    | Leaf -> Leaf
    | Node (a, Leaf, Leaf) -> Node (v, Leaf, Leaf)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          Node (a,tl, update_leaf (leafs/2) (p mod (leafs/2)) v tr)
        else
          Node (a, update_leaf (leafs/2) p v tl,tr)

let rec recalculate_path leafs p t =
  match t with
    | Leaf -> failwith "invariant broken"
    | Node (a, Leaf, Leaf) -> (Node (a, Leaf, Leaf), Or)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          let (Node (v,tl2,tr2), op) =
            recalculate_path (leafs/2) (p mod (leafs/2)) tr in
            match tl with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, tl, Node (v,tl2,tr2)), flip op)
        else
          let (Node (v,tl2,tr2),op) = recalculate_path (leafs/2) p tl in
            match tr with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, Node (v,tl2,tr2), tr), flip op)

let rec print_root = function
    Leaf -> failwith "error"
  | Node (a, _, _) ->
      Printf.printf "%d\n" a

let main =
  let (n,m) = Scanf.scanf "%d %d\n" (fun a b -> (a,b)) in
  let leafs = 2 lsl (n-1) in
  let rec aux f n acc =
    match n with
        0 -> List.rev acc
      | n ->
          aux f (n-1) ((f ()) :: acc)
  in
  let tree =
    aux (fun _ -> Scanf.scanf "%d " (fun n -> Node (n, Leaf, Leaf))) leafs [] |>
      compose_tree
  in
  let queries = aux (fun _ -> Scanf.scanf "%d %d " (fun p v -> (p,v))) m [] in
    List.fold_left (fun t (p,v) ->
                      let t_new =
                        update_leaf leafs p v t |>
                        recalculate_path leafs p |>
                        fst
                      in print_root t_new; t_new) tree queries

type tree = Node of int*tree*tree | Leaf
type operator = Or | Xor

let (|>) x f = f x

let flip = function
    Or -> Xor
  | Xor -> Or

let denote_op = function
  |  Or -> (lor)
  | Xor -> (lxor)
;;

let compose_tree seq =
  let rec build_row op seq acc =
    match seq with
      | [] -> List.rev acc
      | (Node (a,tl,tr)) :: (Node (b,tl2,tr2)) :: xs ->
          build_row op xs
            ((Node (denote_op op a b, Node (a, tl, tr), Node (b, tl2, tr2))) :: acc)
  in
  let rec iter_rows op seq =
    match seq with
        [t] -> t
      | xs ->
          iter_rows (flip op) (build_row op seq [])
  in
    iter_rows Or seq

let rec update_leaf leafs p v t =
  match t with
    | Leaf -> Leaf
    | Node (a, Leaf, Leaf) -> Node (v, Leaf, Leaf)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          Node (a,tl, update_leaf (leafs/2) (p/2) v tr)
        else
          Node (a, update_leaf (leafs/2) p v tl,tr)

let rec recalculate_path leafs p t =
  match t with
    | Leaf -> failwith "invariant broken"
    | Node (a, Leaf, Leaf) -> (Node (a, Leaf, Leaf), Or)
    | Node (a,tl,tr) ->
        if (p > leafs/2) then
          let (Node (v,tl2,tr2), op) =
            recalculate_path (leafs/2) (p/2) tr in
            match tl with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, tl, Node (v,tl2,tr2)), flip op)
        else
          let (Node (v,tl2,tr2),op) = recalculate_path (leafs/2) p tl in
            match tr with
                Leaf -> failwith "invariant broken"
              | Node (b,_,_) ->
                  (Node (denote_op op b v, Node (v,tl2,tr2), tr), flip op)

let rec print_tree = function
    Leaf -> Printf.printf " - "
  | Node (a, tl, tr) ->
      print_tree tl;
      Printf.printf "%d " a;
      print_tree tr

let rec print_root = function
    Leaf -> failwith "error"
  | Node (a, _, _) ->
      Printf.printf "%d\n" a

let main =
  let (n,m) = Scanf.scanf "%d %d\n" (fun a b -> (a,b)) in
  let leafs = 2 lsl (n-1) in
  let rec aux f n acc =
    match n with
        0 -> List.rev acc
      | n ->
          aux f (n-1) ((f ()) :: acc)
  in
  let tree =
    aux (fun _ -> Scanf.scanf "%d " (fun n -> Node (n, Leaf, Leaf))) leafs [] |>
      compose_tree
  in
  let queries = aux (fun _ -> Scanf.scanf "%d %d " (fun p v -> (p,v))) m [] in
    List.fold_left (fun t (p,v) ->
                      let t_new =
                        update_leaf leafs p v t |>
                        recalculate_path leafs p |>
                        fst
                      in print_tree t_new; Printf.printf "\n\n"; t_new) tree queries


A lot of people in Berland hates rain, but you do not. Rain pacifies, puts your thoughts in order. By these years you have developed a good tradition — when it rains, you go on the street and stay silent for a moment, contemplate all around you, enjoy freshness, think about big deeds you have to do. Today everything had changed quietly. You went on the street with a cup contained water, your favorite drink. In a moment when you were drinking a water you noticed that the process became quite long: the cup still contained water because of rain. You decided to make a formal model of what was happening and to find if it was possible to drink all water in that situation. Thus, your cup is a cylinder with diameter equals d centimeters. Initial level of water in cup equals h centimeters from the bottom.   You drink a water with a speed equals v milliliters per second. But rain goes with such speed that if you do not drink a water from the cup, the level of water increases on e centimeters per second. The process of drinking water from the cup and the addition of rain to the cup goes evenly and continuously. Find the time needed to make the cup empty or find that it will never happen. It is guaranteed that if it is possible to drink all water, it will happen not later than after 104 seconds.Note one milliliter equals to one cubic centimeter.

The only line of the input contains four integer numbers d, h, v, e (1 ≤ d, h, v, e ≤ 104), where:   d — the diameter of your cylindrical cup,  h — the initial level of water in the cup,  v — the speed of drinking process from the cup in milliliters per second,  e — the growth of water because of rain if you do not drink from the cup.

If it is impossible to make the cup empty, print "NO" (without quotes). Otherwise print "YES" (without quotes) in the first line. In the second line print a real number — time in seconds needed the cup will be empty. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4. It is guaranteed that if the answer exists, it doesn't exceed 104.

NoteIn the first example the water fills the cup faster than you can drink from it.In the second example area of the cup's bottom equals to , thus we can conclude that you decrease the level of water by  centimeters per second. At the same time water level increases by 1 centimeter per second due to rain. Thus, cup will be empty in  seconds.

open Printf
open Scanf

let read_pair () = bscanf Scanning.stdib " %f %f " (fun x y -> (x,y))
let () =
  let (d,h) = read_pair () in
  let (v,e) = read_pair () in

  let pi = 2.0 *. (asin 1.0) in
  let sq x = x *. x in
  let a = pi *. (sq (d /. 2.0)) in
  let vol = a *. h in

  let e = e *. a in

  if e >= v then (
    printf "NO\n"
  ) else (
    printf "YES\n";
    printf "%.9f\n" (vol /. (v -. e))
  )


Gargari is jealous that his friend Caisa won the game from the previous problem. He wants to prove that he is a genius.He has a n × n chessboard. Each cell of the chessboard has a number written on it. Gargari wants to place two bishops on the chessboard in such a way that there is no cell that is attacked by both of them. Consider a cell with number x written on it, if this cell is attacked by one of the bishops Gargari will get x dollars for it. Tell Gargari, how to place bishops on the chessboard to get maximum amount of money.We assume a cell is attacked by a bishop, if the cell is located on the same diagonal with the bishop (the cell, where the bishop is, also considered attacked by it).

The first line contains a single integer n (2 ≤ n ≤ 2000). Each of the next n lines contains n integers aij (0 ≤ aij ≤ 109) — description of the chessboard.

On the first line print the maximal number of dollars Gargari will get. On the next line print four integers: x1, y1, x2, y2 (1 ≤ x1, y1, x2, y2 ≤ n), where xi is the number of the row where the i-th bishop should be placed, yi is the number of the column where the i-th bishop should be placed. Consider rows are numbered from 1 to n from top to bottom, and columns are numbered from 1 to n from left to right. If there are several optimal solutions, you can print any of them.

let pf = Printf.printf

let epf = Printf.eprintf

let sf = Scanf.scanf

let ( |> ) x f = f x

let ( @@ ) f x = f x

module Array =
  struct
    include ArrayLabels

    let fold_lefti ~f ~init arr =
      let acc = ref init
      in
        (for i = 0 to Pervasives.( - ) (Array.length arr) 1 do
           acc := f i !acc arr.(i)
         done;
         !acc)

  end

module String = StringLabels

module List =
  struct
    include ListLabels

    let rec repeat n a =
      if n = 0 then [] else a :: (repeat (Pervasives.( - ) n 1) a)

    let rec drop n a =
      if n = 0
      then a
      else
        (match a with
         | [] -> failwith "cannot take"
         | x :: xs -> drop (Pervasives.( - ) n 1) xs)

    let init ~f n =
      let res = ref []
      in
        (for i = 0 to Pervasives.( - ) n 1 do res := (f i) :: !res done;
         List.rev !res)

  end

module H = Hashtbl

module SI = Set.Make(struct type t = int
                             let compare = compare
                                end)

let solve n arr =
  let dia1 = Array.create (Pervasives.( + ) n n) 0L in
  let dia2 = Array.create (Pervasives.( + ) n n) 0L
  in
    (Array.iteri arr
       ~f:
         (fun i row ->
            Array.iteri row
              ~f:
                (fun j x ->
                   (dia1.(Pervasives.( + ) i j) <-
                      Int64.add dia1.(Pervasives.( + ) i j) x;
                    dia2.(Pervasives.( + ) (Pervasives.( - ) i j) n) <-
                      Int64.add
                        dia2.(Pervasives.( + ) (Pervasives.( - ) i j) n) x)));
     let (m1, x1, y1) = ((ref 0L), (ref 0), (ref 0)) in
     let (m2, x2, y2) = ((ref 0L), (ref 0), (ref 0))
     in
       (Array.iteri arr
          ~f:
            (fun i row ->
               Array.iteri row
                 ~f:
                   (fun j v ->
                      let cur =
                        Int64.sub
                          (Int64.add dia1.(Pervasives.( + ) i j)
                             dia2.(Pervasives.( + ) (Pervasives.( - ) i j) n))
                          v
                      in
                        if ((Pervasives.( + ) i j) land 1) = 0
                        then
                          if !m1 <= cur
                          then
                            (m1 := cur;
                             x1 := Pervasives.( + ) i 1;
                             y1 := Pervasives.( + ) j 1)
                          else ()
                        else
                          if !m2 <= cur
                          then
                            (m2 := cur;
                             x2 := Pervasives.( + ) i 1;
                             y2 := Pervasives.( + ) j 1)
                          else ()));
        pf "%Ld\n" (Int64.add !m1 !m2);
        pf "%d %d %d %d\n" !x1 !y1 !x2 !y2))

let () =
  sf "%d "
    (fun n ->
       let arr =
         Array.init n
           ~f: (fun i -> Array.init n ~f: (fun j -> sf "%Ld " (fun i -> i)))
       in solve n arr)

let pf = Printf.printf

let epf = Printf.eprintf

let sf = Scanf.scanf

let ( |> ) x f = f x

let ( @@ ) f x = f x

module Array =
  struct
    include ArrayLabels

    let fold_lefti ~f ~init arr =
      let acc = ref init
      in
        (for i = 0 to Pervasives.( - ) (Array.length arr) 1 do
           acc := f i !acc arr.(i)
         done;
         !acc)

  end

module String = StringLabels

module List =
  struct
    include ListLabels

    let rec repeat n a =
      if n = 0 then [] else a :: (repeat (Pervasives.( - ) n 1) a)

    let rec drop n a =
      if n = 0
      then a
      else
        (match a with
         | [] -> failwith "cannot take"
         | x :: xs -> drop (Pervasives.( - ) n 1) xs)

    let init ~f n =
      let res = ref []
      in
        (for i = 0 to Pervasives.( - ) n 1 do res := (f i) :: !res done;
         List.rev !res)

  end

module H = Hashtbl

module SI = Set.Make(struct type t = int
                             let compare = compare
                                end)

let solve n arr =
  let dia1 = Array.create (Pervasives.( + ) n n) 0L in
  let dia2 = Array.create (Pervasives.( + ) n n) 0L
  in
    (Array.iteri arr
       ~f:
         (fun i row ->
            Array.iteri row
              ~f:
                (fun j x ->
                   (dia1.(Pervasives.( + ) i j) <-
                      Int64.add dia1.(Pervasives.( + ) i j) x;
                    dia2.(Pervasives.( + ) (Pervasives.( - ) i j) n) <-
                      Int64.add
                        dia2.(Pervasives.( + ) (Pervasives.( - ) i j) n) x)));
     let (m1, x1, y1) = ((ref 0L), (ref 0), (ref 0)) in
     let (m2, x2, y2) = ((ref 0L), (ref 0), (ref 0))
     in
       (Array.iteri arr
          ~f:
            (fun i row ->
               Array.iteri row
                 ~f:
                   (fun j v ->
                      let cur =
                        Int64.sub
                          (Int64.add dia1.(Pervasives.( + ) i j)
                             dia2.(Pervasives.( + ) (Pervasives.( - ) i j) n))
                          v
                      in
                        if ((Pervasives.( + ) i j) land 1) = 0
                        then
                          if !m1 < cur
                          then
                            (m1 := cur;
                             x1 := Pervasives.( + ) i 1;
                             y1 := Pervasives.( + ) j 1)
                          else ()
                        else
                          if !m2 < cur
                          then
                            (m2 := cur;
                             x2 := Pervasives.( + ) i 1;
                             y2 := Pervasives.( + ) j 1)
                          else ()));
        pf "%Ld\n" (Int64.add !m1 !m2);
        pf "%d %d %d %d\n" !x1 !y1 !x2 !y2))

let () =
  sf "%d "
    (fun n ->
       let arr =
         Array.init n
           ~f: (fun i -> Array.init n ~f: (fun j -> sf "%Ld " (fun i -> i)))
       in solve n arr)

let pf = Printf.printf ;;
let epf = Printf.eprintf ;;
let sf = Scanf.scanf ;;

let (|>) x f = f x ;;
let (@@) f x = f x ;;

module Array = struct
  include ArrayLabels
  let fold_lefti ~f ~init arr =
    let acc = ref init in
    for i = 0 to Array.length arr - 1 do
      acc := f i !acc arr.(i)
    done;
    !acc
  ;;
end
module String = StringLabels ;;
module List  = struct
  include ListLabels ;;
  let rec repeat n a = if n = 0 then [] else a :: repeat (n - 1) a ;;
  let rec drop n a =
    if n = 0 then a
    else match a with
    | [] -> failwith "cannot take"
    | x :: xs -> drop (n - 1) xs ;;

  let init ~f n =
    let res = ref [] in
    for i = 0 to n - 1 do
      res := f i :: !res
    done;
    List.rev !res
  ;;
end ;;
module H = Hashtbl ;;

module SI = Set.Make (struct
  type t = int
  let compare = compare
end)

let solve n arr =
  let dia1 = Array.create (n + n) 0 in
  let dia2 = Array.create (n + n) 0 in

  Array.iteri arr ~f:(fun i row ->
    Array.iteri row ~f:(fun j x ->
      dia1.(i + j) <- dia1.(i + j) + x;
      dia2.(i - j + n) <- dia2.(i - j + n) + x));

  let m1, x1, y1 = ref 0, ref 0, ref 0 in
  let m2, x2, y2 = ref 0, ref 0, ref 0 in

  Array.iteri arr ~f:(fun i row ->
    Array.iteri row ~f:(fun j v ->
      let cur = dia1.(i + j) + dia2.(i - j + n) - v in
      if (i + j) land 1 = 0 then
        (if !m1 < cur then (m1 := cur; x1 := i + 1; y1 := j + 1))
      else
        (if !m2 < cur then (m2 := cur; x2 := i + 1; y2 := j + 1))));

  pf "%d\n" (!m1 + !m2);
  pf "%d %d %d %d\n" !x1 !y1 !x2 !y2
;;

let () =
  sf "%d " (fun n ->
    let arr = Array.init n ~f:(fun i ->
      Array.init n ~f:(fun j -> sf "%d " (fun i -> i))) in
    solve n arr)
;;


You are given $$$q$$$ queries in the following form:Given three integers $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$, find minimum positive integer $$$x_i$$$ such that it is divisible by $$$d_i$$$ and it does not belong to the segment $$$[l_i, r_i]$$$.Can you answer all the queries?Recall that a number $$$x$$$ belongs to segment $$$[l, r]$$$ if $$$l \le x \le r$$$.

The first line contains one integer $$$q$$$ ($$$1 \le q \le 500$$$) — the number of queries. Then $$$q$$$ lines follow, each containing a query given in the format $$$l_i$$$ $$$r_i$$$ $$$d_i$$$ ($$$1 \le l_i \le r_i \le 10^9$$$, $$$1 \le d_i \le 10^9$$$). $$$l_i$$$, $$$r_i$$$ and $$$d_i$$$ are integers.

For each query print one integer: the answer to this query.

let q = int_of_string (input_line stdin);;

let solve l r d =
    let ans =
        if d < l then d
        else if Nativeint.rem r d = Nativeint.zero then Nativeint.add r d
        else Nativeint.add (Nativeint.mul (Nativeint.div r d) d) d
        in Printf.printf "%nd\n" ans ;;

for i = 1 to q do
    Scanf.scanf "%nd %nd %nd\n" solve
done

let q = int_of_string (input_line stdin);;

let solve l r d =
    let ans =
        if d < l then d
        else if r mod d == 0 then r + d
        else r / d * d + d in
            Printf.printf "%d\n" ans ;;

for i = 1 to q do
    Scanf.scanf "%d %d %d\n" solve
done


Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows:  There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n.  The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament.  After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament.  The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament. You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a.Write the code that calculates for each knight, the name of the knight that beat him.

The first line contains two integers n, m (2 ≤ n ≤ 3·105; 1 ≤ m ≤ 3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li, ri, xi (1 ≤ li < ri ≤ n; li ≤ xi ≤ ri) — the description of the i-th fight. It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.

Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0.

NoteConsider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won.

let (|>) x f = f x ;;
let (@@) f x = f x ;;

let ident x = x ;;

let char_to_int x = int_of_char x - int_of_char '0' ;;
let int_to_char x = char_of_int (x + int_of_char '0') ;;

module List = struct
  include ListLabels ;;

  let rec iteri i f = function
    | [] -> ()
    | a::l -> f i a; iteri (i + 1) f l

  let iteri ~f l = iteri 0 f l
end
;;

module Array = struct
  include ArrayLabels ;;

  let reduce ~f xs =
    let acc = ref xs.(0) in
    for i = 1 to length xs - 1 do
      acc := f !acc xs.(i)
    done;
    !acc
  ;;
end
;;

module String = struct
  include StringLabels ;;

  let map_to_list ~f ss =
    let res = ref [] in
    iter ss ~f:(fun c -> res := f c :: !res);
    List.rev !res
  ;;

  let of_char_list ss =
    let res = String.create @@ List.length ss in
    List.iteri ss ~f:(fun i c -> res.[i] <- c);
    res
  ;;

  let iteri ~f a =
    for i = 0 to length a - 1 do f i (unsafe_get a i) done
  ;;
end
;;

module Float = struct
  let (+) = (+.)
  let (-) = (-.)
  let ( * ) = ( *. )
  let (/) = (/.)
end
;;

module Int64 = struct
  include Int64
  let (+) = add
  let (-) = sub
  let ( * ) = mul
  let (/) = div
end
;;

let fold_for ?(skip=1) min max ~init ~f =
  let acc = ref init in
  let cur = ref min in
  while !cur < max do
    acc := f !acc !cur;
    cur := !cur + skip;
  done;
  !acc
;;

let iter_for ?(skip=1) min max ~f =
  let cur = ref min in
  while !cur < max do
    f !cur;
    cur := !cur + skip;
  done
;;

let range x y =
  let rec iter acc i =
    if i >= y then acc
    else iter (i :: acc) (i + 1) in
  iter [] x |> List.rev
;;

module S = Set.Make (struct
  type t = int ;;
  let compare (x : int) (y : int) = compare x y ;;
end)
;;

let interval set x y =
  let (_, _, right) = S.split (x - 1) set in
  let (left, _, _) = S.split (y + 1) set in
  S.inter left right
;;

let () =
  let n, m = Scanf.scanf "%d %d " (fun x y -> x, y) in
  let fights = fold_for 0 m ~init:[] ~f:(fun acc _ ->
    Scanf.scanf "%d %d %d " (fun x y z -> x, y, z) :: acc) |> List.rev in
  let beater = Array.create n 0 in
  let ps = range 1 (n + 1) |> List.fold_left ~init:S.empty ~f:(fun acc i -> S.add i acc) in
  let res = List.fold_left ~init:ps fights ~f:(fun ps (l, r, x) ->
    Printf.eprintf "debug: l, r, x = %d, %d, %d\n" l r x;
    let losers = interval ps l r in
    prerr_string "debug:";
    S.iter (Printf.eprintf "%d ") losers;
    prerr_endline "";
    S.iter (fun i -> if x <> i then beater.(i - 1) <- x) losers;
    S.fold (fun i acc -> if x <> i then S.remove i acc else acc) losers ps) in
  Array.iter beater ~f:(fun i -> Printf.printf "%d " i);
  print_endline "";
;;