PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000076. Truy vấn hai miền giá trị (Range query)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Truy vấn hai miền giá trị (Range query)

Range query

Source: MarisaOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Each query (l,r,a,b)(l,r,a,b) requests two counts among positions l≤i≤rl\le i\le r whose values satisfy a≤Ai≤ba\le A_i\le b: (1) the number of positions; (2) the number of distinct values.

Input

Read n,qn,q, the array and qq quadruples l,r,a,bl,r,a,b.

Output

Print matching_positions distinct_matching_values for each query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤501\le n\le 50, 1≤q≤601\le q\le 60. 1≤Ai,a,b≤1051\le A_i,a,b\le10^5; 1≤l≤r≤n1\le l\le r\le n; a≤ba\le b.
  • Subtask 2 (20 points): 1≤n≤30001\le n\le 3000, 1≤q≤30001\le q\le 3000. 1≤Ai,a,b≤1051\le A_i,a,b\le10^5; 1≤l≤r≤n1\le l\le r\le n; a≤ba\le b.
  • Subtask 3 (30 points): 1≤n≤250001\le n\le 25000, 1≤q≤250001\le q\le 25000. 1≤Ai,a,b≤1051\le A_i,a,b\le10^5; 1≤l≤r≤n1\le l\le r\le n; a≤ba\le b.
  • Subtask 4 (40 points): 1≤n≤1000001\le n\le 100000, 1≤q≤1000001\le q\le 100000. 1≤Ai,a,b≤1051\le A_i,a,b\le10^5; 1≤l≤r≤n1\le l\le r\le n; a≤ba\le b.

Examples

Input

3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3

Output

2 2
1 1
3 2
2 1

Explanation

The array is [1,2,2][1,2,2]. The first query selects two positions with values 1 and 2, so both counts are 2. In the last query, positions 2 and 3 contain two copies of value 2: two positions but only one distinct value.