Under construction

Still being built. Things may change

Wombats

Time limit20sMemory limit256 MB

Summary
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 RR horizontal roads running east to west, numbered 0,1,…,R−10, 1, \dots, R-1 from north to south, and CC vertical roads running north to south, numbered 0,1,…,C−10, 1, \dots, C-1 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 PP and vertical road QQ is written (P,Q)(P, Q). 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 00) to a specified intersection on the southern edge (horizontal road R−1R-1) 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 EE 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 00, and you must find the minimum number of wombats they encounter on the way to a specified intersection on horizontal road R−1R-1.

The figure above shows the initial state of a map with R=3R = 3 horizontal roads and C=4C = 4 vertical roads, with the wombat count on each segment shown. Suppose the following events occur in order.

  • A person arrives at intersection A=(0,2)A = (0, 2) and wants to flee to intersection B=(2,1)B = (2, 1). The minimum number of wombats encountered is 22, which the dashed path in the figure confirms.
  • Another person arrives at intersection X=(0,3)X = (0, 3) and wants to flee to intersection Y=(2,3)Y = (2, 3). The minimum here is 77.
  • Two change events occur. The wombat count on the topmost segment of vertical road 00 becomes 55, and the wombat count on the middle segment of horizontal road 11 becomes 66. Check the circled numbers in the figure below.

A third person again arrives at intersection A=(0,2)A = (0, 2) and wants to flee to intersection B=(2,1)B = (2, 1). This time the minimum number of wombats encountered is 55.

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 RR and the number of vertical roads CC, separated by a space. (2≤R≤50002 \le R \le 5000, 1≤C≤2001 \le C \le 200)

The next RR lines describe the horizontal segments. Line ii (0≤i≤R−10 \le i \le R-1) contains C−1C-1 integers H[i][0] through H[i][C-2], where H[i][j] is the number of wombats on the horizontal segment between intersections (i,j)(i, j) and (i,j+1)(i, j+1).

The next R−1R-1 lines describe the vertical segments. Line ii (0≤i≤R−20 \le i \le R-2) contains CC integers V[i][0] through V[i][C-1], where V[i][j] is the number of wombats on the vertical segment between intersections (i,j)(i, j) and (i+1,j)(i+1, j).

The next line contains the number of events EE.

The following EE 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 (P,Q)(P, Q) and (P,Q+1)(P, Q+1) to WW. (0≤P≤R−10 \le P \le R-1, 0≤Q≤C−20 \le Q \le C-2, 0≤W≤10000 \le W \le 1000)
  • 2 P Q W: set the number of wombats on the vertical segment between (P,Q)(P, Q) and (P+1,Q)(P+1, Q) to WW. (0≤P≤R−20 \le P \le R-2, 0≤Q≤C−10 \le Q \le C-1, 0≤W≤10000 \le W \le 1000)
  • 3 V1 V2: ask for the minimum number of wombats encountered when moving from (0,V1)(0, V1) to (R−1,V2)(R-1, V2). (0≤V1≤C−10 \le V1 \le C-1, 0≤V2≤C−10 \le V2 \le C-1)

Events of type 1 and 2 occur at most 500500 times combined, and events of type 3 occur at most 200000200000 times. At every moment the number of wombats on any single segment is at most 10001000.

Output

For each event of type 3, print the minimum number of wombats encountered when moving from (0,V1)(0, V1) to (R−1,V2)(R-1, V2), one per line in the order the events occur.

Examples3

  1. Example 1

    Input
    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
    
    Expected output
    2
    7
    5
    
  2. Example 2

    Input
    4 1
    2
    5
    3
    4
    3 0 0
    2 1 0 100
    3 0 0
    3 0 0
    
    Expected output
    10
    105
    105
    
  3. Example 3

    Input
    2 3
    1 2
    3 4
    0 5 0
    2
    3 0 2
    3 2 0
    
    Expected output
    3
    3