Under construction

Still being built. Things may change

Brue Marble

Time limit3sMemory limit1024 MB

Summary
Given a grid of arrows and timed point events, find the score after L moves from a start cell, applying events as they are added.
Level

Hard8 of 10

Topics
Graph, Dynamic programming, Tree
Solved
No attempts yet

Problem

Brue Marble is a game where you move the hero Brue across a board so that Brue can join events held at different places on the board. The board is a grid of NN rows and MM columns. Each cell has an arrow pointing in one of 8 directions: up, down, left, right, or one of the four diagonals. The cell in the direction each arrow points to always exists.

An event lasts for exactly one turn. If the player is on the event's cell during that turn, the player can join the event and earn points. The points for each event are set by that event.

The game runs as follows.

  1. Before the game starts, choose a starting cell and place the piece on it.
  2. Repeat the following turn LL times. Each turn works like this:
    • First, move one cell in the direction the arrow on the current cell points.
    • If an event is open on the new cell, the player earns the points for that event. If several events are open there, the player earns the sum of their points.
  3. After LL turns, compute the final score.

Most of the game is finished, but the events have not been decided yet. You are a beta tester and need to simulate the game to check whether its balance is right. You must compute the final score when a given cell is the starting cell. Events may be added during the process, and you must reflect each addition. No events are scheduled at the start.

You must handle the following two kinds of tasks. Write cell (i,j)(i, j) for row ii and column jj.

  • Add an event: add an event that opens at cell (Xi,Yi)(X_i, Y_i) during turn TiT_i. Joining this event earns WiW_i points.
  • Compute the score: compute the final score when the piece starts at cell (Xi,Yi)(X_i, Y_i).

Input

The first line contains two integers NN and MM, the size of the grid, and two integers LL and QQ, the number of turns and the number of tasks.

The next NN lines describe the arrows. Line ii contains MM characters, and character Ai,jA_{i, j} is the arrow in row ii, column jj. Row numbers increase from top to bottom and column numbers increase from left to right. The characters mean the following directions.

  • Q: up-left
  • W: up
  • E: up-right
  • D: right
  • C: down-right
  • X: down
  • Z: down-left
  • A: left

The next QQ lines each describe one task, in one of these formats.

  • 1 Ti Xi Yi Wi1 \ T_i \ X_i \ Y_i \ W_i: add an event that opens at (Xi,Yi)(X_i, Y_i) during turn TiT_i. Joining it earns WiW_i points.
  • 2 Xi Yi2 \ X_i \ Y_i: compute the final score when the piece starts at (Xi,Yi)(X_i, Y_i).

Output

For each score computation task, print the final score on its own line.

Constraints

  • 2≤N,M≤5002 \le N, M \le 500
  • 1≤L≤1091 \le L \le 10^9
  • 1≤Q≤250 0001 \le Q \le 250\,000
  • Ai,j∈{Q,W,E,D,C,X,Z,A}A_{i, j} \in \{\text{Q}, \text{W}, \text{E}, \text{D}, \text{C}, \text{X}, \text{Z}, \text{A}\} for 1≤i≤N1 \le i \le N and 1≤j≤M1 \le j \le M
  • For an add-event task: 1≤Ti≤L1 \le T_i \le L, 1≤Xi≤N1 \le X_i \le N, 1≤Yi≤M1 \le Y_i \le M, 1≤Wi≤1031 \le W_i \le 10^3.
  • For a score computation task: 1≤Xi≤N1 \le X_i \le N and 1≤Yi≤M1 \le Y_i \le M.
  • There is at least one score computation task.
  • Every arrow points to a cell that exists on the board.

Examples2

  1. Example 1

    Input
    3 3 10 5
    DDX
    WQX
    WAA
    1 5 3 3 2
    1 7 1 3 6
    2 2 1
    2 2 2
    2 2 3
    
    Expected output
    2
    2
    6
    
  2. Example 2

    Input
    4 4 20 4
    XDDX
    XWWX
    XWWX
    DWWA
    1 3 4 1 10
    1 8 1 3 20
    1 17 1 4 40
    2 1 1
    
    Expected output
    70