Under construction

Still being built. Things may change

Destroyer King Ttungppup

Time limit1.5sMemory limit1024 MB

Summary
Given a grid of pillars that cost energy to pass and at most T teleports, answer for each query whether a cell is reachable from (1,1) within energy p.
Level

Medium6 of 10

Topics
Graph, Shortest path, Dynamic programming
Solved
No attempts yet

Problem

The world where Ttungppup lives is an N×MN \times M grid of 1×11 \times 1 cells. The top left cell is (1,1)(1,1) and the bottom right cell is (N,M)(N,M). Each cell holds at most one pillar, and each pillar has a strength.

To move from one cell to another in this world, you must follow these rules.

  • From the current cell, you can move to an adjacent cell up, down, left, or right. If the target cell has a pillar, you spend energy equal to its strength to destroy it and move there. You cannot move if the pillar's strength is greater than your remaining energy.
  • If the current cell allows teleportation, you can teleport in one direction. Teleporting costs no energy. If the destination cell has a pillar, the pillar is destroyed automatically. You can teleport at most TT times, and each cell has at most one teleport destination.

Given an energy pp and a destination (x,y)(x,y), Ttungppup wants to know whether he can move from (1,1)(1,1) to (x,y)(x,y) using at most pp energy while following these rules. He asks QQ questions. Pillars destroyed in one question do not affect the others.

Input

The first line contains NN, MM, KK, TT, and QQ, separated by spaces. (1≤N,M,T≤100; 0≤K≤min⁡(1 000,N×M); 1≤Q≤100 000)(1 \le N,M,T \le 100;\ 0 \le K \le \min(1\,000, N \times M);\ 1 \le Q \le 100\,000)

Each of the next NN lines contains MM values separated by spaces. Each value is the strength of the pillar in that cell, between 00 and 1 0001\,000. A value of 00 means the cell has no pillar. (1,1)(1,1) is always 00.

Each of the next KK lines contains four integers x1,y1,x2,y2x_1, y_1, x_2, y_2 separated by spaces. This means you can teleport from (x1,y1)(x_1,y_1) to (x2,y2)(x_2,y_2). (1≤x1,x2≤N; 1≤y1,y2≤M)(1 \le x_1,x_2 \le N;\ 1 \le y_1,y_2 \le M)

Each of the next QQ lines contains an energy pp and a destination x,yx, y separated by spaces. (0≤p≤108; 1≤x≤N; 1≤y≤M)(0 \le p \le 10^8;\ 1 \le x \le N;\ 1 \le y \le M)

All input values are integers.

Output

For each question, print 11 on a separate line if you can reach (x,y)(x,y) from (1,1)(1,1) using at most pp energy. Otherwise, print 00.

Examples2

  1. Example 1

    Input
    5 5 2 2 5
    0 10 4 2 3
    1 1 1 1 1
    100 200 300 400 1
    0 0 1 1 10
    1 1 1 1 0
    2 3 4 4
    1 2 5 1
    8 2 4
    16 5 5
    5 3 5
    3 5 1
    3 2 5
    
    Expected output
    1
    1
    0
    0
    0
    
  2. Example 2

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