#P6774. [NOI2020] 时代的眼泪

[NOI2020] 时代的眼泪

Description

Xiao L likes to communicate and discuss with wise people, and the wise person often gives Xiao L some thinking problems.

Today the wise person came up with another problem for Xiao L. The wise person first abstracted space-time into a two-dimensional plane, then abstracted an event as a point on this plane, and abstracted an era as a rectangle on this plane.

For convenience, let (a,b)(c,d)(a, b) \leq (c, d) denote that two points (a,b)(a, b) and (c,d)(c, d) on the plane satisfy aca \leq c and bdb \leq d.

More specifically, the wise person is given nn events, represented by nn distinct points {(xi,yi)}i=1n\{(x_i, y_i)\}^n_{i=1} on the plane. The wise person is also given mm eras, each represented by a rectangle (ri,1,ri,2,ci,1,ci,2)(r_{i,1}, r_{i,2}, c_{i,1}, c_{i,2}) on the plane, where (ri,1,ci,1)(r_{i,1}, c_{i,1}) is the lower-left corner and (ri,2,ci,2)(r_{i,2}, c_{i,2}) is the upper-right corner. It is guaranteed that (ri,1,ci,1)(ri,2,ci,2)(r_{i,1}, c_{i,1}) \leq (r_{i,2}, c_{i,2}). We say that era ii contains event jj if and only if $(r_{i,1}, c_{i,1}) \leq (x_j, y_j) \leq (r_{i,2}, c_{i,2})$.

The wise person believes that if two events i,ji, j satisfy (xi,yi)(xj,yj)(x_i, y_i) \leq (x_j, y_j), then these two events form a regret. For all events contained in an era, the regrets formed by them are called the tears of this era, and the number of regrets is called the size of the tears of this era. Now the wise person wants Xiao L to compute the size of the tears for each era.

Xiao L understands that if he cannot answer this question, he will also become tears of the times. Please help him.

Input Format

Read from standard input.

The first line contains two integers n,mn, m, representing the number of events and the number of eras.

The second line contains nn integers pip_i, where the ii-th number means that event ii has coordinates (i,pi)(i, p_i) on the plane. It is guaranteed that pip_i is a permutation of 11 to nn.

Then follow mm lines, each containing four integers ri,1,ri,2,ci,1,ci,2r_{i,1}, r_{i,2}, c_{i,1}, c_{i,2}, representing the rectangle corresponding to each era.

Output Format

Output to standard output.

Output mm lines, each containing one integer. On the ii-th line, output the size of the tears of the ii-th era.

9 9
9 8 7 6 2 4 5 3 1
4 9 3 6
2 9 1 8
3 8 2 4
3 9 2 7
2 8 1 6
1 9 1 9
1 3 5 7
2 3 3 3
6 6 6 6
1
4
2
4
4
4
0
0
0

Hint

Explanation for Sample 1

For era 11, the regret it contains is (6,7)(6, 7) (that is, the regret formed by events 66 and 77, same below).

For era 22, the regrets it contains are (5,6),(6,7),(5,7),(5,8)(5, 6), (6, 7), (5, 7), (5, 8).

For era 33, the regrets it contains are (5,6),(5,8)(5, 6), (5, 8).

For era 44, the regrets it contains are (5,6),(6,7),(5,7),(5,8)(5, 6), (6, 7), (5, 7), (5, 8).

For era 55, the regrets it contains are (5,6),(6,7),(5,7),(5,8)(5, 6), (6, 7), (5, 7), (5, 8).

For era 66, the regrets it contains are (5,6),(6,7),(5,7),(5,8)(5, 6), (6, 7), (5, 7), (5, 8).

For eras 7,8,97, 8, 9, none of them contains any regret.

Sample 2

See tears/tears2.in and tears/tears2.ans in the contestant directory.

This sample satisfies Special Constraint A (see test point constraints for details).

Sample 3

See tears/tears3.in and tears/tears3.ans in the contestant directory.

This sample satisfies Special Constraint B (see test point constraints for details).

For all test points: 1n1051 \leq n \leq 10^5, 1m2×1051 \leq m \leq 2 \times 10^5, 1ri,1,ri,2,ci,1,ci,2n1 \leq r_{i,1}, r_{i,2}, c_{i,1}, c_{i,2} \leq n.


Test Point Constraints

The specific limits for each test point are shown in the table below:

Test Point ID nn\le mm\le Special Constraint
131\sim 3 1010 None
44 3×1033\times 10^3
55 4×1034\times 10^3
66 5×1035\times 10^3
77 2.5×1042.5\times 10^4 5×1045\times 10^4 A\text{A}
88 5×1045\times 10^4 10510^5
99 7.5×1047.5\times 10^4 1.5×1051.5\times 10^5
1010 10510^5 2×1052\times 10^5
1111 6×1046\times 10^4 1.2×1051.2\times 10^5 B\text{B}
1212 8×1048\times 10^4 1.6×1051.6\times 10^5
1313 10510^5 2×1052\times 10^5
1414 2×1042\times 10^4 4×1044\times 10^4 None
1515 3×1043\times 10^4 6×1046\times 10^4
1616 4×1044\times 10^4 8×1048\times 10^4
1717 5×1045\times 10^4 10510^5
1818 6×1046\times 10^4 1.2×1051.2\times 10^5
1919 7×1047\times 10^4 1.4×1051.4\times 10^5
202220\sim 22 10510^5 2×1052\times 10^5 C\text{C}
232523\sim 25 None

Special Constraint A: For all eras ii, we have ci,1=1c_{i,1} = 1 and ci,2=nc_{i,2} = n.

Special Constraint B: For any two different eras, the rectangles they represent are either in a containment relationship (one rectangle is inside the other; boundaries may overlap) or are disjoint (the two rectangles share no common point; boundaries are not allowed to touch).

Special Constraint C: There are at most 5050 pairs of events (i,j)(1i<jn)(i, j)(1 \leq i < j \leq n) that do not satisfy (i,pi)(j,pj)(i, p_i) \leq (j, p_j).

Translated by ChatGPT 5