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

#BS0000210. Truy vấn dãy con tăng trên tiền tố (Prefix LIS Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn dãy con tăng trên tiền tố (Prefix LIS Query)

Prefix LIS Query

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

An integer sequence a1,a2,…,ana_1,a_2,\ldots,a_n and qq queries are given. Query jj specifies a prefix ending at position rjr_j and an upper value bound xjx_j. Choose positions 1≤i1<i2<⋯<ik≤rj1\le i_1<i_2<\cdots<i_k\le r_j such that ai1<ai2<⋯<aik≤xja_{i_1}<a_{i_2}<\cdots<a_{i_k}\le x_j. The positions need not be consecutive. Find the maximum kk for each query independently; queries never change the sequence. At least one element of the prefix is at most xjx_j.

Input

The first line contains integers n,qn,q, the sequence length and query count. The second line contains nn integers a1,…,ana_1,\ldots,a_n. Each of the next qq lines contains the two integers rj,xjr_j,x_j of query jj.

Output

Print qq lines, with the maximum length for query jj on line jj.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤401\le n,q\le 40; 1≤ai≤1091\le a_i\le 10^9 (1≤i≤n1\le i\le n); 1≤rj≤n1\le r_j\le n; min⁡(a1,…,arj)≤xj≤109\min(a_1,\ldots,a_{r_j})\le x_j\le 10^9 (1≤j≤q1\le j\le q).
  • Subtask 2 (30 points): 1≤n,q≤20001\le n,q\le 2000; 1≤ai≤1091\le a_i\le 10^9 (1≤i≤n1\le i\le n); 1≤rj≤n1\le r_j\le n; min⁡(a1,…,arj)≤xj≤109\min(a_1,\ldots,a_{r_j})\le x_j\le 10^9 (1≤j≤q1\le j\le q).
  • Subtask 3 (50 points): 1≤n,q≤2000001\le n,q\le 200000; 1≤ai≤1091\le a_i\le 10^9 (1≤i≤n1\le i\le n); 1≤rj≤n1\le r_j\le n; min⁡(a1,…,arj)≤xj≤109\min(a_1,\ldots,a_{r_j})\le x_j\le 10^9 (1≤j≤q1\le j\le q).

Examples

Input

5 3
2 4 1 3 3
2 5
5 2
5 3

Output

2
1
2

Explanation

The sequence is 2,4,1,3,32,4,1,3,3. Query (2,5)(2,5) permits both initial values, 2 and 4, so the length is 2. Query (5,2)(5,2) permits only 2 and 1, in decreasing order, so the best length is 1. Query (5,3)(5,3) permits 2,1,3,32,1,3,3 in that order; 1,31,3 has length 2. The two 3s cannot both be selected and 2 precedes 1, so no length 3 is possible. The answers are 2, 1 and 2.