A - Subsegment Reverse

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

正整数 N,L,R が与えられます。
長さ N の数列 A=(1,2,\dots,N) に対し、 L 項目から R 項目までを逆順に並べ替えるという操作を一度行いました。
操作後の数列を出力してください。

制約

  • 入力は全て整数
  • 1 \le L \le R \le N \le 100

入力

入力は以下の形式で標準入力から与えられる。

N L R

出力

操作後の数列を A'=(A'_1,A'_2,\dots,A'_N) として、以下の形式で出力せよ。

A'_1 A'_2 \dots A'_N

入力例 1

5 2 3

出力例 1

1 3 2 4 5

最初、 A=(1,2,3,4,5) です。
2 項目から 3 項目までを逆順に並べ替えた後の数列は (1,3,2,4,5) なので、これを出力してください。


入力例 2

7 1 1

出力例 2

1 2 3 4 5 6 7

L=R である場合もあります。


入力例 3

10 1 10

出力例 3

10 9 8 7 6 5 4 3 2 1

L=1R=N である場合もあります。

Score : 100 points

Problem Statement

You are given positive integers N, L, and R.
For a sequence A = (1, 2, \dots, N) of length N, an operation of reversing the L-th through R-th elements was performed once.
Print the sequence after this operation.

Constraints

  • All input values are integers.
  • 1 \leq L \leq R \leq N \leq 100

Input

The input is given from Standard Input in the following format:

N L R

Output

Let A' = (A'_1, A'_2, \dots, A'_N) be the sequence after the operation. Print it in the following format:

A'_1 A'_2 \dots A'_N

Sample Input 1

5 2 3

Sample Output 1

1 3 2 4 5

Initially, A = (1, 2, 3, 4, 5).
After reversing the second through third elements, the sequence becomes (1, 3, 2, 4, 5), which should be printed.


Sample Input 2

7 1 1

Sample Output 2

1 2 3 4 5 6 7

It is possible that L = R.


Sample Input 3

10 1 10

Sample Output 3

10 9 8 7 6 5 4 3 2 1

It is possible that L = 1 or R = N.

B - Nutrients

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150

問題文

健康に気を使っている高橋君は、M 種類の栄養素について、食事によって十分な量を摂取できているか気になりました。

i 番目の栄養素は 1 日あたり A_i 以上摂取することが目標です。

高橋君は今日 N 品の食品を食べ、i 品目の食品からは栄養素 jX_{i,j} 摂取しました。

M 種類全ての栄養素で目標を達成しているかどうかを判定してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 0 \leq A_i,X_{i,j} \leq 10^7
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N M
A_1 \ldots A_M
X_{1,1} \ldots X_{1,M}
\vdots
X_{N,1} \ldots X_{N,M}

出力

M 種類全ての栄養素で目標を達成しているなら Yes、そうでないならば No を出力せよ。


入力例 1

2 3
10 20 30
20 0 10
0 100 100

出力例 1

Yes

栄養素 11 品目から 202 品目から 0 摂取したため、合わせて 20 摂取しており、10 以上摂取するという目標を達成しています。
栄養素 2,3 についても同様に目標を達成しています。


入力例 2

2 4
10 20 30 40
20 0 10 30
0 100 100 0

出力例 2

No

栄養素 4 について目標を達成していません。

Score : 150 points

Problem Statement

Takahashi is health-conscious and concerned about whether he is getting enough of M types of nutrients from his diet.

For the i-th nutrient, his goal is to take at least A_i units per day.

Today, he ate N foods, and from the i-th food, he took X_{i,j} units of nutrient j.

Determine whether he has met the goal for all M types of nutrients.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq M \leq 100
  • 0 \leq A_i, X_{i,j} \leq 10^7
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
A_1 \ldots A_M
X_{1,1} \ldots X_{1,M}
\vdots
X_{N,1} \ldots X_{N,M}

Output

Print Yes if the goal is met for all M types of nutrients, and No otherwise.


Sample Input 1

2 3
10 20 30
20 0 10
0 100 100

Sample Output 1

Yes

For nutrient 1, Takahashi took 20 units from the 1-st food and 0 units from the 2-nd food, totaling 20 units, thus meeting the goal of taking at least 10 units.
Similarly, he meets the goal for nutrients 2 and 3.


Sample Input 2

2 4
10 20 30 40
20 0 10 30
0 100 100 0

Sample Output 2

No

The goal is not met for nutrient 4.

C - Keys

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

あなたは N 本の鍵 1,2,\dots,N を持っています。
このうち何本かの鍵は正しい鍵で、それ以外はダミーの鍵です。

また、鍵を何本でも挿し込める ドアX があり、この ドアX は正しい鍵を K 本以上挿し込んだ時、またその時に限って開きます。

あなたはこれらの鍵に対して M 回のテストを行いました。このうち i 回目のテストの内容は次の通りです。

  • C_i 本の鍵 A_{i,1},A_{i,2},\dots,A_{i,C_i} を ドアX に挿し込む。
  • テスト結果はひとつの英文字 R_i で表現される。
    • R_i = o のとき i 回目のテストでドアが開いたことを表す。
    • R_i = x のとき i 回目のテストでドアが開かなかったことを表す。

各鍵が正しいかダミーかの組み合わせは 2^N 通り考えられますが、このうちどのテスト結果にも矛盾しない組み合わせの個数を求めてください。
ただし、与えられるテスト結果が誤っており上記の条件を満たす組み合わせが存在しない場合もあります。その場合は 0 通りと解答してください。

制約

  • N,M,K,C_i,A_{i,j} は整数
  • 1 \le K \le N \le 15
  • 1 \le M \le 100
  • 1 \le C_i \le N
  • 1 \le A_{i,j} \le N
  • j \neq k ならば A_{i,j} \neq A_{i,k}
  • R_io または x

入力

入力は以下の形式で標準入力から与えられる。

N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M

出力

答えを整数として出力せよ。


入力例 1

3 2 2
3 1 2 3 o
2 2 3 x

出力例 1

2

この入力では鍵が 3 本あり、テストは 2 回行われました。
また、 ドアX を開くのに必要な正しい鍵の本数は 2 本です。

  • 1 回目のテストでは鍵 1,2,3 を使い、その結果 ドアX は開きました。
  • 2 回目のテストでは鍵 2,3 を使い、その結果 ドアX は開きませんした。

各鍵が正しいかダミーかの組み合わせであって、どのテスト結果にも矛盾しないものは以下の 2 通りです。

  • 1 は本物、鍵 2 はダミー、鍵 3 は本物である。
  • 1 は本物、鍵 2 は本物、鍵 3 はダミーである。

入力例 2

4 5 3
3 1 2 3 o
3 2 3 4 o
3 3 4 1 o
3 4 1 2 o
4 1 2 3 4 x

出力例 2

0

問題文中でも述べた通り、答えが 0 通りである場合もあります。


入力例 3

11 4 9
10 1 2 3 4 5 6 7 8 9 10 o
11 1 2 3 4 5 6 7 8 9 10 11 o
10 11 10 9 8 7 6 5 4 3 2 x
10 11 9 1 4 3 7 5 6 2 10 x

出力例 3

8

Score : 300 points

Problem Statement

You have N keys numbered 1, 2, \dots, N.
Some of these are real keys, while the others are dummies.

There is a door, Door X, into which you can insert any number of keys. Door X will open if and only if at least K real keys are inserted.

You have conducted M tests on these keys. The i-th test went as follows:

  • You inserted C_i keys A_{i,1}, A_{i,2}, \dots, A_{i,C_i} into Door X.
  • The test result is represented by a single English letter R_i.
    • R_i = o means that Door X opened in the i-th test.
    • R_i = x means that Door X did not open in the i-th test.

There are 2^N possible combinations of which keys are real and which are dummies. Among these, find the number of combinations that do not contradict any of the test results.
It is possible that the given test results are incorrect and no combination satisfies the conditions. In such a case, report 0.

Constraints

  • N, M, K, C_i, and A_{i,j} are integers.
  • 1 \le K \le N \le 15
  • 1 \le M \le 100
  • 1 \le C_i \le N
  • 1 \le A_{i,j} \le N
  • A_{i,j} \neq A_{i,k} if j \neq k.
  • R_i is o or x.

Input

The input is given from Standard Input in the following format:

N M K
C_1 A_{1,1} A_{1,2} \dots A_{1,C_1} R_1
C_2 A_{2,1} A_{2,2} \dots A_{2,C_2} R_2
\vdots
C_M A_{M,1} A_{M,2} \dots A_{M,C_M} R_M

Output

Print the answer as an integer.


Sample Input 1

3 2 2
3 1 2 3 o
2 2 3 x

Sample Output 1

2

In this input, there are three keys and two tests were conducted.
Two correct keys are required to open Door X.

  • In the first test, keys 1, 2, 3 were used, and Door X opened.
  • In the second test, keys 2, 3 were used, and Door X did not open.

There are two combinations of which keys are real and which are dummies that do not contradict any of the test results:

  • Key 1 is real, key 2 is a dummy, and key 3 is real.
  • Key 1 is real, key 2 is real, and key 3 is a dummy.

Sample Input 2

4 5 3
3 1 2 3 o
3 2 3 4 o
3 3 4 1 o
3 4 1 2 o
4 1 2 3 4 x

Sample Output 2

0

As mentioned in the problem statement, the answer may be 0.


Sample Input 3

11 4 9
10 1 2 3 4 5 6 7 8 9 10 o
11 1 2 3 4 5 6 7 8 9 10 11 o
10 11 10 9 8 7 6 5 4 3 2 x
10 11 9 1 4 3 7 5 6 2 10 x

Sample Output 3

8
D - Masked Popcount

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

整数 N,M が与えられるので、 \displaystyle \sum_{k=0}^{N} \rm{popcount}(k \mathbin{\&} M)998244353 で割った余りを求めてください。

ただし、 \mathbin{\&} はビット単位 \rm{AND} 演算を表します。

ビット単位 \rm{AND} 演算とは? 非負整数 a と非負整数 b とのビット単位 \rm{AND} 演算の結果 x = a \mathbin{\&} b は次のように定義されます。
  • x は全ての非負整数 k について以下の条件を満たすただ 1 つの非負整数である。
    • a2 進法で書き下した際の 2^k の位と b2 進法で書き下した際の 2^k の位が共に 1 なら、 x2 進法で書き下した際の 2^k の位は 1 である。
    • そうでないとき、 x2 進法で書き下した際の 2^k の位は 0 である。
例えば 3=11_{(2)}, 5=101_{(2)} なので、 3 \mathbin{\&} 5 = 1 となります。
\rm{popcount} とは? \rm{popcount}(x) は、 x2 進法で書き下した際に登場する 1 の個数を表します。
例えば 13=1101_{(2)} なので、 \rm{popcount}(13) = 3 となります。

制約

  • N0 以上 2^{60} 未満の整数
  • M0 以上 2^{60} 未満の整数

入力

入力は以下の形式で標準入力から与えられる。

N M

出力

答えを整数として出力せよ。


入力例 1

4 3

出力例 1

4
  • \rm{popcount}(0\mathbin{\&}3) = 0
  • \rm{popcount}(1\mathbin{\&}3) = 1
  • \rm{popcount}(2\mathbin{\&}3) = 1
  • \rm{popcount}(3\mathbin{\&}3) = 2
  • \rm{popcount}(4\mathbin{\&}3) = 0

であり、これらの和は 4 です。


入力例 2

0 0

出力例 2

0

N=0 である場合や M=0 である場合もあります。


入力例 3

1152921504606846975 1152921504606846975

出力例 3

499791890

998244353 で割った余りを求めることに注意してください。

Score : 400 points

Problem Statement

Given integers N and M, compute the sum \displaystyle \sum_{k=0}^{N} \rm{popcount}(k \mathbin{\&} M), modulo 998244353.

Here, \mathbin{\&} represents the bitwise \rm{AND} operation.

What is the bitwise \rm{AND} operation? The result x = a \mathbin{\&} b of the bitwise \rm{AND} operation between non-negative integers a and b is defined as follows:
  • x is the unique non-negative integer that satisfies the following conditions for all non-negative integers k:
    • If the 2^k place in the binary representation of a and the 2^k place in the binary representation of b are both 1, then the 2^k place in the binary representation of x is 1.
    • Otherwise, the 2^k place in the binary representation of x is 0.
For example, 3=11_{(2)} and 5=101_{(2)}, so 3 \mathbin{\&} 5 = 1.
What is \rm{popcount}? \rm{popcount}(x) represents the number of 1s in the binary representation of x.
For example, 13=1101_{(2)}, so \rm{popcount}(13) = 3.

Constraints

  • N is an integer between 0 and 2^{60} - 1, inclusive.
  • M is an integer between 0 and 2^{60} - 1, inclusive.

Input

The input is given from Standard Input in the following format:

N M

Output

Print the answer as an integer.


Sample Input 1

4 3

Sample Output 1

4
  • \rm{popcount}(0\mathbin{\&}3) = 0
  • \rm{popcount}(1\mathbin{\&}3) = 1
  • \rm{popcount}(2\mathbin{\&}3) = 1
  • \rm{popcount}(3\mathbin{\&}3) = 2
  • \rm{popcount}(4\mathbin{\&}3) = 0

The sum of these values is 4.


Sample Input 2

0 0

Sample Output 2

0

It is possible that N = 0 or M = 0.


Sample Input 3

1152921504606846975 1152921504606846975

Sample Output 3

499791890

Remember to compute the result modulo 998244353.

E - Max/Min

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^{N}\left\lfloor\frac{\max(A_i,A_j)}{\min(A_i,A_j)}\right\rfloor を求めてください。

ただし、\lfloor x \rfloorx 以下の最大の整数を表します。例えば、\lfloor 3.14 \rfloor=3\lfloor 2 \rfloor=2 です。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^6
  • 入力は全て整数である

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
3 1 4

出力例 1

8

求める値は

\left\lfloor\frac{\max(3,1)}{\min(3,1)}\right\rfloor + \left\lfloor\frac{\max(3,4)}{\min(3,4)}\right\rfloor + \left\lfloor\frac{\max(1,4)}{\min(1,4)}\right\rfloor\\ =\left\lfloor\frac{3}{1}\right\rfloor + \left\lfloor\frac{4}{3}\right\rfloor + \left\lfloor\frac{4}{1}\right\rfloor\\ =3+1+4\\ =8

となります。


入力例 2

6
2 7 1 8 2 8

出力例 2

53

入力例 3

12
3 31 314 3141 31415 314159 2 27 271 2718 27182 271828

出力例 3

592622

Score : 475 points

Problem Statement

You are given a sequence A=(A_1,\ldots,A_N) of length N.

Find \displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^{N}\left\lfloor\frac{\max(A_i,A_j)}{\min(A_i,A_j)}\right\rfloor.

Here, \lfloor x \rfloor represents the greatest integer not greater than x. For example, \lfloor 3.14 \rfloor=3 and \lfloor 2 \rfloor=2.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq 10^6
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3
3 1 4

Sample Output 1

8

The sought value is

\left\lfloor\frac{\max(3,1)}{\min(3,1)}\right\rfloor + \left\lfloor\frac{\max(3,4)}{\min(3,4)}\right\rfloor + \left\lfloor\frac{\max(1,4)}{\min(1,4)}\right\rfloor\\ =\left\lfloor\frac{3}{1}\right\rfloor + \left\lfloor\frac{4}{3}\right\rfloor + \left\lfloor\frac{4}{1}\right\rfloor\\ =3+1+4\\ =8.


Sample Input 2

6
2 7 1 8 2 8

Sample Output 2

53

Sample Input 3

12
3 31 314 3141 31415 314159 2 27 271 2718 27182 271828

Sample Output 3

592622
F - Distance Component Size Query

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

整数 K が与えられます。はじめ空である集合 S に対して、次の 2 種類のクエリを順に Q 個処理してください。

  • 1 x:整数 x が与えられる。Sx が含まれているならば、S から x を取り除く。そうでないならば、Sx を追加する。
  • 2 xS に含まれる整数 x が与えられる。S に含まれる数を頂点とし、差の絶対値が K 以下であるような数の間に辺を張ったグラフにおいて、x が属する連結成分の頂点数を出力する。

制約

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq K \leq 10^{18}
  • 各クエリにおいて、1\leq x \leq 10^{18}
  • 2 種類目のクエリにおいて与えられる x はその時点で S に含まれる。
  • 入力は全て整数である。

入力

入力は以下の形式で標準入力から与えられる。

Q K
\mathrm{query}_1
\vdots
\mathrm{query}_Q

各クエリはそれぞれ次の形式で与えられる。

1 x
2 x

出力

クエリを処理せよ。


入力例 1

7 5
1 3
1 10
2 3
1 7
2 3
1 10
2 3

出力例 1

1
3
2

クエリの処理は次のように進行します。

  • 1 番目のクエリでは、S3 が追加され、S=\{3\} となります。
  • 2 番目のクエリでは、S10 が追加され、S=\{3,10\} となります。
  • 3 番目のクエリでは、3,102 頂点からなる辺のないグラフを考え、3 が属する連結成分のサイズである 1 を出力します。
  • 4 番目のクエリでは、S7 が追加され、S=\{3,7,10\} となります。
  • 5 番目のクエリでは、3,7,103 頂点からなり、37710 の間に辺のあるグラフを考え、3 が属する連結成分のサイズである 3 を出力します。
  • 6 番目のクエリでは、S から 10 が削除され、S=\{3,7\} となります。
  • 7 番目のクエリでは、3,72 頂点からなり、37 の間に辺のあるグラフを考え、3 が属する連結成分のサイズである 2 を出力します。

入力例 2

11 1000000000000000000
1 1
1 100
1 10000
1 1000000
1 100000000
1 10000000000
1 1000000000000
1 100000000000000
1 10000000000000000
1 1000000000000000000
2 1

出力例 2

10

入力例 3

8 0
1 1
1 2
2 1
1 1
1 2
1 1
1 2
2 1

出力例 3

1
1

Score : 525 points

Problem Statement

You are given an integer K. For a set S that is initially empty, process Q queries of the following two types in order:

  • 1 x: An integer x is given. If x is in S, remove x from S. Otherwise, add x to S.
  • 2 x: An integer x that is in S is given. Consider a graph where the vertices are the numbers in S, and there is an edge between two numbers if and only if the absolute difference between them is at most K. Print the count of vertices in the connected component that contains x.

Constraints

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq K \leq 10^{18}
  • For each query, 1 \leq x \leq 10^{18}.
  • For each query of the second type, the given x is in S at that point.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

Q K
\mathrm{query}_1
\vdots
\mathrm{query}_Q

Each query is given in the following format:

1 x
2 x

Output

Process the queries.


Sample Input 1

7 5
1 3
1 10
2 3
1 7
2 3
1 10
2 3

Sample Output 1

1
3
2

The query processing proceeds as follows:

  • In the first query, 3 is added to S, resulting in S=\{3\}.
  • In the second query, 10 is added to S, resulting in S=\{3,10\}.
  • In the third query, consider a graph with vertices 3 and 10 and no edges. The connected component containing 3 has a size of 1, so print 1.
  • In the fourth query, 7 is added to S, resulting in S=\{3,7,10\}.
  • In the fifth query, consider a graph with vertices 3, 7, and 10, with edges between 3 and 7, and 7 and 10. The connected component containing 3 has a size of 3, so print 3.
  • In the sixth query, 10 is removed from S, resulting in S=\{3,7\}.
  • In the seventh query, consider a graph with vertices 3 and 7, with an edge between them. The connected component containing 3 has a size of 2, so print 2.

Sample Input 2

11 1000000000000000000
1 1
1 100
1 10000
1 1000000
1 100000000
1 10000000000
1 1000000000000
1 100000000000000
1 10000000000000000
1 1000000000000000000
2 1

Sample Output 2

10

Sample Input 3

8 0
1 1
1 2
2 1
1 1
1 2
1 1
1 2
2 1

Sample Output 3

1
1
G - Freestyle

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 575

問題文

高橋くんは N 種類の泳ぎ方ができます。
高橋くんが i 種類目の泳ぎ方で泳ぐと、 1 秒あたり体力を A_i 消費して B_i [m] 進みます。

Q 個のクエリに答えてください。そのうち i 個目は次の通りです。

  • 消費する体力の合計を C_i 以下にして D_i [m] 進むことができるか判定し、進める場合は必要な最小の秒数を求めよ。

ただし、高橋くんは泳ぎ方を自由に組み合わせることができ、泳ぎ方を変える時間は無視できます。
具体的には、次の手順で泳ぐことができます。

  • 正整数 m 、全ての要素が正である長さ m の実数列 t=(t_1,t_2,\dots,t_m) 、全ての要素が 1 以上 N 以下の長さ m の整数列 x=(x_1,x_2,\dots,x_m) を選択する。
  • その後、 i=1,2,\dots,m の順で、 x_i 種類目の泳ぎ方で t_i 秒間泳ぐ。

制約

  • 入力は全て整数
  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i,B_i \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 1 \le C_i,D_i \le 10^9

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
Q
C_1 D_1
C_2 D_2
\vdots
C_Q D_Q

出力

全体で Q 行出力せよ。
そのうち i 行目に、 i 個目のクエリに対する解答を次の通りに出力せよ。

  • もし消費する体力の合計を C_i 以下にして D_i [m] 進むことができない場合、 -1 と出力せよ。
  • そうでない場合、必要な最小の秒数を出力せよ。このとき、「出力された値」と「正答の真の値」との絶対誤差または相対誤差が 10^{-9} 以下であれば、出力は正答とみなされる。

入力例 1

4
1 2
2 3
3 3
4 4
5
4 7
7 7
49 100
1000 500
4 5

出力例 1

3.000000000000000000
1.750000000000000000
-1
125.000000000000000000
1.500000000000000000

この入力において、高橋くんは以下の 4 種類の泳ぎ方ができます。

  • 1 秒あたり体力を 1 消費して 2 [m] 進む。
  • 1 秒あたり体力を 2 消費して 3 [m] 進む。
  • 1 秒あたり体力を 3 消費して 3 [m] 進む。
  • 1 秒あたり体力を 4 消費して 4 [m] 進む。

この入力にはクエリが 5 個含まれます。

  • 1 個目の質問では、 C_1=4,D_1=7 です。
    • t=(1,2),x=(2,1) と選択します。このとき高橋くんは次の通りに泳ぎます。
      • 最初の 1 秒で高橋くんは体力を 2 消費して 3 [m] 進みます。
      • 次の 2 秒で高橋くんは体力を 2 消費して 4 [m] 進みます。
    • このとき、高橋くんは全体で体力を 4 消費して 7 [m] 進みました。この場合の所要時間は 3 秒で、これが達成可能な最小です。
  • 2 個目の質問では、 C_2=7,D_2=7 です。
    • t=(7/4),x=(4) と選択します。このとき高橋くんは次の通りに泳ぎます。
      • 最初の 7/4 秒で高橋くんは体力を 7 消費して 7 [m] 進みます。
    • このとき、高橋くんは全体で体力を 7 消費して 7 [m] 進みました。この場合の所要時間は 7/4 秒で、これが達成可能な最小です。
  • 3 個目の質問では、 C_3=49,D_3=100 です。
    • 高橋くんがどのような泳ぎ方をしても、消費する体力の合計を 49 以下にして 100 [m] 進むことはできません。
  • 4 個目の質問では、 C_4=1000,D_4=500 です。
    • t=(125),x=(4) と選択します。このとき高橋くんは次の通りに泳ぎます。
      • 最初の 125 秒で高橋くんは体力を 500 消費して 500 [m] 進みます。
    • このとき、高橋くんは全体で体力を 500 消費して 500 [m] 進みました。この場合の所要時間は 125 秒で、これが達成可能な最小です。
  • 5 個目の質問では、 C_5=4,D_5=5 です。
    • t=(1/2,1),x=(4,2) と選択します。このとき高橋くんは次の通りに泳ぎます。
      • 最初の 1/2 秒で高橋くんは体力を 2 消費して 2 [m] 進みます。
      • 次の 1 秒で高橋くんは体力を 2 消費して 3 [m] 進みます。
    • このとき、高橋くんは全体で体力を 4 消費して 5 [m] 進みました。この場合の所要時間は 3/2 秒で、これが達成可能な最小です。

Score : 575 points

Problem Statement

Takahashi can swim in N different styles.
When he swims in the i-th style, he consumes A_i stamina per second and advances B_i meters per second.

Answer Q queries. The i-th query is as follows:

  • Determine if it is possible to advance D_i meters while keeping the total stamina consumption at most C_i. If it is possible, find the minimum number of seconds required.

Here, he can freely combine different swimming styles, and the time to switch styles is negligible.
Specifically, he can swim using the following steps:

  • Choose a positive integer m, a sequence of positive real numbers t=(t_1,t_2,\dots,t_m) of length m, and a sequence of integers x=(x_1,x_2,\dots,x_m) of length m where each element is between 1 and N, inclusive.
  • Then, swim in the x_i-th style for t_i seconds in the order i=1,2,\dots,m.

Constraints

  • All input values are integers.
  • 1 \le N \le 2 \times 10^5
  • 1 \le A_i, B_i \le 10^9
  • 1 \le Q \le 2 \times 10^5
  • 1 \le C_i, D_i \le 10^9

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N
Q
C_1 D_1
C_2 D_2
\vdots
C_Q D_Q

Output

Print Q lines in total.
For the i-th query, print the answer in the i-th line as follows:

  • If it is impossible to advance D_i meters while keeping the total stamina consumption at most C_i, print -1.
  • Otherwise, print the minimum required time. The output is considered correct if the absolute or relative error between the printed value and the true answer is at most 10^{-9}.

Sample Input 1

4
1 2
2 3
3 3
4 4
5
4 7
7 7
49 100
1000 500
4 5

Sample Output 1

3.000000000000000000
1.750000000000000000
-1
125.000000000000000000
1.500000000000000000

In this input, Takahashi can swim in the following four styles:

  • Consumes 1 stamina and advances 2 meters per second.
  • Consumes 2 stamina and advances 3 meters per second.
  • Consumes 3 stamina and advances 3 meters per second.
  • Consumes 4 stamina and advances 4 meters per second.

This input contains five queries.

  • For the first query, C_1=4, D_1=7.
    • Choose t=(1,2) and x=(2,1). Takahashi swims as follows:
      • In the first 1 second, he consumes 2 stamina and advances 3 meters.
      • In the next 2 seconds, he consumes 2 stamina and advances 4 meters.
    • In total, he consumes 4 stamina and advances 7 meters. The required time is 3 seconds, which is the minimum.
  • For the second query, C_2=7, D_2=7.
    • Choose t=(7/4) and x=(4). Takahashi swims as follows:
      • In the first 7/4 seconds, he consumes 7 stamina and advances 7 meters.
    • In total, he consumes 7 stamina and advances 7 meters. The required time is 7/4 seconds, which is the minimum.
  • For the third query, C_3=49, D_3=100.
    • No matter how Takahashi swims, it is not possible to advance 100 meters while keeping the total stamina consumption at most 49.
  • For the fourth query, C_4=1000, D_4=500.
    • Choose t=(125) and x=(4). Takahashi swims as follows:
      • In the first 125 seconds, he consumes 500 stamina and advances 500 meters.
    • In total, he consumes 500 stamina and advances 500 meters. The required time is 125 seconds, which is the minimum.
  • For the fifth query, C_5=4, D_5=5.
    • Choose t=(1/2,1) and x=(4,2). Takahashi swims as follows:
      • In the first 1/2 seconds, he consumes 2 stamina and advances 2 meters.
      • In the next 1 second, he consumes 2 stamina and advances 3 meters.
    • In total, he consumes 4 stamina and advances 5 meters. The required time is 3/2 seconds, which is the minimum.