#P10764. [BalticOI 2024] Wall

    ID: 12232 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP线段树2024BalticOI(波罗的海)

[BalticOI 2024] Wall

Background

Translated from BalticOI 2024 Day2 T3.

Problem Description

You want to build a wall consisting of NN segments. Each segment ii can have height aia_i or bib_i. For every possible wall height sequence hh, you want to compute the total amount of trapped water, and then sum this value over all possible sequences.

For example, the figure below shows an example with N=10N = 10 and wall heights 4,2,1,8,6,2,7,1,2,34, 2, 1, 8, 6, 2, 7, 1, 2, 3. Its actual water levels are 4,4,4,8,7,7,7,3,3,34, 4, 4, 8, 7, 7, 7, 3, 3, 3.

For a position ii, the water level HH after rain must satisfy that there exist two indices l,rl, r (li,ri)(l \leq i, r \geq i) such that hlHh_l \geq H and hrHh_r \geq H, and HH is as large as possible. Then the trapped water amount at position ii is HhiH - h_i.

Output the sum of trapped water over all possible cases, modulo 109+710^9 + 7.

Input Format

The first line contains an integer NN.

The second line contains NN integers aia_i.

The third line contains NN integers bib_i.

Output Format

Output the sum of trapped water over all possible wall configurations, modulo 109+710^9 + 7.

4
1 1 1 1
2 2 2 2
6
10
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

21116

Hint

For sample 1, the case (2,1,1,2)(2,1,1,2) has trapped water amount 22. The cases (1,2,1,2)(1,2,1,2), (2,1,2,1)(2,1,2,1), (2,1,2,2)(2,1,2,2), and (2,2,1,2)(2,2,1,2) each have trapped water amount 11.

Subtask ID Special Property Points
11 N20N \leq 20 88
22 N100N \leq 100 and ai,bi1000a_i, b_i \leq 1000 1717
33 N10000N \leq 10000 and ai,bi1000a_i, b_i \leq 1000 1919
44 N10000N \leq 10000 1414
55 ai,bi2a_i, b_i \leq 2 1212
66 No special property 3030

Constraints for all testdata: 1N5×1051 \leq N \leq 5 \times 10^5, 1ai,bi1091 \leq a_i, b_i \leq 10^9, and for each 1iN1 \leq i \leq N, aibia_i \neq b_i.

Translated by ChatGPT 5