Codeforces Global Round 24
A. Doremy's Paint
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Doremy has $$$n$$$ buckets of paint which is represented by an array $$$a$$$ of length $$$n$$$. Bucket $$$i$$$ contains paint with color $$$a_i$$$.

Let $$$c(l,r)$$$ be the number of distinct elements in the subarray $$$[a_l,a_{l+1},\ldots,a_r]$$$. Choose $$$2$$$ integers $$$l$$$ and $$$r$$$ such that $$$l \leq r$$$ and $$$r-l-c(l,r)$$$ is maximized.

Input

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 10^5$$$) — the length of the array $$$a$$$.

The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le n$$$).

It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$.

Output

For each test case, output $$$l$$$ and $$$r$$$ such that $$$l \leq r$$$ and $$$r-l-c(l,r)$$$ is maximized.

If there are multiple solutions, you may output any.

Example
Input
7
5
1 3 2 2 4
5
1 2 3 4 5
4
2 1 2 1
3
2 3 3
2
2 2
1
1
9
9 8 5 2 1 1 2 3 3
Output
2 4
1 5
1 4
2 3
1 2
1 1
3 9
Note

In the first test case, $$$a=[1,3,2,2,4]$$$.

  • When $$$l=1$$$ and $$$r=3$$$, $$$c(l,r)=3$$$ (there are $$$3$$$ distinct elements in $$$[1,3,2]$$$).
  • When $$$l=2$$$ and $$$r=4$$$, $$$c(l,r)=2$$$ (there are $$$2$$$ distinct elements in $$$[3,2,2]$$$).

It can be shown that choosing $$$l=2$$$ and $$$r=4$$$ maximizes the value of $$$r-l-c(l,r)$$$ at $$$0$$$.

For the second test case, $$$a=[1,2,3,4,5]$$$.

  • When $$$l=1$$$ and $$$r=5$$$, $$$c(l,r)=5$$$ (there are $$$5$$$ distinct elements in $$$[1,2,3,4,5]$$$).
  • When $$$l=3$$$ and $$$r=3$$$, $$$c(l,r)=1$$$ (there is $$$1$$$ distinct element in $$$[3]$$$).

It can be shown that choosing $$$l=1$$$ and $$$r=5$$$ maximizes the value of $$$r-l-c(l,r)$$$ at $$$-1$$$. Choosing $$$l=3$$$ and $$$r=3$$$ is also acceptable.

B. Doremy's Perfect Math Class
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

"Everybody! Doremy's Perfect Math Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's math class, Doremy is teaching everyone subtraction. Now she gives you a quiz to prove that you are paying attention in class.

You are given a set $$$S$$$ containing positive integers. You may perform the following operation some (possibly zero) number of times:

  • choose two integers $$$x$$$ and $$$y$$$ from the set $$$S$$$ such that $$$x \gt y$$$ and $$$x - y$$$ is not in the set $$$S$$$.
  • add $$$x-y$$$ into the set $$$S$$$.

You need to tell Doremy the maximum possible number of integers in $$$S$$$ if the operations are performed optimally. It can be proven that this number is finite.

Input

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line contains an integer $$$n$$$ ($$$2 \le n\le 10^5$$$) — the size of the set $$$S$$$.

The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$1\le a_1 \lt a_2 \lt \cdots \lt a_n \le 10^9$$$) — the positive integers in $$$S$$$.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.

Output

For each test case, you need to output the maximum possible number of integers in $$$S$$$. It can be proven that this value is finite.

Example
Input
2
2
1 2
3
5 10 25
Output
2
5
Note

In the first test case, no such $$$x$$$ and $$$y$$$ exist. The maximum possible number of integers in $$$S$$$ is $$$2$$$.

In the second test case,

  • $$$S=\{5,10,25\}$$$ at first. You can choose $$$x=25$$$, $$$y=10$$$, then add $$$x-y=15$$$ to the set.
  • $$$S=\{5,10,15,25\}$$$ now. You can choose $$$x=25$$$, $$$y=5$$$, then add $$$x-y=20$$$ to the set.
  • $$$S=\{5,10,15,20,25\}$$$ now. You can not perform any operation now.

After performing all operations, the number of integers in $$$S$$$ is $$$5$$$. It can be proven that no other sequence of operations allows $$$S$$$ to contain more than $$$5$$$ integers.

C. Doremy's City Construction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Doremy's new city is under construction! The city can be regarded as a simple undirected graph with $$$n$$$ vertices. The $$$i$$$-th vertex has altitude $$$a_i$$$. Now Doremy is deciding which pairs of vertices should be connected with edges.

Due to economic reasons, there should be no self-loops or multiple edges in the graph.

Due to safety reasons, there should not be pairwise distinct vertices $$$u$$$, $$$v$$$, and $$$w$$$ such that $$$a_u \leq a_v \leq a_w$$$ and the edges $$$(u,v)$$$ and $$$(v,w)$$$ exist.

Under these constraints, Doremy would like to know the maximum possible number of edges in the graph. Can you help her?

Note that the constructed graph is allowed to be disconnected.

Input

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 2\cdot 10^5$$$) — the number of vertices.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1\le a_i\le 10^6$$$) — the altitudes of each vertex.

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output

For each test case, output the maximum possible number of edges in the graph.

Example
Input
4
4
2 2 3 1
6
5 2 3 1 5 2
12
7 2 4 9 1 4 6 3 7 4 2 3
4
1000000 1000000 1000000 1000000
Output
3
9
35
2
Note

In the first test case, there can only be at most $$$3$$$ edges in the graph. A possible construction is to connect $$$(1,3)$$$, $$$(2,3)$$$, $$$(3,4)$$$. In the picture below the red number above node $$$i$$$ is $$$a_i$$$.

The following list shows all such $$$u$$$, $$$v$$$, $$$w$$$ that the edges $$$(u,v)$$$ and $$$(v,w)$$$ exist.

  • $$$u=1$$$, $$$v=3$$$, $$$w=2$$$;
  • $$$u=1$$$, $$$v=3$$$, $$$w=4$$$;
  • $$$u=2$$$, $$$v=3$$$, $$$w=1$$$;
  • $$$u=2$$$, $$$v=3$$$, $$$w=4$$$;
  • $$$u=4$$$, $$$v=3$$$, $$$w=1$$$;
  • $$$u=4$$$, $$$v=3$$$, $$$w=2$$$.

Another possible construction is to connect $$$(1,4)$$$, $$$(2,4)$$$, $$$(3,4)$$$.

An unacceptable construction is to connect $$$(1,3)$$$, $$$(2,3)$$$, $$$(2,4)$$$, $$$(3,4)$$$. Because when $$$u=4$$$, $$$v=2$$$, $$$w=3$$$, $$$a_u\le a_v \le a_w$$$ holds, and the respective edges exist.

D. Doremy's Pegging Game
time limit per test
1.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Doremy has $$$n+1$$$ pegs. There are $$$n$$$ red pegs arranged as vertices of a regular $$$n$$$-sided polygon, numbered from $$$1$$$ to $$$n$$$ in anti-clockwise order. There is also a blue peg of slightly smaller diameter in the middle of the polygon. A rubber band is stretched around the red pegs.

Doremy is very bored today and has decided to play a game. Initially, she has an empty array $$$a$$$. While the rubber band does not touch the blue peg, she will:

  1. choose $$$i$$$ ($$$1 \leq i \leq n$$$) such that the red peg $$$i$$$ has not been removed;
  2. remove the red peg $$$i$$$;
  3. append $$$i$$$ to the back of $$$a$$$.

Doremy wonders how many possible different arrays $$$a$$$ can be produced by the following process. Since the answer can be big, you are only required to output it modulo $$$p$$$. $$$p$$$ is guaranteed to be a prime number.

game with $$$n=9$$$ and $$$a=[7,5,2,8,3,9,4]$$$ and another game with $$$n=8$$$ and $$$a=[3,4,7,1,8,5,2]$$$
Input

The first line contains two integers $$$n$$$ and $$$p$$$ ($$$3 \leq n \leq 5000$$$, $$$10^8 \le p \le 10^9$$$) — the number of red pegs and the modulo respectively.

$$$p$$$ is guaranteed to be a prime number.

Output

Output a single integer, the number of different arrays $$$a$$$ that can be produced by the process described above modulo $$$p$$$.

Examples
Input
4 100000007
Output
16
Input
1145 141919831
Output
105242108
Note

In the first test case, $$$n=4$$$, some possible arrays $$$a$$$ that can be produced are $$$[4,2,3]$$$ and $$$[1,4]$$$. However, it is not possible for $$$a$$$ to be $$$[1]$$$ or $$$[1,4,3]$$$.

E. Doremy's Number Line
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Doremy has two arrays $$$a$$$ and $$$b$$$ of $$$n$$$ integers each, and an integer $$$k$$$.

Initially, she has a number line where no integers are colored. She chooses a permutation $$$p$$$ of $$$[1,2,\ldots,n]$$$ then performs $$$n$$$ moves. On the $$$i$$$-th move she does the following:

  • Pick an uncolored integer $$$x$$$ on the number line such that either:
    • $$$x \leq a_{p_i}$$$; or
    • there exists a colored integer $$$y$$$ such that $$$y \leq a_{p_i}$$$ and $$$x \leq y+b_{p_i}$$$.
  • Color integer $$$x$$$ with color $$$p_i$$$.

Determine if the integer $$$k$$$ can be colored with color $$$1$$$.

Input

The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1\le t\le 10^4$$$) — the number of test cases. The description of the test cases follows.

The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^5$$$, $$$1 \le k \le 10^9$$$).

Each of the following $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i,b_i \le 10^9$$$).

It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output

For each test case, output "YES" (without quotes) if the point $$$k$$$ can be colored with color $$$1$$$. Otherwise, output "NO" (without quotes).

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

Example
Input
6
4 16
5 3
8 12
10 7
15 1
4 16
8 12
10 7
15 1
5 3
4 16
10 7
15 1
5 3
8 12
4 16
15 1
5 3
8 12
10 7
1 1000000000
500000000 500000000
2 1000000000
1 999999999
1 1
Output
NO
YES
YES
YES
NO
YES
Note

For the first test case, it is impossible to color point $$$16$$$ with color $$$1$$$.

For the second test case, $$$p=[2,1,3,4]$$$ is one possible choice, the detail is shown below.

  • On the first move, pick $$$x=8$$$ and color it with color $$$2$$$ since $$$x=8$$$ is uncolored and $$$x \le a_2$$$.
  • On the second move, pick $$$x=16$$$ and color it with color $$$1$$$ since there exists a colored point $$$y=8$$$ such that $$$y\le a_1$$$ and $$$x \le y + b_1$$$.
  • On the third move, pick $$$x=0$$$ and color it with color $$$3$$$ since $$$x=0$$$ is uncolored and $$$x \le a_3$$$.
  • On the forth move, pick $$$x=-2$$$ and color it with color $$$4$$$ since $$$x=-2$$$ is uncolored and $$$x \le a_4$$$.
  • In the end, point $$$-2,0,8,16$$$ are colored with color $$$4,3,2,1$$$, respectively.

For the third test case, $$$p=[2,1,4,3]$$$ is one possible choice.

For the fourth test case, $$$p=[2,3,4,1]$$$ is one possible choice.

F. Doremy's Experimental Tree
time limit per test
2.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Doremy has an edge-weighted tree with $$$n$$$ vertices whose weights are integers between $$$1$$$ and $$$10^9$$$. She does $$$\frac{n(n+1)}{2}$$$ experiments on it.

In each experiment, Doremy chooses vertices $$$i$$$ and $$$j$$$ such that $$$j \leq i$$$ and connects them directly with an edge with weight $$$1$$$. Then, there is exactly one cycle (or self-loop when $$$i=j$$$) in the graph. Doremy defines $$$f(i,j)$$$ as the sum of lengths of shortest paths from every vertex to the cycle.

Formally, let $$$\mathrm{dis}_{i,j}(x,y)$$$ be the length of the shortest path between vertex $$$x$$$ and $$$y$$$ when the edge $$$(i,j)$$$ of weight $$$1$$$ is added, and $$$S_{i,j}$$$ be the set of vertices that are on the cycle when edge $$$(i,j)$$$ is added. Then,

$$$$$$ f(i,j)=\sum_{x=1}^{n}\left(\min_{y\in S_{i,j}}\mathrm{dis}_{i,j}(x,y)\right). $$$$$$

Doremy writes down all values of $$$f(i,j)$$$ such that $$$1 \leq j \leq i \leq n$$$, then goes to sleep. However, after waking up, she finds that the tree has gone missing. Fortunately, the values of $$$f(i,j)$$$ are still in her notebook, and she knows which $$$i$$$ and $$$j$$$ they belong to. Given the values of $$$f(i,j)$$$, can you help her restore the tree?

It is guaranteed that at least one suitable tree exists.

Input

The first line of input contains a single integer $$$n$$$ ($$$2 \le n \le 2000$$$) — the number of vertices in the tree.

The following $$$n$$$ lines contain a lower-triangular matrix with $$$i$$$ integers on the $$$i$$$-th line; the $$$j$$$-th integer on the $$$i$$$-th line is $$$f(i,j)$$$ ($$$0 \le f(i,j) \le 2\cdot 10^{15}$$$).

It is guaranteed that there exists a tree whose weights are integers between $$$1$$$ and $$$10^9$$$ such that the values of $$$f(i,j)$$$ of the tree match those given in the input.

Output

Print $$$n-1$$$ lines describing the tree. In the $$$i$$$-th line of the output, output three integers $$$u_i$$$, $$$v_i$$$, $$$w_i$$$ ($$$1 \le u_i,v_i \le n$$$, $$$1 \le w_i \le 10^9$$$), representing an edge $$$(u_i,v_i)$$$ whose weight is $$$w_i$$$.

If there are multiple answers, you may output any.

All edges must form a tree and all values of $$$f(i,j)$$$ must match those given in the input.

Examples
Input
3
7
3 5
0 2 8
Output
2 3 3
1 2 2
Input
9
8081910646
8081902766 8081903751
8081902555 8081903540 8081905228
3090681001 3090681986 3090681775 7083659398
7083657913 7083658898 7083658687 2092437133 15069617722
1748216295 1748217280 1748217069 5741194692 749972427 10439821163
2377558289 2377559274 2377559063 6370536686 1379314421 5028071980 8866466178
1746983932 1746984917 1746984706 5739962329 748740064 10438588800 5026839617 10448447704
2341942133 2341943118 2341942907 6334920530 1343698265 4992455824 8830850022 4991223461 9115779270
Output
1 2 985
2 3 211
2 4 998244353
2 5 998244853
4 6 671232353
6 8 1232363
4 7 356561356
7 9 35616156
Note

In the first test case, the picture below, from left to right, from top to bottom, shows the graph when pairs $$$(1,1)$$$, $$$(1,2)$$$, $$$(1,3)$$$, $$$(2,2)$$$, $$$(2,3)$$$, $$$(3,3)$$$ are connected with an edge, respectively. The nodes colored yellow are on the cycle.

G1. Doremy's Perfect DS Class (Easy Version)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The only difference between this problem and the other two versions is the maximum number of queries. In this version, you are allowed to ask at most $$$\mathbf{30}$$$ queries. You can make hacks only if all versions of the problem are solved.

This is an interactive problem.

"Everybody! Doremy's Perfect Data Structure Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's Data Structure class, Doremy is teaching everyone a powerful data structure — Doremy tree! Now she gives you a quiz to prove that you are paying attention in class.

Given an array $$$a$$$ of length $$$m$$$, Doremy tree supports the query $$$Q(l,r,k)$$$, where $$$1 \leq l \leq r \leq m$$$ and $$$1 \leq k \leq m$$$, which returns the number of distinct integers in the array $$$\left[\lfloor\frac{a_l}{k} \rfloor, \lfloor\frac{a_{l+1}}{k} \rfloor, \ldots, \lfloor\frac{a_r}{k} \rfloor\right]$$$.

Doremy has a secret permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$. You can make queries, in one query, you give $$$3$$$ integers $$$l,r,k$$$ ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$) and receive the value of $$$Q(l,r,k)$$$ for the array $$$p$$$. Can you find the index $$$y$$$ ($$$1 \leq y \leq n$$$) such that $$$p_y=1$$$ in at most $$$\mathbf{30}$$$ queries?

Note that the permutation $$$p$$$ is fixed before any queries are made.

Interaction

You begin the interaction by reading an integer $$$n$$$ ($$$3 \le n \le 1024$$$) in the first line — the length of the permutation.

To make a query, you should output

  • "? $$$l\ r\ k$$$" ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$)
in a separate line. After each query, you should read an integer $$$x$$$ — the value of $$$Q(l,r,k)$$$ for $$$p$$$. In this version of the problem, you can make at most $$$30$$$ such queries.

To give the answer, you should output

  • "! $$$y$$$" ($$$1 \leq y \leq n$$$)
in a separate line, where $$$p_y=1$$$.

After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

Hacks Format

The first line of the hack contains an integer $$$n$$$ ($$$3 \le n \le 1024$$$) — the length of the permutation.

The second line of the hack contains $$$n$$$ distinct integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i\le n$$$) — the permutation.

Example
Input
5

2

2

1

3
Output

? 1 3 4

? 3 5 3

? 3 4 5

? 3 5 2

! 4
Note

The permutation in the example is $$$[3,5,2,1,4]$$$.

The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).

In this interaction process:

  • For the first query, $$$\lfloor\frac{3}{4}\rfloor=0,\lfloor\frac{5}{4}\rfloor=1,\lfloor\frac{2}{4}\rfloor=0$$$, so the answer is $$$2$$$.
  • For the second query, $$$\lfloor\frac{2}{3}\rfloor=0,\lfloor\frac{1}{3}\rfloor=0,\lfloor\frac{4}{3}\rfloor=1$$$, so the answer is still $$$2$$$.
  • For the third query, $$$\lfloor\frac{2}{5}\rfloor=0,\lfloor\frac{1}{5}\rfloor=0$$$, so the answer is $$$1$$$.
  • For the fourth query, $$$\lfloor\frac{2}{2}\rfloor=1,\lfloor\frac{1}{2}\rfloor=0,\lfloor\frac{4}{2}\rfloor=2$$$, so the answer is $$$3$$$.

The correct answer is got after $$$4$$$ queries, so this process will be judged correct.

G2. Doremy's Perfect DS Class (Medium Version)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The only difference between this problem and the other two versions is the maximum number of queries. In this version, you are allowed to ask at most $$$\mathbf{25}$$$ queries. You can make hacks only if all versions of the problem are solved.

This is an interactive problem.

"Everybody! Doremy's Perfect Data Structure Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's Data Structure class, Doremy is teaching everyone a powerful data structure — Doremy tree! Now she gives you a quiz to prove that you are paying attention in class.

Given an array $$$a$$$ of length $$$m$$$, Doremy tree supports the query $$$Q(l,r,k)$$$, where $$$1 \leq l \leq r \leq m$$$ and $$$1 \leq k \leq m$$$, which returns the number of distinct integers in the array $$$\left[\lfloor\frac{a_l}{k} \rfloor, \lfloor\frac{a_{l+1}}{k} \rfloor, \ldots, \lfloor\frac{a_r}{k} \rfloor\right]$$$.

Doremy has a secret permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$. You can make queries, in one query, you give $$$3$$$ integers $$$l,r,k$$$ ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$) and receive the value of $$$Q(l,r,k)$$$ for the array $$$p$$$. Can you find the index $$$y$$$ ($$$1 \leq y \leq n$$$) such that $$$p_y=1$$$ in at most $$$\mathbf{25}$$$ queries?

Note that the permutation $$$p$$$ is fixed before any queries are made.

Interaction

You begin the interaction by reading an integer $$$n$$$ ($$$3 \le n \le 1024$$$) in the first line — the length of the permutation.

To make a query, you should output

  • "? $$$l\ r\ k$$$" ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$)
in a separate line. After each query, you should read an integer $$$x$$$ — the value of $$$Q(l,r,k)$$$ for $$$p$$$. In this version of the problem, you can make at most $$$25$$$ such queries.

To give the answer, you should output

  • "! $$$y$$$" ($$$1 \leq y \leq n$$$)
in a separate line, where $$$p_y=1$$$.

After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

Hacks Format

The first line of the hack contains an integer $$$n$$$ ($$$3 \le n \le 1024$$$) — the length of the permutation.

The second line of the hack contains $$$n$$$ distinct integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i\le n$$$) — the permutation.

Example
Input
5

2

2

1

3
Output

? 1 3 4

? 3 5 3

? 3 4 5

? 3 5 2

! 4
Note

The permutation in the example is $$$[3,5,2,1,4]$$$.

The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).

In this interaction process:

  • For the first query, $$$\lfloor\frac{3}{4}\rfloor=0,\lfloor\frac{5}{4}\rfloor=1,\lfloor\frac{2}{4}\rfloor=0$$$, so the answer is $$$2$$$.
  • For the second query, $$$\lfloor\frac{2}{3}\rfloor=0,\lfloor\frac{1}{3}\rfloor=0,\lfloor\frac{4}{3}\rfloor=1$$$, so the answer is still $$$2$$$.
  • For the third query, $$$\lfloor\frac{2}{5}\rfloor=0,\lfloor\frac{1}{5}\rfloor=0$$$, so the answer is $$$1$$$.
  • For the fourth query, $$$\lfloor\frac{2}{2}\rfloor=1,\lfloor\frac{1}{2}\rfloor=0,\lfloor\frac{4}{2}\rfloor=2$$$, so the answer is $$$3$$$.

The correct answer is got after $$$4$$$ queries, so this process will be judged correct.

G3. Doremy's Perfect DS Class (Hard Version)
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The only difference between this problem and the other two versions is the maximum number of queries. In this version, you are allowed to ask at most $$$\mathbf{20}$$$ queries. You can make hacks only if all versions of the problem are solved.

This is an interactive problem.

"Everybody! Doremy's Perfect Data Structure Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's Data Structure class, Doremy is teaching everyone a powerful data structure — Doremy tree! Now she gives you a quiz to prove that you are paying attention in class.

Given an array $$$a$$$ of length $$$m$$$, Doremy tree supports the query $$$Q(l,r,k)$$$, where $$$1 \leq l \leq r \leq m$$$ and $$$1 \leq k \leq m$$$, which returns the number of distinct integers in the array $$$\left[\lfloor\frac{a_l}{k} \rfloor, \lfloor\frac{a_{l+1}}{k} \rfloor, \ldots, \lfloor\frac{a_r}{k} \rfloor\right]$$$.

Doremy has a secret permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$. You can make queries, in one query, you give $$$3$$$ integers $$$l,r,k$$$ ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$) and receive the value of $$$Q(l,r,k)$$$ for the array $$$p$$$. Can you find the index $$$y$$$ ($$$1 \leq y \leq n$$$) such that $$$p_y=1$$$ in at most $$$\mathbf{20}$$$ queries?

Note that the permutation $$$p$$$ is fixed before any queries are made.

Interaction

You begin the interaction by reading an integer $$$n$$$ ($$$3 \le n \le 1024$$$) in the first line — the length of the permutation.

To make a query, you should output

  • "? $$$l\ r\ k$$$" ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$)
in a separate line. After each query, you should read an integer $$$x$$$ — the value of $$$Q(l,r,k)$$$ for $$$p$$$. In this version of the problem, you can make at most $$$20$$$ such queries.

To give the answer, you should output

  • "! $$$y$$$" ($$$1 \leq y \leq n$$$)
in a separate line, where $$$p_y=1$$$.

After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

Hacks Format

The first line of the hack contains an integer $$$n$$$ ($$$3 \le n \le 1024$$$) — the length of the permutation.

The second line of the hack contains $$$n$$$ distinct integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i\le n$$$) — the permutation.

Example
Input
5

2

2

1

3
Output

? 1 3 4

? 3 5 3

? 3 4 5

? 3 5 2

! 4
Note

The permutation in the example is $$$[3,5,2,1,4]$$$.

The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).

In this interaction process:

  • For the first query, $$$\lfloor\frac{3}{4}\rfloor=0,\lfloor\frac{5}{4}\rfloor=1,\lfloor\frac{2}{4}\rfloor=0$$$, so the answer is $$$2$$$.
  • For the second query, $$$\lfloor\frac{2}{3}\rfloor=0,\lfloor\frac{1}{3}\rfloor=0,\lfloor\frac{4}{3}\rfloor=1$$$, so the answer is still $$$2$$$.
  • For the third query, $$$\lfloor\frac{2}{5}\rfloor=0,\lfloor\frac{1}{5}\rfloor=0$$$, so the answer is $$$1$$$.
  • For the fourth query, $$$\lfloor\frac{2}{2}\rfloor=1,\lfloor\frac{1}{2}\rfloor=0,\lfloor\frac{4}{2}\rfloor=2$$$, so the answer is $$$3$$$.

The correct answer is got after $$$4$$$ queries, so this process will be judged correct.

H. Doremy's Paint 2
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Doremy has $$$n$$$ buckets of paint which is represented by an array $$$a$$$ of length $$$n$$$. Bucket $$$i$$$ contains paint with color $$$a_i$$$. Initially, $$$a_i=i$$$.

Doremy has $$$m$$$ segments $$$[l_i,r_i]$$$ ($$$1 \le l_i \le r_i \le n$$$). Each segment describes an operation. Operation $$$i$$$ is performed as follows:

  • For each $$$j$$$ such that $$$l_i \lt j \leq r_i$$$, set $$$a_j := a_{l_i}$$$.

Doremy also selects an integer $$$k$$$. She wants to know for each integer $$$x$$$ from $$$0$$$ to $$$m-1$$$, the number of distinct colors in the array after performing operations $$$x \bmod m +1, (x+1) \bmod m + 1, \ldots, (x+k-1) \bmod m +1$$$. Can you help her calculate these values? Note that for each $$$x$$$ individually we start from the initial array and perform only the given $$$k$$$ operations in the given order.

Input

The first line of input contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1\le n,m\le 2\cdot 10^5$$$, $$$1 \le k \le m$$$) — the length of the array $$$a$$$, the total number of operations, and the integer that Doremy selects.

The $$$i$$$-th line of the following $$$m$$$ lines contains two integers $$$l_i$$$, $$$r_i$$$ ($$$1\le l_i\le r_i\le n$$$) — the bounds of the $$$i$$$-th segment.

Output

Output $$$m$$$ integers. The $$$(x+1)$$$-th integer should be the number of distinct colors in the array if we start from the initial array and perform operations $$$x \bmod m +1, (x+1) \bmod m + 1, \ldots, (x+k-1) \bmod m +1$$$.

Examples
Input
7 5 5
3 5
2 3
4 6
5 7
1 2
Output
3 3 3 3 2 
Input
10 9 4
1 1
2 3
3 4
7 9
6 8
5 7
2 4
9 10
1 3
Output
6 6 7 6 5 5 7 7 7 
Note

In the first test case, the picture below shows the resulting array for the values of $$$x=0,1,2$$$ respectively.