Under construction

Still being built. Things may change

Tractor Paths

Time limit4sMemory limit1024 MB

Summary
Given N intervals with sorted endpoints, answer queries for the shortest path length between two tractors and count special tractors on some shortest path.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming
Solved
No attempts yet

문제

Farmer John has NN (2≤N≤2⋅1052\le N\le 2\cdot 10^5) tractors. Tractor ii can only be used within the closed interval [ℓi,ri][\ell_i, r_i]. The left endpoints satisfy ℓ1<ℓ2<⋯<ℓN\ell_1<\ell_2<\dots<\ell_N, and the right endpoints satisfy r1<r2<⋯<rNr_1<r_2<\dots<r_N. Some of the tractors are special. Two tractors ii and jj are adjacent if [ℓi,ri][\ell_i, r_i] and [ℓj,rj][\ell_j, r_j] intersect. Farmer John can transfer from one tractor to any adjacent tractor. A path between tractors aa and bb is a sequence of transfers that starts at aa, ends at bb, and has every pair of consecutive tractors adjacent. It is guaranteed that a path exists between tractor 1 and tractor NN. The length of a path is the number of transfers, which equals the number of tractors in it minus one. You are given QQ (1≤Q≤2⋅1051\le Q\le 2\cdot 10^5) queries. Each query gives a pair of tractors aa and bb (1≤a<b≤N1\le a<b\le N).

입력

The first line contains NN and QQ. The next line contains a string of length 2N2N made of L and R, giving the left and right endpoints in sorted order. It is guaranteed that every proper prefix of this string has more Ls than Rs. The next line contains a bit string of length NN, where each bit says whether the corresponding tractor is special. The next QQ lines each contain two integers aa and bb, specifying a query.

출력

For each query, print two integers separated by a space. The first is the length of any shortest path from tractor aa to tractor bb. The second is the number of special tractors that lie on at least one shortest path from tractor aa to tractor bb.

Hint

The 88 tractor intervals, in order, are [1,5][1, 5], [2,10][2, 10], [3,11][3, 11], [4,12][4, 12], [6,13][6, 13], [7,14][7, 14], [8,15][8, 15], [9,16][9, 16]. For the 4th query, there are three shortest paths between tractor 1 and tractor 5: 1 to 2 to 5, 1 to 3 to 5, and 1 to 4 to 5. All of them have length 2. Tractors 1, 2, 3, 4, and 5 each lie on one of these shortest paths. Since tractors 1, 2, 4, and 5 are special, there are 4 special tractors that lie on at least one shortest path from tractor 1 to tractor 5.

Examples1

  1. Example 1

    Input
    8 10
    LLLLRLLLLRRRRRRR
    11011010
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    1 8
    2 3
    2 4
    2 5
    
    Expected output
    1 2
    1 1
    1 2
    2 4
    2 3
    2 4
    2 3
    1 1
    1 2
    1 2