#d1602. 丹钓战

丹钓战

Dandiao Stack Game

Problem Description

There are nn pairs (ai,bi)(a_i, b_i), numbered from 11 to nn.

There is an initially empty stack SS. When inserting a pair (ai,bi)(a_i, b_i), first keep popping the top element until the stack is empty or the top element (aj,bj)(a_j, b_j) satisfies ai≠aja_i \neq a_j and bi<bjb_i < b_j, then push (ai,bi)(a_i, b_i) onto the stack.

A pair is called "successful" if, after being pushed, it is the only element in the stack.

There are qq queries [li,ri][l_i, r_i]. If the pairs with indices in [li,ri][l_i, r_i] are pushed onto the stack in increasing index order, how many of them are "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, the values aia_i.

The third line contains nn positive integers, the values bib_i.

The following qq lines each contain two positive integers li,ril_i, r_i, describing one query.

Output Format

qq lines, each containing a non-negative integer: the answer to the corresponding query.

Sample Input

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

Sample Output

3
2
2
3

Sample Explanation

Take the first query [1,4][1, 4] as an example.

Initially the stack is {}\{\}.

After pushing pair 11, the stack is {(3,10)}\{(3, 10)\}; it has only one element, so this pair is "successful".

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

When pushing pair 33 (3,2)(3, 2), the top element has a different aa value and a larger bb value, so no popping is needed; pair 33 is pushed directly, the stack becomes {(1,10),(3,2)}\{(1, 10),(3, 2)\}, which has multiple elements, so this pair is not "successful".

When pushing pair 44 (1,9)(1, 9), the bb value of the top element (3,2)(3, 2) is smaller, so we pop. 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; pair 44 is pushed, the stack becomes {(1,9)}\{(1, 9)\}, so this pair is "successful". In total 33 pairs are "successful", hence the answer is 33.

Constraints

$1 \leq n, q \leq 5 \times 10^5, 1 \leq a_i, b_i \leq n, 1 \leq l_i \leq r_i \leq n$.