Image Analysis

시간 제한2초메모리 제한2048 MB

요약
격자 위 활성 점들에 색 ID가 주어질 때, 고정 크기 창 안에서 빈도가 [A, B]에 드는 색의 개수를 구하는 질의에 답한다.
난이도

어려움10점 중 8점

유형
정렬, 투 포인터, 누적 합, 기하
정답자
아직 제출이 없습니다

문제

A research laboratory is working on a project involving the automatic analysis of digital images.

The image under study is extremely large and is modeled as an n×nn \times n integer grid. The lower-left corner of the grid is (1,1)(1, 1) and the upper-right corner is (n,n)(n, n). Among all the (positive) integer grid points, only pp points are marked as active, each associated with a color ID. These active points are of particular interest to the researchers because they represent features extracted from the image—such as highlighted regions, detected objects, or important signals. It is guaranteed that all xx-coordinates of the active points are distinct, and all yy-coordinates of the active points are distinct.

To better understand the distribution of these features, the researcher analyzes the image using a rectangular query frame of fixed size W×HW \times H, where WW is the width and HH is the height. The query frame must be entirely contained within the n×nn \times n grid and must align exactly with the grid. The researcher observes all the active points within the query frame and records their color IDs.

The task is not simply to count colors in the query frame, but to measure how balanced they are. In particular, the researcher is interested in the number of distinct color IDs that appear with frequencies in some frequency range \[A,B]\[A, B], i.e., lying inclusively between two thresholds AA and BB. A color that appears too rarely or too frequently in the frame may not be considered significant, while a color that occurs within a moderate frequency range is more important. Thus, we want to count the colors of the active points in the query frame whose frequencies are in the given frequency range.

Figure 1 illustrates an example on a 11×1111 \times 11 integer grid. In Figure 1 (a), ten active points are marked with four colors: gold (11), blue (22), red (33), and gray (44). In Figure 1 (b), a query frame of size W=5W = 5 and H=6H = 6 is placed with the lower-left corner at (3,3)(3, 3).

This query frame contains the active points of the colors gold, blue, red, and gray. For a frequency range \[A,B]=\[2,4]\[A, B] = \[2, 4], we calculate the frequency of each color only inside the query frame and select the ones whose frequencies lie in \[2,4]\[2, 4]. In this example, the colors with frequencies in \[2,4]\[2, 4] are gold and blue because gold and blue appear twice, but the red and gray appear only once. Thus, the answer for this query frame is 22.

Figure 1. (a) Active points in 11×1111 \times 11 grid with four colors. (b) A query frame with W=5,H=6W = 5, H = 6 with lower-left corner at (3,3)(3, 3).

Given active points and their colors, you are asked to process qq such queries. Each query specifies a query frame and a frequency range. For each query, you must output how many distinct color IDs inside the query frame have their frequencies lying in the frequency range.

입력

Your program is to read from standard input. The input starts with a line containing three integers nn, pp, and qq (2≤n≤2×1052 ≤ n ≤ 2 \times 10^5; 1≤p≤2×1051 ≤ p ≤ 2 \times 10^5; 1≤q≤2×1051 ≤ q ≤ 2 \times 10^5), where nn is the size of the image, pp is the number of active points, and qq is the number of queries.

The second line contains two integers WW and HH (1≤W,H≤n−11 ≤ W, H ≤ n − 1), the width and height of the query frame, respectively.

Each of the next pp lines contains three integers xx, yy, cc (1≤x,y≤n1 ≤ x, y ≤ n; 1≤c≤1091 ≤ c ≤ 10^9), describing an active point with color ID cc located at the integer grid point (x,y)(x, y). All xx-coordinates are distinct, and all yy-coordinates are distinct.

Each of the following qq lines contains four integers LL, DD, AA, BB (1≤L≤n−W1 ≤ L ≤ n − W; 1≤D≤n−H1 ≤ D ≤ n − H; 1≤A≤B≤p1 ≤ A ≤B ≤ p). This represents a rectangle \[L,L+W]×\[D,D+H]\[L, L + W] \times \[D,D + H], which is a query frame, and the frequency range \[A,B]\[A, B].

출력

Your program is to write to standard output. Print qq lines. The ii-th line should contain the number of distinct color IDs of the active points in the ii-th query frame whose frequencies lie in \[A,B]\[A, B].

예제2

  1. 예제 1

    입력
    5 5 3
    2 3
    1 1 1
    2 4 2
    3 2 2
    4 5 3
    5 3 4
    1 1 1 2
    2 2 1 1
    3 1 2 3
    
    예상 출력
    2
    1
    0
    
  2. 예제 2

    입력
    6 3 3
    4 4
    1 1 1
    4 2 5
    2 6 5
    1 1 1 3
    1 2 1 1
    2 2 2 2
    
    예상 출력
    2
    0
    1