#P6592. [YsOI2020] 幼儿园

[YsOI2020] 幼儿园

Description

Ysuperman loves taking walks in their kindergarten. To make walking easier, they model the kindergarten as a directed graph with nn vertices and mm edges. After walking a lot, they assign each edge an unparalleled closeness value: 1,2,,m1,2,\cdots,m. A larger value means a closer relationship. They also assign each vertex an unparalleled label: 1,2,,n1,2,\cdots,n, where 11 represents the kindergarten gate. However, vertices have no closeness value.

Over the next kk days, Ysuperman has one walking plan each day. Specifically, they want to start from vertex xix_i, use only edges whose closeness values lie in the interval [li,ri][l_i,r_i], and reach the gate vertex 11. Along the way, the closeness values of the edges used must be strictly decreasing; otherwise, due to their OCD, they cannot go home.

Ysuperman is completely confused by the draft they just drew. They realize they cannot plan so many valid routes, so they ask you for help. For each day’s plan, output 1 if it is feasible, otherwise output 0.

Of course, sometimes Ysuperman is in a hurry and needs you to reply immediately; sometimes they can wait and ask all questions first, then wait for your reply.

Input Format

The first line contains four integers n,m,k,wn,m,k,w, representing the number of vertices, the number of edges, the number of Ysuperman’s plans, and Ysuperman’s level of urgency. Here ww will be used later in the input.

Then mm lines follow. The ii-th of these lines contains two integers ui,viu_i,v_i, indicating that there is a directed edge from vertex uiu_i to vertex viv_i in the kindergarten, and its closeness value is ii.

Then kk lines follow. The ii-th of these lines contains three integers xi,li,rix_i,l_i,r_i, representing Ysuperman’s ii-th plan.

If w=0w=0, then xi,li,rix_i,l_i,r_i are in plaintext and can be used directly.

If w=1w=1, then xi,li,rix_i,l_i,r_i are encrypted and you need to decrypt them. The decryption is: let LL be the sum of the outputs of all previous queries (if there were no previous queries, then L=0L=0). You need to XOR xi,li,rix_i,l_i,r_i with LL.

Output Format

Output kk lines. Each line is either 1 or 0. The number on the ii-th line indicates whether the ii-th plan is feasible.

5 7 5 0
3 2
1 2
4 3
5 4
3 1
5 3
5 1
3 1 4
1 2 2
5 5 6
4 5 7
2 1 7

0
1
1
0
0

5 12 10 0
4 2
4 2
5 3
3 3
1 5
1 4
4 4
2 4
5 3
1 5
2 2
4 1
4 3 5
4 2 3
1 4 5
3 1 8
3 1 4
3 5 5
2 1 12
4 10 12
2 5 5
1 1 3

0
0
1
0
0
0
0
1
0
1

5 12 10 1
4 2
4 2
5 3
3 3
1 5
1 4
4 4
2 4
5 3
1 5
2 2
4 1
4 3 5
4 2 3
1 4 5
2 0 9
2 0 5
2 4 4
3 0 13
5 11 13
0 7 7
3 3 1
0
0
1
0
0
0
0
1
0
1

Hint

Sample Explanation

Sample Explanation 11

For the 22-nd plan, Ysuperman is already at the gate, so the plan is feasible.

For the 33-rd plan, Ysuperman can only take the path $5 \overset{6}{\rightarrow}3 \overset{5}{\rightarrow} 1$. (The number above each arrow is the closeness value of the edge.)

All other plans are infeasible.

Sample Explanation 33

Sample 3 is Sample 2 after encryption.


Constraints

This problem uses bundled testdata.

subtask\mathrm{subtask} nn mm kk Special property Score
11 17\le17 2105\le 2\cdot 10^5 / 5 5
22 500\le500 500\le500 1717
33 3000\le 3000 3000\le 3000 1818
4 4 105\le10^5 2105\le2\cdot10^5 vi=1v_i=1 1313
55 105\le 10^5 2105\le 2\cdot10^5 105\le 10^5 li=1,w=0l_i=1,w=0 7 7
66 105\le10^5 2105\le2\cdot10^5 w=0w=0 1313
77 105 \le 10^5 2105\le 2\cdot10^5 / 2727

For 100%100\% of the testdata, $1 \le n \le 10^5,1 \le m \le 2\cdot10^5,0 \le k \le 2\cdot10^5$.

w{0,1},1ui,vinw\in\{0,1\},1 \le u_i,v_i \le n.

After decryption, xi,li,rix_i,l_i,r_i are guaranteed to satisfy 1xn,1li,rim1\le x \le n,1 \le l_i,r_i \le m.

Hint

There may be multiple edges and self-loops, and the graph is not guaranteed to be connected.

Translated by ChatGPT 5