I have N points denoted by (xi,yi).
1<=i<=N
I have Q queries of the following form :
Given a rectangle (aligned with x,y axes) defined by the points x1, y1, x2, y2 where (x1, y1) is the lower left corner and (x2, y2) is the upper right corner, find the number of points inside the rectangle. Points on rectangle are considered outside.
Constraints :
1 ≤ N ≤ 100 000
1 ≤ Q ≤ 100 000
0 ≤ xi, yi ≤ 100 000
0 ≤ x1 < x2 ≤ 100 000
0 ≤ y1 < y2 ≤ 100 000
I have thought of following approaches :
Build a 2D segment tree on N*N matrix. A query will be solved in log N time. But the size of the segment tree built would be >=10^7. Hence memory insufficient.
Keep two arrays(say X and Y), with both array containing all the N points. X is sorted with respect to x coordinates and Y is sorted with respect to y coordinate. Now given x1,y1,x2,y2 : I can find all points >=x1 && <=x2 from X array in log N time. Similarly, I can find all points >=y1 && <=y2 from Y in log N time. But how to find number of points in given rectangle, I cannot workout out further!
Complexity should be O(NlogN) or O(QlogN)