#P8251. [NOI Online 2022 提高组] 丹钓战
[NOI Online 2022 提高组] 丹钓战
Background
After consideration by the administrators, we plan to store the unofficial testdata separately in the last subtask. The score of these test points is 0, but failing to pass any of them will be regarded as not passing this problem.
In this problem, there is an issue where the test point numbers in the judging records are in a wrong order, caused by a naming conflict of unofficial testdata. However, we guarantee that their relative order is not messed up.
Unofficial testdata provider: @Froggy.
Problem Description
There are ordered pairs , numbered from to .
There is an initially empty stack . When inserting an element into it, repeatedly pop the top element until the stack is empty or the top element satisfies and , and then push onto the stack.
If, after an ordered pair is pushed, the stack contains only this one element, then this ordered pair is called “successful”.
There are queries . For each query, if we push the ordered pairs with indices in into the stack one by one in increasing order of index, how many ordered pairs will be “successful”?
The queries are independent of each other.
Input Format
The first line contains two positive integers .
The second line contains positive integers representing .
The third line contains positive integers representing .
The next lines each contain two positive integers , representing one query.
Output Format
Output lines. Each line contains one non-negative integer representing the answer to one query.
10 4
3 1 3 1 2 3 3 2 1 1
10 10 2 9 7 5 4 7 6 1
1 4
7 8
7 10
1 8
3
2
2
3
Hint
[Sample Explanation]
Take the first query as an example.
Initially, the stack is .
After pushing ordered pair , the stack is . There is only one element in the stack, so this ordered pair is “successful”.
When pushing ordered pair , the value of the top element is not greater than that of ordered pair , so we pop it. Now the stack is empty, so ordered pair is pushed, the stack becomes , and this ordered pair is “successful”.
Push ordered pair . Now the top element has a different value from it, and has a larger value than it, so no popping is needed. Push ordered pair directly, and the stack becomes . The stack has multiple elements, so this ordered pair is not “successful”.
Push ordered pair . Now the top element has a smaller value than it, so we pop it. After popping, the top element has the same value as , so we continue popping. Now the stack is empty, so ordered pair is pushed, the stack becomes , and this ordered pair is “successful”. In total, there are “successful” ordered pairs, so the answer is .
[Samples 2, 3, 4]
See the attachments and .
Link: https://pan.baidu.com/s/14XxLN63bxvpJAod81foGOg Extraction code: gugu.
[Constraints and Hints]
For all test points: , , .
The specific limits for each test point are shown in the table below:
| Test Point ID | Special Property |
|---|---|
| None |
Translated by ChatGPT 5