#P11763. [IAMOI R1] 家庭矛盾
[IAMOI R1] 家庭矛盾
Description
Each day that Xiao L and Xiao C spend together can be represented by a tuple . Here, is either or , indicating whether Xiao L and Xiao C had a quarrel on that day. is an integer representing Xiao C's mood value on that day.
Xiao L and Xiao C are simultaneously engaged in family wars. The -th war starts on the -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 -th day, and the number of quarrel days from the -th day to the -th day is exactly , 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 -th day to the -th day. Otherwise, Xiao C's resentment value is .
Now, Xiao L has provided you with the for all days and the and for the 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 yuan as thanks.
Formal Description
You have tuples , where .
There are queries. Each query provides two integers and . 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 if true, and otherwise.
Input Format
The first line contains an integer , the length of the sequence.
The next lines each contain two integers and , describing the tuple for the -th day.
The next line contains an integer , the number of wars.
The next lines each contain two integers and , representing a war.
Output Format
Output 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 meets the condition, so the answer is .
For the second query, only the interval meets the condition, so the answer is .
Data Range
| Test Case ID | Special Properties | |
|---|---|---|
| None | ||
| A | ||
| B | ||
| CD | ||
| C | ||
| D | ||
| None | ||
Special Property A: All .
Special Property B: All .
Special Property C: All .
Special Property D: For all queries, , and .
For of the data: , , , , .
The time and memory limits for this problem are at least twice those of the standard solution.
Translation by DeepSeek R1
京公网安备 11011102002149号