Processing math: 100%
Apple trees 3 - MarisaOJ: Marisa Online Judge

Apple trees 3

Time limit: 1000 ms
Memory limit: 256 MB
Marisa planted n apple trees in a row, the ith tree product apples of size Ai. There are q customers who wish to purchase apples. Each customer, denoted by the index i, has a specific requirement of purchasing ai apples within a given size range, [li,ri]. Additionally, each customer starts purchasing apples from tree xi. Whenever a customer encounters an apple tree that offers apples within their desired size range, they will purchase one apple from that tree. Once a customer has bought the required number of apples, they will stop purchasing. Help Marisa determine for each customer, at which tree will he stop. ### Input - The first line contains two integers n,q. - The second line contains n integer Ai. - The next q lines, each line four integers li,ri,xi,ai. ### Output - Print q integers, the ith integer is the tree at which customer i will stop. In case the customer cannot buy enough apples, print -1. ### Constraints - 1≤n,q≤105. - 1≤Ai≤105. - 1≤li≤ri≤105. - 1≤xi,ai≤n. ### Example Input: 910106610667931014919123151852131792273261422512423113171151151952 Output: -146-153537-1
Topic
Binary search
Data structure
Rating 1800
Solution (1) Solution