#abc365g. G - AtCoder Office

G - AtCoder Office

Score : 575575 points

问题陈述

NN 个人在AtCoder办公室工作。

办公室记录了进出情况,自记录开始以来共有MM次进出。

ii次(1iM1 \leq i \leq M)记录由一对整数(Ti,Pi)(T_i, P_i)表示,表示在时间TiT_i,第PiP_i个人要么进入办公室(如果他们在外面),要么离开办公室(如果他们在里面)。

已知所有人员在记录开始时都在办公室外面,现在他们也都在外面。

以以下格式回答QQ个查询。

对于第ii个(1iQ1 \leq i \leq Q)查询,你将得到一对整数(Ai,Bi)(A_i, B_i)。找出自记录开始以来,第AiA_i个人和第BiB_i个人同时在办公室内的时间段的总长度。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

NN people work at the AtCoder office.

The office keeps records of entries and exits, and there have been MM entries and exits since the records began.

The ii-th (1iM)(1\leq i\leq M) record is represented by a pair of integers (Ti,Pi)(T_i, P_i), indicating that at time TiT_i, the PiP_i-th person either entered the office if they were outside, or exited the office if they were inside.

It is known that all people were outside the office at the beginning of the records, and they are outside now.

Answer QQ queries in the following format.

For the ii-th (1iQ)(1\leq i\leq Q) query, you are given a pair of integers (Ai,Bi)(A_i, B_i). Find the total length of the periods during which both the AiA_i-th and BiB_i-th persons were inside the office since the records began.

Constraints

  • 2N2×1052\leq N\leq2\times10^5
  • 2M2×1052\leq M\leq2\times10^5
  • 1T1<T2<<TM1091\leq T_1\lt T_2\lt\dotsb\lt T_M\leq10^9
  • 1PiN (1iM)1\leq P_i\leq N\ (1\leq i\leq M)
  • For every 1pN1\leq p\leq N, the number of indices ii such that Pi=pP_i=p is even.
  • 1Q2×1051\leq Q\leq2\times10^5
  • 1Ai<BiN (1iQ)1\leq A_i\lt B_i\leq N\ (1\leq i\leq Q)
  • All inputs are integers.

Input

The input is given from Standard Input in the following format:

NN MM

T1T_1 P1P_1

T2T_2 P2P_2

\vdots

TMT_M PMP_M

QQ

A1A_1 B1B_1

A2A_2 B2B_2

\vdots

AQA_Q BQB_Q

Output

Print QQ lines. The ii-th line (1iQ)(1\leq i\leq Q) should contain the answer to the ii-th query.

Sample Input 1

3 8
10 1
20 2
30 1
40 3
60 3
70 1
80 2
90 1
3
1 2
1 3
2 3

Sample Output 1

20
0
20

The following diagram shows the time each of the three people spent inside the office.

The answers to each query are as follows:

  • The 1st and 2nd persons were both inside the office from time 2020 to time 3030 and from time 7070 to time 8080. The lengths of these two periods are both 1010, so print the total, which is 2020.
  • The 1st and 3rd persons were never inside the office at the same time, so print 00.
  • The 2nd and 3rd persons were both inside the office from time 4040 to time 6060. The length of this period is 2020, so print 2020.

Sample Input 2

10 20
10257 9
10490 4
19335 1
25893 5
32538 9
33433 3
38522 9
40629 9
42896 5
52106 1
53024 3
55610 5
56721 9
58286 9
63128 3
70513 3
70977 4
74936 5
79883 9
95116 9
7
1 3
3 9
1 9
4 9
1 5
5 9
3 5

Sample Output 2

18673
2107
15310
25720
17003
10317
16848