Pilot
Time limit1sMemory limit512 MB
For each of Q altitude limits, count subarrays of heights whose maximum is at most that limit.
- Level
Medium7 of 10
- Topics
- Stack, Sorting, Prefix sum, Array
- Solved
- No attempts yet
Problem
Rar the Cat has finally fulfilled his childhood dream of becoming a pilot, and wants to take his friend Dinosaur on a few scenic flights. Rar lives on a linear world, which can be described as a series of N integers, where the ith integer is the height of the ith mountain from the leftmost edge of his world.
For example, the world described by , looks like this:

Rar has a total of Q planes that he wants to show off, where the ith plane has a maximum cruising altitude of metres. Each flight starts at the sth mountain and ends at the eth mountain. We may assume that , i.e. Rar always flies toward the right. Since each of his planes has a maximum cruising altitude, he cannot fly across, take off from, or land on a mountain whose height is greater than its cruising altitude, i.e. Rar can fly over the ith mountain with the jth plane only if .
For the ith plane, help Rar determine the total number of different flights he can take, i.e. the total number of pairs (s, e) such that and no mountain between s and e inclusive has height greater than .
Input
Your program must read from standard input.
The first line of input contains two integers, N and Q.
The second line of input contains N integers, .
The third line of input contains Q integers, .
Output
Your program must print to standard output.
The output contains Q lines with one integer each, where the number on the ith line is the total number of different flights Rar can take with his ith plane.