PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000141. Đếm hình chữ nhật chứa mỗi điểm (Count Number of Rectangles Containing Each Point)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm hình chữ nhật chứa mỗi điểm (Count Number of Rectangles Containing Each Point)

Count Number of Rectangles Containing Each Point

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn axis-aligned rectangles in the plane. Every rectangle has bottom-left corner (0,0)(0,0); rectangle ii has top-right corner (li,hi)(l_i,h_i) and therefore contains exactly the points (x,y)(x,y) satisfying 0≤x≤li0\le x\le l_i and 0≤y≤hi0\le y\le h_i. The boundary is included.

You are also given qq distinct points Pj=(xj,yj)P_j=(x_j,y_j). For each point, count how many rectangles contain it. Rectangles may overlap or contain one another, but all pairs (li,hi)(l_i,h_i) are distinct. Count each rectangle once, and output the answers in input point order.

Input

The first line contains n,qn,q. The next nn lines contain li,hil_i,h_i; the final qq lines contain xj,yjx_j,y_j.

Output

Print qq lines; line jj gives the number of rectangles containing point PjP_j.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤301\le n,q\le 30; 1≤li,xj≤1091\le l_i,x_j\le10^9; 1≤hi,yj≤1001\le h_i,y_j\le100; All rectangle pairs (li,hi)(l_i,h_i) are distinct; all query points (xj,yj)(x_j,y_j) are distinct.
  • Subtask 2 (30 points): 1≤n,q≤30001\le n,q\le 3000; 1≤li,xj≤1091\le l_i,x_j\le10^9; 1≤hi,yj≤1001\le h_i,y_j\le100; All rectangle pairs (li,hi)(l_i,h_i) are distinct; all query points (xj,yj)(x_j,y_j) are distinct.
  • Subtask 3 (50 points): 1≤n,q≤500001\le n,q\le 50000; 1≤li,xj≤1091\le l_i,x_j\le10^9; 1≤hi,yj≤1001\le h_i,y_j\le100; All rectangle pairs (li,hi)(l_i,h_i) are distinct; all query points (xj,yj)(x_j,y_j) are distinct.

Examples

Example 1

Input

3 2
1 2
2 3
2 5
2 1
1 4

Output

2
1

Explanation

Point (2,1)(2,1) belongs to rectangles (2,3)(2,3) and (2,5)(2,5); (1,2)(1,2) is too narrow. Point (1,4)(1,4) belongs only to rectangle (2,5)(2,5).

Example 2

Input

3 2
1 1
2 2
3 3
1 3
1 1

Output

1
3

Explanation

Only (3,3)(3,3) contains point (1,3)(1,3). All three rectangles contain (1,1)(1,1), including the point on the boundary of rectangle (1,1)(1,1).