Limits 3.5s, 512 MB

You are given a tree. Each of NN vertices has time, when it is active (from tstartt_{start} to tendt_{end} inclusive). You are given QQ queries of two kinds:

1 t a b\texttt{1 t a b}, asking how many active nodes are on path between a\texttt{a} and b\texttt{b} in time t\texttt{t}.

2 t a\texttt{2 t a}, asking how many active nodes are in the subtree of a\texttt{a} in time t\texttt{t}.

The tree will be rooted at node number 0.

Input

The first line of input will contain NN and QQ (1N,Q2×1051 \le N, Q \le 2\times10^5), the size of tree and number of queries.

The next line will contain NN numbers (tistartt_{i}-start, the start time of each node).

The next line will contain NN numbers (tiendt_i-end, end time of each node).

Both start times/end times will be in range from 00 to 10610^{6} (and start time will never be after end).

The next N1N-1 lines will contain two integers aa and bb (0a,b<N0 \le a, b < N), the edges of the tree.

The last QQ will be in form described above. Here both aa, bb will be in range from 0 to N1N-1 and tt from range 00 to 10610^6.

Output

For each query, output the number of active nodes in given time.

Sample

InputOutput
5 6
1 2 3 4 5
6 7 8 9 10
0 1
0 2
2 3
2 4
2 3 0
2 11 0
2 6 0
1 10 2 4
1 5 3 4
1 3 4 3
3
0
5
1
3
1

Submit

Login to submit.

Statistics

31% Solution Ratio
IOI_StfuFfsEarliest, Mar '18
IOI_StfuFfsFastest, 663974.8s
Tanzir5Lightest, 52 MB
IOI_StfuFfsShortest, 2644B
Toph uses cookies. By continuing you agree to our Cookie Policy.