PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#LCA0000013. Tìm hoán vị vòng trong đoạn (Lynyrd Skynyrd)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tìm hoán vị vòng trong đoạn (Lynyrd Skynyrd)

Lynyrd Skynyrd

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation pp of 1,…,n1,\ldots,n and an array aa of mm values between 11 and nn.

Select positions of aa in strictly increasing order; they need not be consecutive. A subsequence of length nn is valid if its values equal a cyclic rotation of permutation pp. For example, for p=(2,1,3)p=(2,1,3), (1,3,2)(1,3,2) is also valid.

For each query interval [l,r][l,r], determine whether a valid subsequence of length exactly nn can be formed using only positions in that interval.

Input

  • The first line contains n,m,qn,m,q.
  • The second line contains permutation p1,…,pnp_1,\ldots,p_n.
  • The third line contains mm array values a1,…,ama_1,\ldots,a_m.
  • The next qq lines contain query intervals l,rl,r.

Output

Print one binary string of length qq: character ii is 1 if query ii admits a valid subsequence, otherwise 0. Do not insert spaces.

Subtasks

  • Subtask 1 (20 points): n≤30n\le30; m≤60m\le60; q≤60q\le60; 1≤n,m,q≤2⋅1051\le n,m,q\le2\cdot10^5; 1≤ai≤n1\le a_i\le n; 1≤l≤r≤m1\le l\le r\le m; pp là hoán vị.
  • Subtask 2 (30 points): n≤1000n\le1000; m≤3000m\le3000; q≤3000q\le3000; 1≤n,m,q≤2⋅1051\le n,m,q\le2\cdot10^5; 1≤ai≤n1\le a_i\le n; 1≤l≤r≤m1\le l\le r\le m; pp là hoán vị.
  • Subtask 3 (50 points): 1≤n,m,q≤2⋅1051\le n,m,q\le2\cdot10^5; 1≤ai≤n1\le a_i\le n; 1≤l≤r≤m1\le l\le r\le m; pp là hoán vị.

Examples

Input

3 6 3
2 1 3
1 2 3 1 2 3
1 5
2 6
3 5

Output

110

Explanation

Interval [1,5][1,5] contains (2,1,3)(2,1,3), and [2,6][2,6] contains (1,3,2)(1,3,2), both valid rotations. Interval [3,5][3,5] contains only (3,1,2)(3,1,2), which is not a rotation of (2,1,3)(2,1,3).