#P7349. 「MCOI-04」Dream and the Multiverse

「MCOI-04」Dream and the Multiverse

Description

Dream models time and space as a rooted directed tree with nn nodes, where the root is node 11 and all edges are directed from shallow to deep.
Dream uses his superpower to add mm extra directed edges to this tree, and the final graph is still a directed acyclic graph.
Dream further models an event as a node in the graph, and models an era as a simple path in the graph.
Dream considers a pair of events (i,j)(i,j) to be feasible if and only if there exists an era whose first event is ii and last event is jj.
Dream now has qq queries. The ii-th query is given by two positive integers lil_i and rir_i, where liril_i \le r_i.
For each query, Dream wants to know how many feasible event pairs (i,j)(i,j) satisfy i,j[l,r]i,j \in [l,r].

Input Format

The first line contains two integers n,mn,m.
The next line contains n1n-1 positive integers describing the tree. The ii-th number denotes the index of the parent of node i+1i+1, namely fif_i, which means there is a directed edge from fif_i to i+1i+1.
The next mm lines each contain two positive integers u,vu,v, indicating an extra directed edge added from uu to vv.
The next line contains a positive integer qq.
The next qq lines each contain two positive integers l,rl,r, representing a query.

Output Format

Output qq lines, each containing one integer, which is the answer to the corresponding query.

2 2
1
1 2
1 2
1
1 2
3

Hint

Constraints and Notes

This problem uses bundled testdata.

  • Subtask 1 (1 pts): The tree is a chain.
  • Subtask 2 (11 pts): n,q,m1000n,q,m \le 1000.
  • Subtask 3 (7 pts): m5m \le 5.
  • Subtask 4 (23 pts): n,q,m5×104n,q,m \le 5 \times 10^4.
  • Subtask 5 (17 pts): q105q \le 10^5.
  • Subtask 6 (41 pts): No special constraints.

For 100%100\% of the testdata, 2n1052 \le n \le 10^5, 0m1050 \le m \le 10^5, 1q1061 \le q \le 10^6.
It is guaranteed that the extra edges will not form a cycle, and the given fif_i form a rooted tree with root 11.
It is guaranteed that lrl \le r.

Notes

Minecraft OI Round 4 C
idea & solution: w33z8kqrqk8zzzx33 check: ClCN

Translated by ChatGPT 5