/
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 366 点
問題文
高橋君は魔法使いの見習いで、魔法陣に刻まれた式を読み解く訓練をしています。魔法陣にはポーランド記法(前置記法)で数式が刻まれています。
ポーランド記法とは、演算子をオペランドの前に置く記法です。ここで扱う演算子はすべて二項演算子 +, -, *, / の 4 種類です。ポーランド記法の数式は以下のように再帰的に定義されます。
- 整数単体はポーランド記法の数式であり、その評価結果はその整数自身である。
op A B(opは二項演算子、AとBはそれぞれポーランド記法の数式)もポーランド記法の数式であり、その評価結果はAの評価結果とBの評価結果に対して演算子opを適用した結果である。
除算 / は 0 に向かって切り捨てる整数除算(truncated division)とします。すなわち、実数としての商の小数部分を切り捨て、0 に近い方の整数を結果とします(例: 7 / 2 = 3、(-7) / 2 = -3、7 / (-2) = -3)。
例えば、通常の中置記法で (3 + 5) * 2 と書かれる式は、ポーランド記法では * + 3 5 2 と表されます。この式は 5 個のトークン *, +, 3, 5, 2 からなり、評価結果は 16 です。
数式はトークン(演算子または整数を表す文字列)の列として与えられます。トークンが +, -, *, / のいずれかと完全に一致する 1 文字の文字列であるとき、かつそのときに限り、そのトークンは演算子です。それ以外のトークンは整数を表す文字列です。例えば、-3 は 2 文字からなり +, -, *, / のいずれとも一致しないため、演算子ではなく整数 -3 を表します。一方、トークン - が単独で(1 文字の文字列として)現れた場合は常に減算の演算子を意味し、負号としては解釈しません。
整数を表すトークンは十進表記で与えられます。負の整数の場合は先頭に負号 - が付きます。負号を除いた部分は、先頭に不要な 0 を含みません(ただし整数 0 を表すトークンは 0 の 1 文字です)。-0 は与えられません。
ある日、高橋君は古代の魔法陣を発見し、そこに刻まれたポーランド記法の数式を解読しようとしました。しかし、いたずら好きの青木君がその魔法陣に反転の呪いをかけ、数式中のいくつかの演算子を対になる演算子に置き換えてしまいました。具体的には、青木君はトークン列の中から相異なる K 個の位置(1-indexed)を選びました。選ばれた各位置のトークンは必ず演算子であり、青木君はそれぞれに対して以下の置き換えを行いました。
+は-に、-は+に置き換える*は/に、/は*に置き換える
高橋君は、呪いをかけられる前の元の数式の評価結果と、呪いをかけられた後の数式の評価結果の両方を知りたいと思っています。
あなたには、元のポーランド記法の数式を構成する N 個のトークンの列 t_1, t_2, \ldots, t_N と、青木君が反転させた演算子の位置を表す K 個の整数 p_1, p_2, \ldots, p_K が与えられます。元の数式の評価結果と、反転後の数式の評価結果をそれぞれ求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- N は奇数である
- トークン列 t_1, t_2, \ldots, t_N は有効なポーランド記法の数式を構成する
- 各トークン t_i は演算子(
+,-,*,/のいずれかと完全に一致する 1 文字の文字列)または整数を表す文字列のいずれかである - 整数を表すトークンが表す整数の絶対値は 10^9 以下である
- 0 \leq K \leq N
- 1 \leq p_i \leq N(1 \leq i \leq K)
- p_1, p_2, \ldots, p_K はすべて相異なる
- 各 p_i が指す位置のトークン t_{p_i} は演算子である
- 元の数式および反転後の数式の評価過程において、0 での除算は発生しない
- 元の数式および反転後の数式の評価過程において、現れるすべての部分式の評価結果(中間結果および最終結果)の絶対値は 10^{18} 以下である
入力
K > 0 の場合:
N t_1 t_2 \cdots t_N K p_1 p_2 \cdots p_K
K = 0 の場合:
N t_1 t_2 \cdots t_N 0
- 1 行目には、トークンの個数を表す整数 N が与えられる。
- 2 行目には、ポーランド記法の数式を構成する N 個のトークン t_1, t_2, \ldots, t_N がスペース区切りで与えられる。
- 3 行目には、青木君が反転させた演算子の個数を表す整数 K が与えられる。
- K > 0 のとき、4 行目には、反転対象となる演算子のトークン列中の位置(1-indexed)を表す K 個の整数 p_1, p_2, \ldots, p_K がスペース区切りで与えられる。K = 0 のとき、4 行目は存在しない。
出力
2 行で出力せよ。
- 1 行目には、元の数式(反転前)の評価結果を整数で出力せよ。
- 2 行目には、反転後の数式の評価結果を整数で出力せよ。
入力例 1
5 * + 3 5 2 1 1
出力例 1
16 4
入力例 2
3 / -7 2 1 1
出力例 2
-3 -14
入力例 3
21 + / + * -8 3 7 - 10 2 * - / 20 3 -5 + * 2 -3 4 5 1 2 4 11 18
出力例 3
-24 38
入力例 4
31 - * - + 5 1 * 2 3 + / 21 3 - 9 2 / - * 7 2 + 1 3 * - 9 4 / 50 5 7 1 4 7 11 17 25 29
出力例 4
0 280
入力例 5
1 -1000000000 0
出力例 5
-1000000000 -1000000000
Score : 366 pts
Problem Statement
Takahashi is an apprentice wizard, training to decipher formulas inscribed on magic circles. The magic circles have mathematical expressions written in Polish notation (prefix notation).
Polish notation is a notation where operators are placed before their operands. All operators handled here are the 4 types of binary operators: +, -, *, /. A Polish notation expression is recursively defined as follows:
- A single integer is a Polish notation expression, and its evaluation result is that integer itself.
op A B(whereopis a binary operator, andAandBare each Polish notation expressions) is also a Polish notation expression, and its evaluation result is the result of applying operatoropto the evaluation results ofAandB.
Division / is truncated division toward zero. That is, the fractional part of the quotient as a real number is discarded, and the integer closer to 0 is taken as the result (e.g., 7 / 2 = 3, (-7) / 2 = -3, 7 / (-2) = -3).
For example, the expression written as (3 + 5) * 2 in standard infix notation is represented as * + 3 5 2 in Polish notation. This expression consists of 5 tokens *, +, 3, 5, 2, and its evaluation result is 16.
An expression is given as a sequence of tokens (strings representing operators or integers). A token is an operator if and only if it is a 1-character string that exactly matches one of +, -, *, /. All other tokens represent integers. For example, -3 consists of 2 characters and does not match any of +, -, *, /, so it is not an operator but represents the integer -3. On the other hand, when the token - appears alone (as a 1-character string), it always means the subtraction operator and is never interpreted as a negative sign.
Tokens representing integers are given in decimal notation. For negative integers, a negative sign - is prepended. The part excluding the negative sign does not contain unnecessary leading 0s (however, the token representing the integer 0 is the single character 0). The token -0 is never given.
One day, Takahashi discovered an ancient magic circle and tried to decipher the Polish notation expression inscribed on it. However, the mischievous Aoki cast an inversion curse on the magic circle, replacing some operators with their paired operators. Specifically, Aoki chose K distinct positions (1-indexed) from the token sequence. The token at each chosen position is guaranteed to be an operator, and Aoki performed the following replacements on each:
+is replaced with-, and-is replaced with+*is replaced with/, and/is replaced with*
Takahashi wants to know both the evaluation result of the original expression before the curse and the evaluation result of the expression after the curse.
You are given a sequence of N tokens t_1, t_2, \ldots, t_N constituting the original Polish notation expression, and K integers p_1, p_2, \ldots, p_K representing the positions of the operators that Aoki inverted. Compute the evaluation result of the original expression and the evaluation result of the inverted expression, respectively.
Constraints
- 1 \leq N \leq 2 \times 10^5
- N is odd
- The token sequence t_1, t_2, \ldots, t_N constitutes a valid Polish notation expression
- Each token t_i is either an operator (a 1-character string exactly matching one of
+,-,*,/) or a string representing an integer - The absolute value of the integer represented by each integer token is at most 10^9
- 0 \leq K \leq N
- 1 \leq p_i \leq N (1 \leq i \leq K)
- p_1, p_2, \ldots, p_K are all distinct
- The token t_{p_i} at each position p_i is an operator
- No division by 0 occurs during the evaluation of either the original or inverted expression
- The absolute value of the evaluation result of every subexpression (both intermediate and final results) appearing during the evaluation of either the original or inverted expression is at most 10^{18}
Input
When K > 0:
N t_1 t_2 \cdots t_N K p_1 p_2 \cdots p_K
When K = 0:
N t_1 t_2 \cdots t_N 0
- The first line contains an integer N representing the number of tokens.
- The second line contains N tokens t_1, t_2, \ldots, t_N constituting the Polish notation expression, separated by spaces.
- The third line contains an integer K representing the number of operators Aoki inverted.
- When K > 0, the fourth line contains K integers p_1, p_2, \ldots, p_K separated by spaces, representing the 1-indexed positions in the token sequence of the operators to be inverted. When K = 0, the fourth line does not exist.
Output
Output 2 lines.
- On the first line, output the evaluation result of the original expression (before inversion) as an integer.
- On the second line, output the evaluation result of the inverted expression as an integer.
Sample Input 1
5 * + 3 5 2 1 1
Sample Output 1
16 4
Sample Input 2
3 / -7 2 1 1
Sample Output 2
-3 -14
Sample Input 3
21 + / + * -8 3 7 - 10 2 * - / 20 3 -5 + * 2 -3 4 5 1 2 4 11 18
Sample Output 3
-24 38
Sample Input 4
31 - * - + 5 1 * 2 3 + / 21 3 - 9 2 / - * 7 2 + 1 3 * - 9 4 / 50 5 7 1 4 7 11 17 25 29
Sample Output 4
0 280
Sample Input 5
1 -1000000000 0
Sample Output 5
-1000000000 -1000000000