Under construction

Still being built. Things may change

Vacuum Cleaner

Time limit3sMemory limit1024 MB

Summary
Given an N by M grid where clearing a row or column resets its dust to zero, find the shortest path between two cells that only crosses cells with dust at most k.
Level

Hard9 of 10

Topics
Graph, Shortest path, Segment tree
Solved
No attempts yet

Problem

Paolo works as a janitor in a large storage hall. Two of his tasks are vacuuming and moving objects around the hall. Paolo is fairly pragmatic about cleaning. He waits until there is so much dust that objects can no longer be moved. To make things worse, he recently bought a robot vacuum cleaner and releases it instead of cleaning himself. The robot is broken and cannot turn. It moves in a straight line until it hits a wall and runs out of battery.

The hall is an N×MN \times M grid. Rows are numbered 1 to NN, and columns are numbered 1 to MM. Every cell starts with 0 units of dust. Then QQ days follow. Each day begins with the dust in every cell increasing by 1. After that, exactly one of three things happens.

  1. An event of the form 11 rr means Paolo releases the robot along row rr, so the dust in every cell of that row becomes 0.
  2. An event of the form 22 cc means Paolo releases the robot along column cc, so the dust in every cell of that column becomes 0.
  3. An event of the form 33 r1r_1 c1c_1 r2r_2 c2c_2 kk means Paolo needs to move something from cell (r1,c1)(r_1, c_1) to cell (r2,c2)(r_2, c_2). The value kk is the item's dust tolerance. The item can only be moved across cells with at most kk units of dust. For example, if (r1,c1)(r_1, c_1) or (r2,c2)(r_2, c_2) has more than kk units, the task cannot be completed.

For each event of type 3, output the minimum number of steps Paolo needs to move the item. In one step he moves to a adjacent cell, meaning a cell that shares a side. Cells on the edge have fewer than four neighbors. If the item cannot be moved, output −1-1.

Input

The first line contains three integers NN, MM, and QQ (1≤N,M≤1061 \leq N,M \leq 10^6, 1≤Q≤3⋅1051 \leq Q \leq 3 \cdot 10^5).

Each of the next QQ lines describes an event. Each line starts with an integer tt, which is 1, 2, or 3, indicating the type of event. If t=1t = 1, the same line contains one more integer rr (1≤r≤N1 \leq r \leq N), the row that was chosen. If t=2t = 2, the line contains an integer cc (1≤c≤M1 \leq c \leq M), the column that was chosen. If t=3t = 3, the line contains five more integers r1r_1, c1c_1, r2r_2, c2c_2, kk (1≤r1,r2≤N1 \leq r_1, r_2 \leq N, 1≤c1,c2≤M1 \leq c_1, c_2 \leq M, 0≤k≤Q0 \leq k \leq Q).

For every event of type 3, the cells (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) are different. There is at least one event of type 3.

Output

For each event of type 3, print one line with the minimum number of steps needed to move the item. If the item cannot be moved, print −1-1 instead.

Examples1

  1. Example 1

    Input
    4 3 9
    3 2 1 3 3 1
    3 2 1 3 3 1
    2 1
    2 3
    1 1
    3 2 1 3 3 3
    3 2 1 3 3 3
    2 2
    3 2 1 3 3 6
    
    Expected output
    3
    -1
    5
    -1
    3