#P11763. [IAMOI R1] 家庭矛盾

[IAMOI R1] 家庭矛盾

Description

Each day that Xiao L and Xiao C spend together can be represented by a tuple (ai,ci)(a_i, c_i). Here, cic_i is either 00 or 11, indicating whether Xiao L and Xiao C had a quarrel on that day. aia_i is an integer representing Xiao C's mood value on that day.

Xiao L and Xiao C are simultaneously engaged in mm family wars. The kk-th war starts on the lkl_k-th day.

Since their wars are very intense, you do not know on which day they will end. However, if the war ends on the rr-th day, and the number of quarrel days from the lkl_k-th day to the rr-th day is exactly dkd_k, then this war will bring Xiao C a certain amount of resentment value. The resentment value is the number of inversion pairs in Xiao C's mood values from the lkl_k-th day to the rr-th day. Otherwise, Xiao C's resentment value is 00.

Now, Xiao L has provided you with the (ai,ci)(a_i, c_i) for all nn days and the lkl_k and dkd_k for the mm wars. For each war, Xiao L wants to know the sum of Xiao C's resentment values over all possible ending times, so he can try to mend their relationship. He will be very grateful for your help and reward you with 7420738134810mod126737420738134810 \bmod 12673 yuan as thanks.

Formal Description

You have nn tuples (ai,ci)(a_{i}, c_{i}), where ci{0,1}c_{i} \in \{0,1\}.

There are mm queries. Each query provides two integers lkl_{k} and dkd_{k}. Compute:

$$\sum_{r=l_{k}}^{n} \left[\left(\sum_{i=l_{k}}^{r} c_{i}\right)=d_{k}\right] \sum_{i=l_{k}}^{r} \sum_{j=i+1}^{r} [a_{j}<a_{i}]$$

Here, the expression in square brackets evaluates to 11 if true, and 00 otherwise.

Input Format

The first line contains an integer nn, the length of the sequence.

The next nn lines each contain two integers aia_{i} and cic_{i}, describing the tuple for the ii-th day.

The next line contains an integer mm, the number of wars.

The next mm lines each contain two integers lkl_{k} and dkd_{k}, representing a war.

Output Format

Output mm lines, each indicating the sum of resentment values for all possible ending times in the corresponding war.

4
4 0
5 1
3 0
2 1
2
3 1
1 2
1
5

Hint

Sample Explanation 1

For the first query, only the interval [3,4][3,4] meets the condition, so the answer is 11.

For the second query, only the interval [1,4][1,4] meets the condition, so the answer is 55.

Data Range

Test Case ID n,mn,m \leq Special Properties
11 100100 None
2,32,3 10310^3
4,54,5 10510^5 A
686\sim8 B
9,109,10 CD
11,1211,12 5×1045\times10^4 C
13,1413,14 10510^5
151715\sim17 D
182018\sim20 5×1045\times10^4 None
212521\sim25 10510^5

Special Property A: All ci=0c_i=0.

Special Property B: All dk=1d_{k}=1.

Special Property C: All ci=1c_i=1.

Special Property D: For all queries, k<m\forall k<m, lklk+1l_{k} \le l_{k+1} and dkdk+1d_{k} \le d_{k+1}.

For 100%100\% of the data: 1n,m1051\le n,m\le10^5, 1lkn1\le l_k\le n, 1ai1091\le a_i\le10^9, ci{0,1}c_{i} \in \{0,1\}, 0dk1090\le d_k\le 10^9.

The time and memory limits for this problem are at least twice those of the standard solution.

Translation by DeepSeek R1