Heraclosures

시간 제한5.5초메모리 제한2048 MB

요약
함수 호출 관계가 DAG를 이루고, 기본 실행 시간의 갱신과 전체 실행 시간 질의가 주어질 때 질의 답의 가중 합을 구한다.
난이도

보통10점 중 7점

유형
그래프, 위상 정렬, 동적 계획법, 누적 합
정답자
아직 제출이 없습니다

문제

Prometheus is a programmer. He wrote a large program in Heraclosures, a programming language invented by his friend Heracles. The program contains NN functions, each identified uniquely by an integer from 11 to NN. Each function is made up of many instructions, some of which are call instructions that invoke other functions.

When a call instruction in function ii calls a target function jj, the full code of function jj is executed before continuing with the execution of function ii. 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 ii has a base execution time B_iB\_i, which is its execution time excluding any functions it calls. Execution times are measured in processor cycles, so B_iB\_i is always a non-negative integer. The total execution time of function ii, denoted as T(i)T(i), includes both its base execution time and the total execution time of all functions it directly calls. Formally,

 T(i)=B_i+∑_j∈C(i)T(j),T(i) = B\_i + \sum\_{j \in C(i)}{T(j)}\text{,} 

where B_iB\_i is the base execution time we already defined, and C(i)C(i) is the multiset of functions that function ii directly calls. If function ii calls function jj multiple times, then T(j)T(j) appears multiple times in this sum. If function ii has no call instructions, then C(i)C(i) is empty, and so T(i)=B_iT(i) = B\_i.

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 B_iB\_i to a given value VV. Notice that while this event may modify the base execution time of function ii, the multiset C(i)C(i) of its called functions remains the same.
  • A query calculates the current total execution time T(i)T(i) of function ii, 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 qq queries, numbered sequentially from 11 to qq in the list of events. Then, the summary result is calculated as:

 (∑_k=1qk⋅T(i_k))(mod109+7),\left( \sum\_{k=1}^{q}{k \cdot T(i\_k)} \right) \pmod {10^9+7} \text{,} 

where T(i_k)T(i\_k) is the total execution time of the function specified in the kk-th query, taking into account all updates up to that query.

입력

The first line contains an integer NN (1≤N≤80001 ≤ N ≤ 8000) indicating the number of functions. Each function is identified by a distinct integer from 11 to NN.

The next line contains NN integers B_1,B_2,…,B_NB\_1, B\_2, \dots , B\_N (0≤B_i≤1090 ≤ B\_i ≤ 10^9 ), representing that the base execution time of function ii is B_iB\_i.

The next line contains an integer MM (1≤M≤80001 ≤ M ≤ 8000) denoting the number of call instructions among all the functions.

Each of the next MM lines describes a call instruction with two integers FF and GG (1≤F,G≤N1 ≤ F, G ≤ N and F≠GF \ne G), indicating that function FF contains a call instruction to function GG.

The next line contains an integer EE (1≤E≤1061 ≤ E ≤ 10^6) representing the number of events in the list that your tool must accept.

Each of the next EE 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 II (1≤I≤N1 ≤ I ≤ N) and VV (0≤V≤1090 ≤ V ≤ 10^9), denoting that the base execution time of function II must be set to VV.

If the event is a query, the line contains the uppercase letter “Q” followed by an integer JJ (1≤J≤N1 ≤ J ≤ N), indicating that the total execution time of function JJ 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.

예제2

  1. 예제 1

    입력
    3
    10 20 100
    3
    1 2
    2 3
    1 3
    3
    Q 1
    Q 2
    Q 3
    
    예상 출력
    770
    
  2. 예제 2

    입력
    2
    42 10
    2
    2 1
    2 1
    5
    Q 2
    Q 1
    U 2 0
    Q 1
    Q 2
    
    예상 출력
    640