QOJ.ac

QOJ

実行時間制限: 7 s メモリ制限: 512 MB 満点: 100

#4055. Integer Sequence

統計

Little D learned how to create problems when he was three years old.

Little D has a sequence of positive integers $a_1, a_2, \cdots, a_n$ and a sequence of integers $b_1, b_2, \cdots, b_n$.

Little D has $q$ queries. Each query provides $x$ and $y$. Construct a new sequence $c_1, c_2, \cdots, c_n$, where $c_i = \begin{cases}1 & a_i=x\\-1 & a_i = y\\ 0 & \mathrm{else}\end{cases}$. It is guaranteed that $c_i$ contains at least one $1$ and at least one $-1$. You need to find an interval $[l, r]$ such that $\sum\limits_{i=l}^r c_i=0$, and the value $\sum\limits^r_{i=l}b_i \times [c_i \ne 0]$ is maximized. The interval must contain at least one non-zero $c_i$. You need to output this maximum value.

Input

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

The second line contains $n$ integers $a_i$.

The third line contains $n$ integers $b_i$.

The next $q$ lines each contain two integers $x$ and $y$, representing a query.

Output

Output $q$ lines, each containing an integer representing the maximum value of $\sum\limits_{i=l}^r b_i \times [c_i\ne 0]$.

Examples

Input 1

5 3
1 2 3 1 2
-2 3 2 -1 2
1 2
1 3
2 3

Output 1

2
1
5

Input 2

See the provided files.

Subtasks

There are 20 test cases in total.

For test cases 1, 2, 3, 4, $n, q \leq 5000$.

For test cases 5, 6, the number of distinct values of $a_i$ does not exceed 500.

For test cases 7, 8, $n \leq 150\,000$, $q \leq 500\,000$, $b_i > 0$.

For test case 9, $n \leq 150\,000$, $q \leq 500\,000$.

For test cases 10, 11, $n \leq 200\,000$, $q \leq 500\,000$.

For test cases 12, 13, 14, $b_i = 1$.

For test cases 15, 16, $b_i > 0$.

For all test cases, $1 \leq n \leq 300\,000$, $1 \leq q \leq 1\,000\,000$, $1 \leq a_i \leq n$, $-10^9 \leq b_i \leq 10^9$, $1 \leq x, y \leq n$, $x \ne y$. It is guaranteed that for each query, $c_i$ contains at least one $1$ and at least one $-1$.

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.