Created
January 3, 2012 10:30
-
-
Save phaniram/1554390 to your computer and use it in GitHub Desktop.
Interview Street Challenges - Quadrant queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.interviewstreet.puzzles; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
/** | |
* | |
* @author cypronmaya | |
*/ | |
class Point { | |
int x; | |
int y; | |
Point(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
public class Quad_query { | |
private ArrayList<Point> arrayList; | |
public Quad_query(int capacity) { | |
this.arrayList = new ArrayList<Point>(capacity); | |
} | |
public static void main(String args[]) { | |
Scanner scanner = new Scanner(System.in); | |
int num_of_points = scanner.nextInt(); | |
Quad_query quad_query = new Quad_query(num_of_points); | |
for (int i = num_of_points; --i >= 0;) { | |
int x = scanner.nextInt(); | |
int y = scanner.nextInt(); | |
quad_query.add(x, y); | |
} | |
int num_of_queries = scanner.nextInt(); | |
for (int i = num_of_queries; --i >= 0;) { | |
char c = scanner.next().charAt(0); | |
quad_query.query(c, scanner.nextInt() - 1, scanner.nextInt() - 1); | |
} | |
} | |
public void add(int x, int y) { | |
Point point = new Point(x, y); | |
arrayList.add(point); | |
} | |
public void query(char c, int i, int j) { | |
switch (c) { | |
case 'C': | |
queryC(i, j); | |
break; | |
default: | |
queryXY(c, i, j); | |
break; | |
} | |
} | |
public void queryC(int i, int j) { | |
int quad_1 = 0, quad_2 = 0, quad_3 = 0, quad_4 = 0; | |
for (int k = i; k <= j; k++) { | |
Point pnt = arrayList.get(k); | |
int pnt_x = pnt.x; | |
int pnt_y = pnt.y; | |
if (pnt_x > 0 && pnt_y > 0) { | |
quad_1++; | |
} else if (pnt_x < 0 && pnt_y > 0) { | |
quad_2++; | |
} else if (pnt_x < 0 && pnt_y < 0) { | |
quad_3++; | |
} else if (pnt_x > 0 && pnt_y < 0) { | |
quad_4++; | |
} | |
} | |
System.out.println(quad_1 + " " + quad_2 + " " + quad_3 + " " + quad_4); | |
} | |
private void queryXY(char c, int i, int j) { | |
for (int k = i; k <= j; k++) { | |
Point pnt = arrayList.get(k); | |
int pnt_x = pnt.x; | |
int pnt_y = pnt.y; | |
if (c == 'X') { | |
pnt = new Point(pnt_x, -pnt_y); | |
} else if (c == 'Y') { | |
pnt = new Point(-pnt_x, pnt_y); | |
} | |
arrayList.set(k, pnt); | |
} | |
} | |
} |
this algorithms cannot pass the test casses
it uses o(n) :( need better than that.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quadrant Queries (30 points)
There are N points in the plane. The ith point has coordinates (xi, yi). Perform the following queries:
Input:
The first line contains N, the number of points. N lines follow.
The ith line contains xi and yi separated by a space.
The next line contains Q the number of queries. The next Q lines contain one query each, of one of the above forms.
All indices are 1 indexed.
Output:
Output one line for each query of the type "C i j". The corresponding line contains 4 integers; the number of points having indices in the range [i..j] in the 1st,2nd,3rd and 4th quadrants respectively.
Constraints:
1 <= N <= 100000
1 <= Q <= 1000000
You may assume that no point lies on the X or the Y axis.
All (xi,yi) will fit in a 32-bit signed integer
In all queries, 1 <=i <=j <=N
Sample Input:
4
1 1
-1 1
-1 -1
1 -1
5
C 1 4
X 2 4
C 3 4
Y 1 2
C 1 3
Sample Output:
1 1 1 1
1 1 0 0
0 2 0 1
Explanation
When a query says "X i j", it means that take all the points between indices i and j both including and reflect those points along the X axis. The i and j here have nothing to do with the co-ordinates of the points. They are the indices. i refers to point i and j refers to point j
'C 1 4' asks you to 'Consider the set of points having index in {1,2,3,4}. Amongst those points, how many of them lie in the 1st,2nd,3rd and 4th quads respectively?'
The answer to this is clearly 1 1 1 1.
Next we reflect the points between indices '2 4' along the X axis. So the new coordinates are :
1 1
-1 -1
-1 1
1 1
Now 'C 3 4' is 'Consider the set of points having index in {3,4}. Amongst those points, how many of them lie in the 1st,2nd,3rd and 4th quads respectively?' Point 3 lies in quadrant 2 and point 4 lies in quadrant 1.
So the answer is 1 1 0 0