Heraclosures
시간 제한5.5초메모리 제한2048 MB
함수 호출 관계가 DAG를 이루고, 기본 실행 시간의 갱신과 전체 실행 시간 질의가 주어질 때 질의 답의 가중 합을 구한다.
문제
Prometheus is a programmer. He wrote a large program in Heraclosures, a programming language invented by his friend Heracles. The program contains functions, each identified uniquely by an integer from to . Each function is made up of many instructions, some of which are call instructions that invoke other functions.
When a call instruction in function calls a target function , the full code of function is executed before continuing with the execution of function . Each function may contain multiple call instructions, and it is possible for a function to call another function several times. Some functions, however, may have no call instructions at all. Following advice from his friend Sisyphus, Prometheus ensured that there are no cycles among the function calls, avoiding endless call loops.
Each function has a base execution time , which is its execution time excluding any functions it calls. Execution times are measured in processor cycles, so is always a non-negative integer. The total execution time of function , denoted as , includes both its base execution time and the total execution time of all functions it directly calls. Formally,
where is the base execution time we already defined, and is the multiset of functions that function directly calls. If function calls function multiple times, then appears multiple times in this sum. If function has no call instructions, then is empty, and so .
Prometheus wants to assess the impact of several code adjustments on the performance of his program. Tired of programming, he has hired you to implement a tool that accepts a list of events, where each event is either an update or a query.
- An update sets to a given value . Notice that while this event may modify the base execution time of function , the multiset of its called functions remains the same.
- A query calculates the current total execution time of function , taking into account all updates that have occurred up to that point in the event list.
As a first approach, Prometheus does not want the result of each query but a single summary result. Suppose there are queries, numbered sequentially from to in the list of events. Then, the summary result is calculated as:
where is the total execution time of the function specified in the -th query, taking into account all updates up to that query.
입력
The first line contains an integer () indicating the number of functions. Each function is identified by a distinct integer from to .
The next line contains integers (), representing that the base execution time of function is .
The next line contains an integer () denoting the number of call instructions among all the functions.
Each of the next lines describes a call instruction with two integers and ( and ), indicating that function contains a call instruction to function .
The next line contains an integer () representing the number of events in the list that your tool must accept.
Each of the next lines describes an event, in the order they appear in the list.
If the event is an update, the line contains the uppercase letter “U” followed by two integers () and (), denoting that the base execution time of function must be set to .
If the event is a query, the line contains the uppercase letter “Q” followed by an integer (), indicating that the total execution time of function must be determined, taking into account all previous updates that appear in the list.
It is guaranteed that there are no cycles among the function calls, and there is at least one query in the list of events.
출력
Output a single line with an integer indicating the summary result of all the queries.