#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 nn ordered pairs (ai,bi)(a_i, b_i), numbered from 11 to nn.

There is an initially empty stack SS. When inserting an element (ai,bi)(a_i, b_i) into it, repeatedly pop the top element until the stack is empty or the top element (aj,bj)(a_j, b_j) satisfies aiaja_i \neq a_j and bi<bjb_i < b_j, and then push (ai,bi)(a_i, b_i) 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 qq queries [li,ri][l_i, r_i]. For each query, if we push the ordered pairs with indices in [li,ri][l_i, r_i] 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 n,qn, q.

The second line contains nn positive integers representing aia_i.

The third line contains nn positive integers representing bib_i.

The next qq lines each contain two positive integers li,ril_i, r_i, representing one query.

Output Format

Output qq 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 [1,4][1, 4] as an example.

Initially, the stack is {}\{\}.

After pushing ordered pair 11, the stack is {(3,10)}\{(3, 10)\}. There is only one element in the stack, so this ordered pair is “successful”.

When pushing ordered pair 22 (1,10)(1, 10), the bb value of the top element (3,10)(3, 10) is not greater than that of ordered pair 22, so we pop it. Now the stack is empty, so ordered pair 22 is pushed, the stack becomes {(1,10)}\{(1, 10)\}, and this ordered pair is “successful”.

Push ordered pair 33 (3,2)(3, 2). Now the top element has a different aa value from it, and has a larger bb value than it, so no popping is needed. Push ordered pair 33 directly, and the stack becomes {(1,10),(3,2)}\{(1, 10), (3, 2)\}. The stack has multiple elements, so this ordered pair is not “successful”.

Push ordered pair 44 (1,9)(1, 9). Now the top element (3,2)(3, 2) has a smaller bb value than it, so we pop it. After popping, the top element (1,10)(1, 10) has the same aa value as (1,9)(1, 9), so we continue popping. Now the stack is empty, so ordered pair 44 is pushed, the stack becomes {(1,9)}\{(1, 9)\}, and this ordered pair is “successful”. In total, there are 33 “successful” ordered pairs, so the answer is 33.

[Samples 2, 3, 4]

See the attachments stack/stack*.in\texttt{stack/stack*.in} and stack/stack*.ans\texttt{stack/stack*.ans}.

Link: https://pan.baidu.com/s/14XxLN63bxvpJAod81foGOg Extraction code: gugu.

[Constraints and Hints]

For all test points: 1n,q5×1051 \leq n, q \leq 5 \times 10^5, 1ai,bin1 \leq a_i, b_i \leq n, 1lirin1 \leq l_i \leq r_i \leq n.

The specific limits for each test point are shown in the table below:

Test Point ID Special Property
131 \sim 3 n,q1000n, q \leq 1000
464 \sim 6 n5000n \leq 5000
7107 \sim 10 n,q105n, q \leq 10^5
111211 \sim 12 bi=ni+1b_i = n - i + 1
131513 \sim 15 ai=ia_i = i
162016 \sim 20 None

Translated by ChatGPT 5