#P5897. [IOI 2013] wombats
[IOI 2013] wombats
Problem Description
Brisbane has been occupied by mutated wombats, and you must lead everyone to a safe place.
The roads in Brisbane form a large grid. There are horizontal roads running west to east, numbered from north to south as . There are vertical roads running south to north, numbered from west to east as , as shown in the figure below.

Wombats invade from the north, and people escape to the south. People can move in both directions along horizontal roads, but along vertical roads they can only move south toward safety.
The intersection of horizontal road and vertical road is denoted as . On each road segment between two adjacent intersections, there are some wombats, and the number changes over time. Your task is to guide each person from a specified intersection on the northernmost road (horizontal road ) to a specified intersection on the southernmost road (horizontal road ), while encountering as few wombats as possible along the way.
You are first given the grid size and the number of wombats on each road segment. Then you are given a sequence of events, each of which is one of the following:
- an update, meaning the number of wombats on some road segments changes;
- an escape query, meaning some people have arrived at a specified intersection on horizontal road , and you must tell them a path to a specified intersection on horizontal road that minimizes the number of wombats encountered.
Example

In the initial map shown above, there are horizontal roads () and vertical roads (). The number of wombats on each road segment is marked on the segment. Consider the following events:
- A person arrives at intersection and wants to escape to intersection . As shown by the dashed path, they need to pass at least wombats.
- Another person arrives at intersection and wants to escape to intersection . As shown by the dashed path, they need to pass at least wombats.
- Two update events occur: the number of wombats on the topmost vertical segment of vertical road becomes , and the number of wombats on the middle horizontal segment of horizontal road becomes , as circled in the figure below.

- A third person arrives at intersection and wants to escape to intersection . Now they need to pass at least wombats, as shown by the dashed path in the figure.
Input Format
- Line : is the number of horizontal roads, and is the number of vertical roads.
- Line : .
- Line : .
- Line : .
- : a 2D array of size , where is the number of wombats on the horizontal road segment between intersection and intersection .
- Line : .
- : a 2D array of size , where is the number of wombats on the vertical road segment between intersection and intersection .
- Next line: .
- The next lines: one event per line, given in the order they occur.
If , then the empty lines for the number of wombats on horizontal road segments (lines to ) will be omitted.
The format of each event line is as follows:
1 P Q Wmeans setting the number of wombats on the horizontal road segment between intersection and intersection to .2 P Q Wmeans setting the number of wombats on the vertical road segment between intersection and intersection to .3 V1 V2means computing the minimum number of wombats a person must pass when escaping from intersection to intersection .
For example, the example in the statement should be written in the following format:
3 4
0 2 5
7 1 1
0 4 0
0 0 0 2
0 3 4 7
5
3 2 1
3 3 3
2 0 0 5
1 1 1 6
3 2 1
Output Format
For each query, output the minimum number of wombats that must be passed.
3 4
0 2 5
7 1 1
0 4 0
0 0 0 2
0 3 4 7
5
3 2 1
3 3 3
2 0 0 5
1 1 1 6
3 2 1
2
7
5
Hint
Constraints: for of the data, , , there are at most updates, at most queries, and at any time there are at most wombats on any road segment.
Translated by ChatGPT 5