#P8349. [SDOI/SXOI2022] 整数序列

[SDOI/SXOI2022] 整数序列

Problem Description

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

Little D has a positive integer sequence a1,a2,,ana_1, a_2, \dots, a_n and an integer sequence b1,b2,,bnb_1, b_2, \dots, b_n.

Little D has qq queries. In each query, x,yx, y are given, and a new sequence c1,c2,,cnc_1, c_2, \dots, c_n is constructed, where $c_i=\begin{cases}1 & a_i=x \\-1 & a_i=y \\ 0 & \text{else}\end{cases}$.

It is guaranteed that among the cic_i there is at least one 11 and at least one 1-1. He wants you to find an interval [l,r][l,r] such that i=lrci=0\sum\limits_{i=l}^r c_i=0, and i=lrbi×[ci0]\sum\limits_{i=l}^r b_i \times [c_i \neq 0] is maximized, and the cic_i in the interval cannot all be 00. You need to output this maximum value.

Note: When the condition [P][P] is true, [P]=1[P]=1, otherwise [P]=0[P]=0.

Input Format

The first line contains two integers n,qn, q.
The second line contains nn integers, where the ii-th integer denotes aia_i.
The third line contains nn integers, where the ii-th integer denotes bib_i.
The next qq lines each contain two integers x,yx, y, representing one query.

Output Format

For each query, output one line with one integer, representing the maximum value of i=lrbi×[ci0]\sum\limits_{i=l}^r b_i \times [c_i \neq 0].

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

2
1
5

见附加样例文件中的 ex_sequence2.in
见附加样例文件中的 ex_sequence2.out

Hint

Constraints

This problem has 2020 test points.

  • For test points 1,2,3,41,2,3,4, it is guaranteed that n,q5000n, q \le 5000.
  • For test points 5,65,6, it is guaranteed that the number of distinct values in aa does not exceed 500500.
  • For test points 7,87,8, it is guaranteed that n150000n \le 150000, q500000q \le 500000, and bi>0b_i>0.
  • For test point 99, it is guaranteed that n150000n \le 150000 and q500000q \le 500000.
  • For test points 10,1110,11, it is guaranteed that n200000n \le 200000 and q500000q \le 500000.
  • For test points 12,13,1412,13,14, it is guaranteed that bi=1b_i=1.
  • For test points 15,1615,16, it is guaranteed that bi>0b_i>0.

For all test points, 1n3000001 \le n \le 300000, 1q10000001 \le q \le 1000000, 1ain1 \le a_i \le n, 109<bi109-10^9<b_i \leq 10^9, 1x,yn1 \leq x, y \leq n, xyx \neq y. It is guaranteed that for each query, the cic_i always contain at least one 11 and at least one 1-1.

Translated by ChatGPT 5