Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory).Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..".The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one.The command pwd should display the absolute path to the current directory. This path must not contain "..".Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory.

The first line of the input data contains the single integer n (1 ≤ n ≤ 50) — the number of commands. Then follow n lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter. The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive. Directories in the file system can have the same names.

For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots.

7
pwd
cd /home/vasya
pwd
cd ..
pwd
cd vasya/../petya
pwd

open Printf;;
open Scanf;;

let output_directory dir =
  let rec process_dir dir =
  match dir with
    | []   -> printf "\n"
    | h::t -> printf "%s/" h;
	      process_dir t
  in
  printf "/";
  process_dir (List.rev dir)
;;

let rec change_directory act_dir s idx act_str =
  if idx < (String.length s) then
    match s.[idx] with
    | '/' -> if(act_str = "..") then
		change_directory (List.tl act_dir) s (idx+1) ""
	     else
		change_directory (act_str::act_dir) s (idx+1) ""
    | c   -> change_directory act_dir s (idx+1) (act_str ^ (Char.escaped c))
  else
    act_dir
;;

let process_line dir =
  let s = scanf "%s" (fun x->x) in
(*   printf "mam komende %s\n" s; *)
  if s = "pwd" then begin
(*     printf "%s\n" s; *)
    let _ = scanf "%s\n" (fun x->x) in
    output_directory dir;
    dir
  end else begin
(*     printf "%s\n" s; *)
    let s2 = scanf " %s\n" (fun x->x) in
(*     printf "robie cd do %s\n" s2; *)
    if s2.[0] = '/' then
      change_directory [] (s2^"/") 1 ""
    else
      change_directory dir (s2^"/") 0 ""
  end

;;

let rec solve n directory =
  if n <> 0 then
    solve (n-1) (process_line directory)
;;

let n = scanf "%d\n" (fun x->x) in
solve n []

open Printf
open String

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

let rec msplit t i ll=
  if t="" || i>=String.length t then
    List.rev ll
  else
    begin
      try
        let j = String.index_from t i '/' in
        let s1= String.sub t i (j-i) in
        match s1 with
        |"" -> msplit t (j+1) ll
        |_ -> msplit t (j+1) (s1::ll)
      with
        Not_found ->
        let s1 = String.sub t i ((String.length t) - i) in
        List.rev (s1::ll)
    end

let msplit2 s = msplit s 0 []

              (*
let a = msplit2 "home/xyzt" in
    let () =List.iter (printf "%s ") a in
    print_endline "\n====\n"
               *)

let rec process_cd t cwd =
  if t="" then
    cwd
  else
    begin
      match (String.get t 0) with
      | '/' -> (process_cd (String.sub t 1 ((String.length t)-1)) [])
      | _ ->
         begin
           let ll = msplit t 0 [] in
           process_dirs ll cwd
         end
    end
and process_dirs l c =
  match l with
  | [] -> c
  | hd::lr ->
     begin
       match hd with
       |"." -> process_dirs lr c
       |".."-> process_dirs lr (List.tl c)
       | _ -> process_dirs lr (hd::c)
     end

let rec process_command ls cwd =
  match ls with
  |[] -> ()
  |s::lr ->
    begin
      match s with
      | "pwd" ->
         List.iter (fun x -> printf "/%s" x) (List.rev cwd); print_endline "/";
         process_command lr cwd
      | "cd" ->
         let cwd1 = process_cd (List.hd lr) cwd in
         process_command (List.tl lr) cwd1
    end

let rec readl l =
    let s = Scanf.scanf "%s " (fun x -> x) in
    match s with
    | "" -> List.rev l
    | _ -> readl (s::l)

let commands = readl [];;

  (*
let _ = List.iter (fun x -> printf "%s " x) commands
in print_endline "\n---"
   *)

let _ = process_command commands []


Momiji has got a rooted tree, consisting of n nodes. The tree nodes are numbered by integers from 1 to n. The root has number 1. Momiji decided to play a game on this tree.The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by v) and removes all the subtree nodes with the root in node v from the tree. Node v gets deleted as well. The game finishes when the tree has no nodes left. In other words, the game finishes after the step that chooses the node number 1.Each time Momiji chooses a new node uniformly among all the remaining nodes. Your task is to find the expectation of the number of steps in the described game.

The first line contains integer n (1 ≤ n ≤ 105) — the number of nodes in the tree. The next n - 1 lines contain the tree edges. The i-th line contains integers ai, bi (1 ≤ ai, bi ≤ n; ai ≠ bi) — the numbers of the nodes that are connected by the i-th edge. It is guaranteed that the given graph is a tree.

Print a single real number — the expectation of the number of steps in the described game. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.

NoteIn the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are: 1 × (1 / 2) + 2 × (1 / 2) = 1.5In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are: 1 × (1 / 3) + (1 + 1.5) × (2 / 3) = (1 / 3) + (5 / 3) = 2

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

let () =
  let n = read_int() in
  let edge = Array.make (n+1) [] in
    for i=1 to n-1 do
      let u = read_int() in
      let v = read_int() in
	edge.(u) <- v::edge.(u);
	edge.(v) <- u::edge.(v);
    done;

    let rec dfs d p v =
      (* p=parent, v=the node to search, d=the depth of v (the root is depth 1) *)
      List.fold_left (fun ac w -> if w = p then ac else
			ac +. (dfs (d+1) v w)
		     ) (1.0 /. (float d)) edge.(v)
    in
      Printf.printf "%.7f\n" (dfs 1 0 1)


You are given a string $$$s$$$, consisting of $$$n$$$ letters, each letter is either 'a' or 'b'. The letters in the string are numbered from $$$1$$$ to $$$n$$$.$$$s[l; r]$$$ is a continuous substring of letters from index $$$l$$$ to $$$r$$$ of the string inclusive. A string is called balanced if the number of letters 'a' in it is equal to the number of letters 'b'. For example, strings "baba" and "aabbab" are balanced and strings "aaab" and "b" are not.Find any non-empty balanced substring $$$s[l; r]$$$ of string $$$s$$$. Print its $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$). If there is no such substring, then print $$$-1$$$ $$$-1$$$.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The first line of the testcase contains a single integer $$$n$$$ ($$$1 \le n \le 50$$$) — the length of the string. The second line of the testcase contains a string $$$s$$$, consisting of $$$n$$$ letters, each letter is either 'a' or 'b'.

For each testcase print two integers. If there exists a non-empty balanced substring $$$s[l; r]$$$, then print $$$l$$$ $$$r$$$ ($$$1 \le l \le r \le n$$$). Otherwise, print $$$-1$$$ $$$-1$$$.

NoteIn the first testcase there are no non-empty balanced subtrings.In the second and third testcases there are multiple balanced substrings, including the entire string "abbaba" and substring "baba".

let scan s =
  let rec go i =
    if i < String.length s - 1 then
      if s.[i] <> s.[i + 1] then (i + 1, i + 2) else go (i + 1)
    else (-1, -1) in
  go 0

let input f =
  Scanf.scanf "%d\n" @@ fun n ->
    for _ = 1 to n do
      Scanf.scanf "%d\n%s\n" @@ fun m s -> assert (String.length s = m); f s
    done

let _ = input @@ fun s ->
  let i, j = scan s in Format.printf "%d %d@." i j


For the first place at the competition, Alex won many arrays of integers and was assured that these arrays are very expensive. After the award ceremony Alex decided to sell them. There is a rule in arrays pawnshop: you can sell array only if it can be compressed to a generator.This generator takes four non-negative numbers $$$n$$$, $$$m$$$, $$$c$$$, $$$s$$$. $$$n$$$ and $$$m$$$ must be positive, $$$s$$$ non-negative and for $$$c$$$ it must be true that $$$0 \leq c < m$$$. The array $$$a$$$ of length $$$n$$$ is created according to the following rules:   $$$a_1 = s \bmod m$$$, here $$$x \bmod y$$$ denotes remainder of the division of $$$x$$$ by $$$y$$$;  $$$a_i = (a_{i-1} + c) \bmod m$$$ for all $$$i$$$ such that $$$1 < i \le n$$$. For example, if $$$n = 5$$$, $$$m = 7$$$, $$$c = 4$$$, and $$$s = 10$$$, then $$$a = [3, 0, 4, 1, 5]$$$.Price of such an array is the value of $$$m$$$ in this generator.Alex has a question: how much money he can get for each of the arrays. Please, help him to understand for every array whether there exist four numbers $$$n$$$, $$$m$$$, $$$c$$$, $$$s$$$ that generate this array. If yes, then maximize $$$m$$$.

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$) — the number of arrays. The first line of array description contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the size of this array. The second line of array description contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \leq a_i \leq 10^9$$$ ) — elements of the array.  It is guaranteed that the sum of array sizes does not exceed $$$10^5$$$.

For every array print:    $$$-1$$$, if there are no such four numbers that generate this array;  $$$0$$$, if $$$m$$$ can be arbitrary large;  the maximum value $$$m$$$ and any appropriate $$$c$$$ ($$$0 \leq c < m$$$) in other cases.

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 gcd a b =
  let rec g a b = if a=0L then b else g (b %% a) a in
  g (Int64.abs a) (Int64.abs b)

let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)
let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)
let ( mod ) x n = let y = x %% n in if y<0L then y++n else y

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let () =
  let cases = read_int() in
  for case = 1 to cases do
    let n = read_int() in
    let a = Array.init n (fun i -> long (read_int())) in
    if n <= 2 then printf "0\n" else (
      let d = Array.init (n-1) (fun i -> a.(i+1) -- a.(i)) in
      let e = Array.init (n-2) (fun i -> d.(i+1) -- d.(i)) in

      let rec loop ac i =
	if i = n-2 then ac else loop (gcd ac e.(i)) (i+1)
      in
      let m = loop 0L 0 in
      if m = 0L then printf "0\n" else (
	let c = d.(0) mod m in
	if a.(0) < m && (forall 1 (n-1) (fun i -> a.(i) = (a.(i-1) ++ c) mod m))
	then printf "%Ld %Ld\n" m c
	else printf "-1\n"
      )
    )
  done

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 gcd a b =
  let rec g a b = if a=0L then b else g (b %% a) a in
  g (Int64.abs a) (Int64.abs b)

let rec exists i j f = (i<=j) && ((f i) || exists (i+1) j f)
let rec forall i j f = (i>j) || ((f i) && forall (i+1) j f)

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let () =
  let cases = read_int() in
  for case = 1 to cases do
    let n = read_int() in
    let a = Array.init n (fun i -> long (read_int())) in
    if n<=2 then printf "0\n" else (
      let d = Array.init (n-1) (fun i -> a.(i+1) -- a.(i)) in
      let e = Array.init (n-2) (fun i -> d.(i+1) -- d.(i)) in

      let rec loop ac i =
	if i = n-2 then ac else loop (gcd ac e.(i)) (i+1)
      in
      let m = loop 0L 0 in
      if m = 0L then printf "0\n" else (
	if exists 0 (n-1) (fun i -> a.(i) >= m) then printf "-1\n" else
	  printf "%Ld %Ld\n" m (d.(0) %% m)
      )
    )
  done


Let us remind you part of the rules of Codeforces. The given rules slightly simplified, use the problem statement as a formal document.In the beginning of the round the contestants are divided into rooms. Each room contains exactly n participants. During the contest the participants are suggested to solve five problems, A, B, C, D and E. For each of these problem, depending on when the given problem was solved and whether it was solved at all, the participants receive some points. Besides, a contestant can perform hacks on other contestants. For each successful hack a contestant earns 100 points, for each unsuccessful hack a contestant loses 50 points. The number of points for every contestant is represented by the sum of points he has received from all his problems, including hacks.You are suggested to determine the leader for some room; the leader is a participant who has maximum points.

The first line contains an integer n, which is the number of contestants in the room (1 ≤ n ≤ 50). The next n lines contain the participants of a given room. The i-th line has the format of "handlei plusi minusi ai bi ci di ei" — it is the handle of a contestant, the number of successful hacks, the number of unsuccessful hacks and the number of points he has received from problems A, B, C, D, E correspondingly. The handle of each participant consists of Latin letters, digits and underscores and has the length from 1 to 20 characters. There are the following limitations imposed upon the numbers:    0 ≤ plusi, minusi ≤ 50;  150 ≤ ai ≤ 500 or ai = 0, if problem A is not solved;  300 ≤ bi ≤ 1000 or bi = 0, if problem B is not solved;  450 ≤ ci ≤ 1500 or ci = 0, if problem C is not solved;  600 ≤ di ≤ 2000 or di = 0, if problem D is not solved;  750 ≤ ei ≤ 2500 or ei = 0, if problem E is not solved.  All the numbers are integer. All the participants have different handles. It is guaranteed that there is exactly one leader in the room (i.e. there are no two participants with the maximal number of points).

Print on the single line the handle of the room leader.

NoteThe number of points that each participant from the example earns, are as follows:   Petr — 3860  tourist — 4140  Egor — 4030  c00lH4x0R —  - 350  some_participant — 2220 Thus, the leader of the room is tourist.

let () =
	let n = read_int () in
	let max_name = ref "" in
	let max_i = ref min_int in (
		for i = 1 to n do
			Scanf.scanf " %s %d %d %d %d %d %d %d" (fun n p m a b c d e ->
				let i = p*100-m*50+a+b+c+d+e in
				if i > !max_i then (max_i := i; max_name := n);
			)
		done;
		print_string !max_name
	)
;;

let () =
	let n = read_int () in
	let max_name = ref "" in
	let max_i = ref 0 in (
		for i = 1 to n do
			Scanf.scanf " %s %d %d %d %d %d %d %d" (fun n p m a b c d e ->
				let i = p*100-m*50+a+b+c+d+e in
				if i > !max_i then (max_i := i; max_name := n);
			)
		done;
		print_string !max_name
	)
;;


The only difference between easy and hard versions is constraints.There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed.For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on.Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$.Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids:  after the $$$1$$$-st day it will belong to the $$$5$$$-th kid,  after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid,  after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid,  after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day.You have to answer $$$q$$$ independent queries.

The first line of the input contains one integer $$$q$$$ ($$$1 \le q \le 200$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 \le n \le 200$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, \dots, p_n$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid.

For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query.

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

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

let () =
	let q = read_int () in
	for tests = 1 to q do
		let n = read_int () in
		solve n;
	done;


For some time the program of rounding numbers that had been developed by the Codeforces participants during one of the previous rounds, helped the citizens of Far Far Away to convert numbers into a more easily readable format. However, as time went by, the economy of the Far Far Away developed and the scale of operations grew. So the King ordered to found the Bank of Far Far Away and very soon even the rounding didn't help to quickly determine even the order of the numbers involved in operations. Besides, rounding a number to an integer wasn't very convenient as a bank needed to operate with all numbers with accuracy of up to 0.01, and not up to an integer.The King issued yet another order: to introduce financial format to represent numbers denoting amounts of money. The formal rules of storing a number in the financial format are as follows:   A number contains the integer part and the fractional part. The two parts are separated with a character "." (decimal point).  To make digits in the integer part of a number easier to read, they are split into groups of three digits, starting from the least significant ones. The groups are separated with the character "," (comma). For example, if the integer part of a number equals 12345678, then it will be stored in the financial format as 12,345,678  In the financial format a number's fractional part should contain exactly two digits. So, if the initial number (the number that is converted into the financial format) contains less than two digits in the fractional part (or contains no digits at all), it is complemented with zeros until its length equals 2. If the fractional part contains more than two digits, the extra digits are simply discarded (they are not rounded: see sample tests).  When a number is stored in the financial format, the minus sign is not written. Instead, if the initial number had the minus sign, the result is written in round brackets.  Please keep in mind that the bank of Far Far Away operates using an exotic foreign currency — snakes ($), that's why right before the number in the financial format we should put the sign "$". If the number should be written in the brackets, then the snake sign should also be inside the brackets. For example, by the above given rules number 2012 will be stored in the financial format as "$2,012.00" and number -12345678.9 will be stored as "($12,345,678.90)".The merchants of Far Far Away visited you again and expressed much hope that you supply them with the program that can convert arbitrary numbers to the financial format. Can you help them?

The input contains a number that needs to be converted into financial format. The number's notation length does not exceed 100 characters, including (possible) signs "-" (minus) and "." (decimal point). The number's notation is correct, that is:    The number's notation only contains characters from the set {"0" – "9", "-", "."}.  The decimal point (if it is present) is unique and is preceded and followed by a non-zero quantity on decimal digits  A number cannot start with digit 0, except for a case when its whole integer part equals zero (in this case the integer parts is guaranteed to be a single zero: "0").  The minus sign (if it is present) is unique and stands in the very beginning of the number's notation  If a number is identically equal to 0 (that is, if it is written as, for example, "0" or "0.000"), than it is not preceded by the minus sign.  The input data contains no spaces.  The number's notation contains at least one decimal digit.

Print the number given in the input in the financial format by the rules described in the problem statement.

NotePay attention to the second and third sample tests. They show that the sign of a number in the financial format (and consequently, the presence or absence of brackets) is determined solely by the sign of the initial number. It does not depend on the sign of the number you got after translating the number to the financial format.

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 s = read_string () in
  let n = String.length s in
  let isneg = s.[0] = '-' in
  let rec finddot i = if i=n then n else if s.[i] = '.' then i else finddot (i+1) in
  let dot = finddot 0 in
  let start = if isneg then 1 else 0 in

    if isneg then Printf.printf "(";
    Printf.printf "$";
    for i=start to dot-1 do
      if i>start && i<dot-1 && ((dot-i) mod 3) = 0 then Printf.printf ",";
      Printf.printf "%c" s.[i]
    done;

    for i=dot to dot+2 do
      if i=n && dot=n then Printf.printf "."
      else if i>=n then Printf.printf "0"
      else Printf.printf "%c" s.[i]
    done;

    if isneg then Printf.printf ")";
    Printf.printf "\n";


Vasya decided to go to the grocery store. He found in his wallet $$$a$$$ coins of $$$1$$$ burle and $$$b$$$ coins of $$$2$$$ burles. He does not yet know the total cost of all goods, so help him find out $$$s$$$ ($$$s > 0$$$): the minimum positive integer amount of money he cannot pay without change or pay at all using only his coins.For example, if $$$a=1$$$ and $$$b=1$$$ (he has one $$$1$$$-burle coin and one $$$2$$$-burle coin), then:  he can pay $$$1$$$ burle without change, paying with one $$$1$$$-burle coin,  he can pay $$$2$$$ burle without change, paying with one $$$2$$$-burle coin,  he can pay $$$3$$$ burle without change by paying with one $$$1$$$-burle coin and one $$$2$$$-burle coin,  he cannot pay $$$4$$$ burle without change (moreover, he cannot pay this amount at all). So for $$$a=1$$$ and $$$b=1$$$ the answer is $$$s=4$$$.

The first line of the input contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases in the test. The description of each test case consists of one line containing two integers $$$a_i$$$ and $$$b_i$$$ ($$$0 \le a_i, b_i \le 10^8$$$) — the number of $$$1$$$-burle coins and $$$2$$$-burles coins Vasya has respectively.

For each test case, on a separate line print one integer $$$s$$$ ($$$s > 0$$$): the minimum positive integer amount of money that Vasya cannot pay without change or pay at all.

Note  The first test case of the example is clarified into the main part of the statement.  In the second test case, Vasya has only $$$1$$$ burle coins, and he can collect either any amount from $$$1$$$ to $$$4$$$, but $$$5$$$ can't.  In the second test case, Vasya has only $$$2$$$ burle coins, and he cannot pay $$$1$$$ burle without change.  In the fourth test case you don't have any coins, and he can't even pay $$$1$$$ burle.

open Printf
open Scanf
module LL = Int64
let rd_ll () = bscanf Scanning.stdin " %Ld " (fun x -> x)

let (&) x = LL.of_int x
let ( + ) x y = LL.add x y
let ( - ) x y = LL.sub x y
let ( * ) x y = LL.mul x y
let ( / ) x y = LL.div x y
let ( % ) x y = x - ( y * (x / y) )

let solve x y =
  if x = 0L then 1L
  else x+2L*y+1L

let () =
  let t = read_int () in
  for i=1 to t do
    let n = rd_ll () in
    let x = rd_ll () in
    printf "%Ld\n" (solve n x)
  done;

let n = int_of_string (input_line stdin) in
for i = 0 to n-1 do
  let l , r = Scanf.sscanf (input_line stdin) "%d  %d" (fun l r -> (l,r)) in
  if l = 0 then print_endline "1"
  else begin
    let b = Int64.add (Int64.of_int r) (Int64.of_int r) in
    let a = Int64.add b (Int64.of_int (l+1)) in
    print_endline (Int64.to_string a)
  end
done;;


Someone gave Alyona an array containing n positive integers a1, a2, ..., an. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all.Formally, after applying some operations Alyona will get an array of n positive integers b1, b2, ..., bn such that 1 ≤ bi ≤ ai for every 1 ≤ i ≤ n. Your task is to determine the maximum possible value of mex of this array.Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1.

The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of elements in the Alyona's array. The second line of the input contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.

Print one positive integer — the maximum possible value of mex of the array after Alyona applies some (possibly none) operations.

NoteIn the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5.To reach the answer to the second sample case one must not decrease any of the array elements.

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;
    Array.sort compare a
  in
  let res = ref 1 in
    for i = 0 to n - 1 do
      if a.(i) >= !res
      then incr res
    done;
    Printf.printf "%d\n" !res

open Printf
open Scanf

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

let () =
  let n = read_int () in
  let a = Array.init n (fun _ -> read_int()) in

  Array.sort compare a;

  let rec scan i mval = if i=n then mval else
      if a.(i) >= mval
      then scan (i+1) (mval+1)
      else scan (i+1) mval
  in

  printf "%d\n" (scan 0 1)


Real stupidity beats artificial intelligence every time.— Terry Pratchett, Hogfather, DiscworldYou are given a string $$$s$$$ of length $$$n$$$ and a number $$$k$$$. Let's denote by $$$rev(s)$$$ the reversed string $$$s$$$ (i.e. $$$rev(s) = s_n s_{n-1} ... s_1$$$). You can apply one of the two kinds of operations to the string: replace the string $$$s$$$ with $$$s + rev(s)$$$ replace the string $$$s$$$ with $$$rev(s) + s$$$How many different strings can you get as a result of performing exactly $$$k$$$ operations (possibly of different kinds) on the original string $$$s$$$?In this statement we denoted the concatenation of strings $$$s$$$ and $$$t$$$ as $$$s + t$$$. In other words, $$$s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$$$, where $$$n$$$ and $$$m$$$ are the lengths of strings $$$s$$$ and $$$t$$$ respectively.

The first line contains one integer $$$t$$$ ($$$1 \le t \le 100$$$) — number of test cases. Next $$$2 \cdot t$$$ lines contain $$$t$$$ test cases: The first line of a test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 100$$$, $$$0 \le k \le 1000$$$) — the length of the string and the number of operations respectively. The second string of a test case contains one string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin letters.

For each test case, print the answer (that is, the number of different strings that you can get after exactly $$$k$$$ operations) on a separate line. It can be shown that the answer does not exceed $$$10^9$$$ under the given constraints.

4

3 2

aab

3 3

aab

7 1

abacaba

2 0

ab

NoteIn the first test case of the example:After the first operation the string $$$s$$$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $$$s$$$: aabbaaaabbaa and baaaabbaaaab.

Scanf.scanf "%d" (fun t ->
    let isparin n s =
        let rec loop l r =
            if l >= r then true else
            if s.[l] = s.[r] then loop (l + 1) (r - 1) else false
        in
        loop 0 (n - 1)
    in
    for i = 1 to t do
        Scanf.scanf " %d %d %s" (fun n k s ->
            Printf.printf "%d\n" @@
                if k = 0 then 1 else
                if isparin n s then 1 else 2
        )
    done
)


The Narrator has an integer array $$$a$$$ of length $$$n$$$, but he will only tell you the size $$$n$$$ and $$$q$$$ statements, each of them being three integers $$$i, j, x$$$, which means that $$$a_i \mid a_j = x$$$, where $$$|$$$ denotes the bitwise OR operation.Find the lexicographically smallest array $$$a$$$ that satisfies all the statements.An array $$$a$$$ is lexicographically smaller than an array $$$b$$$ of the same length if and only if the following holds:   in the first position where $$$a$$$ and $$$b$$$ differ, the array $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.

In the first line you are given with two integers $$$n$$$ and $$$q$$$ ($$$1 \le n \le 10^5$$$, $$$0 \le q \le 2 \cdot 10^5$$$). In the next $$$q$$$ lines you are given with three integers $$$i$$$, $$$j$$$, and $$$x$$$ ($$$1 \le i, j \le n$$$, $$$0 \le x < 2^{30}$$$) — the statements. It is guaranteed that all $$$q$$$ statements hold for at least one array.

On a single line print $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i < 2^{30}$$$) — array $$$a$$$.

module A = Array
module L = List
module I = Int64
module B = Big_int

module Opt = struct
  let get = function
    | None -> assert false
    | Some v -> v
end

let pf = Printf.printf
let sf = Scanf.scanf
let read_int () = sf " %d" (fun x -> x)
let read_array read n = A.init n (fun _ -> read ())

let i_bit x i = (x lsr i) land 1

let () =
  let n = read_int () in
  let q = read_int () in
  let ans = Array.make n 0 in
  let gr = Array.make n [] in
  let qrs = Array.init q (fun _ ->
    let x = read_int () - 1 in
    let y = read_int () - 1 in
    let v = read_int () in
    (x, y, v)
  ) in

  Array.iter (fun (x, y, v) ->
    if (x <> y) then begin
      gr.(x) <- (y, v) :: gr.(x);
      gr.(y) <- (x, v) :: gr.(y);
    end
  ) qrs;

  let bit_val = Array.make n (-1) in
  for b = 29 downto 0 do
    Array.fill bit_val 0 n (-1);
    for i = 0 to q - 1 do
      let (x, y, v) = qrs.(i) in
      (* pf "q = %d %d %d %d\n" i x y v;
      pf "q = %d %d %d %b\n" i  (i_bit v x ) (i_bit v y ) (i_bit v x = 0 && i_bit v y = 0); *)
      if (x = y) then bit_val.(x) <- i_bit v x
      else if (i_bit v b = 0 && i_bit v b = 0) then begin
        bit_val.(x) <- 0;
        bit_val.(y) <- 0;
      end
    done;

    for i = 0 to n - 1 do
      if (bit_val.(i) = -1) then begin
        let is_one = List.exists (fun (y, v) -> bit_val.(y) = 0 && i_bit v b = 1) gr.(i) in
        (* pf "%d %d %b\n" b i is_one; *)
        if (is_one) then bit_val.(i) <- 1
        else bit_val.(i) <- 0;
      end;
      ans.(i) <- ans.(i) lor (bit_val.(i) lsl b);
    done;
  done;

  Array.iter (fun x -> pf "%d " x) ans;

open Printf
open Scanf

let read_int _ = bscanf Scanning.stdin " %d " (fun x -> x)
let read_pair _ = bscanf Scanning.stdin " %d %d " (fun x y -> (x,y))
let () =
  let (n,q) = read_pair () in
  let constr = Array.init q (fun _ ->
    let i = -1 + read_int() in
    let j = -1 + read_int() in
    let x = read_int() in
    (i,j,x)
  ) in

  let req0 = Array.make n 0 in
  let req1 = Array.make n 0 in

  let adj = Array.make n [] in

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

  for k = 0 to q-1 do
    let (i,j,x) = constr.(k) in
    if i=j then (
      req0.(i) <- req0.(i) lor (lnot x);
      req1.(i) <- req1.(i) lor x
    ) else (
      req0.(i) <- req0.(i) lor (lnot x);
      req0.(j) <- req0.(j) lor (lnot x);
      add_edge i j x
    )
  done;

  for i = 0 to n-1 do
    req1.(i) <- List.fold_left (fun ac (j,x) ->
      (req0.(j) land x) lor ac
    ) req1.(i) adj.(i);
    req0.(i) <- lnot req1.(i)
  done;

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


Vlad, like everyone else, loves to sleep very much.Every day Vlad has to do $$$n$$$ things, each at a certain time. For each of these things, he has an alarm clock set, the $$$i$$$-th of them is triggered on $$$h_i$$$ hours $$$m_i$$$ minutes every day ($$$0 \le h_i < 24, 0 \le m_i < 60$$$). Vlad uses the $$$24$$$-hour time format, so after $$$h=12, m=59$$$ comes $$$h=13, m=0$$$ and after $$$h=23, m=59$$$ comes $$$h=0, m=0$$$.This time Vlad went to bed at $$$H$$$ hours $$$M$$$ minutes ($$$0 \le H < 24, 0 \le M < 60$$$) and asks you to answer: how much he will be able to sleep until the next alarm clock.If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length $$$0$$$.

The first line of input data contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases in the test. The first line of the case contains three integers $$$n$$$, $$$H$$$ and $$$M$$$ ($$$1 \le n \le 10, 0 \le H < 24, 0 \le M < 60$$$) — the number of alarms and the time Vlad went to bed. The following $$$n$$$ lines contain two numbers each $$$h_i$$$ and $$$m_i$$$ ($$$0 \le h_i < 24, 0 \le m_i < 60$$$) — the time of the $$$i$$$ alarm. It is acceptable that two or more alarms will trigger at the same time. Numbers describing time do not contain leading zeros.

Output $$$t$$$ lines, each containing the answer to the corresponding test case. As an answer, output two numbers  — the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be 0 0.

(*
   start: 22 00
   stop: 02 15
   if start>stop then
    diff = stop + offset
   else
    diff = stop - start *)
let sleep_time = function
| (start_h, start_m), stop_times ->
  let offset_h, offset_m = if (start_m=0) then ((24-start_h),0) else ((23-start_h), (60-start_m)) in
  let rec get_min_time_diff = function
  | [], _min_time_diff -> _min_time_diff
  | (_stop_h, _stop_m)::_stop_times, (_min_h, _min_m) ->
    if ((_stop_h<start_h)||(_stop_h=start_h && _stop_m<start_m)) then begin
      let _h = _stop_h+offset_h in
      let _m = _stop_m+offset_m in
      let _h, _m = if (_m>=60) then ((_h+1),(_m-60)) else (_h, _m) in
      if ((_h<_min_h) || (_h=_min_h && _m<_min_m)) then get_min_time_diff (_stop_times, (_h, _m))
      else get_min_time_diff (_stop_times, (_min_h, _min_m))
    end
    else begin
      let _h = _stop_h-start_h in
      let _m = _stop_m-start_m in
      let _h, _m = if (_m<0) then ((_h-1), (_m+60)) else (_h, _m) in
      if ((_h<_min_h) || (_h=_min_h && _m<_min_m)) then get_min_time_diff (_stop_times, (_h, _m))
      else get_min_time_diff (_stop_times, (_min_h, _min_m))
    end
  in get_min_time_diff (stop_times, (24, 0))
;;

let read_int ()= Scanf.scanf " %d" (fun x-> x);;
let read_time ()= Scanf.scanf " %d %d" (fun h m -> h,m);;

let run_case = function
| () ->
  let n = read_int () in
  let s_h = read_int () in
  let s_m =read_int () in
  let start_time = (s_h, s_m) in
  let rec read_stop_times = function
  | 0 -> []
  | x ->
    let h=read_int () in
    let m=read_int () in
    (h,m)::read_stop_times (x-1)
  in
  let stop_times= read_stop_times n in
  let min_h, min_m = sleep_time (start_time, stop_times) in
  Printf.printf "%d %d\n" min_h min_m
;;

let rec iter_cases = function
| 0 -> ()
| n  -> run_case (); iter_cases (n-1)
;;

let t = read_int () in iter_cases t;;
(*
let h, m = sleep_time ((6,0), [(12, 30); (14, 45)]) in
Printf.printf "%d %d" h m;;  *)

(*
   start: 22 00
   stop: 02 15
   if start>stop then
    diff = stop + offset
   else
    diff = stop - start *)
let sleep_time = function
| (start_h, start_m), stop_times ->
  let offset_h = 23-start_h in
  let offset_m = if (start_m=0) then 0 else 60-start_m in
  let rec get_min_time_diff = function
  | [], _min_time_diff -> _min_time_diff
  | (_stop_h, _stop_m)::_stop_times, (_min_h, _min_m) ->
    if ((_stop_h<start_h)||(_stop_h=start_h && _stop_m<start_m)) then begin
      let _h = _stop_h+offset_h in
      let _m = _stop_m+offset_m in
      let _h, _m = if (_m>=60) then ((_h+1),(_m-60)) else (_h, _m) in
      if ((_h<_min_h) || (_h=_min_h && _m<_min_m)) then get_min_time_diff (_stop_times, (_h, _m))
      else get_min_time_diff (_stop_times, (_min_h, _min_m))
    end
    else begin
      let _h = _stop_h-start_h in
      let _m = _stop_m-start_m in
      let _h, _m = if (_m<0) then ((_h-1), (_m+60)) else (_h, _m) in
      if ((_h<_min_h) || (_h=_min_h && _m<_min_m)) then get_min_time_diff (_stop_times, (_h, _m))
      else get_min_time_diff (_stop_times, (_min_h, _min_m))
    end
  in get_min_time_diff (stop_times, (24, 0))
;;

let read_int ()= Scanf.scanf " %d" (fun x-> x);;
let read_time ()= Scanf.scanf " %d %d" (fun h m -> h,m);;

let run_case = function
| () ->
  let n = read_int () in
  let s_h = read_int () in
  let s_m =read_int () in
  let start_time = (s_h, s_m) in
  let rec read_stop_times = function
  | 0 -> []
  | x ->
    let h=read_int () in
    let m=read_int () in
    (h,m)::read_stop_times (x-1)
  in
  let stop_times= read_stop_times n in
  let min_h, min_m = sleep_time (start_time, stop_times) in
  Printf.printf "%d %d\n" min_h min_m
;;

let rec iter_cases = function
| 0 -> ()
| n  -> run_case (); iter_cases (n-1)
;;

let t = read_int () in iter_cases t;;
(*
let h, m = sleep_time ((6,0), [(12, 30); (14, 45)]) in
Printf.printf "%d %d" h m;;  *)


Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n × m cells and a robotic arm. Each cell of the field is a 1 × 1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized — if you move one of the lasers by a vector, another one moves by the same vector.The following facts about the experiment are known:   initially the whole field is covered with a chocolate bar of the size n × m, both lasers are located above the field and are active;  the chocolate melts within one cell of the field at which the laser is pointed;  all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells;  at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman. You are given n, m and the cells (x1, y1) and (x2, y2), where the lasers are initially pointed at (xi is a column number, yi is a row number). Rows are numbered from 1 to m from top to bottom and columns are numbered from 1 to n from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions.

The first line contains one integer number t (1 ≤ t ≤ 10000) — the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers n, m, x1, y1, x2, y2, separated by a space (2 ≤ n, m ≤ 109, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m). Cells (x1, y1) and (x2, y2) are distinct.

Each of the t lines of the output should contain the answer to the corresponding input test set.

let read_int64 _ = Scanf.bscanf Scanf.Scanning.stdib " %Ld" (fun x -> x)
let (|>) x f = f x

open Int64

let (++) x y = add x y
let (--) x y = sub x y
let ( ** ) x y = mul x y

let () =
  for cc = 1 to read_int () do
    let n = read_int64 0 in
    let m = read_int64 0 in
    let x = read_int64 0 in
    let y = read_int64 0 in
    let xx = read_int64 0 in
    let yy = read_int64 0 in
    let nn = n -- abs (x--xx) in
    let mm = m -- abs (y--yy) in
    n**m--nn**mm**2L++max (nn**2L--n) 0L**max (mm**2L--m) 0L |> Printf.printf "%Ld\n"
  done

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

let () =
  for cc = 1 to read_int () do
    let n = read_int 0 in
    let m = read_int 0 in
    let x = read_int 0 in
    let y = read_int 0 in
    let xx = read_int 0 in
    let yy = read_int 0 in
    let nn = n - abs (x-xx) in
    let mm = m - abs (y-yy) in
    n*m-nn*mm*2+max (nn*2-n) 0*max (mm*2-m) 0 |> Printf.printf "%d\n"
  done


You are given an array of integers. Vasya can permute (change order) its integers. He wants to do it so that as many as possible integers will become on a place where a smaller integer used to stand. Help Vasya find the maximal number of such integers.For instance, if we are given an array $$$[10, 20, 30, 40]$$$, we can permute it so that it becomes $$$[20, 40, 10, 30]$$$. Then on the first and the second positions the integers became larger ($$$20>10$$$, $$$40>20$$$) and did not on the third and the fourth, so for this permutation, the number that Vasya wants to maximize equals $$$2$$$. Read the note for the first example, there is one more demonstrative test case.Help Vasya to permute integers in such way that the number of positions in a new array, where integers are greater than in the original one, is maximal.

The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 10^5$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the elements of the array.

Print a single integer — the maximal number of the array's elements which after a permutation will stand on the position where a smaller element stood in the initial array.

NoteIn the first sample, one of the best permutations is $$$[1, 5, 5, 3, 10, 1, 1]$$$. On the positions from second to fifth the elements became larger, so the answer for this permutation is 4.In the second sample, there is no way to increase any element with a permutation, so the answer is 0.

open Printf open Scanf
module MyInt64 = struct
	(* include Int64 *)
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	let (+) p q = Int64.add p q
	let (-) p q = Int64.sub p q
	let ( * ) p q = Int64.mul p q
	let (/) p q = Int64.div p q

	let labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p

	let (+=) r v = r := !r + v
	let (-=) r v = r := !r - v
	let ( *=) r v= r := !r * v
	let (/=) r v = r := !r / v

	let input_i64_array n =
		Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
	let get_i64 () = Scanf.scanf " %Ld" (fun v -> v)
	let get_2_i64 () = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
	let get_3_i64 () = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
	let get_4_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
	let get_5_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
	let print_i64_endline n =
		n |> Int64.to_string |> print_endline
end open MyInt64

module IMap = Map.Make (struct
	type t = int64
	let compare = compare
end)

exception Hoge
exception Fuga

let () =
	let n = get_i64 ()
	in let ar = input_i64_array n
	in Array.sort (fun u v -> compare u v) ar;
	let lef,rig = ref 0, ref 0
	in
	(* print_endline @@ Array.fold_left (fun u v -> u ^ (sprintf "%Ld " v)) "" ar; *)
	while (!lef < ~|n && !rig < ~|n) do
		(* printf "  %d -- %d : %Ld -- %Ld\n" !lef !rig ar.(!lef) ar.(!rig); *)
		if ar.(!rig) > ar.(!lef) then (
			lef := !lef +$ 1;
			rig := !rig +$ 1;
		) else (
			rig := !rig +$ 1;
		);
	done;
	~|n -$ (!rig -$ !lef)
	|> printf "%d\n"

open Printf open Scanf
module MyInt64 = struct
	(* include Int64 *)
	(* int *)
	let (+$) = (+)
	let (-$) = (-)
	let (/$) = (/)
	let ( *$) = ( * )
	(* int64 *)
	let (+) p q = Int64.add p q
	let (-) p q = Int64.sub p q
	let ( * ) p q = Int64.mul p q
	let (/) p q = Int64.div p q

	let labs p = if p < 0L then -1L*p else p
	(* let (~|) p = Int64.of_int p *)
	let (~|) p = Int64.to_int p

	let (+=) r v = r := !r + v
	let (-=) r v = r := !r - v
	let ( *=) r v= r := !r * v
	let (/=) r v = r := !r / v

	let input_i64_array n =
		Array.init (~| n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
	let get_i64 () = Scanf.scanf " %Ld" (fun v -> v)
	let get_2_i64 () = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
	let get_3_i64 () = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
	let get_4_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
	let get_5_i64 () = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
	let print_i64_endline n =
		n |> Int64.to_string |> print_endline
end open MyInt64

module IMap = Map.Make (struct
	type t = int64
	let compare = compare
end)

exception Hoge
exception Fuga

let () =
	let n = get_i64 ()
	in let ar = input_i64_array n
	in Array.sort (fun u v -> compare u v) ar;
	let lef,rig = ref 0, ref 0
	in
	print_endline @@ Array.fold_left (fun u v -> u ^ (sprintf "%Ld " v)) "" ar;
	while (!lef < ~|n && !rig < ~|n) do
		(* printf "  %d -- %d : %Ld -- %Ld\n" !lef !rig ar.(!lef) ar.(!rig); *)
		if ar.(!rig) > ar.(!lef) then (
			lef := !lef +$ 1;
			rig := !rig +$ 1;
		) else (
			rig := !rig +$ 1;
		);
	done;
	~|n -$ (!rig -$ !lef)
	|> printf "%d\n"


There are $$$n$$$ bags with candies, initially the $$$i$$$-th bag contains $$$i$$$ candies. You want all the bags to contain an equal amount of candies in the end. To achieve this, you will: Choose $$$m$$$ such that $$$1 \le m \le 1000$$$Perform $$$m$$$ operations. In the $$$j$$$-th operation, you will pick one bag and add $$$j$$$ candies to all bags apart from the chosen one.Your goal is to find a valid sequence of operations after which all the bags will contain an equal amount of candies. It can be proved that for the given constraints such a sequence always exists.You don't have to minimize $$$m$$$.If there are several valid sequences, you can output any.

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). Description of the test cases follows. The first and only line of each test case contains one integer $$$n$$$ ($$$2 \le n\le 100$$$).

For each testcase, print two lines with your answer.  In the first line print $$$m$$$ ($$$1\le m \le 1000$$$) — the number of operations you want to take.  In the second line print $$$m$$$ positive integers $$$a_1, a_2, \dots, a_m$$$ ($$$1 \le a_i \le n$$$), where $$$a_j$$$ is the number of bag you chose on the $$$j$$$-th operation.

NoteIn the first case, adding $$$1$$$ candy to all bags except of the second one leads to the arrangement with $$$[2, 2]$$$ candies.In the second case, firstly you use first three operations to add $$$1+2+3=6$$$ candies in total to each bag except of the third one, which gives you $$$[7, 8, 3]$$$. Later, you add $$$4$$$ candies to second and third bag, so you have $$$[7, 12, 7]$$$, and $$$5$$$ candies to first and third bag  — and the result is $$$[12, 12, 12]$$$.

let () =
  let t = () |> read_line |> int_of_string in
  for i = 1 to t do
    let n = () |> read_line |> int_of_string in
    n |> string_of_int |> print_endline;
    for j = 1 to n do
      j |> string_of_int |> print_string;
      print_char ' '
    done;
    print_endline ""
  done


Little girl Margarita is a big fan of competitive programming. She especially loves problems about arrays and queries on them.Recently, she was presented with an array $$$a$$$ of the size of $$$10^9$$$ elements that is filled as follows:   $$$a_1 = -1$$$  $$$a_2 = 2$$$  $$$a_3 = -3$$$  $$$a_4 = 4$$$  $$$a_5 = -5$$$  And so on ... That is, the value of the $$$i$$$-th element of the array $$$a$$$ is calculated using the formula $$$a_i = i \cdot (-1)^i$$$.She immediately came up with $$$q$$$ queries on this array. Each query is described with two numbers: $$$l$$$ and $$$r$$$. The answer to a query is the sum of all the elements of the array at positions from $$$l$$$ to $$$r$$$ inclusive.Margarita really wants to know the answer to each of the requests. She doesn't want to count all this manually, but unfortunately, she couldn't write the program that solves the problem either. She has turned to you — the best programmer.Help her find the answers!

The first line contains a single integer $$$q$$$ ($$$1 \le q \le 10^3$$$) — the number of the queries. Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le 10^9$$$) — the descriptions of the queries.

Print $$$q$$$ lines, each containing one number — the answer to the query.

NoteIn the first query, you need to find the sum of the elements of the array from position $$$1$$$ to position $$$3$$$. The sum is equal to $$$a_1 + a_2 + a_3 = -1 + 2 -3 = -2$$$.In the second query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$5$$$. The sum is equal to $$$a_2 + a_3 + a_4 + a_5 = 2 -3 + 4 - 5 = -2$$$.In the third query, you need to find the sum of the elements of the array from position $$$5$$$ to position $$$5$$$. The sum is equal to $$$a_5 = -5$$$.In the fourth query, you need to find the sum of the elements of the array from position $$$4$$$ to position $$$4$$$. The sum is equal to $$$a_4 = 4$$$.In the fifth query, you need to find the sum of the elements of the array from position $$$2$$$ to position $$$3$$$. The sum is equal to $$$a_2 + a_3 = 2 - 3 = -1$$$.

open Printf
open Scanf

let (--) = Int64.sub
let (//) = Int64.div
let (%%) = Int64.rem

let sum = function
    r when r %% 2L = 0L -> r // 2L
  | r -> r // 2L -- r

let () =
  let q = scanf " %d" (fun x -> x) in
  for i = 0 to q-1 do
    let l, r = scanf " %Ld %Ld" (fun x y -> x,y) in
    printf "%Ld\n" @@ sum r -- sum (l -- 1L)
  done

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

(* let () =
  let hoge = init 100 (fun i -> let i = i+$1 in i*$(if i %$ 2=0 then 1 else -1))
  in
  print_array (sprintf "%3d") hoge;
  let cumul = make 100 0 in
  iteri (fun i v ->
    if i<>0 then cumul.(i) <- cumul.(i-$1)+$hoge.(i-$1)
  ) cumul;
  print_array (sprintf "%3d") cumul *)
let f l = l*(if l mod 2L=0L then 1L else -1L)
let g l = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L)
let () =
  let q = get_i64 0 in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    printf "%Ld\n" @@
    if l=r then f l
    (* else if r=l+1L then f l + f (l+1L)
    else if r=l+2L then f l + f (l+1L) + f (l+2L)
    (*let vl = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L) in
    let vr = ceildiv (r) 2L*(if r mod 2L=0L then 1L else -1L) in
    (* printf "%Ld\n" @@ vl + vr *)
    printf "%Ld\n" @@ vl + vr; *)
    else if l = 1L then g r
    else
    (* if l mod 2L=0L && (r-l) mod 2L=0L then g l + g r
    else if l mod 2L=1L && (r-l) mod 2L=0L then 0L
    else if l mod 2L=0L && (r-l) mod 2L=1L then g l + g r
    else 0L *)
    g l + g r *)
    else
    (* if l=1L then g r
    else g (l-1L) + g r *)
    g r - g (l-1L)
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

(* let () =
  let hoge = init 100 (fun i -> let i = i+$1 in i*$(if i %$ 2=0 then 1 else -1))
  in
  print_array (sprintf "%3d") hoge;
  let cumul = make 100 0 in
  iteri (fun i v ->
    if i<>0 then cumul.(i) <- cumul.(i-$1)+$hoge.(i-$1)
  ) cumul;
  print_array (sprintf "%3d") cumul *)
let f l = l*(if l mod 2L=0L then 1L else -1L)
let g l = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L)
let () =
  let q = get_i64 0 in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    printf "%Ld\n" @@
    if l=r then f l
    else if r=l+1L then f l + f (l+1L)
    else if r=l+2L then f l + f (l+1L) + f (l+2L)
    (*let vl = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L) in
    let vr = ceildiv (r) 2L*(if r mod 2L=0L then 1L else -1L) in
    (* printf "%Ld\n" @@ vl + vr *)
    printf "%Ld\n" @@ vl + vr; *)
    else
    (* if l mod 2L=0L && (r-l) mod 2L=0L then g l + g r
    else if l mod 2L=1L && (r-l) mod 2L=0L then 0L
    else if l mod 2L=0L && (r-l) mod 2L=1L then g l + g r
    else 0L *)
    g l + g r
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

(* let () =
  let hoge = init 100 (fun i -> let i = i+$1 in i*$(if i %$ 2=0 then 1 else -1))
  in
  print_array (sprintf "%3d") hoge;
  let cumul = make 100 0 in
  iteri (fun i v ->
    if i<>0 then cumul.(i) <- cumul.(i-$1)+$hoge.(i-$1)
  ) cumul;
  print_array (sprintf "%3d") cumul; *)
let f l = l*(if l mod 2L=0L then 1L else -1L)
let g l = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L)
let () =
  let q = get_i64 0 in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    printf "%Ld\n" @@
    if l=r then f l
    else if r=l+1L then f l + f (l+1L)
    else if r=l+2L then f l + f (l+1L) + f (l+2L)
    (*let vl = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L) in
    let vr = ceildiv (r) 2L*(if r mod 2L=0L then 1L else -1L) in
    (* printf "%Ld\n" @@ vl + vr *)
    printf "%Ld\n" @@ vl + vr; *)
    else if l mod 2L=0L && (r-l) mod 2L=0L then g l + g r
    else if l mod 2L=1L && (r-l) mod 2L=0L then 0L
    else if l mod 2L=0L && (r-l) mod 2L=1L then g r
    else 0L
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

(* let () =
  let hoge = init 100 (fun i -> let i = i+$1 in i*$(if i %$ 2=0 then 1 else -1))
  in
  print_array (sprintf "%3d") hoge;
  let cumul = make 100 0 in
  iteri (fun i v ->
    if i<>0 then cumul.(i) <- cumul.(i-$1)+$hoge.(i-$1)
  ) cumul;
  print_array (sprintf "%3d") cumul *)
let f l = l*(if l mod 2L=0L then 1L else -1L)
let g l = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L)
let () =
  let q = get_i64 0 in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    printf "%Ld\n" @@
    if l=r then f l
    else if r=l+1L then f l + f (l+1L)
    else if r=l+2L then f l + f (l+1L) + f (l+2L)
    (*let vl = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L) in
    let vr = ceildiv (r) 2L*(if r mod 2L=0L then 1L else -1L) in
    (* printf "%Ld\n" @@ vl + vr *)
    printf "%Ld\n" @@ vl + vr; *)
    else if l mod 2L=0L && (r-l) mod 2L=0L then g l + g r
    else if l mod 2L=1L && (r-l) mod 2L=0L then 0L
    else if l mod 2L=0L && (r-l) mod 2L=1L then g l + g r
    else 0L
  )

open Printf open Scanf
module Lib = struct
  let i32,i64,ist = Int64.(to_int,of_int,to_string)
  let (+$) = (+) let (-$) = (-) let (/$) = (/) let ( *$) = ( * ) let (%$) = (mod)
  let labs = Int64.abs
  let (+),(-),( * ),(/),(mod),(%) = Int64.(add,sub,mul,div,rem,rem)
  let (+=) r v = r := !r + v
  let (-=) r v = r := !r - v
  let ( *= ) r v = r := !r * v
  let (/=) r v = r := !r / v
  let (%=) r v = r := !r % v
  let max_i64,min_i64 = Int64.(max_int,min_int)
  (* math *)
  let ceildiv p q = (p+q-1L) / q
  let rec gcd m n = match m,n with | m,0L -> m | m,n -> gcd n (m mod n)
  let lcm m n = m / gcd m n * n
  module I64_infix = struct
    let (land),(lor),(lxor),lnot,(lsl),(lsr),(asr) =
      let open Int64 in
      (logand),(logor),(logxor),lognot,
      (fun u v -> shift_left u (i32 v)),
      (fun u v -> shift_right u (i32 v)),
      (fun u v -> shift_right_logical u (i32 v))
  end
  (* input *)
  let input_i64_array n = Array.init (i32 n) (fun _ -> Scanf.scanf " %Ld" (fun v -> v))
  let get_i64 _ = Scanf.scanf " %Ld" (fun v -> v)
  let get_2_i64 _ = Scanf.scanf " %Ld %Ld" (fun u v -> u,v)
  let get_3_i64 _ = Scanf.scanf " %Ld %Ld %Ld" (fun u v w -> u,v,w)
  let get_4_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld" (fun u v w x -> u,v,w,x)
  let get_5_i64 _ = Scanf.scanf " %Ld %Ld %Ld %Ld %Ld" (fun u v w x y -> u,v,w,x,y)
  let i32_get_int _ = Scanf.scanf " %d" (fun v -> v) let i32_get_2_ints _ = Scanf.scanf " %d %d" (fun u v -> u,v) let i32_get_3_ints _ = Scanf.scanf " %d %d %d" (fun u v w -> u,v,w) let i32_get_4_ints _ = Scanf.scanf " %d %d %d %d" (fun u v w x -> u,v,w,x) let i32_get_5_ints _ = Scanf.scanf " %d %d %d %d %d" (fun u v w x y -> u,v,w,x,y)
  (* utils *)
  external id : 'a -> 'a = "%identity"
  let ( *< ) f g x = f (g x)
  let ( *> ) f g x = g (f x)
  let rv f p q = f q p (* ex. 24 |> rv (/) 6 *)
  let alen a = i64 @@ Array.length a
  let llen l = i64 @@  List.length l
  let string_of_list ?(separator=" ") f ls = ls |> List.map f |> String.concat separator
  let string_of_array ?(separator=" ") f a = a |> Array.to_list |> string_of_list ~separator f
  let char_list_of_string str = let rec f0 i a = if i<0 then a else f0 (i-$1) (str.[i]::a) in f0 (String.length str -$ 1) []
  let string_of_char_list ls = List.fold_left (fun u v -> u ^ (String.make 1 v)) "" ls
  let fail _ = failwith "fail"
  let dx,dy = [|1;0;-1;0|],[|0;1;0;-1|]
  let range l r = let ls = ref [] in for i=i32 l to i32 r do ls := i :: !ls done;!ls |> List.rev
  let cartesian_product u v = u |> List.map (fun p -> List.map (fun q -> p,q) v) |> List.flatten
  let shuffle a = Array.sort (fun _ _ -> (Random.int 3) -$ 1) a
  let binary_search ng ok f = let d = if ng < ok then 1L else -1L in
    let rec f0 ng ok =
      if labs (ok - ng) <= 1L then ok
      else let mid = ng + (ok - ng) / 2L in if f mid then f0 ng mid else f0 mid ok
    in f0 (ng-1L*d) (ok+1L*d)
  let lower_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >= v)
  let upper_bound a v = binary_search 0L (alen a - 1L) (fun i -> a.(i32 i) >  v)
  let equal_range a v = lower_bound a v, upper_bound a v (* #=snd-fst *)
  let rec fix f x = f (fix f) x
  let fix_memo ?(size=10000) f x =
    let tb = Hashtbl.create ~random:true size in
    let rec f0 x =
      try Hashtbl.find tb x with
      | Not_found -> (let v = f f0 x in Hashtbl.add tb x v; v)
    in f0 x
  (* imperative *)
  let rep f t ?(stride=1L) fbod = let i = ref f in while !i <= t do fbod !i; i := !i + stride done
  let repb f t ?(stride=1L) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i + stride done
  let repm f t ?(stride=1L) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i + stride done; !m
  let repi f t ?(stride=1) fbod = let i = ref f in while !i <= t do fbod !i; i := !i +$ stride done
  let repib f t ?(stride=1) fbod = let i,c = ref f,ref true in while !i <= t && !c do
    match fbod !i with | `Break -> c := false | `Ok -> i := !i +$ stride done
  let repim f t ?(stride=1) m0 fbod =
    let i,c,m = ref f,ref true,ref m0 in
    while !i<=t && !c do match fbod !m !i with
    | `Break -> c := false | `Break_m m' -> (c := false; m := m') | `Ok m' -> m := m'; i := !i +$ stride done; !m
  let rep_rev f t ?(stride=1L) fbod = rep t f ~stride @@ fun v -> fbod @@ f + t - v
  let repb_rev f t ?(stride=1L) fbod = repb t f ~stride @@ fun v -> fbod @@ f + t - v
  let repm_rev f t ?(stride=1L) m0 fbod = repm f t ~stride m0 @@ fun u v -> fbod u @@ f + t - v
  let repi_rev f t ?(stride=1) fbod = repi t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repib_rev f t ?(stride=1) fbod = repib t f ~stride @@ fun v -> fbod @@ f +$ t -$ v
  let repim_rev f t ?(stride=1) m0 fbod = repim f t ~stride m0 @@ fun u v -> fbod u @@ f +$ t -$ v
  (* output *)
  let print_list_mle f ls = string_of_list f ls |> print_endline
  let print_array_mle f a = string_of_array f a |> print_endline
  let print_list f ls = ls |> List.iter (fun v -> printf "%s " @@ f v); print_newline ()
  let print_array f a = a |> Array.iter (fun v -> printf "%s " @@ f v); print_newline ()
  (* debug *)
  let dump_2darr f a = a |> Array.iter (fun b -> print_array f b)
  let dump_i64_list ls = print_list ist ls
  let dump_i64_array a = print_array ist a
end open Lib open Array

(* let () =
  let hoge = init 100 (fun i -> let i = i+$1 in i*$(if i %$ 2=0 then 1 else -1))
  in
  print_array (sprintf "%3d") hoge;
  let cumul = make 100 0 in
  iteri (fun i v ->
    if i<>0 then cumul.(i) <- cumul.(i-$1)+$hoge.(i-$1)
  ) cumul;
  print_array (sprintf "%3d") cumul *)
let f l = l*(if l mod 2L=0L then 1L else -1L)
let g l = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L)
let () =
  let q = get_i64 0 in
  rep 1L q (fun _ ->
    let l,r = get_2_i64 0 in
    printf "%Ld\n" @@
    if l=r then f l
    else if r=l+1L then f l + f (l+1L)
    else if r=l+2L then f l + f (l+1L) + f (l+2L)
    (*let vl = ceildiv (l) 2L*(if l mod 2L=0L then 1L else -1L) in
    let vr = ceildiv (r) 2L*(if r mod 2L=0L then 1L else -1L) in
    (* printf "%Ld\n" @@ vl + vr *)
    printf "%Ld\n" @@ vl + vr; *)
    else if l = 1L then g r
    else
    (* if l mod 2L=0L && (r-l) mod 2L=0L then g l + g r
    else if l mod 2L=1L && (r-l) mod 2L=0L then 0L
    else if l mod 2L=0L && (r-l) mod 2L=1L then g l + g r
    else 0L *)
    g l + g r
  )


You are given a program that consists of $$$n$$$ instructions. Initially a single variable $$$x$$$ is assigned to $$$0$$$. Afterwards, the instructions are of two types:   increase $$$x$$$ by $$$1$$$;  decrease $$$x$$$ by $$$1$$$. You are given $$$m$$$ queries of the following format:   query $$$l$$$ $$$r$$$ — how many distinct values is $$$x$$$ assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order?

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then the description of $$$t$$$ testcases follows. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of instructions in the program and the number of queries. The second line of each testcase contains a program — a string of $$$n$$$ characters: each character is either '+' or '-' — increment and decrement instruction, respectively. Each of the next $$$m$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the description of the query. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

For each testcase print $$$m$$$ integers — for each query $$$l$$$, $$$r$$$ print the number of distinct values variable $$$x$$$ is assigned to if all the instructions between the $$$l$$$-th one and the $$$r$$$-th one inclusive are ignored and the rest are executed without changing the order.

NoteThe instructions that remain for each query of the first testcase are:   empty program — $$$x$$$ was only equal to $$$0$$$;  "-" — $$$x$$$ had values $$$0$$$ and $$$-1$$$;  "---+" — $$$x$$$ had values $$$0$$$, $$$-1$$$, $$$-2$$$, $$$-3$$$, $$$-2$$$ — there are $$$4$$$ distinct values among them;  "+--+--+" — the distinct values are $$$1$$$, $$$0$$$, $$$-1$$$, $$$-2$$$.

open Str
open Big_int
open Num

let multitest = true

module Int = struct
   type t = int
   let compare = compare
 end

module Ints = Set.Make(Int)

module Shortcuts = struct
  let loop ?(from = 0) n f = for i = from to n - 1 do f i; done
  let rep ?(too = 0) n f = for i = n-1 downto too do f i; done

end

module Helper = struct
  open Shortcuts

  let read_int ~endl () =
    if endl then Scanf.scanf "%d\n" (fun x -> x)
    else Scanf.scanf "%d " (fun x -> x)

  let read_s () = Scanf.scanf "%s\n" (fun x -> x)

  type _ t =
    | I : int t
    | S : string t
    | II : (int * int) t
    | A : int -> int array t

  let read x =
    let rec aux : type a. bool -> a t -> a  = fun endl -> function
      | I -> read_int ~endl ()
      | S -> read_s ()
      | II ->
        let l = aux false I in
        let r = aux true I in
        l, r
      | A n ->
        let a = Array.make n 0 in
        loop (n-1) (fun i -> a.(i) <- aux false I);
        a.(n-1) <- aux true I;
        a
    in
    aux true x

end

module Ext = struct
  module List = struct
    let rec make n x =
      if (n <= 0) then []
      else x :: make (n-1) x

  end

end

module Alg = struct
  let rec gcd x y =
    if y <> 0 then (gcd y (x mod y))
    else (abs x)

  let lcm x y =
    if x = 0 || y = 0 then 0
    else abs (x * y) / (gcd x y)

end

let solve _ =
  let open Shortcuts in
  let open Helper in
  let open Printf in
  let open Ext in
  let open Alg in
  (* ***** Solution code here ***** *)
  let n, d = read II in
  let s = read S in
  let pref = Array.make (n + 1) (0,0,0) in
  let x = ref 0 in
  let pmin = ref 0 in
  let pmax = ref 0 in
  loop n (fun i ->
      if String.get s i = '-' then begin
        decr x;
        if !x < !pmin then pmin := !x
      end else begin
        incr x;
        if !pmax < !x then pmax := !x
      end;
      pref.(i+1) <- (!pmin, !x, !pmax)
    );
  let suf = Array.make (n+1) (0,0,0) in
  let x = ref 0 in
  let smin = ref 0 in
  let smax = ref 0 in
  loop (n - 1) (fun i ->
      if String.get s (n-i-1) = '+' then begin
        decr x;
        if !x < !smin then smin := !x
      end else begin
        incr x;
        if !smax < !x then smax := !x
      end;
      suf.(n-1-i) <- (!smin, !x, !smax)
    );
  loop d (fun i ->
      let l,r = read II in
      let p_min, p_last, p_max = pref.(l-1) in
      let s_min, s_first, s_max = suf.(r) in
      printf "%d\n" (max p_max (p_last + s_max - s_first) - min p_min (p_last + s_min - s_first) + 1);
    );
  (* ***** ******************* ***** *)
;;

let () =
  let open Helper in
  let open Shortcuts in
  let t = if multitest then read I else 1 in
  loop t solve;;


Sherlock Holmes and Dr. Watson played some game on a checkered board n × n in size. During the game they put numbers on the board's squares by some tricky rules we don't know. However, the game is now over and each square of the board contains exactly one number. To understand who has won, they need to count the number of winning squares. To determine if the particular square is winning you should do the following. Calculate the sum of all numbers on the squares that share this column (including the given square) and separately calculate the sum of all numbers on the squares that share this row (including the given square). A square is considered winning if the sum of the column numbers is strictly greater than the sum of the row numbers.For instance, lets game was ended like is shown in the picture. Then the purple cell is winning, because the sum of its column numbers equals 8 + 3 + 6 + 7 = 24, sum of its row numbers equals 9 + 5 + 3 + 2 = 19, and 24 > 19.

The first line contains an integer n (1 ≤ n ≤ 30). Each of the following n lines contain n space-separated integers. The j-th number on the i-th line represents the number on the square that belongs to the j-th column and the i-th row on the board. All number on the board are integers from 1 to 100.

Print the single number — the number of the winning squares.

NoteIn the first example two upper squares are winning.In the third example three left squares in the both middle rows are winning:5 7 8 49 5 3 21 6 6 49 5 7 3

open Scanf
open Printf

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

let main () =
  let n : int = read_int () in
  let cols = Array.make n 0 in
  let rows = Array.make n 0 in
  let rec compute i j =
    let num = read_int () in
    Array.set cols j ((Array.get cols j)+num);
    Array.set rows i ((Array.get rows i)+num);
    match i, j with
      | 0, 0 -> ()
      | _, 0 -> compute (i-1) (n-1)
      | _, _ -> compute i (j-1)
  in

  compute (n-1) (n-1);

  let total =
    Array.fold_left (fun total i ->
      Array.fold_left (fun total j -> if (i > j) then total+1 else total)
        total
        rows)
      0
      cols
  in
  print_int total

let () = main ()

(*
  let rec compute i =
    let line = read_line () in
    let nums = Str.split (Str.regexp "\b") line in
    ignore (List.fold_left (fun j s ->
      let num = int_of_string s in
      Array.set cols j ((Array.get cols j)+num);
      Array.set rows i ((Array.get rows i)+num);
      j-1
    ) (n-1) nums);
    if i == 0
    then ()
    else compute (i-1)
  in
*)


There are $$$n$$$ candies in a row, they are numbered from left to right from $$$1$$$ to $$$n$$$. The size of the $$$i$$$-th candy is $$$a_i$$$.Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).Alice makes the first move. During the first move, she will eat $$$1$$$ candy (its size is $$$a_1$$$). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.For example, if $$$n=11$$$ and $$$a=[3,1,4,1,5,9,2,6,5,3,5]$$$, then:  move 1: Alice eats one candy of size $$$3$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3,5]$$$.  move 2: Alice ate $$$3$$$ on the previous move, which means Bob must eat $$$4$$$ or more. Bob eats one candy of size $$$5$$$ and the sequence of candies becomes $$$[1,4,1,5,9,2,6,5,3]$$$.  move 3: Bob ate $$$5$$$ on the previous move, which means Alice must eat $$$6$$$ or more. Alice eats three candies with the total size of $$$1+4+1=6$$$ and the sequence of candies becomes $$$[5,9,2,6,5,3]$$$.  move 4: Alice ate $$$6$$$ on the previous move, which means Bob must eat $$$7$$$ or more. Bob eats two candies with the total size of $$$3+5=8$$$ and the sequence of candies becomes $$$[5,9,2,6]$$$.  move 5: Bob ate $$$8$$$ on the previous move, which means Alice must eat $$$9$$$ or more. Alice eats two candies with the total size of $$$5+9=14$$$ and the sequence of candies becomes $$$[2,6]$$$.  move 6 (the last): Alice ate $$$14$$$ on the previous move, which means Bob must eat $$$15$$$ or more. It is impossible, so Bob eats the two remaining candies and the game ends. Print the number of moves in the game and two numbers:  $$$a$$$ — the total size of all sweets eaten by Alice during the game;  $$$b$$$ — the total size of all sweets eaten by Bob during the game.

The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases in the input. The following are descriptions of the $$$t$$$ test cases. Each test case consists of two lines. The first line contains an integer $$$n$$$ ($$$1 \le n \le 1000$$$) — the number of candies. The second line contains a sequence of integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 1000$$$) — the sizes of candies in the order they are arranged from left to right. It is guaranteed that the sum of the values of $$$n$$$ for all sets of input data in a test does not exceed $$$2\cdot10^5$$$.

For each set of input data print three integers — the number of moves in the game and the required values $$$a$$$ and $$$b$$$.

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

open Printf

let init (x : int) (f : int -> 'a) : 'a list =
  let rec build k =
    if k = x then []
    else (f k) :: build (k + 1) in
  build 0
;;

let run () =
  let n = scan_int () in
  let a = Array.init n (fun _x -> scan_int ()) in
  let posA = ref 0 and posB = ref (n-1) in
  let totA = ref 0 and totB = ref 0 in
  let prv = ref 0 and now = ref 0 in
  let turn = ref 0 and totturn = ref 0 in
  while !posA <= !posB do
    if !turn = 0 then begin
      now  := !now  + a.(!posA);
      totA := !totA + a.(!posA);
      posA := !posA + 1
    end
    else begin
      now  := !now  + a.(!posB);
      totB := !totB + a.(!posB);
      posB := !posB - 1
    end;
    if !now > !prv then begin
      prv := !now;
      now := 0;
      turn := 1 - !turn;
      totturn := !totturn + 1
    end
  done;
  if !now > 0 then incr totturn;
  let lst = [!totturn; !totA; !totB] in
  List.iter (printf "%d ") lst;
  print_endline ""
;;

let () =
  let t = scan_int () in
  for i = 1 to t do
    run ()
  done
;;

(* Codeforces 1352 D Alice, Bob and Candies comp   *)

(* you can use either gr or rdln but not both *)
let gr () = Scanf.scanf " %d" (fun i -> i);;
let grf () = Scanf.scanf " %f" (fun i -> i);;
let grl () = Scanf.scanf " %Ld" (fun i -> i);;
let rdln() = input_line stdin;;
(* let spli s = Str.split (Str.regexp " ") s;; *)
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printarri ai = Array.iter (fun i -> (print_int i; print_string " ")) ai;;
let debug = false;;

(* reading reversed list *)
let rec readlist n acc = match n with
	| 0 -> acc
	| _ -> readlist (n -1) (gr() :: acc);;
let printlisti li = List.iter (fun i -> (print_int i; print_string " ")) li;;
let printlists ls = List.iter (fun s -> (print_string s; print_string " ")) ls;;

(* ============= above library below custom ========== *)

let rec alice ia sma ib smb lastbob nmov v =
	let rec eat i eaten =
		(* let _ = Printf.printf "al eat i eaten = %d %d\n" i eaten in *)
		if eaten > lastbob then (i, eaten)
		else if i > ib then (i, eaten)
		else
			let eaten = eaten + v.(i) in
			let i = i + 1 in
			eat i eaten in
	let idx,aeat = eat ia 0 in
	let sma = sma + aeat in
  let nmov = nmov + 1 in
	if idx > ib then (nmov, sma, smb)
	else
	bob idx sma ib smb aeat nmov v

and

bob ia sma ib smb lastalice nmov v =
    let rec eat i eaten =
        (* let _ = Printf.printf "bo eat i eaten = %d %d\n" i eaten in *)
        if eaten > lastalice then (i, eaten)
        else if i < ia then (i, eaten)
        else
            let eaten = eaten + v.(i) in
            let i = i - 1 in
            eat i eaten in
    let idx,beat = eat ib 0 in
    let smb = smb + beat in
    let nmov = nmov + 1 in
    if idx < ia then (nmov, sma, smb)
    else
    alice ia sma idx smb beat nmov v;;

let do_one () =
    let n = gr () in
    let a = readlist n [] in
		let a = (List.rev a) in
		let va = Array.of_list a in
		let nm,ca,cb = alice 0 0 (n-1) 0 0 0 va in
  	Printf.printf "%d %d %d\n" nm ca cb;;

let do_all t =
    for i = 1 to t do
        do_one ()
    done;;

let main() =
	let t = gr () in
	do_all t;;

main();;