#13582. Dream and the Multiverse REMATCH
Dream and the Multiverse REMATCH
Dream and the Multiverse REMATCH
题目描述
[Link](https://youtu.be/tylNqtyj0gs?t=2388)I have gone over the scenarios in my head,
and there are 6.96969 billion outcomes, and only one of them -
- do I win.
Dream abstracts the fabric of spacetime as a directed rooted tree (arborescence) with nodes (numbered through ). Node is the root and for each (), the parent of node is . All edges of this tree are directed away from the root.
Then, Dream employs a magical superpower and adds directed edges to this tree in such a way that the resulting directed graph remains acyclic (a DAG).
Let's call a node of this DAG an event and further call a simple path on this DAG an era. Dream considers a pair of events to be plausible if there is an era whose first event is and last event is . Note that does not have to hold for a plausible pair.
Dream now wants you to answer queries. In each query, he gives you two positive integers and , where , and he wishes to know the number of plausible pairs of events such that .
输入格式
The first line of the input contains two space-separated integers $N$ and $M$.The second line contains space-separated integers .
lines follow. Each of these lines contains two space-separated integers and describing an additional edge from node to node .
The following line contains a single integer .
lines follow. Each of these lines contains two space-separated integers and describing a query.
输出格式
For each query, print a single line containing one integer ― the number of plausible pairs $(i,j)$ such that $l \leq i \lt j \leq r$.8 2
1 2 5 1 4 3 3
2 4
4 7
3
4 6
5 7
1 8
2
2
18
提示
- $2 \leq N \leq 7 \cdot 10^5$ - $1 \leq Q \leq 7 \cdot 10^5$ - $0 \leq M \leq 20$ - $1 \le f_i \le N$ for each valid $i$ - $1 \le u, v \le N$ - the graph described on the input is acyclic - $1 \le l \le r \le N$Subtasks
Subtask #1 (17 points):
Subtask #2 (83 points): original constraints 标签: P7331|O2优化