#P8530. [Ynoi2003] 博丽灵梦

[Ynoi2003] 博丽灵梦

Problem Description

Counting colors in rectangles, with weights.

You are given a 2D plane with nn points. The ii-th point has coordinates (i,pi)(i, p_i) and a weight aia_i.

You are given an array bb of length nn, indexed from 11 to nn.

There are mm queries. Each query gives a rectangle l1,r1,l2,r2l_1, r_1, l_2, r_2. Define the set $S = \{ a_i \mid l_1 \le i \le r_1 \land l_2 \le p_i \le r_2 \}$. For all elements jj in the set SS, compute the sum of bjb_j.

Input Format

The first line contains an integer nn.

The next line contains nn numbers, where the ii-th number denotes pip_i.

The next line contains nn numbers, where the ii-th number denotes aia_i.

The next line contains nn numbers, where the ii-th number denotes bib_i.

The next line contains an integer mm.

The next mm lines each contain 44 numbers l1,r1,l2,r2l_1, r_1, l_2, r_2, describing one query.

Output Format

For each query, output one line containing an integer, which is the answer. Output the answer modulo 2322^{32}.

9
9 8 7 6 2 4 5 3 1
1 1 4 5 1 4 1 9 1
9 1 1 4 5 1 4 1 9
9
4 9 3 6
2 9 1 8
3 8 2 4
3 9 2 7
2 8 1 6
1 9 1 9
1 3 5 7
2 3 3 3
6 6 6 6
27
27
22
27
27
27
4
0
0

Hint

Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078.

Sample Explanation

For the query whose answer is 2727, S={1,4,5,9}S = \{1,4,5,9\}, the corresponding bj=9,4,5,9b_j = 9,4,5,9, and the sum is 2727.

For the query whose answer is 2222, S={1,4,9}S = \{1,4,9\}, the corresponding bj=9,4,9b_j = 9,4,9, and the sum is 2222.

For the query whose answer is 44, S={4}S = \{4\}, the corresponding bj=4b_j = 4, and the sum is 44.

For the query whose answer is 00, S=S = \emptyset, and the sum is 00.

Constraints

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

Test Point ID nn \le mm \le Special Constraint
11 1010 None
22 5×1035\times 10^3
33 2.5×1042.5\times 10^4 5×1045\times 10^4 A\text{A}
44 5×1045\times 10^4 10510^5
55 7.5×1047.5\times 10^4 1.5×1051.5\times 10^5
66 10510^5 2×1052\times 10^5
77 2×1042\times 10^4 None
88 3×1043\times 10^4 6×1046\times 10^4
99 4×1044\times 10^4 8×1048\times 10^4
1010 5×1045\times 10^4 10510^5
1111 6×1046\times 10^4 1.2×1051.2\times 10^5
1212 7×1047\times 10^4 1.4×1051.4\times 10^5
131513\sim 15 10510^5 2×1052\times 10^5 C\text{C}
161816\sim 18 None
1919 3×1053\times 10^5
2020 4×1054\times 10^5
2121 5×1055\times 10^5
2222 6×1056\times 10^5
2323 8×1058\times 10^5
2424 10610^6
2525

For convenience, we write (a,b)(c,d)(a, b) \leq (c, d) to mean that two points (a,b)(a, b) and (c,d)(c, d) on the plane satisfy aca \leq c and bdb \leq d.

Special constraint A: For all queries, l2=1l_2 = 1 and r2=nr_2 = n.

Special constraint B: This special constraint is too easy to exploit for weak testdata, so it is removed.

Special constraint C: At most 5050 pairs (i,j)(i, j) (1i<jn)(1 \leq i < j \leq n) do not satisfy (i,pi)(j,pj)(i, p_i) \leq (j, p_j).

For all test points: 2n1052 \leq n \leq 10^5, 1m1061 \leq m \leq 10^6, 1l1r1n1 \leq l_1 \le r_1 \leq n, 1l2r2n1 \leq l_2 \le r_2 \leq n. It is guaranteed that pip_i is a permutation, and 1pi,ai,bin1 \le p_i, a_i, b_i \le n.

Translated by ChatGPT 5