Under construction

Still being built. Things may change

Captivating process

Time limit4sMemory limit1024 MB

Summary
Given functions f and g on 1 to N, for each query (x, y) decide whether applying both functions in lockstep ever makes the two values equal.
Level

Hard8 of 10

Topics
Graph, Number theory, Math
Solved
No attempts yet

Problem

Yulia wrote the number xx on the blackboard, and Zakhar wrote yy. Once every minute, they erase their numbers at the same time and write new numbers. If Yulia's number is ii, it becomes fif_i. If Zakhar's number is ii, it becomes gig_i.

They stop when their numbers match. This can happen right away if x=yx=y, later, or never. For each pair xx and yy, determine whether the process ever ends.

Input

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

The second line contains NN integers f1,…,fNf_1, \ldots, f_N.

The third line contains NN integers g1,…,gNg_1, \ldots, g_N.

Each of the following QQ lines contains the starting numbers xjx_j and yjy_j. All values are integers from 1 to NN.

Output

For each query jj, print YES if the process starting from xjx_j and yjy_j ends, and NO otherwise.

Examples2

  1. Example 1

    Input
    3 2
    2 3 1
    2 3 1
    1 2
    1 1
    
    Expected output
    NO
    YES
    
  2. Example 2

    Input
    4 2
    2 3 4 2
    2 4 4 1
    1 2
    1 4
    
    Expected output
    NO
    YES