This page is still under construction.

Parts of this page are still being built. What you see may change.

Stock Exchange

Time limit7sMemory limit32 MB

Summary
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 nn days; on the ii-th day a Noway share was worth pip_i dollars (1≤i≤n1 \le i \le n). Assume that all prices are distinct.

The professor wants to run mm queries on this data. A query has the form ⟨b,e,l,u⟩\langle b, e, l, u \rangle and asks: among the days from the bb-th to the ee-th (inclusive), how many had a share price between ll and uu dollars (inclusive)?

The queries are given in an encoded (online) form. For the ii-th query (1≤i≤m1 \le i \le m) you are given four integers bib_i, eie_i, lil_i, uiu_i. You must compute sis_i, the answer to the query ⟨bi,ei,li+si−1,ui+si−1⟩\langle b_i, e_i, l_i + s_{i-1}, u_i + s_{i-1} \rangle, where si−1s_{i-1} is the answer to the previous query and s0=0s_0 = 0. 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 nn and mm (1≤n≤100,0001 \le n \le 100{,}000, 1≤m≤1,000,0001 \le m \le 1{,}000{,}000) separated by a space. Each of the next nn lines contains one integer pip_i (1≤pi≤1091 \le p_i \le 10^9), the share price on day ii. Each of the following mm lines contains four integers bib_i, eie_i, lil_i, uiu_i (1≤bi≤ei≤n1 \le b_i \le e_i \le n and 1≤li+si−1≤ui+si−1≤1091 \le l_i + s_{i-1} \le u_i + s_{i-1} \le 10^9) separated by spaces. The given lil_i and uiu_i may themselves be non-positive; only the decoded bounds li+si−1l_i + s_{i-1} and ui+si−1u_i + s_{i-1} are guaranteed to lie in [1,109][1, 10^9].

Output

Print mm lines. The ii-th line must contain the single integer sis_i, the answer to the ii-th (decoded) query.

Examples4

  1. Example 1

    Input
    5 4
    17
    3
    5
    94
    8
    1 5 4 113
    3 4 -2 0
    2 5 2 93
    2 2 0 0
    
    Expected output
    4
    0
    3
    1
    
  2. Example 2

    Input
    1 1
    42
    1 1 1 100
    
    Expected output
    1
    
  3. Example 3

    Input
    1 3
    10
    1 1 5 15
    1 1 9 20
    1 1 10 10
    
    Expected output
    1
    1
    0
    
  4. Example 4

    Input
    5 1
    7
    3
    9
    1
    5
    1 5 1 1000000000
    
    Expected output
    5