A - Odd Position Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

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

A の奇数番目の要素の総和を求めてください。すなわち、N 以下の最大の奇数を m としたとき A_1+A_3+A_5+\ldots+A_m を求めてください。

制約

  • 1\leq N\leq 100
  • 1\leq A_i\leq 100
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

7
3 1 4 1 5 9 2

出力例 1

14

A の奇数番目の要素の総和は A_1+A_3+A_5+A_7=3+4+5+2=14 です。


入力例 2

1
100

出力例 2

100

入力例 3

14
100 10 1 10 100 10 1 10 100 10 1 10 100 10

出力例 3

403

Score : 100 points

Problem Statement

You are given a sequence of positive integers of length N: A=(A_1,A_2,\dots,A_N).

Find the sum of the odd-indexed elements of A. That is, find A_1 + A_3 + A_5 + \dots + A_m, where m is the largest odd number not exceeding N.

Constraints

  • 1 \le N \le 100
  • 1 \le A_i \le 100
  • All input values are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

7
3 1 4 1 5 9 2

Sample Output 1

14

The sum of the odd-indexed elements of A is A_1+A_3+A_5+A_7=3+4+5+2=14.


Sample Input 2

1
100

Sample Output 2

100

Sample Input 3

14
100 10 1 10 100 10 1 10 100 10 1 10 100 10

Sample Output 3

403
B - Four Hidden

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250

問題文

英小文字と ? からなる文字列 T と、英小文字のみからなる文字列 U が与えられます。

T は、英小文字のみからなる文字列 S のちょうど 4 文字を ? で置き換えることで得られた文字列です。

SU を連続部分文字列として含んでいた可能性があるかどうか判定してください。

制約

  • T は長さ 4 以上 10 以下の英小文字と ? からなる文字列
  • T にはちょうど 4 つの ? が含まれる
  • U は長さ 1 以上 |T| 以下の英小文字からなる文字列

入力

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

T
U

出力

SU を部分文字列として含んでいた可能性があるならば Yes を、そうでないならば No を出力せよ。


入力例 1

tak??a?h?
nashi

出力例 1

Yes

例えば、Stakanashi である場合、これは nashi を連続部分文字列として含みます。


入力例 2

??e??e
snuke

出力例 2

No

? がどのような文字であっても、Ssnuke を連続部分文字列として含むことがありません。


入力例 3

????
aoki

出力例 3

Yes

Score : 250 points

Problem Statement

You are given a string T consisting of lowercase English letters and ?, and a string U consisting of lowercase English letters.

The string T is obtained by taking some lowercase-only string S and replacing exactly four of its characters with ?.

Determine whether it is possible that the original string S contained U as a contiguous substring.

Constraints

  • T is a string of length between 4 and 10, inclusive, consisting of lowercase letters and ?.
  • T contains exactly four occurrences of ?.
  • U is a string of length between 1 and |T|, inclusive, consisting of lowercase letters.

Input

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

T
U

Output

Print Yes if it is possible that the original string S contained U as a contiguous substring; otherwise, print No.


Sample Input 1

tak??a?h?
nashi

Sample Output 1

Yes

For example, if S is takanashi, it contains nashi as a contiguous substring.


Sample Input 2

??e??e
snuke

Sample Output 2

No

No matter what characters replace the ?s in T, S cannot contain snuke as a contiguous substring.


Sample Input 3

????
aoki

Sample Output 3

Yes
C - 403 Forbidden

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

WAtCoder には N 人のユーザーがおり、1 から N までの番号がつけられています。 また、M 個のコンテストページがあり、1 から M までの番号がつけられています。 はじめ、すべてのユーザはどのコンテストページの閲覧権限も持っていません。

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

  • 1 X Y: ユーザ X にコンテストページ Y の閲覧権限を付与する。
  • 2 X: ユーザ X にすべてのコンテストページの閲覧権限を付与する。
  • 3 X Y: ユーザ X がコンテストページ Y を閲覧できるかを答える。

クエリの中で、あるユーザがすでに閲覧権限を持っているコンテストページについて、重ねて閲覧権限を付与されることもあります。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq X \leq N
  • 1 \leq Y \leq M
  • 入力はすべて整数

入力

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

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

各クエリ \mathrm{query}_i は以下の 3 種類のいずれかの形式で与えられる。

1 X Y
2 X
3 X Y

出力

3 種類目のクエリのそれぞれについて、ユーザ X がコンテストページ Y を閲覧できるならば Yes を、そうでなければ No を改行区切りで出力せよ。


入力例 1

2 3 5
1 1 2
3 1 1
3 1 2
2 2
3 2 3

出力例 1

No
Yes
Yes
  • 1 つ目のクエリで、ユーザ 1 にコンテストページ 2 の閲覧権限を付与します。
  • 2 つ目のクエリの時点で、ユーザ 1 が閲覧できるコンテストページは 2 のみです。コンテストページ 1 の閲覧権限を持っていないので、No を出力します。
  • 3 つ目のクエリの時点で、ユーザ 1 はコンテストページ 2 の閲覧権限を持っているので、Yes を出力します。
  • 4 つ目のクエリで、ユーザ 2 にすべてのコンテストページの閲覧権限を付与します。
  • 5 つ目のクエリの時点で、ユーザ 2 が閲覧できるコンテストページは 1,2,3 です。コンテストページ 3 の閲覧権限を持っているので、Yes を出力します。

入力例 2

5 5 10
2 2
3 4 4
1 1 1
1 4 1
1 4 2
1 4 4
1 2 4
3 3 2
3 5 4
3 2 1

出力例 2

No
No
No
Yes

Score : 300 points

Problem Statement

There are N users on WAtCoder, numbered from 1 to N, and M contest pages, numbered from 1 to M. Initially, no user has view permission for any contest page.

You are given Q queries to process in order. Each query is of one of the following three types:

  • 1 X Y: Grant user X view permission for contest page Y.
  • 2 X: Grant user X view permission for all contest pages.
  • 3 X Y: Answer whether user X can view contest page Y.

It is possible for a user to be granted permission for the same contest page multiple times.

Constraints

  • 1 \le N \le 2\times 10^5
  • 1 \le M \le 2\times 10^5
  • 1 \le Q \le 2\times 10^5
  • 1 \le X \le N
  • 1 \le Y \le M
  • All input values are integers.

Input

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

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

Each \mathrm{query}_i is in one of the following formats:

1 X Y
2 X
3 X Y

Output

For each query of the third type, print Yes if user X can view contest page Y, otherwise print No, each on its own line.


Sample Input 1

2 3 5
1 1 2
3 1 1
3 1 2
2 2
3 2 3

Sample Output 1

No
Yes
Yes
  • In the first query, user 1 is granted permission to view contest page 2.
  • At the second query, user 1 can view only page 2; they cannot view page 1, so print No.
  • At the third query, user 1 can view page 2, so print Yes.
  • In the fourth query, user 2 is granted permission to view all pages.
  • At the fifth query, user 2 can view pages 1,2,3; they can view page 3, so print Yes.

Sample Input 2

5 5 10
2 2
3 4 4
1 1 1
1 4 1
1 4 2
1 4 4
1 2 4
3 3 2
3 5 4
3 2 1

Sample Output 2

No
No
No
Yes
D - Forbidden Difference

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) と非負整数 D が与えられます。 A の要素をいくつか削除して、以下の条件を満たす数列 B を得たいです。

  • すべての i,j \; (1 \leq i < j \leq |B|) について、|B_i-B_j| \neq D

最小でいくつの要素を削除すればよいか求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq D \leq 10^6
  • 0 \leq A_i \leq 10^6
  • 入力はすべて整数

入力

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

N D
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

5 2
3 1 4 1 5

出力例 1

1

A_1=3 を削除して B=(1,4,1,5) とすることで、すべての i<j について |B_i-B_j|\neq 2 となります。


入力例 2

4 3
1 6 1 8

出力例 2

0

A がすでに条件を満たしていることもあります。


入力例 3

10 3
1 6 2 10 2 3 2 10 6 4

出力例 3

2

Score : 425 points

Problem Statement

You are given a length-N integer sequence A=(A_1,A_2,\dots,A_N) and a non-negative integer D. We wish to delete as few elements as possible from A to obtain a sequence B that satisfies the following condition:

  • |B_i - B_j|\neq D for all i,j \; (1 \leq i < j \leq |B|).

Find the minimum number of deletions required.

Constraints

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

Input

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

N D
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

5 2
3 1 4 1 5

Sample Output 1

1

Deleting A_1=3 yields B=(1,4,1,5), which satisfies |B_i - B_j|\neq 2 for all i<j.


Sample Input 2

4 3
1 6 1 8

Sample Output 2

0

The sequence A may already satisfy the condition.


Sample Input 3

10 3
1 6 2 10 2 3 2 10 6 4

Sample Output 3

2
E - Forbidden Prefix

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

文字列の多重集合 X,Y があります。はじめ、両方ともに空です。

Q 個のクエリが与えられるので、順に処理してください。i 番目のクエリでは、整数 T_i と文字列 S_i が与えられるので、T_i=1 ならば XS_i を追加し、T_i=2 ならば YS_i を追加してください。

各クエリの処理後、以下の値を出力してください。

  • Y に含まれる文字列のうち、X のどの要素も接頭辞として持たないものの個数

制約

  • Q1 以上 2 \times 10^5 以下の整数
  • T_i \in \{1,2\}
  • S_i は長さ 1 以上 5 \times 10^5 以下の英小文字のみからなる文字列
  • \displaystyle \sum_{i=1}^Q |S_i| \leq 5 \times 10^5

入力

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

Q
T_1 S_1
T_2 S_2
\vdots
T_Q S_Q

出力

Q 行出力せよ。i 行目 (1 \leq i \leq Q) には、i 番目のクエリの処理後の答えを出力せよ。


入力例 1

4
1 at
2 watcoder
2 atcoder
1 wa

出力例 1

0
1
1
0

i=1,2,3,4 番目のクエリの処理後の答えはそれぞれ以下のようになります。

  • i=1: Y は空なので、求める個数は 0 です。
  • i=2: watcoderX のどの要素も接頭辞として持たないので、求める個数は 1 です。
  • i=3: watcoderX のどの要素も接頭辞として持たず、atcoderat を接頭辞として持つので、求める個数は 1 個です。
  • i=4: watcoderwa を、atcoderat を接頭辞として持つので、求める個数は 0 個です。

入力例 2

10
1 w
1 avko
2 atcoder
1 bzginn
2 beginner
1 atco
2 contest
1 ntxcdg
1 atc
1 contest

出力例 2

0
0
1
1
2
1
2
2
2
1

Score : 500 points

Problem Statement

There are two multisets of strings, X and Y, both initially empty.

You are given Q queries to process in order. In the i-th query, you receive an integer T_i and a string S_i. If T_i=1, insert S_i into X; if T_i=2, insert S_i into Y.

After processing each query, print this value:

  • the number of strings in Y that have no element of X as a prefix.

Constraints

  • Q is an integer between 1 and 2 \times 10^5, inclusive.
  • T_i \in \{1,2\}
  • Each S_i is a string of length between 1 and 5\times 10^5, inclusive, consisting of lowercase English letters.
  • \displaystyle \sum_{i=1}^Q |S_i| \leq 5 \times 10^5

Input

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

Q
T_1\ S_1
T_2\ S_2
\vdots
T_Q\ S_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the count after processing the i-th query.


Sample Input 1

4
1 at
2 watcoder
2 atcoder
1 wa

Sample Output 1

0
1
1
0

The counts after processing the queries for i=1,2,3,4 are as follows.

  • i=1: Y is empty, so the count is 0.
  • i=2: watcoder has no element of X as a prefix, so the count is 1.
  • i=3: watcoder has no element of X as a prefix, while atcoder has at as a prefix, so the count is 1.
  • i=4: watcoder has wa as a prefix, and atcoder has at as a prefix, so the count is 0.

Sample Input 2

10
1 w
1 avko
2 atcoder
1 bzginn
2 beginner
1 atco
2 contest
1 ntxcdg
1 atc
1 contest

Sample Output 2

0
0
1
1
2
1
2
2
2
1
F - Shortest One Formula

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

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

1, +, *, (, ) のみからなる数式のうち、値が N となるものの中で、文字列としての長さが最小のものを一つ求めてください。

より厳密には、以下の条件をすべて満たす文字列 S のうち長さが最小のものを一つ求めてください。

  • S は下の BNF 記法<expr> シンボルに従う文字列である。
  • S が表す数式の値は N である。
<expr>   ::= <term> | <expr> "+" <term>
<term>   ::= <factor> | <term> "*" <factor>
<factor> ::= <number> | "(" <expr> ")"
<number> ::= "1" | "1" <number> 

<expr> シンボルに従う文字列として、以下のようなものがあります。

  • 1111+111 : 1111+111 を表します。
  • (1+1)*(1+1) : (1+1)\times (1+1) を表します。
  • (11+(1+1)*(1+1))+1 : (11+(1+1)\times (1+1))+1 を表します。

一方、以下の文字列は <expr> シンボルに従いません。

  • (1+1)(1+1)
  • 1+2
  • 1-1
  • 1/1
  • )1(
  • 1++1
  • +1
  • (+1)
  • 1*+1

制約

  • 1\leq N\leq 2000
  • 入力は全て整数

入力

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

N

出力

答えを出力せよ。


入力例 1

9

出力例 1

(1+1+1)*(1+1+1)

値が 9 となるような数式は例えば以下のようなものがあります。

  • (1+1+1)*(1+1+1)
  • 1+1+1+1+1+1+1+1+1
  • (1+1)*(1+1)*(1+1)+1

値が 9 となるような数式のうち、長さが最小となるものは (1+1+1)*(1+1+1) です。


入力例 2

11

出力例 2

11

入力例 3

403

出力例 3

1+(1+1+1)*(1+11+11+111)

Score : 500 points

Problem Statement

You are given a positive integer N.

Among all valid arithmetic expressions consisting of the characters 1, +, *, (, and ), find one of the minimum length whose value is N.

More formally, among the strings S satisfying all of the following conditions, find one of the minimum length:

  • S conforms to the symbol <expr> in the BNF below.
  • The value of the expression represented by S is N.
<expr>   ::= <term> | <expr> "+" <term>
<term>   ::= <factor> | <term> "*" <factor>
<factor> ::= <number> | "(" <expr> ")"
<number> ::= "1" | "1" <number>

Strings that conform to <expr> include:

  • 1111+111 representing 1111+111.
  • (1+1)*(1+1) representing (1+1)\times(1+1).
  • (11+(1+1)*(1+1))+1 representing (11+(1+1)\times(1+1))+1.

Strings that do not conform to <expr> include:

  • (1+1)(1+1)
  • 1+2
  • 1-1
  • 1/1
  • )1(
  • 1++1
  • +1
  • (+1)
  • 1*+1

Constraints

  • 1 \le N \le 2000
  • All input values are integers.

Input

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

N

Output

Print a solution.


Sample Input 1

9

Sample Output 1

(1+1+1)*(1+1+1)

Expressions whose value is 9 include:

  • (1+1+1)*(1+1+1)
  • 1+1+1+1+1+1+1+1+1
  • (1+1)*(1+1)*(1+1)+1

Among them, a shortest is (1+1+1)*(1+1+1).


Sample Input 2

11

Sample Output 2

11

Sample Input 3

403

Sample Output 3

1+(1+1+1)*(1+11+11+111)
G - Odd Position Sum Query

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 600

問題文

空の数列 A があります。

Q 個のクエリが与えられるので順に処理してください。i 番目のクエリは以下で説明されます。

整数 y_i が与えられる。 zi=1 のときは 0、それ以外のときは i-1 番目のクエリに対する答えとし、 x_ix_i=((y_i+z)\bmod 10^9)+1 で定義する。A の末尾に x_i を追加する。
その後、A を昇順に並べた列を B=(B_1,B_2,\ldots,B_i) としたとき B の奇数番目の要素の総和を求めよ。すなわち、i 以下の最大の奇数を m としたとき B_1+B_3+B_5+\ldots+B_m を求めよ。

制約

  • 1\leq Q\leq 3\times 10^5
  • 0\leq y_i\lt 10^9
  • 1\leq x_i\leq 10^9
  • 入力は全て整数

入力

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

Q
y_1
y_2
\vdots
y_Q

出力

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


入力例 1

5
1
3
1
999999994
999999993

出力例 1

2
2
8
6
1000000006
  • 1 つ目のクエリについて、y_1=1,z=0 であるため x_1=((1+0)\bmod 10^9)+1=2 です。A の末尾に x_1 を追加すると A=(2) となります。A を昇順に並べた列 BB=(2) であるため、求める値は B_1=2 です。
  • 2 つ目のクエリについて、y_2=3,z=2 であるため x_2=((3+2)\bmod 10^9)+1=6 です。A の末尾に x_2 を追加すると A=(2,6) となります。A を昇順に並べた列 BB=(2,6) であるため、求める値は B_1=2 です。
  • 3 つ目のクエリについて、y_3=1,z=2 であるため x_3=((1+2)\bmod 10^9)+1=4 です。A の末尾に x_3 を追加すると A=(2,6,4) となります。A を昇順に並べた列 BB=(2,4,6) であるため、求める値は B_1+B_3=8 です。
  • 4 つ目のクエリについて、y_4=999999994,z=8 であるため x_4=((999999994+8)\bmod 10^9)+1=3 です。A の末尾に x_4 を追加すると A=(2,6,4,3) となります。A を昇順に並べた列 BB=(2,3,4,6) であるため、求める値は B_1+B_3=6 です。
  • 5 つ目のクエリについて、y_5=999999993,z=6 であるため x_5=((999999993+6)\bmod 10^9)+1=1000000000 です。A の末尾に x_5 を追加すると A=(2,6,4,3,1000000000) となります。A を昇順に並べた列 BB=(2,3,4,6,1000000000) であるため、求める値は B_1+B_3+B_5=1000000006 です。

入力例 2

8
105282053
695234822
468007124
120710491
568831200
700753895
765188109
262666319

出力例 2

105282054
105282054
905798931
599798602
995656103
891549225
1652393438
1652393438

x_1,x_2,\ldots,x_8 の値は順に以下の通りです。

105282054
800516877
573289179
26509423
168629803
696409999
656737335
915059758

Score : 600 points

Problem Statement

There is an initially empty sequence A.

You are given Q queries to process in order. The i-th query is explained below:

You are given an integer y_i. If i=1, let z be 0; otherwise, let z be the answer to the (i-1)-th query. Define x_i=((y_i+z)\bmod 10^9)+1. Append x_i to the end of A.
Then, let B=(B_1,B_2,\ldots,B_i) be the sequence A sorted in ascending order, and find the sum of the odd-indexed elements of B. That is, find B_1 + B_3 + B_5 + \dots + B_m, where m is the largest odd number not exceeding i.

Constraints

  • 1 \le Q \le 3\times 10^5
  • 0 \le y_i < 10^9
  • 1 \le x_i \le 10^9
  • All input values are integers.

Input

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

Q
y_1
y_2
\vdots
y_Q

Output

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


Sample Input 1

5
1
3
1
999999994
999999993

Sample Output 1

2
2
8
6
1000000006
  • For the 1st query, y_1=1, z=0, so x_1=((1+0)\bmod 10^9)+1=2. Appending this to the end of A gives A=(2). Sorting A in ascending order yields B=(2), and the sought value is B_1=2.
  • For the 2nd query, y_2=3, z=2, so x_2=((3+2)\bmod 10^9)+1=6. Appending gives A=(2,6), so B=(2,6) and the sought value is B_1=2.
  • For the 3rd query, y_3=1, z=2, so x_3=((1+2)\bmod 10^9)+1=4. Appending gives A=(2,6,4), so B=(2,4,6) and the sought value is B_1+B_3=8.
  • For the 4th query, y_4=999999994, z=8, so x_4=((999999994+8)\bmod 10^9)+1=3. Appending gives A=(2,6,4,3), so B=(2,3,4,6) and the sought value is B_1+B_3=6.
  • For the 5th query, y_5=999999993, z=6, so x_5=((999999993+6)\bmod 10^9)+1=1000000000. Appending gives A=(2,6,4,3,1000000000), so B=(2,3,4,6,1000000000) and the sought value is B_1+B_3+B_5=1000000006.

Sample Input 2

8
105282053
695234822
468007124
120710491
568831200
700753895
765188109
262666319

Sample Output 2

105282054
105282054
905798931
599798602
995656103
891549225
1652393438
1652393438

Below are the values of x_1,x_2,\dots,x_8 in order:

105282054
800516877
573289179
26509423
168629803
696409999
656737335
915059758