Captivating process

시간 제한4초메모리 제한1024 MB

요약
1..N에서 정의된 두 함수 f와 g가 매분 두 수를 각각 f, g로 옮길 때, 각 질의 (x, y)에 대해 두 수가 언젠가 같아지는지 판정한다.
난이도

어려움10점 중 8점

유형
그래프, 이분 탐색, 누적 합, 수학
정답자
아직 제출이 없습니다

문제

Yulia wrote the number xx on the blackboard, and Zakhar wrote yy. The kids are bored and have come up with an extremely captivating activity. Once every minute, they erase their numbers simultaneously and write new numbers instead. Yulia writes new numbers according to the following rule: if her number equaled ii, it is substituted by f_if\_i. Zakhar does the same, but the rule is different: if his number equaled ii, it is substituted by g_ig\_i.

They will stop when their numbers match. This coould happen right away (if x=yx=y), or later, or maybe never. Your task is to determine for different values of xx and yy, if the kids will ever end writing out numbers.

입력

The first line contains two integers: NN and QQ (1≤N,Q≤1051\leq N,Q\leq 10^5).

The second line contains NN numbers separated by spaces: f_1,…,f_Nf\_1,\ldots,f\_N.

The third line contains numbers in the same format: g_1,…,g_Ng\_1,\ldots,g\_N.

In the jjth of the following QQ lines there are the initial numbers x_jx\_j and y_jy\_j.

It is guaranteed that the numbers f_if\_i, g_ig\_i, x_jx\_j, y_jy\_j are all integers and fall within the range of 11 through NN.

출력

Print QQ lines: in the jjth line, print YES, if the process that started from the numbers x_jx\_j and y_jy\_j, ends, and NO otherwise.

예제2

  1. 예제 1

    입력
    3 2
    2 3 1
    2 3 1
    1 2
    1 1
    
    예상 출력
    NO
    YES
    
  2. 예제 2

    입력
    4 2
    2 3 4 2
    2 4 4 1
    1 2
    1 4
    
    예상 출력
    NO
    YES