A - Middle Letter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

英小文字からなる長さが奇数の文字列 S が与えられます。

S の中央の文字を出力してください。

中央の文字とは ある長さが奇数の文字列 T について、 T の長さを |T| として、T の前から \frac{|T|+1}{2} 番目の文字を中央の文字とします。

制約

  • S は英小文字からなる長さが奇数の文字列
  • S の長さは 1 以上 99 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

atcoder

出力例 1

o

atcoder の中央の文字は o です。


入力例 2

a

出力例 2

a

Score : 100 points

Problem Statement

You are given an odd-length string S consisting of lowercase English letters.

Print the central character of S.

What is the central character? For an odd-length string T, its central character is the \frac{|T|+1}{2}-th character from the beginning, where |T| is the length of T.

Constraints

  • S is an odd-length string consisting of lowercase English letters.
  • The length of S is between 1 and 99 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

atcoder

Sample Output 1

o

The central character of atcoder is o.


Sample Input 2

a

Sample Output 2

a
B - 2^N

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

N が与えられます。2^N を出力してください。

制約

  • 0 \leq N \leq 30
  • N は整数である

入力

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

N

出力

答えを出力せよ。


入力例 1

3

出力例 1

8

2^3=8 です。


入力例 2

30

出力例 2

1073741824

Score : 100 points

Problem Statement

Given N, print 2^N.

Constraints

  • 0 \leq N \leq 30
  • N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

3

Sample Output 1

8

We have 2^3=8.


Sample Input 2

30

Sample Output 2

1073741824
C - Restaurant Queue

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

高橋君はAtCoderレストランの前の待ち行列の管理をしたいです。はじめ、待ち行列に並んでいる人はいません。 また、待ち行列に並ぶ人は必ず注文する料理のメニュー番号が書かれた食券を持って並びます。

Q 個のクエリが与えられるので順に処理してください。クエリは 2 種類あり、以下のいずれかの形式で与えられます。

  • 1 X: 待ち行列の末尾に 1 人並ぶ。このとき並ぶ人はメニュー番号が X の食券を持って並ぶ。
  • 2: 待ち行列の先頭にいる人をレストランに案内する。このとき案内される人が持っている食券のメニュー番号を出力する。

制約

  • 1 \leq Q \leq 100
  • 1 \leq X \leq 100
  • 2 つ目の形式のクエリについて、案内する前に待ち行列に並んでいる人がいる
  • 入力は全て整数

入力

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

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

各クエリは以下の 2 種類のいずれかの形式で与えられる。

1 X
2

出力

問題文の指示に従ってクエリへの答えを改行区切りで出力せよ。


入力例 1

6
1 3
1 1
1 15
2
1 3
2

出力例 1

3
1

はじめ、待ち行列に並んでいる人はいません。

  • 1 つ目のクエリについて、メニュー番号が 3 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3 です。
  • 2 つ目のクエリについて、メニュー番号が 1 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3,1 です。
  • 3 つ目のクエリについて、メニュー番号が 15 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 3,1,15 です。
  • 4 つ目のクエリについて、待ち行列の先頭にいる人をレストランに案内します。案内する人はメニュー番号が 3 の食券を持っているので 3 を出力します。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 1,15 です。
  • 5 つ目のクエリについて、メニュー番号が 3 の食券を持った人が待ち行列の末尾に並びます。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 1,15,3 です。
  • 6 つ目のクエリについて、待ち行列の先頭にいる人をレストランに案内します。案内する人はメニュー番号が 1 の食券を持っているので 1 を出力します。この時、待ち行列に並んでいる人が持っている食券のメニュー番号は先頭の人から順に 15,3 です。

入力例 2

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

出力例 2


2 つ目の形式のクエリがないことがあることに注意してください。

Score : 200 points

Problem Statement

Takahashi wants to manage the waiting line in front of the AtCoder Restaurant. Initially, the waiting line is empty. Each person who joins the line holds a meal ticket with the menu number of the dish they will order.

Process Q queries in order. There are two types of queries, given in the following formats:

  • 1 X: One person joins the end of the waiting line holding a ticket with menu number X.
  • 2: Takahashi guides the person at the front of the waiting line into the restaurant. Print the menu number on that person’s ticket.

Constraints

  • 1 \leq Q \leq 100
  • 1 \leq X \leq 100
  • For each query of the second type, there is at least one person in the line before guiding.
  • All input values are integers.

Input

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

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

Each query has one of the following two formats:

1 X
2

Output

For each query, print the answer as specified in the problem statement, each on its own line.


Sample Input 1

6
1 3
1 1
1 15
2
1 3
2

Sample Output 1

3
1

Initially, the waiting line is empty.

  • For the first query, a person holding a ticket with menu number 3 joins the end of the line. The sequence of menu numbers held by people in line from front to back is 3.
  • For the second query, a person holding a ticket with menu number 1 joins the end of the line. The sequence becomes 3,1.
  • For the third query, a person holding a ticket with menu number 15 joins the end of the line. The sequence becomes 3,1,15.
  • For the fourth query, guide the person at the front into the restaurant. That person holds menu number 3, so print 3. The sequence becomes 1,15.
  • For the fifth query, a person holding a ticket with menu number 3 joins the end of the line. The sequence becomes 1,15,3.
  • For the sixth query, guide the person at the front into the restaurant. That person holds menu number 1, so print 1. The sequence becomes 15,3.

Sample Input 2

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

Sample Output 2


Note that there may be no queries of the second type.

D - Qualification Contest

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

N 人の人があるコンテストに参加し、i 位の人のハンドルネームは S_i でした。
上位 K 人のハンドルネームを辞書順に出力してください。

辞書順とは?

辞書順とは簡単に説明すると「単語が辞書に載っている順番」を意味します。より厳密な説明として、相異なる文字列 S と文字列 T の大小を判定するアルゴリズムを以下に説明します。

以下では「 S の i 文字目の文字」を S_i のように表します。また、 S が T より辞書順で小さい場合は S \lt T 、大きい場合は S \gt T と表します。

  1. S と T のうち長さが短い方の文字列の長さを L とします。i=1,2,\dots,L に対して S_i と T_i が一致するか調べます。
  2. S_i \neq T_i である i が存在する場合、そのような i のうち最小のものを j とします。そして、S_j と T_j を比較して、 S_j がアルファベット順で T_j より小さい場合は S \lt T 、大きい場合は S \gt T と決定して、アルゴリズムを終了します。
  3. S_i \neq T_i である i が存在しない場合、 S と T の長さを比較して、S が T より短い場合は S \lt T 、長い場合は S \gt T と決定して、アルゴリズムを終了します。

制約

  • 1 \leq K \leq N \leq 100
  • K, N は整数
  • S_i は英小文字からなる長さ 10 以下の文字列
  • i \neq j ならば S_i \neq S_j

入力

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

N K
S_1
S_2
\vdots
S_N

出力

答えを改行区切りで出力せよ。


入力例 1

5 3
abc
aaaaa
xyz
a
def

出力例 1

aaaaa
abc
xyz

このコンテストには 5 人が参加し、1 位の人のハンドルネームは abc 、2 位の人のハンドルネームは aaaaa 、3 位の人のハンドルネームは xyz 、4 位の人のハンドルネームは a 、5 位の人のハンドルネームは def でした。

上位 3 人のハンドルネームは abc、aaaaa、xyz であるため、これを辞書順に並べ替えて aaaaa 、abc 、xyz の順に出力します。


入力例 2

4 4
z
zyx
zzz
rbg

出力例 2

rbg
z
zyx
zzz

入力例 3

3 1
abc
arc
agc

出力例 3

abc

Score : 200 points

Problem Statement

There were N participants in a contest. The participant ranked i-th had the nickname S_i.
Print the nicknames of the top K participants in lexicographical order.

What is lexicographical order?

Simply put, the lexicographical order is the order of words in a dictionary. As a formal description, below is an algorithm to order distinct strings S and T.

Let S_i denote the i-th character of a string S. We write S \lt T if S is lexicographically smaller than T, and S \gt T if S is larger.

  1. Let L be the length of the shorter of S and T. For i=1,2,\dots,L, check whether S_i equals T_i.
  2. If there is an i such that S_i \neq T_i, let j be the smallest such i. Compare S_j and T_j. If S_j is alphabetically smaller than T_j, we get S \lt T; if S_j is larger, we get S \gt T.
  3. If there is no i such that S_i \neq T_i, compare the lengths of S and T. If S is shorter than T, we get S \lt T; if S is longer, we get S \gt T.

Constraints

  • 1 \leq K \leq N \leq 100
  • K and N are integers.
  • S_i is a string of length 10 consisting of lowercase English letters.
  • S_i \neq S_j if i \neq j.

Input

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

N K
S_1
S_2
\vdots
S_N

Output

Print the nicknames, separated by newlines.


Sample Input 1

5 3
abc
aaaaa
xyz
a
def

Sample Output 1

aaaaa
abc
xyz

This contest had five participants. The participants ranked first, second, third, fourth, and fifth had the nicknames abc, aaaaa, xyz, a, and def, respectively.

The nicknames of the top three participants were abc, aaaaa, xyz, so print these in lexicographical order: aaaaa, abc, xyz.


Sample Input 2

4 4
z
zyx
zzz
rbg

Sample Output 2

rbg
z
zyx
zzz

Sample Input 3

3 1
abc
arc
agc

Sample Output 3

abc
E - Avoid K Palindrome 2

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

英小文字のみからなる長さ N の文字列 S が与えられます。

S の文字を並び替えて得られる文字列(S 自身を含む)であって、長さ K の回文を部分文字列として 含まない ものの個数を求めてください。

ただし、長さ N の文字列 T が「長さ K の回文を部分文字列として含む」とは、
ある (N-K) 以下の非負整数 i が存在して、1 以上 K 以下の任意の整数 j について T_{i+j}=T_{i+K+1-j} が成り立つことをいいます。
ここで、T_k は文字列 T の k 文字目を表すものとします。

制約

  • 2\leq K \leq N \leq 10
  • N,K は整数
  • S は英小文字のみからなる長さ N の文字列

入力

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

N K
S

出力

S の文字を並び替えて得られる文字列であって、長さ K の回文を部分文字列として含まないものの個数を出力せよ。


入力例 1

3 2
aab

出力例 1

1

aab を並び替えて得られる文字列は aab, aba, baa の 3 つであり、このうち aab および baa は長さ 2 の回文 aa を部分文字列として含んでいます。
よって、条件をみたす文字列は aba のみであり、1 を出力します。


入力例 2

5 3
zzyyx

出力例 2

16

zzyyx を並べて得られる文字列は 30 個ありますが、そのうち長さ 3 の回文を含まないようなものは 16 個です。よって、16 を出力します。


入力例 3

10 5
abcwxyzyxw

出力例 3

440640

Score : 300 points

Problem Statement

You are given a string S of length N consisting only of lowercase English letters.

Find the number of strings obtained by permuting the characters of S (including the string S itself) that do not contain a palindrome of length K as a substring.

Here, a string T of length N is said to "contain a palindrome of length K as a substring" if and only if there exists a non-negative integer i not greater than (N-K) such that T_{i+j} = T_{i+K+1-j} for every integer j with 1 \leq j \leq K.
Here, T_k denotes the k-th character of the string T.

Constraints

  • 2 \leq K \leq N \leq 10
  • N and K are integers.
  • S is a string of length N consisting only of lowercase English letters.

Input

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

N K
S

Output

Print the number of strings obtained by permuting S that do not contain a palindrome of length K as a substring.


Sample Input 1

3 2
aab

Sample Output 1

1

The strings obtained by permuting aab are aab, aba, and baa. Among these, aab and baa contain the palindrome aa of length 2 as a substring.
Thus, the only string that satisfies the condition is aba, so print 1.


Sample Input 2

5 3
zzyyx

Sample Output 2

16

There are 30 strings obtained by permuting zzyyx, 16 of which do not contain a palindrome of length 3. Thus, print 16.


Sample Input 3

10 5
abcwxyzyxw

Sample Output 3

440640
F - Alternated

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

長さ 2N の文字列 S が与えられます。 S は A, B を N 個ずつ含みます。

S に対して隣り合う文字を入れ替える操作を好きな回数( 0 回でもよい)行って、同じ文字が隣り合う箇所がない状態にするために必要な操作回数の最小値を求めてください。

制約

  • 1\leq N \leq 5\times 10^5
  • N は整数
  • S は長さ 2N の文字列であり、N 個の A と N 個の B からなる

入力

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

N
S

出力

答えを出力せよ。


入力例 1

3
AABBBA

出力例 1

2

次のように操作することで 2 回の操作で同じ文字が隣り合う箇所がない状態にすることができます。

  • 2 文字目と 3 文字目を入れ替える。S は ABABBA になる。
  • 5 文字目と 6 文字目を入れ替える。S は ABABAB になる。

入力例 2

3
AAABBB

出力例 2

3

入れ替えることができるのは隣り合う文字に限ることに注意してください。


入力例 3

17
AAABABABBBABABBABABABABBAAABABABBA

出力例 3

15

Score : 350 points

Problem Statement

You are given a string S of length 2N. S contains exactly N occurrences of A and N occurrences of B.

Find the minimum number of operations (possibly zero) needed to make S have no adjacent identical characters, where an operation consists of swapping two adjacent characters in S.

Constraints

  • 1\leq N \leq 5\times 10^5
  • N is an integer.
  • S is a string of length 2N consisting of N occurrences of A and N occurrences of B.

Input

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

N
S

Output

Print the answer.


Sample Input 1

3
AABBBA

Sample Output 1

2

By performing operations as follows, you can achieve a state with no adjacent identical characters in two operations:

  • Swap the 2nd and 3rd characters. S becomes ABABBA.
  • Swap the 5th and 6th characters. S becomes ABABAB.

Sample Input 2

3
AAABBB

Sample Output 2

3

Note that you can only swap adjacent characters.


Sample Input 3

17
AAABABABBBABABBABABABABBAAABABABBA

Sample Output 3

15
G - Home Garden

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君は 10^{100} 個の植木鉢を持っています。最初、高橋君は植物を 1 個も育てていません。

Q 個のクエリが与えられるので、順に処理してください。

クエリは次の 3 種類です。

  • 1 : 植物が植えられていない植木鉢を 1 個用意し、その植木鉢に植物を植える。このとき植物の高さは 0 である。
  • 2 T : T 日待つ。このとき植えてあるすべての植物の高さが T 増加する。
  • 3 H : 高さが H 以上の植物をすべて収穫し、収穫した植物の数を出力する。収穫した植物は植木鉢から取り除かれる。

ただし、高橋君が 1 種類目と 3 種類目のクエリを行うとき、かかる時間は 0 であるとします。

制約

  • 1 \leq Q \leq 2 \times 10^{5}
  • 1 \leq T,H \leq 10^{9}
  • 3 種類目のクエリが 1 つ以上存在する
  • 入力は全て整数

入力

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは以下のいずれかの形式で与えられる。

1
2 T
3 H

出力

3 種類目のクエリが K 個あるとき、K 行出力せよ。 i 行目 (1\leq i\leq K) には、i 個目の 3 種類目のクエリに対する答えを出力せよ。


入力例 1

6
1
2 15
1
3 10
2 20
3 20

出力例 1

1
1

クエリは次の順で処理されます。

  • 1 個目のクエリでは高さ 0 の植物が 1 個植えられます。
  • 2 個目のクエリでは高さ 0 の植物が高さ 15 になります。
  • 3 個目のクエリでは高さ 0 の植物が 1 個植えられます。このとき、高さ 0 と高さ 15 の植物が 1 個ずつあります。
  • 4 個目のクエリでは高さ 10 以上の植物が収穫されます。このとき、高さ 15 の植物が 1 個収穫されて高さ 0 の植物が 1 個残ります。1 個の植物を収穫したため、1 行目に 1 と出力します。
  • 5 個目のクエリでは高さ 0 の植物が高さ 20 になります。
  • 6 個目のクエリでは高さ 20 以上の植物が収穫されます。このとき、高さ 20 の植物が 1 個収穫されます。よって、2 行目に 1 と出力します。

入力例 2

15
1
1
2 226069413
3 1
1
1
2 214168203
1
3 214168203
1
1
1
2 314506461
2 245642315
3 1

出力例 2

2
2
4

Score : 400 points

Problem Statement

Takahashi has 10^{100} flower pots. Initially, he is not growing any plants.

You are given Q queries to process in order.

There are three types of queries as follows.

  • 1: Prepare one empty flower pot and put a plant in it. Here, the plant's height is 0.
  • 2 T: Wait for T days. During this time, the height of every existing plants increases by T.
  • 3 H: Harvest all plants with a height of at least H, and output the number of plants harvested. The harvested plants are removed from their flower pots.

Assume that performing queries of the first and third types takes zero time.

Constraints

  • 1 \leq Q \leq 2 \times 10^{5}
  • 1 \leq T,H \leq 10^{9}
  • There is at least one query of the third type.
  • All input values are integers.

Input

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in one of the following formats:

1
2 T
3 H

Output

Let there be K queries of the third type, and print K lines. The i-th line (1 \leq i \leq K) should contain the answer to the i-th query of type 3.


Sample Input 1

6
1
2 15
1
3 10
2 20
3 20

Sample Output 1

1
1

Queries are processed in the following order:

  • In the first query, a plant of height 0 is planted.
  • In the second query, the height of the plant increases to 15.
  • In the third query, another plant of height 0 is planted. Now there is one plant of height 15 and one plant of height 0.
  • In the fourth query, all plants with height at least 10 are harvested. Here, one plant of height 15 gets harvested, and one plant of height 0 remains. Since one plant was harvested, print 1 on the first line.
  • In the fifth query, the height of the remaining plant increases to 20.
  • In the sixth query, all plants with height at least 20 are harvested. Here, one plant of height 20 gets harvested. Thus, print 1 on the second line.

Sample Input 2

15
1
1
2 226069413
3 1
1
1
2 214168203
1
3 214168203
1
1
1
2 314506461
2 245642315
3 1

Sample Output 2

2
2
4
H - Multiple-Free Sequences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

0 \leq x,y \leq M-1 を満たす整数組 (x, y) のうち、以下の漸化式で表される無限長の数列 (s_1, s_2, \dots) が M の倍数を全く含まないようなものは何通りありますか?

  • s_1 = x
  • s_2 = y
  • s_n = A s_{n-1} + B s_{n-2} (n \geq 3)

制約

  • 2 \leq M \leq 1000
  • 0 \leq A, B \leq M-1
  • 入力される値はすべて整数

入力

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

M A B

出力

答えを 1 行に出力せよ。


入力例 1

4 1 2

出力例 1

7

問題文中の条件を満たす整数組は (x,y) = (1,1), (1,3), (2,1), (2,2), (2,3), (3,1), (3,3) の 7 通りです。

たとえば (x,y) = (2,1) としたとき、対応する数列は (2,1,5,7,17,31,65,127,\dots) となります。この数列は 4 の倍数を全く含みません。よって、(x,y) = (2,1) は問題文中の条件を満たします。

一方で (x,y) = (3,2) としたとき、対応する数列は (3,2,8,12,28,52,108,212,\dots) となります。この数列の第 3 項は 8 であり、これは 4 の倍数です。よって、(x,y) = (3,2) は問題文中の条件を満たしません。


入力例 2

446 1 1

出力例 2

0

問題文中の条件を満たす整数組は存在しません。


入力例 3

1000 784 385

出力例 3

995373

Score : 450 points

Problem Statement

Among integer pairs (x, y) satisfying 0 \leq x,y \leq M-1, how many are there such that the infinite sequence (s_1, s_2, \dots) defined by the following recurrence relation contains no multiples of M?

  • s_1 = x
  • s_2 = y
  • s_n = A s_{n-1} + B s_{n-2} (n \geq 3)

Constraints

  • 2 \leq M \leq 1000
  • 0 \leq A, B \leq M-1
  • All input values are integers.

Input

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

M A B

Output

Output the answer on one line.


Sample Input 1

4 1 2

Sample Output 1

7

The integer pairs satisfying the condition in the problem statement are (x,y) = (1,1), (1,3), (2,1), (2,2), (2,3), (3,1), (3,3), for a total of seven pairs.

For example, when (x,y) = (2,1), the corresponding sequence is (2,1,5,7,17,31,65,127,\dots). This sequence contains no multiples of 4. Thus, (x,y) = (2,1) satisfies the condition in the problem statement.

On the other hand, when (x,y) = (3,2), the corresponding sequence is (3,2,8,12,28,52,108,212,\dots). The third term of this sequence is 8, which is a multiple of 4. Thus, (x,y) = (3,2) does not satisfy the condition in the problem statement.


Sample Input 2

446 1 1

Sample Output 2

0

No integer pairs satisfy the condition in the problem statement.


Sample Input 3

1000 784 385

Sample Output 3

995373
I - Rated Range

Time Limit: 2.5 sec / Memory Limit: 1024 MiB

配点 : 525 点

問題文

高橋君はAtCoderのコンテストに N 回参加しようとしています。

i 回目 (1 \leq i \leq N) のコンテストでは、レーティングが L_i 以上 R_i 以下である場合、レーティングが 1 増加します。

以下の形式で与えられる Q 個のクエリに答えてください。

  • 整数 X が与えられる。高橋君の最初のレーティングが X であった場合、N 回のコンテストを終えた後のレーティングを求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
  • 1 \leq Q \leq 3 \times 10^5
  • 各クエリについて 1 \leq X \leq 5 \times 10^5
  • 入力は全て整数

入力

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ただし、\text{query}_i は i 個目のクエリを表し、以下の形式である。

X

出力

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


入力例 1

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

出力例 1

6
6
8

1 個目のクエリでは以下のようにコンテストごとにレーティングが変化します。

  • 1 回目のコンテストではレーティングが 1 以上 5 以下のため、レーティングが 1 増加し 4 になります。
  • 2 回目のコンテストではレーティングが 1 以上 3 以下でないため、レーティングが増加せず 4 のままです。
  • 3 回目のコンテストではレーティングが 3 以上 6 以下のため、レーティングが 1 増加し 5 になります。
  • 4 回目のコンテストではレーティングが 2 以上 4 以下でないため、レーティングが増加せず 5 のままです。
  • 5 回目のコンテストではレーティングが 4 以上 7 以下のため、レーティングが 1 増加し 6 になります。

2 個目のクエリでは 1,2,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 6 になります。

3 個目のクエリでは 1,3,5 回目のコンテストでレーティングが 1 増加するため、レーティングは 8 になります。


入力例 2

10
1 1999
1 1999
1200 2399
1 1999
1 1999
1 1999
2000 500000
1 1999
1 1999
1600 2799
7
1
1995
2000
2399
500000
2799
1000

出力例 2

8
2002
2003
2402
500001
2800
1007

入力例 3

15
260522 414575
436426 479445
148772 190081
190629 433447
47202 203497
394325 407775
304784 463982
302156 468417
131932 235902
78537 395728
223857 330739
286918 329211
39679 238506
63340 186568
160016 361868
10
287940
296263
224593
101449
336991
390310
323355
177068
11431
8580

出力例 3

287946
296269
224599
101453
336997
390315
323363
177075
11431
8580

Score : 525 points

Problem Statement

Takahashi plans to participate in N AtCoder contests.

In the i-th contest (1 \leq i \leq N), if his rating is between L_i and R_i (inclusive), his rating increases by 1.

You are given Q queries in the following format:

  • An integer X is given. Assuming that Takahashi's initial rating is X, determine his rating after participating in all N contests.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq R_i \leq 5 \times 10^5 (1 \leq i \leq N)
  • 1 \leq Q \leq 3 \times 10^5
  • For each query, 1 \leq X \leq 5 \times 10^5.
  • All input values are integers.

Input

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

N
L_1 R_1
L_2 R_2
\vdots
L_N R_N
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i is the i-th query in the form:

X

Output

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


Sample Input 1

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

Sample Output 1

6
6
8

For the 1st query, the rating changes as follows:

  • In the 1st contest, the rating is between 1 and 5, so it increases by 1, becoming 4.
  • In the 2nd contest, the rating is not between 1 and 3, so it remains 4.
  • In the 3rd contest, the rating is between 3 and 6, so it increases by 1, becoming 5.
  • In the 4th contest, the rating is not between 2 and 4, so it remains 5.
  • In the 5th contest, the rating is between 4 and 7, so it increases by 1, becoming 6.

For the 2nd query, the rating increases in the 1st, 2nd, 3rd, and 5th contests, ending at 6.

For the 3rd query, the rating increases in the 1st, 3rd, and 5th contests, ending at 8.


Sample Input 2

10
1 1999
1 1999
1200 2399
1 1999
1 1999
1 1999
2000 500000
1 1999
1 1999
1600 2799
7
1
1995
2000
2399
500000
2799
1000

Sample Output 2

8
2002
2003
2402
500001
2800
1007

Sample Input 3

15
260522 414575
436426 479445
148772 190081
190629 433447
47202 203497
394325 407775
304784 463982
302156 468417
131932 235902
78537 395728
223857 330739
286918 329211
39679 238506
63340 186568
160016 361868
10
287940
296263
224593
101449
336991
390310
323355
177068
11431
8580

Sample Output 3

287946
296269
224599
101453
336997
390315
323363
177075
11431
8580