Under construction

Still being built. Things may change

Jinx and the Peacekeeper Camp

Time limit3sMemory limit1024 MB

Summary
Given guard rectangles with strengths, find for each horizontal or vertical segment the minimum protection among its points that at least one guard covers.
Level

Hard9 of 10

Topics
Segment tree, Sorting, Divide and conquer
Solved
No attempts yet

Statement

Jinx plans an attack on a peacekeeper camp.

The camp is a field on the coordinate plane.

The camp has nn peacekeeper guards. Guard ii has strength viv_i and is responsible for one rectangle on the plane. Each rectangle is given by its upper left corner (x1,y1)(x_1, y_1) and lower right corner (x2,y2)(x_2, y_2), with x1≤x2x_1 \leq x_2 and y1≤y2y_1 \leq y_2. A point (x,y)(x, y) is protected by the guard of this rectangle if x1≤x≤x2x_1 \leq x \leq x_2 and y1≤y≤y2y_1 \leq y \leq y_2.

A point of the camp can be protected by several guards, and their rectangles may overlap. Unfortunately for Jinx, this helps little. The protection sx,ys_{x,y} of point (x,y)(x, y) equals the smallest strength among the guards protecting it.

The camp has mm approaches. The jj-th approach leads to all points inside one of two rectangles: either the rectangle with corners (x,y1)(x, y_1) and (x,y2)(x, y_2), or the rectangle with corners (x1,y)(x_1, y) and (x2,y)(x_2, y). Through a vertical approach, Jinx can reach a point (x‾,y‾)(\overline{x}, \overline{y}) if x‾=x\overline{x} = x and y1≤y‾≤y2y_1 \leq \overline{y} \leq y_2. Through a horizontal approach, she can reach it if y‾=y\overline{y} = y and x1≤x‾≤x2x_1 \leq \overline{x} \leq x_2.

For each approach, find the minimum protection sx‾,y‾s_{\overline{x},\overline{y}} over all points she can reach through it that are protected by at least one guard.

Input

The first line contains two integers nn and mm (1≤n≤3⋅1041 \leq n \leq 3 \cdot 10^4, 1≤m≤1051 \leq m \leq 10^5), the number of guards and the number of approaches.

Each of the next nn lines describes one guard. Line ii contains five integers xi,1x_{i,1}, xi,2x_{i,2}, yi,1y_{i,1}, yi,2y_{i,2} and viv_i. The first four values are the coordinates of the upper left and lower right corners of the rectangle of guard ii, and viv_i is the strength of that guard (0≤xi,1,xi,2,yi,1,yi,2≤2⋅1050 \leq x_{i,1}, x_{i,2}, y_{i,1}, y_{i,2} \leq 2 \cdot 10^5, 0≤vi≤1090 \leq v_i \leq 10^9).

Each of the next mm lines describes one approach. The first character of the line is either x or y. If it is x, the approach is defined by (x,y1)(x, y_1) and (x,y2)(x, y_2), and the line continues with xjx_j, yj,1y_{j,1}, yj,2y_{j,2}. Otherwise, the approach is defined by (x1,y)(x_1, y) and (x2,y)(x_2, y), and the line continues with yjy_j, xj,1x_{j,1}, xj,2x_{j,2}.

Output

For each of the mm approaches, print on its own line the minimum protection among the points it can reach that are protected by at least one guard. If none of the points reachable through the approach is protected by a guard, print −1-1.

Examples1

  1. Example 1

    Input
    4 4
    1 4 1 4 3
    1 3 2 4 2
    1 5 2 4 4
    1 2 1 3 1
    x 2 3 5
    x 3 1 4
    x 5 1 1
    x 4 1 3
    
    Expected output
    1
    2
    -1
    3