Number of points in a rectangle - arrays

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)

This problem is called Orthogonal Range Searching:
Given a set of n points in Rd, preprocess them such that reporting or
counting the k points inside a d-dimensional axis-parallel box will be
most efficient.
Your queries are range counting queries (not range reporting queries).
A two-dimensional range tree can be used to answer a range counting query in O(log n) time using O(n log n) storage (see for example Ch.36 of Handbook of Discrete and Computational Geometry 2Ed, 2004)
If your x's and y's are on a grid, and the grid is narrow, see Orthogonal range searching in linear and almost-linear space [Nekrich, 2009] where an O((logn / log logn)2) time data structure is presented.

Create a map<x, y>, populate it with all coordinates, then sort with comparator (sort by key then by value):
return a.first != b.first? a.first < b.first : a.second < b.second;
So that you can have y value sorted as well with a particular x key.
Now you need to traverse within >=x1 && <=x2 incrementally, and apply binary search to find y_min and y_max by passing iterator_start_for_xi and iterator_end_for_xi as start and end index.
Count at xi = iterator_for_xi_y_max - iterator_for_xi_y_min.
You need to find summation of count at xi where x1<=xi<=x2
For example, a map upon sorting would look like this:
2 6
3 4
3 5
3 10
3 12
3 25
5 1
5 5
5 15
6 6
6 20
8 0
Lets say the x1 = 3, x2 = 7, y1 = 3, y2 = 12
2 6
3 4 < y_min for x = 3
3 5 <
3 10 <
3 12 < y_max for x = 3
3 25
5 1
5 5 < y_min and y_max for x = 5
5 15
6 6 < y_min and y_max for x = 6
6 20
8 0

Related

Check subset sum for special array equation

I was trying to solve the following problem.
We are given N and A[0]
N <= 5000
A[0] <= 10^6 and even
if i is odd then
A[i] >= 3 * A[i-1]
if i is even
A[i]= 2 * A[i-1] + 3 * A[i-2]
element at odd index must be odd and at even it must be even.
We need to minimize the sum of the array.
and We are given a Q numbers
Q <= 1000
X<= 10^18
We need to determine is it possible to get subset-sum = X from our array.
What I have tried,
Creating a minimum sum array is easy. Just follow the equations and constraints.
The approach that I know for subset-sum is dynamic programming which has time complexity sum*sizeof(Array) but since sum can be as large as 10^18 that approach won't work.
Is there any equation relation that I am missing?
We can make it with a bit of math:
sorry for latex I am not sure it is possible on stack?
let X_n be the sequence (same as being defined by your A)
I assume X_0 is positive.
Thus sequence is strictly increasing and minimization occurs when X_{2n+1} = 3X_{2n}
We can compute the general term of X_{2n} and X_{2n+1}
v_0 =
X0
X1
v_1 =
X1
X2
the relation between v_0 and v_1 is
M_a =
0 1
3 2
the relation between v_1 and v_2 is
M_b =
0 1
0 3
hence the relation between v_2 and v_0 is
M = M_bM_a =
3 2
9 6
we deduce
v_{2n} =
X_{2n}
X_{2n+1}
v_{2n} = M^n v_0
Follow the classical diagonalization... and we (unless mistaken) get
X_{2n} = 9^n/3 X_0 + 2*9^{n-1}X_1
X_{2n+1} = 9^n X_0 + 2*9^{n-1}/3X_1
recall that X_1 = 3X_0 thus
X_{2n} = 9^n X_0
X_{2n+1} = 3.9^n X_0
Now if we represent the sum we want to check in base 9 we get
9^{n+1} 9^n
___ ________ ___ ___
X^{2n+2} X^2n
In the X^{2n} places we can only put a 1 or a 0 (that means we take the 2n-th elem from the A)
we may also put a 3 in the place of the X^{2n} place which means we selected the 2n+1th elem from the array
so we just have to decompose number in base 9, and check whether all its digits or either 0,1 or 3 (and also if its leading digit is not out of bound of our array....)

Algorithm find number position in snail 2D array

I have a 2D array square size.
such as :
(3x3) (4x4)
1 2 3 or 1 2 3 4
8 9 4 12 13 14 5
7 6 5 11 16 15 6
10 9 8 7
I am trying to find a solution to get by giving a value and the array size the Y, X position of the 2D array.
Exemple:
>> find_y_x_in_snail(3, 4)
1, 2
# in a 3x3 array, search value 4
return y=1 x=2
the only idea i have to create the snail in a 2D array and return the position.. not that great.
I found the opposite algorithm here (first exemple)
Any idea ?
You could use this function:
def find_y_x_in_snail(n, v):
r = 0
span = n
while v > span:
v -= span
r += 1
span -= r%2
d, m = divmod(r,4);
c = n-1-d
return [d, d+v, c, c-v][m], [d+v-1, c, c-v, d][m] # y, x
Explanation
r is the number of corners the "snake" needs to take to get to the targetted value.
span is the number of values in the current, straight segment of the snake, i.e. it starts with n, and decreases after the next corner, and again every second next corner.
d is the distance the snake has from the nearest side of the matrix, i.e. the "winding" level.
m indicates which of the 4 sides the segment -- containing the targetted value -- is at:
0: up
1: right
2: down
3: left
Depending on m a value is taken from a list with 4 expressions, each one tailored for the corresponding side: it defines the y coordinate. A similar method is applied (but with different expressions) for x.

How to find all possible options in C?

I'm trying to find a efficient algorithm in C, which provides me all options of a given equation.
I have equation AX + BY = M, where A, B and M i got on input (scanf).
For example lets have: 5X + 10Y = 45
1st option: 5 * 9 + 10 * 0
2nd option: 5 * 7 + 10 * 1
n-th option: 5 * 1 +
10 * 4
And also I need to count how many possible options exist?
Some tips, hints?
I forgot to say that X and Y are in Z and >= 0, so there is no infinite options.
The question makes sense if you restrict to non-negative unknowns.
Rewrite the equation as
AX = M - BY.
There can be positive solutions as long as the RHS is positive, i.e.
BY ≤ M,
or
Y ≤ M/B.
Then for a given Y, there is a solution iff
A|(M - BY)
You can code this in Python as
for Y in range(M / B + 1):
if (M - B * Y) % A == 0:
X= (M - B * Y) / A
The solutions are
9 0
7 1
5 2
3 3
1 4
The number of iterations equals M / B. If A > B, it is better to swap X and Y.
you can calcule every solution if you put some limit in your input value, for example: use X and Y in a value included from 0 to 9... in this way you can use for to calculate every solution.
The number of solution is infinite:
find a first solution like: X=9, Y=0.
you can create another solution by using:
X' = X+2*p
Y' = Y-p
For any p in Z.
This proves your program will never terminate.

Matlab: how to find an enclosing grid cell index for multiple points

I am trying to allocate (x, y) points to the cells of a non-uniform rectangular grid. Simply speaking, I have a grid defined as a sorted non-equidistant array
xGrid = [x1, x2, x3, x4];
and an array of numbers x lying between x1 and x4. For each x, I want to find its position in xGrid, i.e. such i that
xGrid(i) <= xi <= xGrid(i+1)
Is there a better (faster/simpler) way to do it than arrayfun(#(x) find(xGrid <= x, 1, 'last'), x)?
You are looking for the second output of histc:
[~,where] = histc(x, xGrid)
This returns the array where such that xGrid(where(i)) <= x(i) < xGrid(where(i)+1) holds.
Example:
xGrid = [2,4,6,8,10];
x = [3,5,6,9,11];
[~,where] = histc(x, xGrid)
Yields the following output:
where =
1 2 3 4 0
If you want xGrid(where(i)) < x(i) <= xGrid(where(i)+1), you need to do some trickery of negating the values:
[~,where] = histc(-x,-flip(xGrid));
where(where~=0) = numel(xGrid)-where(where~=0)
This yields:
where =
1 2 2 4 0
Because x(3)==6 is now counted for the second interval (4,6] instead of [6,8) as before.
Using bsxfun for the comparisons and exploiting find-like capabilities of max's second output:
xGrid = [2 4 6 8]; %// example data
x = [3 6 5.5 10 -10]; %// example data
comp = bsxfun(#gt, xGrid(:), x(:).'); %'// see if "x" > "xGrid"
[~, result] = max(comp, [], 1); %// index of first "xGrid" that exceeds each "x"
result = result-1; %// subtract 1 to find the last "xGrid" that is <= "x"
This approach gives 0 for values of x that lie outside xGrid. With the above example values,
result =
1 3 2 0 0
See if this works for you -
matches = bsxfun(#le,xGrid(1:end-1),x(:)) & bsxfun(#ge,xGrid(2:end),x(:))
[valid,pos] = max(cumsum(matches,2),[],2)
pos = pos.*(valid~=0)
Sample run -
xGrid =
5 2 1 6 8 9 2 1 6
x =
3 7 14
pos =
8
4
0
Explanation on the sample run -
First element of x, 3 occurs last between ...1 6 with the criteria of xGrid(i) <= xi <= xGrid(i+1) at the backend of xGrid and that 1 is at the eight position, so the first element of the output pos is 8. This continues for the second element 7, which is found between 6 and 8 and that 6 is at the fourth place in xGrid, so the second element of the output is 4. For the third element 14 which doesn't find any neighbours to satisfy the criteria xGrid(i) <= xi <= xGrid(i+1) and is therefore outputted as 0.
If x is a column this might help
xg1=meshgrid(xGrid,1:length(x));
xg2=ndgrid(x,1:length(xGrid));
[~,I]=min(floor(abs(xg1-xg2)),[],2);
or a single line implementation
[~,I]=min(floor(abs(meshgrid(xGrid,1:length(x))-ndgrid(x,1:length(xGrid)))),[],2);
Example: xGrid=[1 2 3 4 5], x=[2.5; 1.3; 1.7; 4.8; 3.3]
Result:
I =
2
1
1
4
3

How can i find the number of lowest possible square that can fit in the given square

let's suppose i have a square of 7x7.i can fill the square with other squares(i.e the squares of dimension 1x1,2x2.....6x6).How can i can fill the square with least possible smaller squares.please help me.
Consider a square with dimensions s x s. Cutting a smaller square of dimensions m x m out will result in a square of m x m, a square of n x n, and two rectangles of dimensions m x n, where m + n = s.
When s is even, the square can be divided such that m = n, in which case the rectangles will also be squares, resulting in an answer of 4.
However, when s is odd, values of m and n must be chosen such that the resulting rectangle can be filled with the least number of squares possible. There doesn't seem to be an immediately obvious way to figure out the best configuration, so I would suggest coming up with an algorithm to figure out the least number of squares that can be used to fill a rectangle of size m x n (this is a slightly simpler problem and I believe it can be solved with a recursive algorithm). The total number of squares needed will then be equal to 2 x ([number of squares in m x n rectangle] + 1). You can use a loop to check all the sizes of m between 1 and s/2.
Hope that gets you started.
Consider a square with dimensions s x s.
Factorialise s into primes. Then solve the problem for each prime sp. The answer will be the same for sp x sp as for s x s. It is probable that the smallest prime will give the lowest result. I have have no proof of this, but I have checked by hand up to 17 x 17.
This is a generalisation of Otaias notion of an even s resulting in an answer of 4.
Placiing algorithm:
You need to loop from n = (s+1)/2, rounded down, to n = s-1.
Put the n x n square in a corner.
Let m = s - n.
Place m x m squares in the adjacent corners and keep placing them until they (almost) reach the end of the n x n square.
The remaining space will be m x m (if you are lucky), or up to 2m-1 x 2m-1 with a corner piece missing.
Fill the remaining space with a similar algorithm. Start with placing a n2 x n2 square in the corner opposite to the missing corner piece.
Working by hand I have obtained the following results:
s minimum number of squares:
2 4
3 6
5 8
7 9
11 10
13 11
17 12
First check if n is even. If n is even, then the answer is four since there isn't a way to fit 3 squares or 2 squares together to make another square so that solves it for half of all possible cases
BEFORE YOU PROCEED: This approach is incomplete and this may be the WRONG approach
I just intend to throw out an out-of-the-box idea just because I feel like this may help and, hopefully, advance the problem. I feel like it may have some correlation with Goldbach's weak conjecture. The algorithm may be too long to compute for larger values, and I'm not sure how much optimization is happening.
Now my idea would be to try to enumerate all triples (n1,n2,n3) where n1 + n2 + n3 = n AND n1, n2, n3 are all prime (which are >= 2) AND n >= 7 AND n1 <= n2 <= n3
Now let me literally depict my algorithm:
Now my idea is find all possible triples (n1,n2,n3) so it fits the definition stated above. Next set n_s = n1 + n2. IF n_s > n3 follow the depiction above else flip n_s and n3
Now the problem is the white rectangles left over (that should be congruent to each other).
Let n4 x n3 denote the rectangles where:
n4 = n - 2 * n3 \\if following the depicted example
Enumerate all possible triples (n41, n42, n43) (treating n as n = n4, so n3 >= 7) and (n31, n32, n33) (treating n as n = n3, so n3 >= 7). Next find the value where n_s3 == n_s4 and both are the greatest they could be. For example:
Let's suppose x3 = 17 and x4 = 13
Enumeration of x3 = 17:
2 + 2 + 13
3 + 3 + 11
5 + 5 + 7
Enumeration of x_s3:
4 = 2 + 2
6 = 3 + 3
10 = 5 + 5
12 = 5 + 7
14 = 3 + 11
15 = 2 + 13
Enumeration of x4 = 13:
2 + 2 + 7
3 + 5 + 5
Enumeration of x_s4:
4 = 2 + 2
8 = 3 + 5
9 = 2 + 7
10 = 5 + 5
Since 10 is the largest value shared between 13 and 17, you fit a 10 by 10 square in the (both rectangles) and now you have a none parallelogram which get further and further more difficult to fill, but may be (I feel) towards the right direction.
All feed back appreciated.

Resources