Under construction

Still being built. Things may change

Sequence and Queries 44

Time limit1sMemory limit1024 MB

Summary
For each query m k, find the longest subsequence of the first m elements whose longest increasing subsequence has length at most k.
Level

Hard8 of 10

Topics
Segment tree, Dynamic programming
Solved
No attempts yet

Problem

Given a sequence A1,A2,…,ANA_1, A_2, \ldots, A_N of length NN, write a program that processes the following query.

  • m k: Print the maximum length, among the subsequences of A1,A2,…,AmA_1, A_2, \ldots, A_m whose longest increasing subsequence has length at most kk. (1≤k≤m≤N1 \le k \le m \le N)

Input

The first line contains NN and QQ, the length of the sequence and the number of queries. (1≤N≤500001 \le N \le 50000, 1≤Q≤2000001 \le Q \le 200000)

The second line contains A1,A2,…,ANA_1, A_2, \ldots, A_N. (1≤Ai≤500001 \le A_i \le 50000)

Each of the next QQ lines contains a query as described above.

Output

For each query, print the answer on its own line.

Examples1

  1. Example 1

    Input
    11 6
    9 6 3 1 5 12 8 4 2 2 2
    5 1
    7 2
    9 1
    9 2
    11 1
    11 11
    
    Expected output
    4
    6
    5
    8
    7
    11