A - Overall Winner

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋くんと青木くんが N 回の試合を行いました。 これらの試合の結果を表す長さ N の文字列 S が与えられます。 i 回目の試合の勝者は、Si 文字目が T ならば高橋くん、A ならば青木くんです。

高橋くんと青木くんのうち、勝った試合の数が多い方を総合勝者とします。 ただし、勝った試合の数が同じである場合は、先にその勝ち数に達した者を総合勝者とします。 高橋くんと青木くんのどちらが総合勝者であるか求めてください。

制約

  • 1\leq N \leq 100
  • N は整数
  • ST および A からなる長さ N の文字列

入力

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

N
S

出力

総合勝者が高橋くんならば T を、青木くんならば A を出力せよ。


入力例 1

5
TTAAT

出力例 1

T

高橋くんは 3 回の試合に勝ち、青木くんは 2 回の試合に勝ちました。 よって、勝った試合の数が多い高橋くんが総合勝者です。


入力例 2

6
ATTATA

出力例 2

T

高橋くんと青木くんのどちらも 3 回の試合に勝ちました。 また、高橋くんは 5 回目の試合で 3 勝目に達し、青木くんは 6 回目の試合で 3 勝目に達しました。 よって、先に 3 勝目に達した高橋くんが総合勝者です。


入力例 3

1
A

出力例 3

A

Score : 100 points

Problem Statement

Takahashi and Aoki played N games. You are given a string S of length N, representing the results of these games. Takahashi won the i-th game if the i-th character of S is T, and Aoki won that game if it is A.

The overall winner between Takahashi and Aoki is the one who won more games than the other. If they had the same number of wins, the overall winner is the one who reached that number of wins first. Find the overall winner: Takahashi or Aoki.

Constraints

  • 1\leq N \leq 100
  • N is an integer.
  • S is a string of length N consisting of T and A.

Input

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

N
S

Output

If the overall winner is Takahashi, print T; if it is Aoki, print A.


Sample Input 1

5
TTAAT

Sample Output 1

T

Takahashi won three games, and Aoki won two. Thus, the overall winner is Takahashi, who won more games.


Sample Input 2

6
ATTATA

Sample Output 2

T

Both Takahashi and Aoki won three games. Takahashi reached three wins in the fifth game, and Aoki in the sixth game. Thus, the overall winner is Takahashi, who reached three wins first.


Sample Input 3

1
A

Sample Output 3

A
B - Four Points

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

xy 平面上に長方形があります。この長方形の各辺は x 軸または y 軸に平行であり、面積は 0 ではありません。

この長方形の 4 つの頂点のうち異なる 3 つの頂点の座標 (x_1, y_1), (x_2, y_2), (x_3, y_3) が与えられるので、残る 1 つの頂点の座標を求めてください。

制約

  • -100 \leq x_i, y_i \leq 100
  • (x_1, y_1), (x_2, y_2), (x_3, y_3) のすべてを頂点に持つ長方形がただ一つ存在し、その各辺は x 軸または y 軸に平行であり、面積は 0 ではない。
  • 入力はすべて整数

入力

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

x_1 y_1
x_2 y_2
x_3 y_3

出力

答えとなる頂点の座標 (x, y) を下記の形式にしたがい空白区切りで出力せよ。

x y

入力例 1

-1 -1
-1 2
3 2

出力例 1

3 -1

(-1, -1), (-1, 2), (3, 2) を頂点とする長方形の残る 1 つの頂点は (3, -1) です。


入力例 2

-60 -40
-60 -80
-20 -80

出力例 2

-20 -40

Score : 100 points

Problem Statement

There is a rectangle in the xy-plane. Each edge of this rectangle is parallel to the x- or y-axis, and its area is not zero.

Given the coordinates of three of the four vertices of this rectangle, (x_1, y_1), (x_2, y_2), and (x_3, y_3), find the coordinates of the other vertex.

Constraints

  • -100 \leq x_i, y_i \leq 100
  • There uniquely exists a rectangle with all of (x_1, y_1), (x_2, y_2), (x_3, y_3) as vertices, edges parallel to the x- or y-axis, and a non-zero area.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

x_1 y_1
x_2 y_2
x_3 y_3

Output

Print the sought coordinates (x, y) separated by a space in the following format:

x y

Sample Input 1

-1 -1
-1 2
3 2

Sample Output 1

3 -1

The other vertex of the rectangle with vertices (-1, -1), (-1, 2), (3, 2) is (3, -1).


Sample Input 2

-60 -40
-60 -80
-20 -80

Sample Output 2

-20 -40
C - V

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は漢文の勉強をしていて、漢字を読む順番が分からず困っています。高橋君を助けましょう!

1 から N までの N 個の整数が小さい方から順に 1 列に並んでいます。
整数の間に M 個の「レ」が挟まっています。i 個目の「レ」は、整数 a_i と整数 a_i + 1 の間にあります。

あなたは次の手順に従って、N 個の整数を 1 回ずつ全て読みます。

  • まず、頂点に 1 から N までの番号がついた N 頂点 M 辺の無向グラフ G を考える。i 本目の辺は頂点 a_i と頂点 a_i + 1 を結んでいる。
  • そして、読まれていない整数が無くなるまで次の操作を繰り返す。
    • 読まれていない整数のうち最小のものを x とする。頂点 x が含まれる連結成分 C を選び、C に含まれる頂点の番号を大きい方から順に全て読む。

例えば、整数と「レ」が

image

という順番で並んでいる場合を考えます。(この場合 N = 5, M = 3, a = (1, 3, 4) です。)
このとき、整数が読まれる順番は以下の手順によって 2, 1, 5, 4, 3 に決定します。

  • 最初、読まれていない整数のうち最小のものは 1 であり、グラフ G の頂点 1 を含む連結成分に含まれる頂点は \lbrace 1, 2 \rbrace である。よって 2, 1 がこの順で読まれる。
  • 次に、読まれていない整数のうち最小のものは 3 であり、グラフ G の頂点 3 を含む連結成分に含まれる頂点は \lbrace 3, 4, 5 \rbrace である。よって 5, 4, 3 がこの順で読まれる。
  • すべての整数が読まれたので手順を終了する。

N, M, (a_1, a_2, \dots, a_M) が入力として与えられるので、 N 個の整数を読む順番を出力してください。

連結成分とは あるグラフの 部分グラフ とは、元のグラフのいくつかの頂点といくつかの辺を選んでできるグラフのことをいいます。
グラフが 連結 であるとは、グラフに含まれるすべての頂点同士が辺を経由して互いに行き来できることをいいます。
連結成分 とは、連結な部分グラフのうち、そのグラフを含んだより大きい連結な部分グラフが存在しないものをいいます。

制約

  • 1 \leq N \leq 100
  • 0 \leq M \leq N - 1
  • 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
  • 入力される値は全て整数

入力

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

N M
a_1 a_2 \dots a_M

出力

答えを以下の形式で出力せよ。ここで p_i は、i 番目に読まれる整数を意味する。

p_1 p_2 \dots p_N

入力例 1

5 3
1 3 4

出力例 1

2 1 5 4 3

問題文の例にある通り、整数と「レ」が

image

という順で並んでいる場合は 2, 1, 5, 4, 3 の順で読みます。


入力例 2

5 0

出力例 2

1 2 3 4 5

「レ」が存在しない場合もあります。


入力例 3

10 6
1 2 3 7 8 9

出力例 3

4 3 2 1 5 6 10 9 8 7

Score : 200 points

Problem Statement

Studying Kanbun, Takahashi is having trouble figuring out the order to read words. Help him out!

There are N integers from 1 through N arranged in a line in ascending order.
Between them are M "レ" marks. The i-th "レ" mark is between the integer a_i and integer (a_i + 1).

You read each of the N integers once by the following procedure.

  • Consider an undirected graph G with N vertices numbered 1 through N and M edges. The i-th edge connects vertex a_i and vertex (a_i+1).
  • Repeat the following operation until there is no unread integer:
    • let x be the minimum unread integer. Choose the connected component C containing vertex x, and read all the numbers of the vertices contained in C in descending order.

For example, suppose that integers and "レ" marks are arranged in the following order:

image

(In this case, N = 5, M = 3, and a = (1, 3, 4).)
Then, the order to read the integers is determined to be 2, 1, 5, 4, and 3, as follows:

  • At first, the minimum unread integer is 1, and the connected component of G containing vertex 1 has vertices \lbrace 1, 2 \rbrace, so 2 and 1 are read in this order.
  • Then, the minimum unread integer is 3, and the connected component of G containing vertex 3 has vertices \lbrace 3, 4, 5 \rbrace, so 5, 4, and 3 are read in this order.
  • Now that all integers are read, terminate the procedure.

Given N, M, and (a_1, a_2, \dots, a_M), print the order to read the N integers.

What is a connected component? A subgraph of a graph is a graph obtained by choosing some vertices and edges from the original graph.
A graph is said to be connected if and only if one can travel between any two vertices in the graph via edges.
A connected component is a connected subgraph that is not contained in any larger connected subgraph.

Constraints

  • 1 \leq N \leq 100
  • 0 \leq M \leq N - 1
  • 1 \leq a_1 \lt a_2 \lt \dots \lt a_M \leq N-1
  • All values in the input are integers.

Input

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

N M
a_1 a_2 \dots a_M

Output

Print the answer in the following format, where p_i is the i-th integers to read.

p_1 p_2 \dots p_N

Sample Input 1

5 3
1 3 4

Sample Output 1

2 1 5 4 3

As described in the Problem Statement, if integers and "レ" marks are arranged in the following order:

image

then the integers are read in the following order: 2, 1, 5, 4, and 3.


Sample Input 2

5 0

Sample Output 2

1 2 3 4 5

There may be no "レ" mark.


Sample Input 3

10 6
1 2 3 7 8 9

Sample Output 3

4 3 2 1 5 6 10 9 8 7
D - Substring

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

英小文字からなる文字列 S が与えられます。S の空でない部分文字列は何種類ありますか?

ただし、部分文字列とは連続する部分列のことを指します。例えば、xxxyxxxy の部分文字列ですが、xxyxx の部分文字列ではありません。

制約

  • S は英小文字からなる長さ 1 以上 100 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

yay

出力例 1

5

S の空でない部分文字列は以下の 5 種類です。

  • a
  • y
  • ay
  • ya
  • yay

入力例 2

aababc

出力例 2

17

入力例 3

abracadabra

出力例 3

54

Score: 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. How many different non-empty substrings does S have?

A substring is a contiguous subsequence. For example, xxx is a substring of yxxxy but not of xxyxx.

Constraints

  • S is a string of length between 1 and 100, inclusive, consisting of lowercase English letters.

Input

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

S

Output

Print the answer.


Sample Input 1

yay

Sample Output 1

5

S has the following five different non-empty substrings:

  • a
  • y
  • ay
  • ya
  • yay

Sample Input 2

aababc

Sample Output 2

17

Sample Input 3

abracadabra

Sample Output 3

54
E - Minimize Abs 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

正整数 D が与えられます。

非負整数 x,y に対する |x^2+y^2-D| の最小値を求めてください。

制約

  • 1\leq D \leq 2\times 10^{12}
  • 入力は全て整数

入力

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

D

出力

答えを出力せよ。


入力例 1

21

出力例 1

1

x=4,y=2 のとき |x^2+y^2-D| = |16+4-21|=1 となります。

|x^2+y^2-D|=0 を満たすような非負整数 x,y は存在しないので、答えは 1 です。


入力例 2

998244353

出力例 2

0

入力例 3

264428617

出力例 3

32

Score : 300 points

Problem Statement

You are given a positive integer D.

Find the minimum value of |x^2+y^2-D| for non-negative integers x and y.

Constraints

  • 1\leq D \leq 2\times 10^{12}
  • All input values are integers.

Input

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

D

Output

Print the answer.


Sample Input 1

21

Sample Output 1

1

For x=4 and y=2, we have |x^2+y^2-D| = |16+4-21|=1.

There are no non-negative integers x and y such that |x^2+y^2-D|=0, so the answer is 1.


Sample Input 2

998244353

Sample Output 2

0

Sample Input 3

264428617

Sample Output 3

32
F - Cheese

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

ピザ屋で働く高橋くんは、まかないとして美味しいチーズピザを作ることにしました。
今、高橋くんの目の前に N 種類のチーズがあります。
i 種類目のチーズは 1 [g] あたりのおいしさが A_i で、 B_i [g] あります。
ピザのおいしさは、ピザに乗せたチーズのおいしさの総和で決まります。
但し、チーズを使いすぎると怒られてしまうため、乗せたチーズの重さは合計で W [g] 以下である必要があります。
この条件のもとで、可能なピザのおいしさの最大値を求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • 1 \le W \le 3 \times 10^8
  • 1 \le A_i \le 10^9
  • 1 \le B_i \le 1000

入力

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

N W
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

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


入力例 1

3 5
3 1
4 2
2 3

出力例 1

15

1 種類目のチーズを 1 [g] 、 2 種類目のチーズを 2 [g] 、 3 種類目のチーズを 2 [g] 乗せるのが最適です。
このとき、ピザのおいしさは 15 となります。


入力例 2

4 100
6 2
1 5
3 9
8 7

出力例 2

100

チーズの重量の総和が W [g] に満たないケースもあります。


入力例 3

10 3141
314944731 649
140276783 228
578012421 809
878510647 519
925326537 943
337666726 611
879137070 306
87808915 39
756059990 244
228622672 291

出力例 3

2357689932073

Score : 300 points

Problem Statement

Takahashi, who works for a pizza restaurant, is making a delicious cheese pizza for staff meals.
There are N kinds of cheese in front of him.
The deliciousness of the i-th kind of cheese is A_i per gram, and B_i grams of this cheese are available.
The deliciousness of the pizza will be the total deliciousness of cheese he puts on top of the pizza.
However, using too much cheese would make his boss angry, so the pizza can have at most W grams of cheese on top of it.
Under this condition, find the maximum possible deliciousness of the pizza.

Constraints

  • All values in input are integers.
  • 1 \le N \le 3 \times 10^5
  • 1 \le W \le 3 \times 10^8
  • 1 \le A_i \le 10^9
  • 1 \le B_i \le 1000

Input

Input is given from Standard Input in the following format:

N W
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer as an integer.


Sample Input 1

3 5
3 1
4 2
2 3

Sample Output 1

15

The optimal choice is to use 1 gram of cheese of the first kind, 2 grams of the second kind, and 2 grams of the third kind.
The pizza will have a deliciousness of 15.


Sample Input 2

4 100
6 2
1 5
3 9
8 7

Sample Output 2

100

There may be less than W grams of cheese in total.


Sample Input 3

10 3141
314944731 649
140276783 228
578012421 809
878510647 519
925326537 943
337666726 611
879137070 306
87808915 39
756059990 244
228622672 291

Sample Output 3

2357689932073
G - Poisonous Full-Course

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

高橋くんはレストランで、 N 品からなる奇妙なフルコースを楽しむことにしました。
このコースのうち i 番目の料理は以下の通りです。

  • X_i=0 の場合、美味しさが Y_i解毒剤入り の料理
  • X_i=1 の場合、美味しさが Y_i毒入り の料理

高橋くんが料理を食べると、高橋くんの状態は以下のように変化します。

  • 最初、高橋くんはお腹を壊していない。
  • 高橋くんが お腹を壊していない 時、
    • 解毒剤入り の料理を食べても、高橋くんは お腹を壊していないまま である。
    • 毒入り の料理を食べると、高橋くんは お腹を壊す
  • 高橋くんが お腹を壊している 時、
    • 解毒剤入り の料理を食べると、高橋くんは お腹を壊していない状態になる
    • 毒入り の料理を食べると、高橋くんは 死ぬ

コースは以下の流れで進行します。

  • i = 1, \ldots, N についてこの順に、以下の処理を繰り返す。
    • まず、 i 番目の料理が高橋くんに提供される。
    • 次に、 高橋くんはこの料理に対し「食べる」か「下げてもらう」かを選択する。
      • 「食べる」を選択した場合、高橋くんは i 番目の料理を食べる。食べた料理に応じて高橋くんの状態も変化する。
      • 「下げてもらう」を選択した場合、高橋くんは i 番目の料理を食べない。この料理を後で提供してもらったり何らかの手段で保存したりすることはできない。
    • 最後に、 (状態が変化するなら変化後の時点で) 高橋くんが死んでいない場合、
      • i \neq N なら次の料理に進む。
      • i = N なら高橋くんは生きて退店する。

高橋くんはこのあと重要な仕事があるため、高橋くんは生きて退店しなければなりません。
この条件の下で高橋くんが各料理に対し「食べる」「下げてもらう」を選択したとき、高橋くんが 食べた料理の美味しさの総和として考えられる最大値 ( 但し、何も食べなかった場合は 0 ) を出力してください。

制約

  • 入力は全て整数
  • 1 \le N \le 3 \times 10^5
  • X_i \in \{0,1\}
    • つまり、 X_i0,1 のどちらかである。
  • -10^9 \le Y_i \le 10^9

入力

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

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


入力例 1

5
1 100
1 300
0 -200
1 500
1 300

出力例 1

600

以下のように選択することで食べた料理の美味しさの総和を 600 にでき、これが考えられる最大値です。

  • 1 番目の料理を下げてもらう。高橋くんはお腹を壊していません。
  • 2 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は 300 となります。
  • 3 番目の料理を食べる。高橋くんはお腹を壊していない状態に戻り、食べた料理の美味しさの総和は 100 となります。
  • 4 番目の料理を食べる。高橋くんはお腹を壊し、食べた料理の美味しさの総和は 600 となります。
  • 5 番目の料理を下げてもらう。高橋くんはお腹を壊しています。
  • 最終的に高橋くんは死んでいないので、高橋くんは生きて退店する。

入力例 2

4
0 -1
1 -2
0 -3
1 -4

出力例 2

0

この入力の場合何も食べないことが最善ですが、この場合答えは 0 となります。


入力例 3

15
1 900000000
0 600000000
1 -300000000
0 -700000000
1 200000000
1 300000000
0 -600000000
1 -900000000
1 600000000
1 -100000000
1 -400000000
0 900000000
0 200000000
1 -500000000
1 900000000

出力例 3

4100000000

答えが 32 bit 符号付き整数に収まらない可能性があります。

Score : 400 points

Problem Statement

Takahashi has decided to enjoy a wired full-course meal consisting of N courses in a restaurant.
The i-th course is:

  • if X_i=0, an antidotal course with a tastiness of Y_i;
  • if X_i=1, a poisonous course with a tastiness of Y_i.

When Takahashi eats a course, his state changes as follows:

  • Initially, Takahashi has a healthy stomach.
  • When he has a healthy stomach,
    • if he eats an antidotal course, his stomach remains healthy;
    • if he eats a poisonous course, he gets an upset stomach.
  • When he has an upset stomach,
    • if he eats an antidotal course, his stomach becomes healthy;
    • if he eats a poisonous course, he dies.

The meal progresses as follows.

  • Repeat the following process for i = 1, \ldots, N in this order.
    • First, the i-th course is served to Takahashi.
    • Next, he chooses whether to "eat" or "skip" the course.
      • If he chooses to "eat" it, he eats the i-th course. His state also changes depending on the course he eats.
      • If he chooses to "skip" it, he does not eat the i-th course. This course cannot be served later or kept somehow.
    • Finally, (if his state changes, after the change) if he is not dead,
      • if i \neq N, he proceeds to the next course.
      • if i = N, he makes it out of the restaurant alive.

An important meeting awaits him, so he must make it out of there alive.
Find the maximum possible sum of tastiness of the courses that he eats (or 0 if he eats nothing) when he decides whether to "eat" or "skip" the courses under that condition.

Constraints

  • All input values are integers.
  • 1 \le N \le 3 \times 10^5
  • X_i \in \{0,1\}
    • In other words, X_i is either 0 or 1.
  • -10^9 \le Y_i \le 10^9

Input

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

N
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the answer as an integer.


Sample Input 1

5
1 100
1 300
0 -200
1 500
1 300

Sample Output 1

600

The following choices result in a total tastiness of the courses that he eats amounting to 600, which is the maximum possible.

  • He skips the 1-st course. He now has a healthy stomach.
  • He eats the 2-nd course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 300.
  • He eats the 3-rd course. He now has a healthy stomach again, and the total tastiness of the courses that he eats amounts to 100.
  • He eats the 4-th course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to 600.
  • He skips the 5-th course. He now has an upset stomach.
  • In the end, he is not dead, so he makes it out of the restaurant alive.

Sample Input 2

4
0 -1
1 -2
0 -3
1 -4

Sample Output 2

0

For this input, it is optimal to eat nothing, in which case the answer is 0.


Sample Input 3

15
1 900000000
0 600000000
1 -300000000
0 -700000000
1 200000000
1 300000000
0 -600000000
1 -900000000
1 600000000
1 -100000000
1 -400000000
0 900000000
0 200000000
1 -500000000
1 900000000

Sample Output 3

4100000000

The answer may not fit into a 32-bit integer type.

H - Prefix Equality

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N の整数列 A = (a_1,\ldots,a_N)B = (b_1,\ldots,b_N) が与えられます。

i=1,...,Q に対し、次の形式のクエリに答えてください。

  • A の先頭 x_i(a_1,\ldots,a_{x_i}) に含まれる値の集合と B の先頭 y_i(b_1,\ldots,b_{y_i}) に含まれる値の集合が等しいならば Yes と、そうでなければ No と出力せよ。

制約

  • 1 \leq N,Q \leq 2 \times 10^5
  • 1 \leq a_i,b_i \leq 10^9
  • 1 \leq x_i,y_i \leq N
  • 入力は全て整数

入力

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

N
a_1 \ldots a_N
b_1 \ldots b_N
Q
x_1 y_1
\vdots
x_Q y_Q

出力

Q 行出力せよ。 i 行目には、i 番目のクエリに対する出力をせよ。


入力例 1

5
1 2 3 4 5
1 2 2 4 3
7
1 1
2 2
2 3
3 3
4 4
4 5
5 5

出力例 1

Yes
Yes
Yes
No
No
Yes
No

集合は各値が含まれるかどうかのみに注目した概念であることに気を付けてください。
3 番目のクエリにおいて、A の先頭 2 項には 121 個ずつ、B の先頭 3 項には 11 個と 22 個含まれます。しかし、それぞれに含まれる値の集合はどちらも \{ 1,2 \} となり、一致します。
また、6 番目のクエリにおいては各値が現れる順番が異なりますが、やはり集合としては一致します。

Score : 500 points

Problem Statement

You are given integer sequences A = (a_1,\ldots,a_N) and B = (b_1,\ldots,b_N), each of length N.

For i=1,...,Q, answer the query in the following format.

  • If the set of values contained in the first x_i terms of A, (a_1,\ldots,a_{x_i}), and the set of values contained in the first y_i terms of B, (b_1,\ldots,b_{y_i}), are equal, then print Yes; otherwise, print No.

Constraints

  • 1 \leq N,Q \leq 2 \times 10^5
  • 1 \leq a_i,b_i \leq 10^9
  • 1 \leq x_i,y_i \leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 \ldots a_N
b_1 \ldots b_N
Q
x_1 y_1
\vdots
x_Q y_Q

Output

Print Q lines. The i-th line should contain the response to the i-th query.


Sample Input 1

5
1 2 3 4 5
1 2 2 4 3
7
1 1
2 2
2 3
3 3
4 4
4 5
5 5

Sample Output 1

Yes
Yes
Yes
No
No
Yes
No

Note that sets are a concept where it matters only whether each value is contained or not.
For the 3-rd query, the first 2 terms of A contain one 1 and one 2, while the first 3 terms of B contain one 1 and two 2's. However, the sets of values contained in the segments are both \{ 1,2 \}, which are equal.
Also, for the 6-th query, the values appear in different orders, but they are still equal as sets.

I - Predilection

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N の数列 A が与えられます。 数列の長さが 2 以上のとき、隣接する二つの値を選び、それらを削除し、それらが元にあった位置にそれらの和を挿入するという操作を好きなだけ行えます。 0 回以上の操作の後の数列として考えられるものは何通りあるか求め、998244353 で割ったあまりを出力してください。

制約

  • 1 \leq N \leq 2\times 10^5
  • |A_i| \leq 10^9
  • 入力はすべて整数

入力

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

N
A_1 A_2 \cdots A_N

出力

答えを出力せよ。


入力例 1

3
1 -1 1

出力例 1

4

0 回以上の操作の後の数列として考えられるのは以下の 4 通りです。

  • {1,-1,1}
  • {1,0}
  • {0,1}
  • {1}

入力例 2

10
377914575 -275478149 0 -444175904 719654053 -254224494 -123690081 377914575 -254224494 -21253655

出力例 2

321

Score : 500 points

Problem Statement

Given is a sequence A of length N. You can do this operation any number of times: when the length of the sequence is at least 2, choose two adjacent values, delete them, and insert their sum where they were. How many sequences can result from zero or more operations? Find the count modulo 998244353.

Constraints

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

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \cdots A_N

Output

Print the answer.


Sample Input 1

3
1 -1 1

Sample Output 1

4

The following four sequences can result from zero or more operations.

  • {1,-1,1}
  • {1,0}
  • {0,1}
  • {1}

Sample Input 2

10
377914575 -275478149 0 -444175904 719654053 -254224494 -123690081 377914575 -254224494 -21253655

Sample Output 2

321