QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#8749. Trade

统计

The school where Xiao Z lives is like a chain: very long in diameter and very narrow in width.

Specifically, there is a sequence of length $n$, where each position has an attribute $a_i \in \{0, 1\}$ and a type $c_i$. Various trade events occur here.

Xiao Z moves through this sequence from left to right. If they encounter a node with attribute $0$, Xiao Z can purchase at most one item of type $c_i$. If they encounter a node with attribute $1$, Xiao Z can sell at most one item of type $c_i$. Obviously, they cannot sell an item of a certain type if they do not currently possess one.

A valid trade is defined as buying an item at some node and selling it at some later node. Note that you must ensure Xiao Z does not hold any items at the end.

Given $q$ queries, each asking for the maximum number of valid trades Xiao Z can make while walking from $l_i$ to $r_i$ in order.

For each query, given an interval, enumerate the elements in the interval in order. When encountering an attribute $0$, purchase an item of that color; when encountering an attribute $1$, sell an item of that color. The value is defined as the number of successful sales (a sale is successful if the current number of items of that color purchased minus the number sold is $\ge 1$).

Input

Read data from standard input.

The first line contains two positive integers $n$ and $q$.

The next line contains $n$ integers, where the $i$-th integer represents $a_i$.

The next line contains $n$ integers, where the $i$-th integer represents $c_i$.

The next $q$ lines each contain two integers representing $l_i$ and $r_i$.

Output

Output to standard output.

Output $q$ lines, each containing a single integer representing the maximum number of valid trades for the current query.

Examples

Input 1

10 5
1 1 0 0 0 0 0 1 1 1 
1 1 1 1 1 1 1 1 1 1 
4 6
2 4
2 6
7 10
4 7

Output 1

0
0
0
1
0

Subtasks

For all data, $1 \le n, q \le 5 \times 10^5$, $1 \le c_i \le n$, $1 \le l_i \le r_i \le n$, and $a_i \in \{0, 1\}$.

Note

Please pay attention to input and output efficiency.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.