E - The Kth Time Query

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の数列 A = (a_1, a_2, \dots, a_N) があります。
以下で説明される Q 個のクエリに答えてください。

  • クエリ i : 整数の組 (x_i, k_i) が与えられます。A の要素を a_1, a_2, \dots と前から順に見ていったときに、数 x_ik_i 回目に登場するのは A の前から何番目の要素を見たときかを出力してください。
    ただし条件を満たす要素が存在しない場合は -1 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
  • 1 \leq k_i \leq N (1 \leq i \leq Q)
  • 入力はすべて整数である。

入力

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

N Q
a_1 a_2 \dots a_N
x_1 k_1
x_2 k_2
\vdots
x_Q k_Q

出力

Q 行出力せよ。i 行目ではクエリ i に対する答えを出力せよ。


入力例 1

6 8
1 1 2 3 1 2
1 1
1 2
1 3
1 4
2 1
2 2
2 3
4 1

出力例 1

1
2
5
-1
3
6
-1
-1

A の中で 1a_1, a_2, a_5 に登場します。よって、クエリ 1 からクエリ 4 の答えは順に 1, 2, 5, -1 となります。


入力例 2

3 2
0 1000000000 999999999
1000000000 1
123456789 1

出力例 2

2
-1

Score : 300 points

Problem Statement

We have a sequence of N numbers: A = (a_1, a_2, \dots, a_N).
Process the Q queries explained below.

  • Query i: You are given a pair of integers (x_i, k_i). Let us look at the elements of A one by one from the beginning: a_1, a_2, \dots Which element will be the k_i-th occurrence of the number x_i?
    Print the index of that element, or -1 if there is no such element.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 0 \leq a_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq x_i \leq 10^9 (1 \leq i \leq Q)
  • 1 \leq k_i \leq N (1 \leq i \leq Q)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 a_2 \dots a_N
x_1 k_1
x_2 k_2
\vdots
x_Q k_Q

Output

Print Q lines. The i-th line should contain the answer to Query i.


Sample Input 1

6 8
1 1 2 3 1 2
1 1
1 2
1 3
1 4
2 1
2 2
2 3
4 1

Sample Output 1

1
2
5
-1
3
6
-1
-1

1 occurs in A at a_1, a_2, a_5. Thus, the answers to Query 1 through 4 are 1, 2, 5, -1 in this order.


Sample Input 2

3 2
0 1000000000 999999999
1000000000 1
123456789 1

Sample Output 2

2
-1
F - Colorful Beans

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

N 種類のビーンズが 1 粒ずつあります。 i 種類目のビーンズはおいしさが A_i で色が C_i です。ビーンズは混ぜられており、色でしか区別することができません。

あなたはビーンズの色を 1 つ選び、その色のビーンズをどれか 1 粒食べます。ビーンズの色をうまく選ぶことで、食べる可能性のあるビーンズのおいしさの最小値を最大化してください。

制約

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq A_i \leq 10^{9}
  • 1 \leq C_i \leq 10^{9}
  • 入力は全て整数である。

入力

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

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

出力

食べる可能性のあるビーンズのおいしさの最小値の最大値を整数として出力せよ。


入力例 1

4
100 1
20 5
30 5
40 1

出力例 1

40

同じ色のビーンズは互いに区別がつかないことに注意してください。

選ぶことができる色は 色 1 と 色 5 です。

  • 1 のビーンズは 2 種類あり、美味しさはそれぞれ 100, 40 です。よって、色 1 を選んだときのおいしさの最小値は 40 です。
  • 5 のビーンズは 2 種類あり、美味しさはそれぞれ 20, 30 です。よって、色 5 を選んだときのおいしさの最小値は 20 です。

おいしさの最小値を最大化するには 色 1 を選べばよいため、そのときの最小値である 40 を出力します。


入力例 2

10
68 3
17 2
99 2
92 4
82 4
10 3
100 2
78 1
3 1
35 4

出力例 2

35

Score: 250 points

Problem Statement

There are N types of beans, one bean of each type. The i-th type of bean has a deliciousness of A_i and a color of C_i. The beans are mixed and can only be distinguished by color.

You will choose one color of beans and eat one bean of that color. By selecting the optimal color, maximize the minimum possible deliciousness of the bean you eat.

Constraints

  • 1 \leq N \leq 2 \times 10^{5}
  • 1 \leq A_i \leq 10^{9}
  • 1 \leq C_i \leq 10^{9}
  • All input values are integers.

Input

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

N
A_1 C_1
A_2 C_2
\vdots
A_N C_N

Output

Print as an integer the maximum value of the minimum possible deliciousness of the bean you eat.


Sample Input 1

4
100 1
20 5
30 5
40 1

Sample Output 1

40

Note that beans of the same color cannot be distinguished from each other.

You can choose color 1 or color 5.

  • There are two types of beans of color 1, with deliciousness of 100 and 40. Thus, the minimum deliciousness when choosing color 1 is 40.
  • There are two types of beans of color 5, with deliciousness of 20 and 30. Thus, the minimum deliciousness when choosing color 5 is 20.

To maximize the minimum deliciousness, you should choose color 1, so print the minimum deliciousness in that case: 40.


Sample Input 2

10
68 3
17 2
99 2
92 4
82 4
10 3
100 2
78 1
3 1
35 4

Sample Output 2

35
G - AND and SUM

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

T 個のテストケースについて、以下の問題を解いてください。

非負整数 a,s が与えられます。以下の条件を両方とも満たす非負整数の組 (x,y) は存在しますか?

  • x\ \text{AND}\ y=a
  • x+y=s
\text{AND} とは

非負整数 n, m の bit ごとの論理積 n\ \text{AND}\ m は、以下のように定義されます。

  • n\ \text{AND}\ m を二進表記した際の 2^k \, (k \geq 0) の位の数は、n, m を二進表記した際の 2^k の位の数のうち両方1 であれば 1、そうでなければ 0 である。
例えば、4\ \text{AND}\ 6 = 4 となります(二進表記すると: 100\ \text{AND}\ 110 = 100)。

制約

  • 1 \leq T \leq 10^5
  • 0 \leq a,s \lt 2^{60}
  • 入力はすべて整数

入力

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

T

その後、 T 個のテストケースが続く。各テストケースは以下の形式で与えられる。

a s

出力

T 行出力せよ。i\ (1 \leq i \leq T) 行目には、i 番目に与えられるテストケースについて問題文中の条件を両方とも満たす非負整数の組 (x,y) が存在するなら Yes を、存在しないなら No を出力せよ。


入力例 1

2
1 8
4 2

出力例 1

Yes
No

1 番目のテストケースにおいては、(x,y)=(3,5) などが条件を満たします。

2 番目のテストケースにおいては、条件を満たす非負整数の組 (x,y) は存在しません。


入力例 2

4
201408139683277485 381410962404666524
360288799186493714 788806911317182736
18999951915747344 451273909320288229
962424162689761932 1097438793187620758

出力例 2

No
Yes
Yes
No

Score : 400 points

Problem Statement

Solve the following problem for T test cases.

Given are non-negative integers a and s. Is there a pair of non-negative integers (x,y) that satisfies both of the conditions below?

  • x\ \text{AND}\ y=a
  • x+y=s
What is bitwise \mathrm{AND}?

The bitwise \mathrm{AND} of integers A and B, A\ \mathrm{AND}\ B, is defined as follows:

  • When A\ \mathrm{AND}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if those of A and B are both 1, and 0 otherwise.
For example, we have 4\ \mathrm{AND}\ 6 = 4 (in base two: 100\ \mathrm{AND}\ 110 = 100).

Constraints

  • 1 \leq T \leq 10^5
  • 0 \leq a,s \lt 2^{60}
  • All values in input are integers.

Input

Input is given from Standard Input. The first line is in the following format:

T

Then, T test cases follow. Each test case is in the following format:

a s

Output

Print T lines. The i-th line (1 \leq i \leq T) should contain Yes if, in the i-th test case, there is a pair of non-negative integers (x,y) that satisfies both of the conditions in the Problem Statement, and No otherwise.


Sample Input 1

2
1 8
4 2

Sample Output 1

Yes
No

In the first test case, some pairs such as (x,y)=(3,5) satisfy the conditions.

In the second test case, no pair of non-negative integers satisfies the conditions.


Sample Input 2

4
201408139683277485 381410962404666524
360288799186493714 788806911317182736
18999951915747344 451273909320288229
962424162689761932 1097438793187620758

Sample Output 2

No
Yes
Yes
No
H - Snowflake Tree

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

ユ木」を、以下の手順で生成することができる木と定義します。

  1. 正整数 x,y を選ぶ
  2. 頂点を 1 つ用意する
  3. 別に x 個の頂点を用意し、それぞれを手順 2 で用意した頂点と辺で結ぶ
  4. 手順 3 で用意した x 個の頂点それぞれに、 y 個の葉をつける

x=4,y=2 のユ木を下図に示します。手順 2,3,4 で用意される頂点をそれぞれ赤、青、緑で示しています。

N 頂点の木 T が与えられます。頂点には 1 から N の番号が付けられており、 i\;(=1,2,\dots,N-1) 番目の辺は頂点 u_i と頂点 v_i を結びます。

T0 個以上の頂点とそれに隣接する辺を削除して 1 つのユ木にするとき、削除する頂点数の最小値を求めてください。なお、本問題の制約下で、T をかならずユ木にすることができます。

制約

  • 3 \leq N \leq 3 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • 与えられるグラフは木である
  • 入力はすべて整数

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

答えを出力せよ。


入力例 1

8
1 3
2 3
3 4
4 5
5 6
5 7
4 8

出力例 1

1

頂点 8 を削除することで、与えられた木を x=2,y=2 のユ木にすることができます。


入力例 2

3
1 2
2 3

出力例 2

0

与えられた木はすでに x=1,y=1 のユ木です。


入力例 3

10
1 3
1 2
5 7
6 10
2 8
1 6
8 9
2 7
1 4

出力例 3

3

Score : 450 points

Problem Statement

A "Snowflake Tree" is defined as a tree that can be generated by the following procedure:

  1. Choose positive integers x,y.
  2. Prepare one vertex.
  3. Prepare x more vertices, and connect each of them to the vertex prepared in step 2.
  4. For each of the x vertices prepared in step 3, attach y leaves to it.

The figure below shows a Snowflake Tree with x=4,y=2. The vertices prepared in steps 2, 3, 4 are shown in red, blue, and green, respectively.

You are given a tree T with N vertices. The vertices are numbered 1 to N, and the i-th edge (i=1,2,\dots,N-1) connects vertices u_i and v_i.

Consider deleting zero or more vertices of T and the edges adjacent to them so that the remaining graph becomes a single Snowflake Tree. Find the minimum number of vertices that must be deleted. Under the constraints of this problem, it is always possible to transform T into a Snowflake Tree.

Constraints

  • 3 \leq N \leq 3 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • The given graph is a tree.
  • All input values are integers.

Input

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print the answer.


Sample Input 1

8
1 3
2 3
3 4
4 5
5 6
5 7
4 8

Sample Output 1

1

By deleting vertex 8, the given tree can be transformed into a Snowflake Tree with x=2,y=2.


Sample Input 2

3
1 2
2 3

Sample Output 2

0

The given tree is already a Snowflake Tree with x=1,y=1.


Sample Input 3

10
1 3
1 2
5 7
6 10
2 8
1 6
8 9
2 7
1 4

Sample Output 3

3
I - Damage over Time

Time Limit: 3.5 sec / Memory Limit: 1024 MiB

配点 : 525

問題文

あなたの前に体力 H のモンスターが現れ、ターン制の戦闘が開始しました。  

あなたは、ターン 1,2,… のそれぞれに呪文 1,…,NN 種類の呪文のうち一つを唱えます。  

ターン i に呪文 j を唱えると、その呪文の効果としてターン i,i+1,…,i+t_j -1 のそれぞれでモンスターの体力が d_j 減ります。

モンスターの体力を 0 以下にすることが可能な最も早いターンを求めてください。

制約

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq H \leq 10^{18}
  • 1 \leq t_i,d_i \leq 10^9
  • 入力はすべて整数

入力

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

N H
t_1 d_1
\vdots
t_N d_N

出力

答えを出力せよ。


入力例 1

2 20
2 2
5 1

出力例 1

6

以下のようにするとターン 6 にモンスターの体力を 0 以下に出来ます。これが最も早いターンです。

  • ターン 1 に魔法 1 を使う。ターン 1 に使った魔法の効果でモンスターの体力が 2 減り、18 になる。
  • ターン 2 に魔法 2 を使う。ターン 1,2 に使った魔法の効果でモンスターの体力が 2+1=3 減り、15 になる。
  • ターン 3 に魔法 1 を使う。ターン 2,3 に使った魔法の効果でモンスターの体力が 1+2=3 減り、12 になる。
  • ターン 4 に魔法 2 を使う。ターン 2,3,4 に使った魔法の効果でモンスターの体力が 1+2+1=4 減り、8 になる。
  • ターン 5 に魔法 1 を使う。ターン 2,4,5 に使った魔法の効果でモンスターの体力が 1+1+2=4 減り、4 になる。
  • ターン 6 に魔法 2 を使う。ターン 2,4,5,6 に使った魔法の効果でモンスターの体力が 1+1+2+1=5 減り、-1 になる。

入力例 2

10 200
1 21
1 1
1 1
8 4
30 1
3 1
10 2
8 1
9 1
4 4

出力例 2

9

Score : 525 points

Problem Statement

A monster with health H has appeared in front of you, and a turn-based battle has started.

In each turn 1,2,…, you cast one of N spells, spells 1,…,N.

If you cast spell i in turn j, the monster's health reduces by d_i in each turn j,j+1,…,j+t_i -1.

Find the earliest turn that you can make the monster's health 0 or less.

Constraints

  • 1 \leq N \leq 3 \times 10^5
  • 1 \leq H \leq 10^{18}
  • 1 \leq t_i,d_i \leq 10^9
  • All values in the input are integers.

Input

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

N H
t_1 d_1
\vdots
t_N d_N

Output

Print the answer.


Sample Input 1

2 20
2 2
5 1

Sample Output 1

6

The following procedure makes the monster's health 0 or less in turn 6, which is the earliest.

  • Cast spell 1 in turn 1. Due to the spell cast in turn 1, the monster's health reduces by 2 and becomes 18.
  • Cast spell 2 in turn 2. Due to the spells cast in turns 1 and 2, the monster's health reduces by 2+1=3 and becomes 15.
  • Cast spell 1 in turn 3. Due to the spells cast in turns 2 and 3, the monster's health reduces by 1+2=3 and becomes 12.
  • Cast spell 2 in turn 4. Due to the spells cast in turns 2,3 and 4, the monster's health reduces by 1+2+1=4 and becomes 8.
  • Cast spell 1 in turn 5. Due to the spells cast in turns 2,4 and 5, the monster's health reduces by 1+1+2=4 and becomes 4.
  • Cast spell 2 in turn 6. Due to the spells cast in turns 2,4,5 and 6, the monster's health reduces by 1+1+2+1=5 and becomes -1.

Sample Input 2

10 200
1 21
1 1
1 1
8 4
30 1
3 1
10 2
8 1
9 1
4 4

Sample Output 2

9