Heraclosures
Time limit5.5sMemory limit2048 MB
Functions form a call DAG with base times and point updates; answer each query for total time T(i) and output the query-index-weighted sum mod 1e9+7.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Graph, Topological sort
- Solved
- No attempts yet
Problem
Prometheus is a programmer. He wrote a large program in Heraclosures, a programming language invented by his friend Heracles. The program has functions, each identified by a unique integer from to . Each function consists of many instructions, some of which are call instructions that invoke other functions.
When a call instruction in function calls function , the full code of function runs before function continues. A function may contain several call instructions, and it may call the same function several times. Some functions have no call instructions at all. Following advice from his friend Sisyphus, Prometheus made sure the function calls form no cycles, so no call loop can run forever.
Function has a base execution time , which is its execution time excluding the functions it calls. Execution times are measured in processor cycles, so is always a non-negative integer. The total execution time of function , written , is its base time plus the total time of every function it calls directly:
Here is the multiset of functions that calls directly. If calls several times, appears that many times in the sum. If has no call instructions, is empty and .
An update sets to a value . The update changes only the base time, and the multiset stays the same. A query asks for the current total execution time of function , using all updates that appear before it in the event list.
Prometheus wants one summary value instead of each query result. With queries numbered 1 to in event order, the summary is
where is the total execution time of the function in the -th query, using all updates before that query.
Input
The first line contains an integer (), the number of functions.
The next line contains integers (), the base execution time of each function.
The next line contains an integer (), the number of call instructions among all functions. Each of the next lines contains two integers and (, ), meaning function contains a call instruction to function .
The next line contains an integer (), the number of events. Each of the next lines is either an update, written as the uppercase letter U followed by two integers () and (), which sets to , or a query, written as the uppercase letter Q followed by an integer (), which asks for .
The input has no call cycles, and there is at least one query.
Output
Print a single integer, the summary result of all queries modulo .