Wombats
Time limit20sMemory limit256 MB
Find the cheapest southbound path across a grid with free east-west moves and south-only vertical roads under weight updates and escape queries.
- Level
Hard9 of 10
- Topics
- Segment tree, Shortest path, Graph
- Solved
- No attempts yet
Problem
The city of Brisbane has been overrun by mutant wombats (an Australian animal that looks a bit like a raccoon) grown to an enormous size. Your job is to guide people safely to the south.
Brisbane's roads form a large grid. There are horizontal roads running east to west, numbered from north to south, and vertical roads running north to south, numbered from west to east. The figure below shows an example of roads numbered this way.

The wombats advance from the north and people flee to the south. A person may move freely east or west along horizontal roads, but along vertical roads they may only move south, the safe direction.
The intersection of horizontal road and vertical road is written . Each road segment between two neighboring intersections may contain some wombats, and that count can change over time. You must guide a person who arrives at some intersection on the northern edge (horizontal road ) to a specified intersection on the southern edge (horizontal road ) so that the total number of wombats encountered along the way is as small as possible.
First you are given the size of the grid and the number of wombats on each road segment. Then events occur in order, each of one of two kinds.
- change: the number of wombats on some road segment changes.
- escape: a person arrives at some intersection on horizontal road , and you must find the minimum number of wombats they encounter on the way to a specified intersection on horizontal road .

The figure above shows the initial state of a map with horizontal roads and vertical roads, with the wombat count on each segment shown. Suppose the following events occur in order.
- A person arrives at intersection and wants to flee to intersection . The minimum number of wombats encountered is , which the dashed path in the figure confirms.
- Another person arrives at intersection and wants to flee to intersection . The minimum here is .
- Two change events occur. The wombat count on the topmost segment of vertical road becomes , and the wombat count on the middle segment of horizontal road becomes . Check the circled numbers in the figure below.

A third person again arrives at intersection and wants to flee to intersection . This time the minimum number of wombats encountered is .
Given the layout of the roads, the number of wombats on each segment, and the events in the order they occur, write a program that processes them.
Input
The first line contains the number of horizontal roads and the number of vertical roads , separated by a space. (, )
The next lines describe the horizontal segments. Line () contains integers H[i][0] through H[i][C-2], where H[i][j] is the number of wombats on the horizontal segment between intersections and .
The next lines describe the vertical segments. Line () contains integers V[i][0] through V[i][C-1], where V[i][j] is the number of wombats on the vertical segment between intersections and .
The next line contains the number of events .
The following lines each contain one event, in the order they occur. Each event has one of the three forms below.
1 P Q W: set the number of wombats on the horizontal segment between and to . (, , )2 P Q W: set the number of wombats on the vertical segment between and to . (, , )3 V1 V2: ask for the minimum number of wombats encountered when moving from to . (, )
Events of type 1 and 2 occur at most times combined, and events of type 3 occur at most times. At every moment the number of wombats on any single segment is at most .
Output
For each event of type 3, print the minimum number of wombats encountered when moving from to , one per line in the order the events occur.