Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
長さ N の英小文字からなる文字列 S と英小文字 c_1,c_2 が与えられます。
S の文字のうち c_1 であるもの 以外 を全て c_2 に置き換えた文字列を求めてください。
制約
- 1\le N\le 100
- N は整数
- c_1,c_2 は英小文字
- S は英小文字からなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N c_1 c_2 S
出力
答えを出力せよ。
入力例 1
3 b g abc
出力例 1
gbg
S= abc
のうち、 b
でない a
と c
を g
に置き換えた結果 gbg
になります。したがって、 gbg
を出力してください。
入力例 2
1 s h s
出力例 2
s
置き換えた後の文字列が元の文字列と変わらない場合もあります。
入力例 3
7 d a atcoder
出力例 3
aaaadaa
入力例 4
10 b a acaabcabba
出力例 4
aaaabaabba
Score : 100 points
Problem Statement
You are given a string S of length N consisting of lowercase English letters, along with lowercase English letters c_1 and c_2.
Find the string obtained by replacing every character of S that is not c_1 with c_2.
Constraints
- 1\le N\le 100
- N is an integer.
- c_1 and c_2 are lowercase English letters.
- S is a string of length N consisting of lowercase English letters.
Input
The input is given in the following format from Standard Input:
N c_1 c_2 S
Output
Print the answer.
Sample Input 1
3 b g abc
Sample Output 1
gbg
Replacing a
and c
(which are not b
) with g
in S= abc
results in gbg
, so print gbg
.
Sample Input 2
1 s h s
Sample Output 2
s
It is possible that the resulting string after replacement is the same as the original string.
Sample Input 3
7 d a atcoder
Sample Output 3
aaaadaa
Sample Input 4
10 b a acaabcabba
Sample Output 4
aaaabaabba
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
AtCoder では、ARC が 2 つの division に分けられています。
- ARC Div. 1 では、コンテスト開始時のレーティングが 1600 以上 2799 以下の参加者がレーティング更新の対象です。
- ARC Div. 2 では、コンテスト開始時のレーティングが 1200 以上 2399 以下の参加者がレーティング更新の対象です。
高橋くんは、これから N 回の ARC に参加することにしました。
はじめ、高橋くんのレーティングは R です。
i 回目 (1\leq i\leq N) の ARC は Div. D _ i で、高橋くんが取った成績は整数 A _ i で表されます。
i 回目の ARC において高橋くんがレーティング更新の対象ならば、コンテスト開始時の高橋くんのレーティングを T として、更新後の高橋くんのレーティングは T+A _ i になります。 高橋くんがレーティング更新の対象でなければ、高橋くんのレーティングは変化しません。
ARC でのレーティングの更新はコンテストが終了したあと直ちに行われ、次のコンテストのレーティング更新の対象であるかは更新後のレーティングをもとに判定されます。
N 回の ARC を終えたとき、高橋くんのレーティングがいくつになっているか求めてください。
ただし、高橋くんはこの N 回の ARC 以外のコンテストには参加せず、ARC 以外でレーティングが変動することはありません。
制約
- 1\leq N\leq 100
- 0\leq R\leq 4229
- 1\leq D _ i\leq 2\ (1\leq i\leq N)
- -1000\leq A _ i\leq 1000\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N R D _ 1 A _ 1 D _ 2 A _ 2 \vdots D _ N A _ N
出力
N 回の ARC を終えた後の、高橋くんのレーティングを出力せよ。
入力例 1
4 1255 2 900 1 521 2 600 1 52
出力例 1
2728
はじめ、高橋くんのレーティングは 1255 です。
それぞれのコンテストで、高橋くんのレーティングは次のように変化します。
- 1 回目の ARC は Div. 2 です。高橋くんはレーティング更新の対象なので、高橋くんのレーティングは 1255+900=2155 になります。
- 2 回目の ARC は Div. 1 です。高橋くんはレーティング更新の対象なので、高橋くんのレーティングは 2155+521=2676 になります。
- 3 回目の ARC は Div. 2 です。高橋くんはレーティング更新の対象ではないので、高橋くんのレーティングは変化しません。
- 4 回目の ARC は Div. 1 です。高橋くんはレーティング更新の対象なので、高橋くんのレーティングは 2676+52=2728 になります。
4 回の ARC を終えた後、高橋くんのレーティングは 2728 となっているので、2728
を出力してください。
入力例 2
2 3031 1 1000 2 -1000
出力例 2
3031
高橋くんはレッドコーダーなので、ARC でどのような成績を取ってもレーティングは変動しません。
入力例 3
15 2352 2 -889 2 420 2 -275 1 957 1 -411 1 -363 1 151 2 -193 2 289 2 -770 2 109 1 345 2 551 1 -702 1 355
出力例 3
1226
Score : 200 points
Problem Statement
AtCoder Regular Contest (ARC) is divided into two divisions.
- In ARC Div. 1, participants whose rating at the start of the contest is between 1600 and 2799, inclusive, are subject to rating updates.
- In ARC Div. 2, participants whose rating at the start of the contest is between 1200 and 2399, inclusive, are subject to rating updates.
Takahashi decided to participate in N ARCs.
Initially, his rating is R.
The i-th (1\leq i\leq N) ARC is Div. D _ i, and his performance in that contest is represented by an integer A _ i.
If he is subject to a rating update in the i-th ARC, let T be his rating at the start of that contest. Then, after the contest, his rating becomes T+A _ i.
If his is not subject to a rating update, his rating does not change.
Rating updates for ARCs are performed immediately after the contest ends, and whether he is subject to rating updates in the next contest is determined based on his rating after the update.
Find his rating after finishing the N ARCs.
He does not participate in any contests other than these N ARCs, and his rating does not change in other ways.
Constraints
- 1\leq N\leq 100
- 0\leq R\leq 4229
- 1\leq D _ i\leq 2\ (1\leq i\leq N)
- -1000\leq A _ i\leq 1000\ (1\leq i\leq N)
- All input values are integers.
Input
The input is given in the following format from Standard Input:
N R D _ 1 A _ 1 D _ 2 A _ 2 \vdots D _ N A _ N
Output
Print Takahashi's rating after finishing the N ARCs.
Sample Input 1
4 1255 2 900 1 521 2 600 1 52
Sample Output 1
2728
Initially, Takahashi's rating is 1255.
For each contest, Takahashi's rating changes as follows:
- The 1st ARC is Div. 2. He is subject to rating updates, so his rating becomes 1255+900=2155.
- The 2nd ARC is Div. 1. He is subject to rating updates, so his rating becomes 2155+521=2676.
- The 3rd ARC is Div. 2. He is not subject to rating updates, so his rating does not change.
- The 4th ARC is Div. 1. He is subject to rating updates, so his rating becomes 2676+52=2728.
After the four ARCs, his rating is 2728, so print 2728
.
Sample Input 2
2 3031 1 1000 2 -1000
Sample Output 2
3031
He is a Red coder, so his rating does not change upon his performance in ARC.
Sample Input 3
15 2352 2 -889 2 420 2 -275 1 957 1 -411 1 -363 1 151 2 -193 2 289 2 -770 2 109 1 345 2 551 1 -702 1 355
Sample Output 3
1226
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋くんは、プログラミングコンテストを主催することにしました。
コンテストは A 問題、B 問題、C 問題、D 問題、E 問題の 5 問からなり、それぞれの配点は a 点、b 点、c 点、d 点、e 点です。
コンテストには 31 人が参加し、全員が 1 問以上解きました。
より具体的には、文字列 ABCDE
の空でない(連続するとは限らない)部分列すべてについて、その部分列を名前とする参加者が存在し、その参加者は名前に含まれる文字に対応する問題をすべて解き、それ以外の問題は解きませんでした。
例えば、A さんは A 問題のみを、BCE さんは B 問題、C 問題、E 問題を解きました。
参加者の名前を、取った点数が大きいほうから順に出力してください。 ただし、参加者が取った点数は、その参加者が解いた問題の配点の合計です。
ただし、同じ点数を獲得した参加者については、名前が辞書順で小さいほうを先に出力してください。
辞書順で小さいとは?
辞書順とは、一言で説明すると「単語が辞書に載っている順番」を意味します。
より厳密には、英大文字からなる相異なる文字列 S,T について、S が T より辞書順で小さいとは、以下の条件のどちらかが成り立つことを意味します。
- S の長さ |S| が T の長さより短く、T の先頭 |S| 文字が S と一致する
- ある整数 1\leq i\leq\min\lbrace|S|,|T|\rbrace が存在して、次の 2 つの条件を両方を満たす
- 1\leq j\lt i を満たすすべての整数 j に対して S の j 文字目と T の j 文字目が等しい
- S の i 文字目が T の i 文字目よりアルファベット順で小さい
例えば、S= AB
,T= ABC
とすると、ひとつめの条件が成り立つため S は T より小さいです。
また、S= ABD
,T= ACD
とすると、ふたつめの条件が i=2 で成り立つため S は T より小さいです。
制約
- 100\leq a\leq b\leq c\leq d\leq e\leq 2718
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
a b c d e
出力
31 行出力せよ。 i 行目 (1\leq i\leq 31) には、i 番目に高い得点を獲得した参加者の名前を出力せよ。 同じ得点を獲得した参加者については、それらのうち辞書順で小さい名前をもつ参加者を先に出力せよ。
入力例 1
400 500 600 700 800
出力例 1
ABCDE BCDE ACDE ABDE ABCE ABCD CDE BDE ADE BCE ACE BCD ABE ACD ABD ABC DE CE BE CD AE BD AD BC AC AB E D C B A
それぞれの参加者の得点は以下のようになります。
例えば、ADE さんと BCE さんは同じ得点を獲得していますが、ADE さんのほうが辞書順で小さい名前をもつため、ADE さんを先に出力してください。
入力例 2
800 800 900 900 1000
出力例 2
ABCDE ACDE BCDE ABCE ABDE ABCD CDE ACE ADE BCE BDE ABE ACD BCD ABC ABD CE DE AE BE CD AC AD BC BD AB E C D A B
入力例 3
128 256 512 1024 2048
出力例 3
ABCDE BCDE ACDE CDE ABDE BDE ADE DE ABCE BCE ACE CE ABE BE AE E ABCD BCD ACD CD ABD BD AD D ABC BC AC C AB B A
Score : 300 points
Problem Statement
Takahashi decided to hold a programming contest.
The contest consists of five problems: A, B, C, D, E, with scores a, b, c, d, e, respectively.
There are 31 participants, and all of them solved at least one problem.
More specifically, for every non-empty subsequence (not necessarily contiguous) of the string ABCDE
, there is a participant named after that subsequence who solved the problems corresponding to the letters in their name and did not solve the other problems.
For example, participant A solved only problem A, and participant BCE solved problems B, C, and E.
Print the names of the participants in order of their obtained scores, from the largest to the smallest. The score obtained by a participant is the sum of the scores of the problems they solved.
If two participants obtained the same score, print the one whose name is lexicographically smaller first.
What does "lexicographically smaller" mean?
In short, "lexicographically smaller" refers to the order in which words would appear in a dictionary.
More precisely, for distinct strings S,T consisting of uppercase English letters, S is lexicographically smaller than T if either of the following conditions holds:
- The length |S| of S is less than the length of T, and the first |S| characters of T match S.
- There exists an integer 1\leq i\leq\min\{ |S|,|T|\} that satisfy both of the following two conditions:
- For every integer j with 1\leq j\lt i, the j-th character of S equals the j-th character of T.
- The i-th character of S is alphabetically smaller than the i-th character of T.
For example, if S= AB
and T= ABC
, the first condition holds, so S is lexicographically smaller than T.
If S= ABD
and T= ACD
, the second condition holds for i=2, so S is lexicographically smaller than T.
Constraints
- 100\leq a\leq b\leq c\leq d\leq e\leq 2718
- All input values are integers.
Input
The input is given from Standard Input in the following format:
a b c d e
Output
Print 31 lines. The i-th line (1\leq i\leq 31) should contain the name of the participant who obtained the i-th highest score. If multiple participants have the same score, print them in lexicographical order.
Sample Input 1
400 500 600 700 800
Sample Output 1
ABCDE BCDE ACDE ABDE ABCE ABCD CDE BDE ADE BCE ACE BCD ABE ACD ABD ABC DE CE BE CD AE BD AD BC AC AB E D C B A
The score of each participant is as follows:
For example, ADE and BCE obtained the same score, and ADE is lexicographically smaller, so print ADE before BCE.
Sample Input 2
800 800 900 900 1000
Sample Output 2
ABCDE ACDE BCDE ABCE ABDE ABCD CDE ACE ADE BCE BDE ABE ACD BCD ABC ABD CE DE AE BE CD AC AD BC BD AB E C D A B
Sample Input 3
128 256 512 1024 2048
Sample Output 3
ABCDE BCDE ACDE CDE ABDE BDE ADE DE ABCE BCE ACE CE ABE BE AE E ABCD BCD ACD CD ABD BD AD D ABC BC AC C AB B A
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
周期 N をもつ無限数列 A=(A _ 1,A _ 2,A _ 3,\dotsc) の先頭 N 項 A _ 1,A _ 2,\dotsc,A _ N が与えられます。
この数列の空でない連続する部分列のうち、和が S となるものが存在するか判定してください。
ただし、無限数列 A が周期 N をもつとは、i\gt N を満たすすべての整数 i に対して A _ i=A _ {i-N} が成り立つことをいいます。
制約
- 1\leq N\leq2\times10 ^ 5
- 1\leq A _ i\leq 10 ^ 9
- 1\leq S\leq 10 ^ {18}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N S A _ 1 A _ 2 \dotsc A _ N
出力
A の連続する部分列 (A _ l,A _ {l+1},\dotsc,A _ r) であって、A _ l+A _ {l+1}+\dotsb+A _ r=S となるものが存在するならば Yes
を、そうでないならば No
を出力せよ。
入力例 1
3 42 3 8 4
出力例 1
Yes
数列 A は (3,8,4,3,8,4,3,8,4,\dotsc) のようになります。
A の部分列 (A _ 2,A _ 3,A _ 4,A _ 5,A _ 6,A _ 7,A _ 8,A _ 9)=(8,4,3,8,4,3,8,4) について 8+4+3+8+4+3+8+4=42 が成り立つので、Yes
を出力してください。
入力例 2
3 1 3 8 4
出力例 2
No
A の要素はすべて 3 以上なので、A の空でない連続する部分列の総和は 3 以上です。
よって、総和が 1 となるような部分列は存在しないため、No
を出力してください。
入力例 3
20 83298426 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772
出力例 3
Yes
入力例 4
20 85415869 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772
出力例 4
No
Score : 400 points
Problem Statement
You are given the first N terms A _ 1,A _ 2,\dotsc,A _ N of an infinite sequence A=(A _ 1,A _ 2,A _ 3,\dotsc) that has period N.
Determine if there exists a non-empty contiguous subsequence of this infinite sequence whose sum is S.
Here, an infinite sequence A has period N when A _ i=A _ {i-N} for every integer i>N.
Constraints
- 1\leq N\leq2\times10 ^ 5
- 1\leq A _ i\leq 10 ^ 9
- 1\leq S\leq 10 ^ {18}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N S A _ 1 A _ 2 \dotsc A _ N
Output
If there exists a contiguous subsequence (A _ l,A _ {l+1},\dotsc,A _ r) of A for which A _ l+A _ {l+1}+\dotsb+A _ r=S, print Yes
. Otherwise, print No
.
Sample Input 1
3 42 3 8 4
Sample Output 1
Yes
The sequence A is (3,8,4,3,8,4,3,8,4,\dotsc).
For the subsequence (A _ 2,A _ 3,A _ 4,A _ 5,A _ 6,A _ 7,A _ 8,A _ 9)=(8,4,3,8,4,3,8,4), we have 8+4+3+8+4+3+8+4=42, so print Yes
.
Sample Input 2
3 1 3 8 4
Sample Output 2
No
All elements of A are at least 3, so the sum of any non-empty contiguous subsequence is at least 3.
Thus, there is no subsequence with sum 1, so print No
.
Sample Input 3
20 83298426 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772
Sample Output 3
Yes
Sample Input 4
20 85415869 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772
Sample Output 4
No
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
縦 H 行横 W 列のマス目があります。 上から i 行目 (1\leq i\leq H)、左から j 列目 (1\leq j\leq W) のマスをマス (i,j) と呼ぶことにします。
はじめ、マス (i,j) には強さ S _ {i,j} のスライムがおり、マス (P,Q) にいるスライムが高橋くんです。
高橋くんが以下の行動を好きな回数(0 回でもよい)行ったあとの、高橋くんの強さとしてありえる最大値を求めてください。
- 高橋くんに隣接するスライムのうち、強さが高橋くんの強さの \dfrac1X 倍未満のものを選んで吸収する。 その結果、吸収されたスライムは消滅し、高橋君の強さは吸収したスライムの強さだけ増加する。
上記の行動の際、スライムが吸収され消滅したことで生じた隙間は直ちに高橋くんによって埋められ、消滅したスライムに隣接していたスライム(それらが存在すれば)は新たに高橋くんと隣接します(入出力例1の説明も参照してください)。
制約
- 1\leq H,W\leq500
- 1\leq P\leq H
- 1\leq Q\leq W
- 1\leq X\leq10^9
- 1\leq S _ {i,j}\leq10^{12}
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
H W X P Q S _ {1,1} S _ {1,2} \ldots S _ {1,W} S _ {2,1} S _ {2,2} \ldots S _ {2,W} \vdots S _ {H,1} S _ {H,2} \ldots S _ {H,W}
出力
高橋くんが行動を行ったあとの高橋くんの強さとしてありえる最大値を出力せよ。
入力例 1
3 3 2 2 2 14 6 9 4 9 20 17 15 7
出力例 1
28
はじめ、それぞれのマスにいるスライムの強さは以下の図のようになっています。
例えば、高橋くんは次のように行動を行うことができます。
- マス (2,1) にいるスライムを吸収する。高橋くんの強さは 9+4=13 となり、新たにマス (1,1) のスライムとマス (3,1) のスライムが高橋くんと隣接する。
- マス (1,2) にいるスライムを吸収する。高橋くんの強さは 13+6=19 となり、新たにマス (1,3) のスライムが高橋くんと隣接する。
- マス (1,3) にいるスライムを吸収する。高橋くんの強さは 19+9=28 となる。
以上の行動を行ったあと、高橋くんの強さは 28 となります。
高橋くんがどのように行動を行っても、高橋くんの強さを 28 より大きくすることはできないため、28
を出力してください。
高橋くんの強さの \dfrac12 倍未満のスライムしか吸収できないことに注意してください。 例えば、上図の右側の状態からマス (1,1) にいるスライムを吸収することはできません。
入力例 2
3 4 1 1 1 5 10 1 1 10 1 1 1 1 1 1 1
出力例 2
5
高橋くんはどのスライムも吸収できません。
入力例 3
8 10 2 1 5 388 130 971 202 487 924 247 286 237 316 117 166 918 106 336 928 493 391 235 398 124 280 425 955 212 988 227 222 307 226 336 302 478 246 950 368 291 236 170 101 370 200 204 141 287 410 388 314 205 460 291 104 348 337 404 399 416 263 415 339 105 420 302 334 231 481 466 366 401 452 119 432 292 403 371 417 351 231 482 184
出力例 3
1343
Score : 450 points
Problem Statement
There is a grid with H horizontal rows and W vertical columns. Let (i, j) denote the cell at the i-th row (1\leq i\leq H) from the top and j-th column (1\leq j\leq W) from the left.
Initially, there is a slime with strength S _ {i,j} in cell (i,j), and Takahashi is the slime in the cell (P,Q).
Find the maximum possible strength of Takahashi after performing the following action any number of times (possibly zero):
- Among the slimes adjacent to him, choose one whose strength is strictly less than \dfrac{1}{X} times his strength and absorb it. As a result, the absorbed slime disappears, and Takahashi's strength increases by the strength of the absorbed slime.
When performing the above action, the gap left by the disappeared slime is immediately filled by Takahashi, and the slimes that were adjacent to the disappeared one (if any) become newly adjacent to Takahashi (refer to the explanation in sample 1).
Constraints
- 1\leq H,W\leq500
- 1\leq P\leq H
- 1\leq Q\leq W
- 1\leq X\leq10^9
- 1\leq S _ {i,j}\leq10^{12}
- All input values are integers.
Input
The input is given in the following format from Standard Input:
H W X P Q S _ {1,1} S _ {1,2} \ldots S _ {1,W} S _ {2,1} S _ {2,2} \ldots S _ {2,W} \vdots S _ {H,1} S _ {H,2} \ldots S _ {H,W}
Output
Print the maximum possible strength of Takahashi after performing the action.
Sample Input 1
3 3 2 2 2 14 6 9 4 9 20 17 15 7
Sample Output 1
28
Initially, the strength of the slime in each cell is as follows:
For example, Takahashi can act as follows:
- Absorb the slime in cell (2,1). His strength becomes 9+4=13, and the slimes in cells (1,1) and (3,1) become newly adjacent to him.
- Absorb the slime in cell (1,2). His strength becomes 13+6=19, and the slime in cell (1,3) becomes newly adjacent to him.
- Absorb the slime in cell (1,3). His strength becomes 19+9=28.
After these actions, his strength is 28.
No matter how he acts, it is impossible to get a strength greater than 28, so print 28
.
Note that Takahashi can only absorb slimes whose strength is strictly less than half of his strength. For example, in the figure on the right above, he cannot absorb the slime in cell (1,1).
Sample Input 2
3 4 1 1 1 5 10 1 1 10 1 1 1 1 1 1 1
Sample Output 2
5
He cannot absorb any slimes.
Sample Input 3
8 10 2 1 5 388 130 971 202 487 924 247 286 237 316 117 166 918 106 336 928 493 391 235 398 124 280 425 955 212 988 227 222 307 226 336 302 478 246 950 368 291 236 170 101 370 200 204 141 287 410 388 314 205 460 291 104 348 337 404 399 416 263 415 339 105 420 302 334 231 481 466 366 401 452 119 432 292 403 371 417 351 231 482 184
Sample Output 3
1343
Time Limit: 4 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
正整数 x に対して f(x) を「 x が偶数である間 x を 2 で割り続けたときの、最終的な x の値」として定義します。例えば f(4)=f(2)=f(1)=1 、 f(12)=f(6)=f(3)=3 です。
長さ N の整数列 A=(A_1,A_2,\ldots, A_N) が与えられるので、 \displaystyle \sum_{i=1}^N \sum_{j=i}^N f(A_i+A_j) を求めてください。
制約
- 1\le N\le 2\times 10^5
- 1\le A_i\le 10^7
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを出力せよ。
入力例 1
2 4 8
出力例 1
5
f(A_1+A_1)=f(8)=1 、 f(A_1+A_2)=f(12)=3 、 f(A_2+A_2)=f(16)=1 です。したがって、 1+3+1=5 を出力してください。
入力例 2
3 51 44 63
出力例 2
384
入力例 3
8 577752 258461 183221 889769 278633 577212 392309 326001
出力例 3
20241214
Score : 500 points
Problem Statement
For a positive integer x, define f(x) as follows: "While x is even, keep dividing it by 2. The final value of x after these divisions is f(x)." For example, f(4)=f(2)=f(1)=1, and f(12)=f(6)=f(3)=3.
Given an integer sequence A=(A_1,A_2,\ldots,A_N) of length N, find \displaystyle \sum_{i=1}^N \sum_{j=i}^N f(A_i+A_j).
Constraints
- 1\le N\le 2\times 10^5
- 1\le A_i\le 10^7
- All input values are integers.
Input
The input is given in the following format from Standard Input:
N A_1 A_2 \ldots A_N
Output
Print the answer.
Sample Input 1
2 4 8
Sample Output 1
5
f(A_1+A_1)=f(8)=1, f(A_1+A_2)=f(12)=3, f(A_2+A_2)=f(16)=1. Thus, Print 1+3+1=5.
Sample Input 2
3 51 44 63
Sample Output 2
384
Sample Input 3
8 577752 258461 183221 889769 278633 577212 392309 326001
Sample Output 3
20241214
Time Limit: 5 sec / Memory Limit: 1024 MiB
配点 : 575 点
問題文
長さ N の整数列 A=(A_1,A_2,\ldots, A_N) 、 B=(B_1,B_2,\ldots,B_N) と長さ K の整数列 X=(X_1,X_2,\ldots,X_K) 、 Y=(Y_1,Y_2,\ldots,Y_K) が与えられます。
k=1,2,\ldots,K に対して \displaystyle \sum_{i=1}^{X_k} \sum_{j=1}^{Y_k} \left|A_i-B_j \right| を求めてください。
制約
- 1\le N\le 10^5
- 0\le A_i,B_j\le 2\times 10^8
- 1\le K\le 10^4
- 1\le X_k,Y_k\le N
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N K X_1 Y_1 X_2 Y_2 \vdots X_K Y_K
出力
K 行出力せよ。 i 行目 (1\le i\le K) には、 k=i の場合の答えを出力せよ。
入力例 1
2 2 4 3 5 4 1 1 1 2 2 1 2 2
出力例 1
1 4 2 6
k=1 の場合、答えは |A_1-B_1|=1 となります。したがって、 1 行目には 1 を出力してください。
k=2 の場合、答えは |A_1-B_1|+|A_1-B_2|=1+3=4 となります。したがって、 2 行目には 4 を出力してください。
k=3 の場合、答えは |A_1-B_1|+|A_2-B_1|=1+1=2 となります。したがって、 3 行目には 2 を出力してください。
k=4 の場合、答えは |A_1-B_1|+|A_1-B_2|+|A_2-B_1|+|A_2-B_2|=1+3+1+1=6 となります。したがって、 4 行目には 6 を出力してください。
入力例 2
5 1163686 28892 1263085 2347878 520306 1332157 1202905 2437161 1291976 563395 5 5 3 1 5 2 3 1 2 5 5
出力例 2
13331322 2209746 6366712 207690 20241215
Score : 575 points
Problem Statement
You are given integer sequences A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N) of length N, and integer sequences X=(X_1,X_2,\ldots,X_K) and Y=(Y_1,Y_2,\ldots,Y_K) of length K.
For each k=1,2,\ldots,K, find \displaystyle \sum_{i=1}^{X_k} \sum_{j=1}^{Y_k} |A_i-B_j|.
Constraints
- 1\le N\le 10^5
- 0\le A_i,B_j\le 2\times 10^8
- 1\le K\le 10^4
- 1\le X_k,Y_k\le N
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N K X_1 Y_1 X_2 Y_2 \vdots X_K Y_K
Output
Print K lines. The i-th line (1\le i\le K) should contain the answer for k=i.
Sample Input 1
2 2 4 3 5 4 1 1 1 2 2 1 2 2
Sample Output 1
1 4 2 6
For k=1, the answer is |A_1-B_1|=1, so print 1 on the first line.
For k=2, the answer is |A_1-B_1|+|A_1-B_2|=1+3=4, so print 4 on the second line.
For k=3, the answer is |A_1-B_1|+|A_2-B_1|=1+1=2, so print 2 on the third line.
For k=4, the answer is |A_1-B_1|+|A_1-B_2|+|A_2-B_1|+|A_2-B_2|=1+3+1+1=6, so print 6 on the fourth line.
Sample Input 2
5 1163686 28892 1263085 2347878 520306 1332157 1202905 2437161 1291976 563395 5 5 3 1 5 2 3 1 2 5 5
Sample Output 2
13331322 2209746 6366712 207690 20241215