DP (Large)
InterviewTime limit4sMemory limit1024 MB
For each queried problem, find the longest subsequence of problems whose numbers and difficulties both strictly increase and that includes it.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Binary search
- Solved
- No attempts yet
Problem
This problem differs from DP (Small) in the limits on the number of problems, the number of queries, and the difficulty range.
DP is short for DYNAMIC Porani.
A DYNAMIC Porani likes to solve the problems she can solve in a DYNAMIC way. A DYNAMIC solution means solving as many problems as possible so that both the problem number and the difficulty strictly increase.
Over break, Porani received a set of recommended problems from her senior to improve her algorithm skills. Being lazy, she does not want to solve every problem. When a senior tells her to solve a certain problem, tell her how many problems she must solve in a DYNAMIC way that includes that problem.
Input
The first line contains two integers and (, ), separated by a space.
The second line contains non-negative integers (; ), the difficulty of each problem in order of problem number, separated by spaces. Problem numbers are consecutive positive integers starting from .
From the third line, lines follow, each containing a problem number (; ) that a senior asked Porani to solve. The problem numbers in the queries are distinct.
Output
Print lines. On line , print the number of problems Porani must solve for the -th senior instruction, in order.