This page is still under construction.

Parts of this page are still being built. What you see may change.

Fixing the Path

Time limit1sMemory limit1024 MB

Summary
Given a walk string of L, R, U, D moves, answer for each target point the fewest character changes so the final position equals that point.
Level

Medium7 of 10

Topics
Math, Greedy, Prefix sum, Implementation
Solved
No attempts yet

Problem

Seokpyo lives at (0,0)(0,0) on an ordinary coordinate plane where the xx-coordinate increases to the right and the yy-coordinate increases upward. He wants to go to Junho's house at (X,Y)(X,Y). Every second Seokpyo moves 1 unit horizontally or vertically, and the string SS of length NN describes his movement plan. Depending on whether the ii-th character of SS is L, R, D, U, Seokpyo moves 1 unit left, right, down, or up, respectively.

The movement plan made by the careless Seokpyo may not be correct. A movement plan is correct if Seokpyo is at (X,Y)(X,Y) after completing all the moves. Even if he reaches (X,Y)(X,Y) in the middle, the plan is not correct unless he is at (X,Y)(X,Y) at the end.

Seokpyo repeatedly changes one character of SS to another character to turn it into a correct movement plan. He went through all 4N4^N cases the hard way and found the minimum number of changes needed. But sadly, news arrived that Junho has moved. Junho has moved to one of QQ locations. The ii-th location is (Xi,Yi)(X_i,Y_i). Now Seokpyo must find the minimum number of changes needed for each of the QQ locations.

Seokpyo fainted on hearing that he must find the minimum number of changes QQ times. Let us find the answers for him.

Input

The first line contains NN and QQ separated by a space. (1≤N,Q≤300 000)(1 \leq N,Q \leq 300\,000)

The next line contains SS.

The following QQ lines contain XiX_i and YiY_i separated by a space. (∣Xi∣,∣Yi∣≤300 000)(|X_i|,|Y_i| \leq 300\,000)

Output

For each location, output -1 if it is impossible to turn the plan into a correct movement plan, or the minimum number of changes needed otherwise, each on its own line.

Examples1

  1. Example 1

    Input
    10 3
    DLRURDRLDU
    2 3
    3 3
    -4 8
    
    Expected output
    -1
    3
    -1