Stock Exchange
Time limit7sMemory limit32 MB
Answer m online queries, each counting prices in a day range that fall within a decoded value range.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Segment tree, Sorting, Binary search
- Solved
- No attempts yet
Problem
Professor G. Reedy is writing a program to help him make money buying and selling shares on a stock exchange. He is interested in shares of a company called Noway, and he believes the key to success is a careful study of the exchange's history. He observed share prices for days; on the -th day a Noway share was worth dollars (). Assume that all prices are distinct.
The professor wants to run queries on this data. A query has the form and asks: among the days from the -th to the -th (inclusive), how many had a share price between and dollars (inclusive)?
The queries are given in an encoded (online) form. For the -th query () you are given four integers , , , . You must compute , the answer to the query , where is the answer to the previous query and . Because each query depends on the previous answer, the queries must be processed in order.
Write a program that reads the price history and the queries from standard input, computes each answer, and writes the answers to standard output.
Input
The first line contains two integers and (, ) separated by a space. Each of the next lines contains one integer (), the share price on day . Each of the following lines contains four integers , , , ( and ) separated by spaces. The given and may themselves be non-positive; only the decoded bounds and are guaranteed to lie in .
Output
Print lines. The -th line must contain the single integer , the answer to the -th (decoded) query.