CF1535E.Gold Transfer

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a rooted tree. Each vertex contains aia_i tons of gold, which costs cic_i per one ton. Initially, the tree consists only a root numbered 00 with a0a_0 tons of gold and price c0c_0 per ton.

There are qq queries. Each query has one of two types:

  1. Add vertex ii (where ii is an index of query) as a son to some vertex pip_i ; vertex ii will have aia_i tons of gold with cic_i per ton. It's guaranteed that ci>cpic_i > c_{p_i} .
  2. For a given vertex viv_i consider the simple path from viv_i to the root. We need to purchase wiw_i tons of gold from vertices on this path, spending the minimum amount of money. If there isn't enough gold on the path, we buy all we can.

If we buy xx tons of gold in some vertex vv the remaining amount of gold in it decreases by xx (of course, we can't buy more gold that vertex has at the moment). For each query of the second type, calculate the resulting amount of gold we bought and the amount of money we should spend.

Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query, so don't forget to flush output after printing answers. You can use functions like fflush(stdout) in C++ and BufferedWriter.flush in Java or similar after each writing in your program. In standard (if you don't tweak I/O), endl flushes cout in C++ and System.out.println in Java (or println in Kotlin) makes automatic flush as well.

输入格式

The first line contains three integers qq , a0a_0 and c0c_0 ( 1q31051 \le q \le 3 \cdot 10^5 ; 1a0,c0<1061 \le a_0, c_0 < 10^6 ) — the number of queries, the amount of gold in the root and its price.

Next qq lines contain descriptions of queries; The ii -th query has one of two types:

  • " 11 pip_i aia_i cic_i " ( 0pi<i0 \le p_i < i ; 1ai,ci<1061 \le a_i, c_i < 10^6 ): add vertex ii as a son to vertex pip_i . The vertex ii will have aia_i tons of gold with price cic_i per one ton. It's guaranteed that pip_i exists and ci>cpic_i > c_{p_i} .
  • " 22 viv_i wiw_i " ( 0vi<i0 \le v_i < i ; 1wi<1061 \le w_i < 10^6 ): buy wiw_i tons of gold from vertices on path from viv_i to 00 spending the minimum amount of money. If there isn't enough gold, we buy as much as we can. It's guaranteed that vertex viv_i exist.

It's guaranteed that there is at least one query of the second type.

输出格式

For each query of the second type, print the resulting amount of gold we bought and the minimum amount of money we should spend.

输入输出样例

  • 输入#1

    5 5 2
    2 0 2
    1 0 3 4
    2 2 4
    1 0 1 3
    2 4 2

    输出#1

    2 4
    4 10
    1 3

说明/提示

Explanation of the sample:

At the first query, the tree consist of root, so we purchase 22 tons of gold and pay 22=42 \cdot 2 = 4 . 33 tons remain in the root.

At the second query, we add vertex 22 as a son of vertex 00 . Vertex 22 now has 33 tons of gold with price 44 per one ton.

At the third query, a path from 22 to 00 consists of only vertices 00 and 22 and since c0<c2c_0 < c_2 we buy 33 remaining tons of gold in vertex 00 and 11 ton in vertex 22 . So we bought 3+1=43 + 1 = 4 tons and paid 32+14=103 \cdot 2 + 1 \cdot 4 = 10 . Now, in vertex 00 no gold left and 22 tons of gold remain in vertex 22 .

At the fourth query, we add vertex 44 as a son of vertex 00 . Vertex 44 now has 11 ton of gold with price 33 .

At the fifth query, a path from 44 to 00 consists of only vertices 00 and 44 . But since no gold left in vertex 00 and only 11 ton is in vertex 44 , we buy 11 ton of gold in vertex 44 and spend 13=31 \cdot 3 = 3 . Now, in vertex 44 no gold left.

首页