#P15839. [蓝桥杯第一届国际赛] 莲蓬池

[蓝桥杯第一届国际赛] 莲蓬池

Problem Description

Xiao C has a huge pond where many lotus flowers are planted. In summer, many large lotus seedpods grow in the pond.

To understand how the seedpods grow, Xiao C measured the position of each seedpod. Each seedpod corresponds to a coordinate (x,y)(x, y) in the 2D Cartesian coordinate system. These coordinates form a sequence, denoted as A\mathbf{A}, where AiA_i represents the coordinate of the ii-th seedpod.

Xiao C plans to use magic to collect some of the seedpods. Because his power is limited, he can only collect all seedpods in an interval [l,r][l, r] of the sequence A\mathbf{A}. The energy required for one collection is the sum of the Manhattan distances between every pair of seedpods in that interval (where dist(a,b)=∣xa−xb∣+∣ya−yb∣dist(a,b) = |x_a - x_b| + |y_a - y_b|).

Now Xiao C gives you multiple collection plans [l,r][l, r] and hopes you can help compute the energy needed to collect the seedpods in each interval. (Each plan is independent.)

Input Format

The first line contains two positive integers nn and mm, representing the number of seedpods and the number of queries.

The next nn lines each contain two integers xix_i and yiy_i, representing the xx- and yy-coordinates of the ii-th seedpod AiA_i in the sequence.

The next mm lines each contain two integers lil_i and rir_i, representing the left and right endpoints of the interval to be collected in this query (the interval includes endpoints).

Output Format

Output mm lines. The ii-th line outputs one integer, representing the answer to the ii-th query.

5 3
1 1
3 3
2 2
1 3
3 1
2 2
2 4
1 5
0
6
24

Hint

Sample Explanation

For the first query: there is only one seedpod.

For the second query:

  • dist[(3,3),(2,2)]=2dist[(3,3), (2,2)] = 2
  • dist[(3,3),(1,3)]=2dist[(3,3), (1,3)] = 2
  • dist[(2,2),(1,3)]=2dist[(2,2), (1,3)] = 2

For the third query:

  • dist[(1,1),(3,3)]=4dist[(1,1), (3,3)] = 4
  • dist[(1,1),(2,2)]=2dist[(1,1), (2,2)] = 2
  • dist[(1,1),(1,2)]=2dist[(1,1), (1,2)] = 2
  • dist[(1,1),(3,1)]=2dist[(1,1), (3,1)] = 2
  • dist[(3,3),(2,2)]=2dist[(3,3), (2,2)] = 2
  • dist[(3,3),(1,2)]=2dist[(3,3), (1,2)] = 2
  • dist[(2,2),(1,2)]=2dist[(2,2), (1,2)] = 2
  • dist[(2,2),(3,1)]=2dist[(2,2), (3,1)] = 2
  • dist[(1,3),(3,1)]=4dist[(1,3), (3,1)] = 4

Constraints

For 10%10\% of the testdata, 1≤n,m≤101 \le n, m \le 10, ∣xi∣,∣yi∣≤10|x_i|, |y_i| \le 10.

For 20%20\% of the testdata, 1≤n,m≤10001 \le n, m \le 1000, ∣xi∣,∣yi∣≤103|x_i|, |y_i| \le 10^3.

For 40%40\% of the testdata, 1≤n≤50001 \le n \le 5000, 1≤m≤1051 \le m \le 10^5, ∣xi∣,∣yi∣≤105|x_i|, |y_i| \le 10^5.

For another 10%10\% of the testdata, 1≤n≤1051 \le n \le 10^5, 1≤m≤50001 \le m \le 5000, ∣xi∣,∣yi∣≤105|x_i|, |y_i| \le 10^5.

For all testdata, 1≤n,m≤1000001 \le n, m \le 100000, 0≤∣xi∣,∣yi∣≤1080 \le |x_i|, |y_i| \le 10^8, 1≤l≤r≤n1 \le l \le r \le n. There may be two seedpods with the same coordinates. The testdata scale has multiple levels.

Translated by ChatGPT 5