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

#BS0000342. Sói tìm kiếm trên hoán vị (Wolf)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Sói tìm kiếm trên hoán vị (Wolf)

Wolf

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given a permutation pp of 1,…,n1,\ldots,n, a query (l,r,k)(l,r,k) runs binary search even though the range may be unsorted. An empty range fails. At midpoint m=⌊(l+r)/2⌋m=\lfloor(l+r)/2\rfloor, equality succeeds, a value smaller than k moves right, and a larger value moves left. Before a query you may select d distinct positions anywhere in the permutation, none holding k, and rearrange the selected values. Minimize d so the search succeeds, or report -1. Each query starts with the original permutation.

Input

First line: n,qn,q. Second line: permutation pp. The next qq lines each contain l,r,kl,r,k.

Output

Print one integer per query, each on a new line.

Subtasks

  • Subtask 1 (20 points): 1≤n≤71\le n\le7; 1≤q≤81\le q\le8; pp is a permutation of 1 through nn; 1≤l≤r≤n1\le l\le r\le n; 1≤k≤n1\le k\le n.
  • Subtask 2 (30 points): 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; pi=ip_i=i; 1≤l≤r≤n1\le l\le r\le n; 1≤k≤n1\le k\le n.
  • Subtask 3 (50 points): 1≤n,q≤2⋅1051\le n,q\le2\cdot10^5; pp is a permutation of 1 through nn; 1≤l≤r≤n1\le l\le r\le n; 1≤k≤n1\le k\le n.

Examples

Input 1

7 4
3 1 5 2 7 6 4
3 4 2
2 3 5
1 5 6
1 7 3

Output 1

2
0
-1
4

Explanation

Query 1 swaps positions 2 and 3. Query 2 already succeeds. Query 3 excludes the position of 6. Query 4 requires larger values at positions 4 and 2, both initially too small; two donor positions are also necessary.