#P5618. [SDOI2015] 道路修建
[SDOI2015] 道路修建
Description
A certain country has cities. These cities form a -row, -column grid. The government now has a tourism development plan: it needs to choose two columns and , and build some dedicated roads so that among all cities between these two columns (including these two columns), every city can reach any other of these cities using only the dedicated roads.
A dedicated road can only be built between two cities in the same row and adjacent columns, or between the two cities in the same column, and building a road costs some amount.
Since the government wants to reduce expenses as much as possible, after choosing and , it will build only dedicated roads, so that these roads form a tree structure. Now you need to help the government write a program to complete this task.
Specifically, the task contains operations, each in one of the following formats:
-
C x0 y0 x1 y1 w: Because the situation between the city at row , column and the city at row , column has been re-evaluated, the cost to build a dedicated road between them becomes . -
Q L R: If the government chooses columns and , ask for the minimum total expense.
Input Format
The first line contains two integers and .
The second line contains integers. The -th integer indicates the initial cost to build a dedicated road between the city at row , column and the city at row , column .
The third line contains integers. The -th integer indicates the initial cost to build a dedicated road between the city at row , column and the city at row , column .
The fourth line contains integers. The -th integer indicates the initial cost to build a dedicated road between the city at row , column and the city at row , column .
The next lines each contain one operation.
Output Format
For each query operation, output one line containing the minimum expense you computed for the government.
3 3
1 2
2 1
3 1 2
Q 1 3
C 1 2 2 2 3
Q 2 3
7
5
Hint
For all testdata, , and at any time, the construction cost of any dedicated road does not exceed .
Translated by ChatGPT 5
京公网安备 11011102002149号