Under construction

Still being built. Things may change

Fishing

Time limit10sMemory limit1024 MB

Summary
Given K weighted cells in an N by M grid, answer online queries for the maximum sum of a contiguous column block within columns X to Y over the first H rows.
Level

Medium7 of 10

Topics
Segment tree, Prefix sum, Dynamic programming
Solved
No attempts yet

Statement

There is a small village on the coast of the Adriatic Sea. The fishermen map the sea as a grid of N×MN × M cells. The first row is adjacent to the coast, and the last row is the farthest away. They track the movement of fish and other items floating in the sea. Most of the sea is empty, but there are KK cells of interest. The location of the ii-th cell is row RiR_i and column CiC_i. The fishermen estimate that their catch from fishing in the ii-th cell is worth ViV_i. ViV_i can be zero or negative if that area is mostly occupied by undesired items. All other cells have a value of 00.

Every day, the local council approves a rectangular fishing area that includes columns from XX to YY and extends HH rows from the shore into the sea. To fish in the selected area, the fishermen prepare a fishing net exactly HH units long. The net has a fixed length, but it can be rolled out to any width WW up to Y−X+1Y - X + 1. Based on what they know about the sea, the fishermen drop the net somewhere within the approved area so that the sum of the cell values it covers is as large as possible.

Write a program that finds the best catch for the approved fishing areas over the next QQ days. Cell values stay constant. Fishing on earlier days does not deplete them.

Input

The first line contains the number of rows NN, the number of columns MM, and the number of non-empty cells KK. Each of the next KK lines gives the row RiR_i, column CiC_i, and value ViV_i of one cell, separated by spaces. Rows are numbered from 1 to NN, and columns from 1 to MM. All ViV_i are integers.

The next line contains the number of queries QQ. The jj-th query is given as three integers Aj′A'_j, Xj′X'_j, and Yj′Y'_j. The queries are encoded so that your program must answer them in the given order. The actual query is computed as follows.

Hj=Hj′⊕Aj−3H_j = H'_j \oplus A_{j-3}

Xj=Xj′⊕Aj−2X_j = X'_j \oplus A_{j-2}

Yj=Yj′⊕Aj−1Y_j = Y'_j \oplus A_{j-1}

Here AjA_j is the answer to the jj-th query, and AjA_j is 00 when j≤0j \le 0. ⊕\oplus denotes bitwise XOR. Your program must find the region with the maximum catch value that spans the first HjH_j rows and some subrange of columns from XjX_j to YjY_j.

Output

For each query, output the maximum catch value on its own line. The fishermen can always keep an empty net, which has a value of 00.

Constraints

  • 1≤N,M,K,Q≤300 0001 \le N, M, K, Q \le 300\,000
  • ∣Vi∣≤1000|V_i| \le 1000

Hint

The decoded list of queries:

5 1 5
10 1 7
7 6 6
8 2 6
4 1 6
3 1 2

Examples1

  1. Example 1

    Input
    10 7
    12
    2 6 -5
    3 3 3
    4 2 -2
    4 6 2
    5 3 -1
    5 5 5
    7 1 8
    7 7 4
    8 4 -3
    8 5 1
    9 6 -4
    10 3 2
    6
    5 1 5
    10 1 0
    7 1 11
    15 15 6
    9 1 0
    3 7 1
    
    Expected output
    7
    13
    0
    6
    3
    0