#P10540. [THUPC 2024 决赛] 古明地枣的袜子

[THUPC 2024 决赛] 古明地枣的袜子

Problem Description

You need to maintain a sequence a1,,ana_1,\dots,a_n.

You are given an operation sequence (x1,y1),,(xn,yn)(x_1,y_1),\dots,(x_n,y_n). An operation (x,y)(x,y) means adding yy to the values of a1,,axa_1,\dots,a_x.

There are mm queries. Each query gives l,rl,r and asks: starting from an initial sequence aa where all values are 00, execute the operations (xl,yl),,(xr,yr)(x_l,y_l),\dots,(x_r,y_r) in order. What is the final value of maxi=1nai\max\limits_{i=1}^n a_i?

Input Format

The first line contains two integers n,mn,m (1n,m5×1051\le n,m\le 5\times 10^5).

The next nn lines each contain two integers xi,yix_i,y_i (1xin,yin1\le x_i\le n, |y_i|\le n).

The next mm lines each contain two integers l,rl,r (1lrn1\le l\le r\le n).

Output Format

Output mm lines, each containing one integer, which is the answer to each query.

6 5
6 4
2 6
5 -5
3 6
1 2
3 6
1 6
1 6
2 6
2 6
5 6

19
19
15
15
8

Hint

Source and Acknowledgements

From the THUPC2024 (2024 Tsinghua University Student Programming Contest and Intercollegiate Invitational) finals.

For testdata, statement, reference solution, editorial, etc., please refer to the official THUPC repository https://thusaac.com/public.

Translated by ChatGPT 5