#P11191. 「KDOI-10」超级演出

    ID: 10628 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树2024洛谷原创O2优化扫描线洛谷月赛根号分治

「KDOI-10」超级演出

Description

Meguru has prepared a super show!

The stage and the waiting rooms can be seen as a directed acyclic graph (DAG) consisting of nn vertices and mm edges.

The stage is at vertex 11, and the rest vertices are all waiting rooms. It is guaranteed that each vertex has at least one path to vertex 11. For each waiting room, there is a troupe in it initially.

Meguru can send an enter command to a waiting room uu:

  • If the troupe in this room has not come to the stage yet, and there exists a path from uu to 11 such that there are no troupe waiting for entrance, then the troupe in waiting room uu will come to the stage and subsequently exit. Note that a troupe will not return to the waiting room after the exit.
  • Otherwise, this command is considered as inoperative.

Meguru has a command sequence a1,a2,,aka_1,a_2,\ldots, a_k and qq queries. In each query, an interval [l,r][l,r] is given, and Meguru asks you that, if she sends the enter command to waiting room al,al+1,,ara_l,a_{l+1},\ldots,a_r successively, how many troupes are still in the waiting room.

Note that each query is independent, i.e., before each query, there is a troupe in each waiting room.

Input Format

The first line of the input contains a single integer cc — the id of the test. c=0c=0 represents that this is a sample test.

The second line contains four integers nn, mm, kk, and qq — the number of vertices, the number of edges, the length of the command sequence, and the number of queries.

Then mm lines follow, each containing two integer uu and vv — there is an edge connecting uu and vv. It is guaranteed that there are no self-loops or multiple-edges.

The next line contains kk integers a1,a2,,aka_1,a_2,\ldots, a_k — the command sequence.

Then qq lines follow, each containing two integers ll and rr — the interval in the query.

Output Format

Print qq lines, each line containing a single integer — the answer to the query.

0
5 5 5 4
2 1
3 1
5 1
4 2
4 3
2 4 4 3 5
1 2
1 5
3 5
2 3

2
0
2
4

Hint

Sample 1 Explanation

  • In the query l=1l=1, r=2r=2:

    • For the command a1=2a_1=2, 22 comes to the stage by 212\to 1;
    • For the command a2=4a_2=4, 44 comes to the stage by 4214\to 2\to 1.

    Troupes 3,53,5 are left, so output 22.

  • In the query l=2l=2, r=3r=3:

    • For the command a2=4a_2=4, there does not exist a valid path, so this command is inoperative.
    • For the command a3=4a_3=4, there does not exist a valid path, so this command is inoperative.

    Troupes 2,3,4,52,3,4,5 are left, so output 44.

Sample 2

See show/show2.in and show/show2.ans in the attachments.

This sample satisfies the constraints of test 1,21,2.

Sample 3

See show/show3.in and show/show3.ans in the attachments.

This sample satisfies the constraints of test 585\sim 8.

Sample 4

See show/show4.in and show/show4.ans in the attachments.

This sample satisfies the constraints of test 9119\sim 11.

Sample 5

See show/show5.in and show/show5.ans in the attachments.

This sample satisfies the constraints of test 12,1312,13.

Sample 6

See show/show6.in and show/show6.ans in the attachments.

This sample satisfies the constraints of test 18,1918,19.


Constraints

For all the tests, it is guaranteed that:

  • 1n,k,q2×1051\leq n,k,q\leq2\times10^5;
  • 1m4×1051\leq m\leq4\times10^5;
  • 1v<un1\leq v<u\leq n, and all (u,v)(u,v)-s are distinct;
  • For each 1ik1\le i\le k, 2ain2\leq a_i\leq n;
  • For each query, 1lrk1\leq l\leq r\leq k;
  • The input forms a DAG, and each vertex has at least one path to vertex 11.
Test Id n,k,qn,k,q\leq mm\leq Special Properties
1,21,2 300300 600600 -
3,43,4 20002\,000 40004\,000 A
585\sim 8 -
9119\sim 11 2×1052\times10^5 4×1054\times10^5 A
12,1312,13 BC
14,1514,15 C
16,1716,17 BD
18,1918,19 D
202220\sim 22 B
232523\sim 25 -
  • Property A: The graph is degenerated into an inward-directed tree, i.e. each vertex except vertex 11 has out-degree 11;
  • Property B: For each query, r=kr=k;
  • Property C: For each 1i<jk1\leq i< j\leq k, aiaja_i\neq a_j;
  • Property D: The in-degree and out-degree of each vertex does not exceed 3030, respectively.