Search is not available for this dataset
name stringlengths 2 112 | description stringlengths 29 13k | source int64 1 7 | difficulty int64 0 25 | solution stringlengths 7 983k | language stringclasses 4 values |
|---|---|---|---|---|---|
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
n = int(input())
arr = []
for _ in range(n):
a, b, c = map(int, input().split())
arr.append((a,b,c))
res = 0
for i in arr:
a, b, c = i
if(b != 0):
y00 = -c/b
y01 = -(a+c)/b
s1 = x1*(y01-y00)-(y1-y00)
s2 = x2*(y01-y00)-(y2-y00)
if(s1<0<s2 or s1>0>s2):
res += 1
else:
x = -c/a
if(x1<x<x2 or x1>x>x2):
res += 1
print(res) | PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 |
# Crazy Town (2 points on same/opposite side of a line)
# input
k1 = list(map(int, input().split()));
(x1,y1) = (k1[0],k1[1])
k2 = list(map(int, input().split()));
(x2,y2) = (k2[0],k2[1])
t = int(input())
d = 0;
for i in range(t):
k3 = list(map(int, input().split()));
(a,b,c) = (k3[0],k3[1],k3[2])
#
k = (a*(x1) + b*(y1) + c)/ (a*(x2) + b*(y2) + c)
if k<0:
d +=1;
print(d);
| PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | xh, yh = list(map(int, input().split()))
xu, yu = list(map(int, input().split()))
n = int(input())
res = 0
for x in range(n):
a, b, c = list(map(int, input().split()))
if (a*xh + b*yh + c)*(a*xu + b*yu + c ) < 0:
res += 1
print(res) | PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long linear(int a, int b, int c, int x, int y) {
return 1ll * a * x + 1ll * b * y + 1ll * c;
}
int main() {
int xa, ya, xb, yb, ans = 0, n;
scanf("%d %d %d %d", &xa, &ya, &xb, &yb);
scanf("%d", &n);
while (n--) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
long long A = linear(a, b, c, xa, ya);
long long B = linear(a, b, c, xb, yb);
ans += ((A > 0 && B < 0) || (A < 0 && B > 0));
}
printf("%d", ans);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter pw;
static Scanner sc;
static long ceildiv(long x, long y) { return y==0?inf:(x+y-1)/y; }
static int mod(long x, int m) { return (int)((x%m+m)%m); }
static void put(TreeMap<Integer, Integer> map, Integer p){if(map.containsKey(p)) map.replace(p, map.get(p)+1); else map.put(p, 1); }
static void rem(TreeMap<Integer, Integer> map, Integer p){ if(map.get(p)==1) map.remove(p);else map.replace(p, map.get(p)-1); }
static void printf(double x, int dig){ String s="%."+dig+"f"; pw.printf(s, x); }
static long gcd(long x, long y) {return y==0?x:gcd(y, x%y);}
static int Int(boolean x){ return x?1:0; }
static final int inf=(int)1e9, mod=inf+7;
static boolean[] visit;
static final double r2=Math.sqrt(2), eps=1e-7;
public static void main(String[] args) throws IOException {
sc = new Scanner(System.in);
pw=new PrintWriter(System.out);
point st=new point(sc.nextInt(), sc.nextInt()), end=new point(sc.nextInt(), sc.nextInt());
segment path=new segment(st, end);
int n=sc.nextInt();
int ans=0;
for(int i=0; i<n; i++){
int a=sc.nextInt(), b=sc.nextInt(), c=sc.nextInt();
ans+=path.intersect(new segment(a, b, c))?1:0;
}
pw.println(ans);
pw.close();
}
static class point{
double x, y;
public point(double a, double b){
x=a;
y=b;
}
public String toString(){
return x+" "+y;
}
}
static class segment{
point st, end;
double x, y, c;
public segment(point st, point end){
this.st=st;
this.end=end;
double dx=end.x-st.x, dy=end.y-st.y;
double m;
if(Math.abs(dx)<eps)
m=inf;
else
m=dy/dx;
double b=st.y-(m*st.x);
if(m==inf){
x=1;
y=0;
c=-st.x;
}else{
x=-m;
y=1;
c=-b;
}
}
public segment(int a, int b, int z){
x=a; y=b; c=z;
if(y==0){
st=new point(-c, -1e6);
end=new point(-c, 1e6);
}else if(zero(x/y)){
st=new point(-1e6, -c);
end=new point(1e6, -c);
}else{
double m=-x/y;
if(m>1){
st=new point(-1e6/m, -1e6);
end=new point(1e6/m, 1e6);
}else{
st=new point(-1e6, -1e6*m);
end=new point(1e6, 1e6*m);
}
}
}
public boolean intersect(segment s){
double m1=s.y==0?inf:s.x/s.y, m2=y==0?inf:x/y;
if(Math.abs(m1-m2)<eps)
return Math.abs(s.c-c)<eps;
double a,b, r;
if(!zero(s.x) && !zero(s.y) && !zero(x) && !zero(y)){
r=(s.y/y);
a=(c*r-s.c)/(s.x-x*r);
r=s.x/x;
b=(c*r-s.c)/(s.y-y*r);
}else{
if(!zero(s.y) && !zero(y)){
r=(s.y/y);
a=(c*r-s.c)/(s.x-x*r);
b=zero(s.x)?-s.c/s.y:-c/y;
}else if(!zero(s.x) && !zero(x)){
r=s.x/x;
b=(c*r-s.c)/(s.y-y*r);
a=zero(s.y)?-s.c/s.x:-c/x;
}else{
if(y==0){
a=-c/x;
b=-s.c/s.y;
}else{
a=-s.c/s.x;
b=-c/y;
}
}
}
return inSeg(a, b, this);
}
public String toString(){
return x+" "+y+" "+c;
}
public boolean zero(double x){
return Math.abs(x)<eps;
}
public boolean inSeg(double x, double y, segment s){
return validX(x, s) && validY(y, s);
}
public boolean validX(double x, segment s){
if(Math.abs(x-s.st.x)<eps || Math.abs(x-s.end.x)<eps)
return true;
return x>Math.min(s.st.x, s.end.x) && x<Math.max(s.st.x, s.end.x);
}
public boolean validY(double y, segment s){
if(Math.abs(y-s.st.y)<eps || Math.abs(y-s.end.y)<eps)
return true;
return y>Math.min(s.st.y, s.end.y) && y<Math.max(s.st.y, s.end.y);
}
}
static void printArr(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
pw.print(arr[i] + " ");
pw.println(arr[arr.length - 1]);
}
static void printArr(long[] arr) {
for (int i = 0; i < arr.length - 1; i++)
pw.print(arr[i] + " ");
pw.println(arr[arr.length - 1]);
}
static void printArr(Integer[] arr) {
for (int i = 0; i < arr.length; i++)
pw.print(arr[i] + " ");
pw.println();
}
static void printArr(char[] arr) {
for (int i = 0; i < arr.length; i++)
pw.print(arr[i]==0? '1': arr[i]);
pw.println();
}
static void printArr(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++)
pw.print(list.get(i)+" ");
pw.println();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public int[] nextDigits() throws IOException{
String s=nextLine();
int[] arr=new int[s.length()];
for(int i=0; i<arr.length; i++)
arr[i]=s.charAt(i)-'0';
return arr;
}
public int[] nextArr(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++)
arr[i] = nextInt();
return arr;
}
public Integer[] nextsort(int n) throws IOException{
Integer[] arr=new Integer[n];
for(int i=0; i<n; i++)
arr[i]=nextInt();
return arr;
}
public Pair nextPair() throws IOException{
return new Pair(nextInt(), nextInt());
}
public Pair[] nextPairArr(int n) throws IOException{
Pair[] arr=new Pair[n];
for(int i=0; i<n; i++)
arr[i]=nextPair();
return arr;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class Pair implements Comparable<Pair>{
int x;
int y;
public Pair(int x, int y) {
this.x=x;
this.y=y;
}
public int hashCode() {
return (this.x*1000+this.y);
}
public int compareTo(Pair p){
return x-p.x;
}
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (this.getClass() != obj.getClass()) {
return false;
}
Pair p = (Pair) obj;
return this.x==p.x && this.y==p.y;
}
public Pair clone(){
return new Pair(x, y);
}
public String toString(){
return this.x+" "+this.y;
}
public void add(Pair p){
x+=p.x;
y+=p.y;
}
}
static class Union{
int[] rep, size;
int sets;
public Union(int n){
rep=new int[n];
size=new int[n];
sets=n;
for(int i=0; i<n; i++){
rep[i]=i;
size[i]=1;
}
}
public void join(int x, int y){
int a=getSet(x), b=getSet(y);
if(a==b){
return;
}
sets--;
if(size[a]>size[b]){
rep[b]=a;
size[a]+=size[b];
}else{
rep[a]=b;
size[b]+=size[a];
}
}
public int getSet(int x){
if(rep[x]==x)
return x;
return rep[x]=getSet(rep[x]);
}
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int x1, yy1, x2, y2;
int result;
void init() { scanf("%d%d%d%d", &x1, &yy1, &x2, &y2); }
long long calc(int a, int b, int c, int x, int y) {
long long t1, t2, t;
t1 = a;
t1 *= x;
t2 = b;
t2 *= y;
return t1 + t2 + c;
}
void work() {
int n;
int a, b, c;
long long t1, t2, t;
scanf("%d", &n);
result = 0;
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &a, &b, &c);
t1 = calc(a, b, c, x1, yy1);
t2 = calc(a, b, c, x2, y2);
if ((t1 > 0) && (t2 < 0)) ++result;
if ((t1 < 0) && (t2 > 0)) ++result;
}
}
void output() { printf("%d\n", result); }
int main() {
init();
work();
output();
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | a1, b1 = map(int, raw_input().split(' '))
a2, b2 = map(int, raw_input().split(' '))
n = input()
count = 0
for i in range(n):
a, b, c = map(int, raw_input().split(' '))
t1 = a1 * a + b1 * b + c
t2 = a2 * a + b2 * b + c
if (t1 > 0 and t2 < 0) or (t1 < 0 and t2 > 0):
count += 1
print count | PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long max(long long a, long long b) {
if (a > b) {
return a;
} else {
return b;
}
}
long long min(long long a, long long b) {
if (a < b) {
return a;
} else {
return b;
}
}
struct P {
double x, y;
P() {}
P(double x, double y) : x(x), y(y) {}
P operator+(const P &a) const { return P(x + a.x, y + a.y); }
P operator-(const P &a) const { return P(x - a.x, y - a.y); }
double operator^(const P &a) const { return x * a.y - y * a.x; }
void in() { scanf("%lf%lf", &x, &y); }
void out() { printf("REQUIRED %.7f %.7f\n", x, y); }
double dist(P a, P b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double sqdist(P a, P b) {
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
};
int xx, yy, xxx, yyy;
int dx, dy;
P PA, PB, PC;
int cross = 0;
int n;
int a, b, c;
double d1, d2, e1, e2;
;
int main() {
scanf("%d%d%d%d", &xx, &yy, &xxx, &yyy);
scanf("%d", &n);
for (int i = (1); i <= (n); i++) {
scanf("%d%d%d", &a, &b, &c);
if (a == 0) {
d1 = 0.00;
e1 = 2.00;
d2 = e2 = (double)(-1 * c) / b;
} else if (b == 0) {
d2 = e2 = 0;
e2 += 2.00;
d1 = e1 = (double)(-1 * c) / a;
} else {
d1 = 0.00;
d2 = (double)(-1 * c) / b;
e2 = d2 + 1.00;
e1 = (double)(-1 * c - b * e2) / a;
}
PA = P(e1 - d1, e2 - d2);
PB = P((double)xx - e1, (double)yy - e2);
PC = P((double)xxx - e1, (double)yyy - e2);
double res1 = PA ^ PB;
double res2 = PA ^ PC;
if ((res1 > 0 && res2 < 0) || (res1 < 0 && res2 > 0)) cross++;
}
printf("%d\n", cross);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = 300 + 10;
int a, b, c, n, sx, sy, tx, ty;
long long calc(int x, int y) { return 1ll * a * x + 1ll * b * y + 1ll * c; }
int main() {
ios::sync_with_stdio(0);
cin >> sx >> sy >> tx >> ty >> n;
int ans = 0;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
long long k1 = calc(sx, sy), k2 = calc(tx, ty);
if ((k1 > 0 && k2 < 0) || (k1 < 0 && k2 > 0)) ++ans;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, k, m;
int X1, X2, Y1, Y2;
struct ac {
int a, b, c;
};
void input() {
int i, ans = 0;
long long a, b, c;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%lld %lld %lld", &a, &b, &c);
long long x = (long long)a * X1 + (long long)b * Y1 + (long long)c;
long long y = (long long)a * X2 + (long long)b * Y2 + (long long)c;
if (x > 0 && y < 0 || x < 0 && y > 0) {
ans++;
}
}
printf("%d\n", ans);
}
void solve() {}
int main() {
while (scanf("%d %d %d %d", &X1, &Y1, &X2, &Y2) != EOF) {
input();
solve();
}
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Main {
static PrintWriter pr = new PrintWriter(System.out);
public static final double EPS = 1e-9;
static Main X = new Main();
public static void main(String args[]) throws IOException{
//FileReader in = new FileReader("C:/test.txt");
//BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
Scanner sc = new Scanner(System.in);
Point p1 = new Point(sc.nextDouble(), sc.nextDouble());
Point p2 = new Point(sc.nextDouble(),sc.nextDouble());
Line lM = X.new Line(p1,p2);
int n = sc.nextInt();
int counter=0;
for(int i=0; i<n; i++) {
double a = sc.nextDouble();
double b = sc.nextDouble();
double c = sc.nextDouble();
Line l;
if(Math.abs(a)<EPS) {
//l = X.new Line(new Point(5.0, -c/b), new Point(6.0,-c/b));
Point pi1 = new Point(5.0, -c/b);
Point pi2 = new Point(6.0,-c/b);
Vector v = X.new Vector(pi1,pi2);
Vector v2 = X.new Vector(pi1,p1);
Vector v3 = X.new Vector(pi1, p2);
if((v.cross(v2)<EPS && v.cross(v3)+EPS>0)||(v.cross(v2)+EPS>0 && v.cross(v3)<EPS))
counter++;
}else if(Math.abs(b)<EPS) {
//l = X.new Line(new Point(-c/a,9.0), new Point(-c/a, 5.0));
Point pi1 = new Point(-c/a, 9.0);
Point pi2 = new Point(-c/a,5.0);
Vector v = X.new Vector(pi1,pi2);
Vector v2 = X.new Vector(pi1,p1);
Vector v3 = X.new Vector(pi1, p2);
if((v.cross(v2)<EPS && v.cross(v3)+EPS>0)||(v.cross(v2)+EPS>0 && v.cross(v3)<EPS))
counter++;
}else {
//l = X.new Line(new Point(0.0,-c/b), new Point(-c/a,0.0));
Point pi1 = new Point(1.0, (-c-a)/b);
Point pi2 = new Point((-c-b)/a,1.0);
Vector v = X.new Vector(pi1,pi2);
Vector v2 = X.new Vector(pi1,p1);
Vector v3 = X.new Vector(pi1, p2);
if((v.cross(v2)<EPS && v.cross(v3)+EPS>0)||(v.cross(v2)+EPS>0 && v.cross(v3)<EPS))
counter++;
}
}
pr.println(counter);
pr.flush();
pr.close();
}
static class Point
{
double x,y;
public Point(double a, double b) { x = a;y = b;}
boolean between(Point p, Point q) {
return x<Math.max(p.x,q.x)+EPS && x+EPS>Math.min(p.y, q.y) && y<Math.max(p.y, q.y)+EPS && y+EPS>Math.min(p.y, q.y);
}
}
public class Line {
static final double INF = 1e9, EPS = 1e-9;
double a, b, c;
Line(Point p, Point q)
{
if(Math.abs(p.x - q.x) < EPS) { a = 1; b = 0; c = -p.x; }
else
{
a = (p.y - q.y) / (q.x - p.x);
b = 1.0;
c = -(a * p.x + p.y);
}
}
Line(Point p, double m) { a = -m; b = 1; c = -(a * p.x + p.y); }
Line(double a, double b, double c){
if(Math.abs(b)<EPS) {
this.b = 0;
this.a = 1.0;
this.c = c/a;
}else {
this.a = a/b;
this.b = 1.0;
this.c = c/b;
}
}
boolean parallel(Line l) { return Math.abs(a - l.a) < EPS && Math.abs(b - l.b) < EPS; }
boolean same(Line l) { return parallel(l) && Math.abs(c - l.c) < EPS; }
Point intersect(Line l)
{
if(parallel(l))
return null;
double x = (b * l.c - c * l.b) / (a * l.b - b * l.a);
double y;
if(Math.abs(b) < EPS)
y = -l.a * x - l.c;
else
y = -a * x - c;
return new Point(x, y);
}
Point closestPoint(Point p)
{
if(Math.abs(b) < EPS) return new Point(-c, p.y);
if(Math.abs(a) < EPS) return new Point(p.x, -c);
return intersect(new Line(p, 1 / a));
}
}
public class LineSegment {
Point p, q;
LineSegment(Point a, Point b) { p = a; q = b; }
boolean intersect(LineSegment ls)
{
Line l1 = new Line(p, q), l2 = new Line(ls.p, ls.q);
if(l1.parallel(l2))
{
if(l1.same(l2))
return p.between(ls.p, ls.q) || q.between(ls.p, ls.q) || ls.p.between(p, q) || ls.q.between(p, q);
return false;
}
Point c = l1.intersect(l2);
return c.between(p, q) && c.between(ls.p, ls.q);
}
}
public class Vector {
double x, y;
Vector(double a, double b) { x = a; y = b; }
Vector(Point a, Point b) { this(b.x - a.x, b.y - a.y); }
Vector scale(double s) { return new Vector(x * s, y * s); } //s is a non-negative value
double dot(Vector v) { return (x * v.x + y * v.y); }
double cross(Vector v) { return x * v.y - y * v.x; }
double norm2() { return x * x + y * y; }
Vector reverse() { return new Vector(-x, -y); }
Vector normalize()
{
double d = Math.sqrt(norm2());
return scale(1 / d);
}
}
static class pair{
int num;
LineSegment l;
pair(int num, LineSegment l){this.num = num;this.l = l;}
}
static class UnionFind {
int[] p, rank;
UnionFind(int N)
{
p = new int[N];
rank = new int[N];
for (int i = 0; i < N; i++)
p[i] = i;
}
int findSet(int x) { return p[x] == x ? x : (p[x] = findSet(p[x])); }
boolean union(int x, int y)
{
x = findSet(x);
y = findSet(y);
if(x == y)
return false;
if (rank[x] > rank[y])
p[y] = x;
else
{
p[x] = y;
if(rank[x] == rank[y])
++rank[y];
}
return true;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(String f) throws FileNotFoundException {
br = new BufferedReader(new FileReader(f));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public int[] nextIntArray1(int n) throws IOException {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
public int[] shuffle(int[] a, int n) {
int[] b = new int[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
int t = b[i];
b[i] = b[j];
b[j] = t;
}
return b;
}
public int[] nextIntArraySorted(int n) throws IOException {
int[] a = nextIntArray(n);
a = shuffle(a, n);
Arrays.sort(a);
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArray1(int n) throws IOException {
long[] a = new long[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextLong();
return a;
}
public long[] nextLongArraySorted(int n) throws IOException {
long[] a = nextLongArray(n);
Random r = new Random();
for (int i = 0; i < n; i++) {
int j = i + r.nextInt(n - i);
long t = a[i];
a[i] = a[j];
a[j] = t;
}
Arrays.sort(a);
return a;
}
}
//
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
public class CF284C {
static long x1, y1, x2, y2;
static int result;
public static void main(String[] args) throws Exception {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(r.readLine());
x1 = Integer.parseInt(st.nextToken());
y1 = Integer.parseInt(st.nextToken());
st = new StringTokenizer(r.readLine());
x2 = Integer.parseInt(st.nextToken());
y2 = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(r.readLine());
result = 0;
for (int i = 0; i < n; i++) {
st = new StringTokenizer(r.readLine());
long a = Integer.parseInt(st.nextToken());
long b = Integer.parseInt(st.nextToken());
long c = Integer.parseInt(st.nextToken());
long p1 = a * x1 + b * y1 + c;
long p2 = a * x2 + b * y2 + c;
if (p1 > 0 && p2 < 0) {
result++;
}
if (p1 < 0 && p2 > 0) {
result++;
}
}
pw.println(result);
pw.flush();
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long x1, y1, x2, y2, n, a, b, c, step = 0, i;
cin >> x1 >> y1 >> x2 >> y2 >> n;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
if ((a * x1 + b * y1 + c > 0) && (a * x2 + b * y2 + c < 0)) step++;
if ((a * x1 + b * y1 + c < 0) && (a * x2 + b * y2 + c > 0)) step++;
}
cout << step;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.StringTokenizer;
public class A
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
int xh = Integer.parseInt(tokenizer.nextToken());
int yh = Integer.parseInt(tokenizer.nextToken());
tokenizer = new StringTokenizer(reader.readLine());
int xu = Integer.parseInt(tokenizer.nextToken());
int yu = Integer.parseInt(tokenizer.nextToken());
int calc = 0;
int n = Integer.parseInt(reader.readLine());
for(int i = 0 ; i < n ; i++)
{
tokenizer = new StringTokenizer(reader.readLine());
long a = Integer.parseInt(tokenizer.nextToken());
long b = Integer.parseInt(tokenizer.nextToken());
long c = Integer.parseInt(tokenizer.nextToken());
long v1 = a*xh + b*yh + c > 0 ? 1 : -1;
long v2 = a*xu + b*yu + c > 0 ? 1 : -1;
if(v1*v2 == -1)
calc++;
}
System.out.println(calc);
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);
struct Point {
int x, y;
Point() {}
Point(int x, int y) : x(x), y(y) {}
void read() { scanf("%d%d", &x, &y); }
} A, B;
struct Segment {
Point p, q;
Segment() {}
Segment(Point _p, Point _q) {
p = _p;
q = _q;
}
} second;
struct Line {
int a, b, c;
Line() {}
Line(int a, int b, int c) : a(a), b(b), c(c) {}
void read() { scanf("%d%d%d", &a, &b, &c); }
long long getValue(Point p) {
return 1ll * a * p.x + 1ll * b * p.y + 1ll * c;
}
int sgn(Point p) {
long long val = getValue(p);
if (val > 0) return 1;
return -1;
}
bool intersect(Segment second) {
int s1 = sgn(second.p);
int s2 = sgn(second.q);
return s1 != s2;
}
} a[100100];
int n;
int main() {
A.read();
B.read();
scanf("%d", &n);
for (int i = 0; i < n; i++) a[i].read();
int ans = 0;
second = Segment(A, B);
for (int i = 0; i < n; i++) {
if (a[i].intersect(second)) ans++;
}
printf("%d\n", ans);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long a, b, c;
int n;
long long X1, Y1, X2, Y2;
long long sign(long long a) {
if (a > 0) return 1;
if (a < 0) return -1;
return 0;
}
int main() {
cin >> X1 >> Y1 >> X2 >> Y2;
cin >> n;
int ans = 0;
for (int i = 0; i < n; ++i) {
cin >> a >> b >> c;
if (sign(a * X1 + b * Y1 + c) != sign(a * X2 + b * Y2 + c)) ans++;
}
cout << ans;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
struct point {
long long x, y;
};
struct line {
long long a, b, c;
};
const int MAXN = 300;
int n, ans;
point a, b;
line l[MAXN + 9];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cin >> a.x >> a.y >> b.x >> b.y >> n;
ans = 0;
for (int i = 1; i <= n; i++) {
cin >> l[i].a >> l[i].b >> l[i].c;
if ((l[i].a * a.x + l[i].b * a.y + l[i].c < 0) !=
(l[i].a * b.x + l[i].b * b.y + l[i].c < 0)) {
ans++;
}
}
cout << ans << "\n";
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int long long hx, hy, ux, uy, n, a, b, c, val1, val2, count = 0;
cin >> hx >> hy;
cin >> ux >> uy;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
val1 = a * hx + b * hy + c;
val2 = a * ux + b * uy + c;
if (val1 > 0 && val2 < 0)
count++;
else if (val1 < 0 && val2 > 0)
count++;
}
cout << count << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
/**
* Created by hama_du
*/
public class ProblemA {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
long x1 = in.nextInt();
long y1 = in.nextInt();
long x2 = in.nextInt();
long y2 = in.nextInt();
int n = in.nextInt();
long[][] lines = new long[n][3];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
lines[i][j] = in.nextInt();
}
}
boolean[] r1 = doit(lines, x1, y1);
boolean[] r2 = doit(lines, x2, y2);
int d = 0;
for (int i = 0 ; i < r1.length ; i++) {
d += (r1[i] ^ r2[i]) ? 1 : 0;
}
out.println(d);
out.flush();
}
private static boolean[] doit(long[][] lines, long x1, long y1) {
boolean[] r = new boolean[lines.length];
for (int i = 0 ; i < r.length ; i++) {
long[] abc = lines[i];
long val = abc[0] * x1 + abc[1] * y1 + abc[2];
r[i] = val > 0;
}
return r;
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int next() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = next();
while (isSpaceChar(c))
c = next();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static void solve() throws Exception {
int x1 = nextInt();
int y1 = nextInt();
int x2 = nextInt();
int y2 = nextInt();
int res = 0;
int n = nextInt();
for (int i = 0; i < n; ++i) {
int a = nextInt();
int b = nextInt();
int c = nextInt();
long s1 = (long)a * x1 + (long)b * y1 + c;
long s2 = (long)a * x2 + (long)b * y2 + c;
if ((s1 < 0 && s2 > 0) || (s1 > 0 && s2 < 0)) {
++res;
}
}
out.println(res);
}
static long sqr(int x) {
return (long)x * x;
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long lcm(int a, int b) {
return (long)a / gcd(a, b) * b;
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static BigInteger nextBigInteger() throws IOException {
return new BigInteger(next());
}
static String next() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static String nextLine() throws IOException {
tok = new StringTokenizer("");
return in.readLine();
}
static boolean hasNext() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
String s = in.readLine();
if (s == null) {
return false;
}
tok = new StringTokenizer(s);
}
return true;
}
public static void main(String args[]) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
//in = new BufferedReader(new FileReader("input.in"));
//out = new PrintWriter(new FileWriter("output.out"));
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
java.lang.System.exit(1);
}
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
namespace qwq {
inline long long read(long long &o) {
o = 0;
long long w = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
o = (o << 3) + (o << 1) + (c ^ 48);
c = getchar();
}
return o * w;
}
inline void write(long long x) {
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
inline long long max(long long x, long long y) { return x > y ? x : y; }
inline long long min(long long x, long long y) { return x < y ? x : y; }
} // namespace qwq
using namespace qwq;
class Point {
public:
long long x, y;
void add() { scanf("%lld%lld", &x, &y); }
} a, b;
class Line {
public:
long long a, b, c;
void add() { scanf("%lld%lld%lld", &a, &b, &c); }
} l;
inline bool check(Point p, Line l) {
if (l.a * p.x + l.b * p.y + l.c < 0)
return 1;
else
return 0;
}
long long n, ans;
signed main() {
a.add(), b.add();
read(n);
for (register long long i = 1; i <= n; ++i) {
l.add();
if (check(a, l) != check(b, l)) ++ans;
}
write(ans);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.util.Scanner;
public class A498 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int x1 = input.nextInt();
int y1 = input.nextInt();
int x2 = input.nextInt();
int y2 = input.nextInt();
int N = input.nextInt();
int answer = 0;
for (int n=0; n<N; n++) {
long a = input.nextInt();
long b = input.nextInt();
long c = input.nextInt();
if (Math.signum(a*x1+b*y1+c) != Math.signum(a*x2+b*y2+c)) {
answer++;
}
}
System.out.println(answer);
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long bgm(long long a, long long b, long long m) {
a %= m;
long long rem = 1;
while (b) {
if (b & 1) rem = (rem * a) % m;
a = (a * a) % m;
b >>= 1;
}
return rem;
}
long long inv(long long a, long long mod) { return bgm(a, mod - 2, mod); }
long long in() {
long long a;
assert(scanf("%lld", &a) != EOF);
return a;
}
double dl() {
double a;
assert(scanf("%lf", &a) != EOF);
return a;
}
const int MAX = 105;
const int LEN = 13;
const long long MOD = 1000000007;
double ttm;
long long x1, x2, yy1, y2;
int n;
long long a, b, c;
long long xx, yy;
long long ans;
int main() {
x1 = in(), yy1 = in(), x2 = in(), y2 = in();
n = in();
while (n--) {
a = in(), b = in(), c = in();
xx = a * x1 + b * yy1 + c;
yy = a * x2 + b * y2 + c;
ans += (xx > 0 and yy < 0) or (xx < 0 and yy > 0);
}
printf("%lld", (long long)(ans)), puts("");
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | homex,homey=map(int, raw_input().split())
unix,uniy=map(int, raw_input().split())
n=int(raw_input())
lines=0
for i in range(n):
a,b,c=map(int, raw_input().split())
if((a*homex + b*homey + c > 0) and (a*unix + b*uniy + c < 0)):
lines+=1
elif((a*homex + b*homey + c < 0) and (a*unix + b*uniy + c > 0)):
lines+=1
print lines | PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | x1, y1 = map(float, input().split())
x2, y2 = map(float, input().split())
n = int(input())
ans = 0
for i in range(n):
a, b, c = map(int, input().split())
if (a * x1 + b * y1 + c) * (a * x2 + b * y2 + c) < 0:
ans += 1
print(ans)
| PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
}
class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
double homex = in.nextDouble();
double homey = in.nextDouble();
double universityx = in.nextDouble();
double universityy = in.nextDouble();
int numberOfRoad = in.nextInt();
double a,b,c;
long count = 0;
Line des = new Line(homex,homey,universityx,universityy);
Line cur;
Point inter;
double dab,da,db;
dab = distance(homex,homey,universityx,universityy);
for(int i = 0; i < numberOfRoad; ++i){
a = in.nextDouble();
b = in.nextDouble();
c = in.nextDouble();
cur = new Line(a,b,c);
inter = des.intersect(cur);
if(inter == null) continue;
da = distance(inter.x,inter.y,homex,homey);
db = distance(inter.x,inter.y,universityx,universityy);
if(da < dab && db < dab) ++count;
}
out.println(count);
}
double distance(double x1, double y1, double x2, double y2){
return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
}
class Point implements Comparable<Point> {
public double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public int compareTo(Point o) {
// return Double.compare(Math.atan2(y, x), Math.atan2(o.y, o.x));
return Double.compare(x, o.x) != 0 ? Double.compare(x, o.x) : Double.compare(y, o.y);
}
}
class Line {
public double a, b, c;
double EPS = 1e-10;
public Line(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public Line(double p1x,double p1y, double p2x,double p2y) {
a = +(p1y - p2y);
b = -(p1x - p2x);
c = p1x * p2y - p2x * p1y;
}
public int sign(double a) {
return a < -EPS ? -1 : a > EPS ? 1 : 0;
}
public Point intersect(Line line) {
double d = a * line.b - line.a * b;
if (sign(d) == 0) {
return null;
}
double x = -(c * line.b - line.c * b) / d;
double y = -(a * line.c - line.a * c) / d;
return new Point(x, y);
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | // practice with rainboy
import java.io.*;
import java.util.*;
public class CF498A extends PrintWriter {
CF498A() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF498A o = new CF498A(); o.main(); o.flush();
}
void main() {
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
int n = sc.nextInt();
int cnt = 0;
while (n-- > 0) {
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if (Long.signum((long) a * x1 + (long) b * y1 + c) != Long.signum((long) a * x2 + (long) b * y2 + c))
cnt++;
}
println(cnt);
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 10;
const int K = 11;
const int MOD = int(1e9) + 7;
const int INF = int(2e9) + 5;
const long long INF64 = 1e18;
void Solve() {
long long x1, y1;
cin >> x1 >> y1;
long long x2, y2;
cin >> x2 >> y2;
int cnt = 0;
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
long long a, b, c;
cin >> a >> b >> c;
long long f = a * x1 + b * y1 + c;
long long s = a * x2 + b * y2 + c;
if ((f > 0 && s < 0) || (f < 0 && s > 0)) cnt++;
}
cout << cnt << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
t = 1;
while (t--) {
Solve();
}
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 |
def side(x, y, a, b, c):
return a*x + b*y + c > 0
xA, yA = map(int, raw_input().split())
xB, yB = map(int, raw_input().split())
numSteps = 0
for _ in xrange(input()):
a, b, c = map(int, raw_input().split())
if side(xA, yA, a, b, c) != side(xB, yB, a, b, c):
numSteps += 1
print numSteps
| PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 |
import java.io.*;
public class scratch6 {
public static void main (String [] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
PrintWriter pw=new PrintWriter (new OutputStreamWriter (System.out));
String []S=br.readLine().split(" ");//first pt
long x1=Long.parseLong(S[0]);
long y1=Long.parseLong(S[1]);
S=br.readLine().split(" ");//2nd pt
long x2=Long.parseLong(S[0]);
long y2=Long.parseLong(S[1]);
int n=Integer.parseInt(br.readLine());//num of roads
int oppSideCount=0;
for (int i=0;i<n;i++){
S=br.readLine().split(" ");
long a=Long.parseLong(S[0]);
long b=Long.parseLong(S[1]);
long c=Long.parseLong(S[2]);
//long p1=(a*x1)+(b*y1)+c;
//long p2=(a*x2)+(b*y2)+c;
if (((double)((a*x1)+(b*y1)+c)/((a*x2)+(b*y2)+c))>0){
} else {
oppSideCount++;
//System.out.println ("i "+i+" and p1/p2 ="+(p1/p2));
}
}
pw.println (oppSideCount);
pw.flush();
pw.close();
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
double hx, hy, ux, uy;
int n, steps = 0;
cin >> hx >> hy >> ux >> uy >> n;
while (n--) {
double a, b, c;
cin >> a >> b >> c;
steps += int((a * hx + b * hy + c) * (a * ux + b * uy + c) < 0);
}
cout << steps;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
template <class T>
inline T min(T &a, T &b) {
return a < b ? a : b;
}
template <class T>
inline T max(T &a, T &b) {
return a > b ? a : b;
}
template <class T>
void read(T &x) {
char ch;
while ((ch = getchar()) && !(ch >= '0' && ch <= '9'))
;
x = ch - '0';
while ((ch = getchar()) && (ch >= '0' && ch <= '9')) x = x * 10 + ch - '0';
}
struct point {
int x, y;
point() {}
point(int _x, int _y) : x(_x), y(_y) {}
};
long long Pow(long long a, long long b, long long mod) {
long long res = 1;
a %= mod;
for (; b; b >>= 1) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
}
return res;
}
long long Pow(long long a, long long b) {
long long res = 1;
for (; b; b >>= 1) {
if (b & 1) res = res * a;
a = a * a;
}
return res;
}
long long x1, Y1, x2, y2, a, b, c;
int n, ans;
int main() {
cin >> x1 >> Y1 >> x2 >> y2;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a >> b >> c;
long long p = a * x1 + b * Y1 + c, q = a * x2 + b * y2 + c;
if ((p < 0 && q > 0) || (p > 0 && q < 0)) ans++;
}
cout << ans;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int n, cnt = 0;
long long x1, y1, x2, y2, x3, y3, c;
cin >> x1 >> y1;
cin >> x2 >> y2;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> x3 >> y3 >> c;
cnt += (x1 * x3 + y1 * y3 + c < 0 && x2 * x3 + y2 * y3 + c > 0) ||
(x1 * x3 + y1 * y3 + c > 0 && x2 * x3 + y2 * y3 + c < 0);
}
cout << cnt;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new PrintWriter(System.out));
String[] in = br.readLine().split(" ");
long x1 = Long.parseLong(in[0]);
long y1 = Long.parseLong(in[1]);
in = br.readLine().split(" ");
long x2 = Long.parseLong(in[0]);
long y2 = Long.parseLong(in[1]);
int n = Integer.parseInt(br.readLine());
long[] a =new long[n];
long[] b = new long[n];
long[] c = new long[n];
for(int i=0;i<n;i++){
in = br.readLine().split(" ");
a[i] = Long.parseLong(in[0]);
b[i] = Long.parseLong(in[1]);
c[i] = Long.parseLong(in[2]);
}
long cnt = 0;
for(int i=0;i<n;i++){
if( (1L*a[i]*x1+b[i]*y1+c[i] >= 0) && (1L*a[i]*x2+b[i]*y2+c[i] < 0) || (1L*a[i]*x1+b[i]*y1+c[i] < 0) && (1L*a[i]*x2+b[i]*y2+c[i] >= 0)) {
cnt +=1;
}
}
System.out.println(cnt);
bw.close();
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long x1, y1, x2, y2, n, ai, bi, ci, ans = 0;
cin >> x1 >> y1 >> x2 >> y2 >> n;
for (int i = 0; i < n; i++) {
cin >> ai >> bi >> ci;
long long f1 = ai * x1 + bi * y1 + ci;
long long f2 = ai * x2 + bi * y2 + ci;
if ((f1 > 0 && f2 < 0) || (f1 < 0 && f2 > 0)) ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const long long inf = 1e9;
const long long inf64 = 1e18;
const long long MOD = inf + 7;
const long double PI = acos(-1.0);
int32_t main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
;
long long x[2], y[2];
cin >> x[0] >> y[0];
cin >> x[1] >> y[1];
long long ans = 0;
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
long long a, b, c;
cin >> a >> b >> c;
long long t1 = a * x[0] + b * y[0] + c;
long long t2 = a * x[1] + b * y[1] + c;
long long c1 = t1 / abs(t1);
long long c2 = t2 / abs(t2);
if (c1 * c2 < 0) ans++;
}
cout << ans;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | x1,y1=map(int,input().split())
x2,y2=map(int,input().split())
a1=y1-y2
b1=x2-x1
c1=x2*(y2-y1)-y2*(x2-x1)
def intersect(a2,b2,c2):
global a1,b1,c1,x1,y1,x2,y2
if(a1*b2==a2*b1):
return False
x=(b1*c2-b2*c1)/(a1*b2-b1*a2)
y=(a1*c2-c1*a2)/(b1*a2-a1*b2)
if(min(x1,x2)<=x<=max(x1,x2) and min(y1,y2)<=y<=max(y1,y2)):
return True
return False
m=int(input())
ans=0
for i in range(m):
a2,b2,c2=map(int,input().split())
if(intersect(a2,b2,c2)):
ans+=1
print(ans) | PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
long long n, i, m, x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
long long ans = 0;
cin >> n;
long long a, b, c;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
if ((a * x1 + b * y1 + c < 0 && a * x2 + b * y2 + c > 0) ||
(a * x1 + b * y1 + c > 0 && a * x2 + b * y2 + c < 0))
ans++;
}
cout << ans;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long a, b, c;
int main() {
int n, i, ans = 0;
long long r1, r2, c1, c2, t1, t2;
cin >> r1 >> c1 >> r2 >> c2;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> a >> b >> c;
t1 = (r1 * a + c1 * b + c);
t2 = (r2 * a + c2 * b + c);
if (t1 > 0 && t2 < 0 || t1 < 0 && t2 > 0) ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
public class CF284C
{
static int ans=0;
static long x1,y1,x2,y2;
public static void solver(long a,long b,long c)
{
long x=a*x1+b*y1+c,y=a*x2+b*y2+c;
if((x<0l && y>0l) || (x>0l && y<0l))
ans++;
}
public static void main(String args[])
{
InputReader in = new InputReader(System.in);
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
/*------------------------------My Code starts here------------------------------*/
int i=0,n;
x1=in.nextLong();
y1=in.nextLong();
x2=in.nextLong();
y2=in.nextLong();
n=in.nextInt();
for(;i<n;i++)
solver(in.nextLong(), in.nextLong(), in.nextLong());
out.println(ans);
out.close();
/*------------------------------The End------------------------------------------*/
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream inputstream) {
reader = new BufferedReader(new InputStreamReader(inputstream));
tokenizer = null;
}
public String nextLine(){
String fullLine=null;
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
fullLine=reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return fullLine;
}
return fullLine;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import sys
#sys.stdin = open('input.txt', 'r')
#sys.stdout = open('output.txt', 'w')
x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
def sign(a, b, c, x, y):
value = a * x + b * y + c
if value < 0:
return -1
if value == 0:
return 0
return 1
n, ans = int(input()), 0
for i in range(n):
a, b, c = map(int, input().split())
if sign(a, b, c, x1, y1) != sign(a, b, c, x2, y2):
ans += 1
print(ans) | PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
import java.math.BigInteger;
public class Main{
final boolean isFileIO = false;
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
String delim = " ";
public static void main(String[] args) throws IOException {
Main m = new Main();
m.initIO();
m.solve();
m.in.close();
m.out.close();
}
public void initIO() throws IOException {
if(!isFileIO) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String nextToken() throws IOException {
if(!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken(delim);
}
//
String readLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public void solve() throws IOException {
double x1, y1, x2, y2;
x1 = nextDouble(); y1 = nextDouble(); x2 = nextDouble(); y2 = nextDouble();
Line a = new Line((y2 - y1), (x1 - x2), x2 * y1 - x1 * y2);
int n = nextInt();
Line[] lines = new Line[n];
for(int i = 0; i < n; i++) {
lines[i] = new Line(nextDouble(), nextDouble(), nextDouble());
}
int ans = 0;
for(int i = 0; i < n; i++) {
Point p = intersection(a, lines[i]);
if(p != null) {
if((p.x > Math.min(x1, x2) && p.x < Math.max(x1, x2)) || (p.y > Math.min(y1, y2) && p.y < Math.max(y1, y2))) {
ans++;
}
}
}
out.println(ans);
}
class Point {
public double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
class Line {
public double a, b, c;
public Line(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
}
Point intersection(Line line1, Line line2) {
double den = det(line1.a, line1.b, line2.a, line2.b);
double eps = 1E-13;
if(Math.abs(den) <= eps) {
return null;
}
return new Point(-det(line1.c, line1.b, line2.c, line2.b) / den,
-det(line1.a, line1.c, line2.a, line2.c) / den);
}
double det(double a, double b, double c, double d) {
return a * d - b * c;
}
}
class Utils {
public static long binpow(long a, long exp, long mod) {
if(exp == 0) {
return 1;
}
if(exp % 2 == 0) {
long temp = binpow(a, exp / 2, mod);
return (temp * temp) % mod;
} else {
return (binpow(a, exp - 1, mod) * a) % mod;
}
}
public static long inv(long a, long mod) {
return binpow(a, mod - 2, mod);
}
public static long addmod(long a, long b, long mod) {
return ((a + b) % mod + mod) % mod;
}
public static long gcd(long a, long b) {
if(b == 0)
return a;
return gcd(b, a % b);
}
//mul must be < 10^18
public static long mulmod(long a, long b, long mod) {
return (a * b + (((a * b) / mod) + 1) * mod) % mod;
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long i, j, k = 0, n, a, b, t = 1, m, l = 0, y, c, ans, count1, countc,
x1, x2, y1, y2, first, second;
while (t--) {
cin >> x1 >> y1 >> x2 >> y2;
cin >> n;
ans = 0;
first;
second;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
if (a * x1 + b * y1 + c < 0)
first = 0;
else
first = 1;
if (a * x2 + b * y2 + c < 0)
second = 0;
else
second = 1;
if (first != second) ans++;
}
cout << ans;
}
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
long long power(long long a, long long b) {
long long res = 1;
while (b) {
if (b & 1) res = res * a;
res %= 1000000007;
a = (a * a) % 1000000007;
b /= 2;
}
return res;
}
int a[101], b[101];
struct node {
int req;
int dep;
} node[102];
int dp[101][30002];
bool compare(const struct node &a, const struct node &b) {
return a.dep < b.dep;
}
int main() {
int n;
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
long long a, b, c;
scanf("%d", &n);
int i;
int ans = 0;
for (i = 1; i <= n; ++i) {
cin >> a >> b >> c;
long long d1 = a * x1 + b * y1 + c;
long long d2 = a * x2 + b * y2 + c;
if (d1 > 0 && d2 < 0)
ans++;
else if (d1 < 0 && d2 > 0)
ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.util.*;
import java.io.*;
public class Main{
BufferedReader in;
StringTokenizer str = null;
PrintWriter out;
private String next() throws Exception{
while (str == null || !str.hasMoreElements())
str = new StringTokenizer(in.readLine());
return str.nextToken();
}
private int nextInt() throws Exception{
return Integer.parseInt(next());
}
long []a, b, c;
final static double EPS = 1e-7;
final static int oo = Integer.MAX_VALUE / 2;
public void run() throws Exception{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// List<Double> x = new ArrayList<Double>();
// List<Double> y = new ArrayList<Double>();
long x1 = nextInt(), y1 = nextInt(), x2 = nextInt(), y2 = nextInt();
// x.add(x1);y.add(y1);
// x.add(x2);y.add(y2);
int n = nextInt();
a = new long[n];
b = new long[n];
c = new long[n];
for(int i = 0; i < n; ++i) {
a[i] = nextInt();
b[i] = nextInt();
c[i] = nextInt();
}
long a1 = y2 - y1;
long b1 = x1 - x2;
long c1 = -a1 * x1 - b1 * y1;
int cnt = 0;
for(int i = 0; i < n; ++i)
if (intersect(a[i], b[i], c[i], a1, b1, c1, x1, y1, x2, y2)) ++cnt;
out.println(cnt);
// System.out.println(x);
// System.out.println(y);
// int N = x.size();
// int [][]d = new int[N][N];
// for(int i = 0; i < N; ++i) {
// Arrays.fill(d[i], oo);
// d[i][i] = 0;
// }
// for(int i = 0; i < N; ++i) {
// for(int j = i + 1; j < N; ++j) {
// double a1 = y.get(j) - y.get(i);
// double b1 = x.get(i) - x.get(j);
// double c1 = -a1 * x.get(i) - b1 * y.get(i);
// boolean crossed = false;
// for(int k = 0; k < n; ++k) {
// double []inter = intersect(a[k], b[k], c[k], a1, b1, c1);
// if (inter.length == 0) continue;
// if (equal(x.get(i), y.get(i), inter[0], inter[1])) continue;
// if (equal(x.get(j), y.get(j), inter[0], inter[1])) continue;
// if (!between(x.get(i), y.get(i), x.get(j), y.get(j), inter[0], inter[1])) continue;
// // System.out.println("Because of (" + inter[0] + ", " + inter[1] + ") " + i + " " + j);
// crossed = true;
// break;
// }
// if (!crossed) {
// d[i][j] = d[j][i] = 1;
// }
// }
// }
// int []dis = new int[N];
// Arrays.fill(dis, oo);
// dis[0] = 0;
// boolean used[] = new boolean[N];
// int cur = 0;
// used[cur] = true;
// while(true) {
// for(int i = 0; i < N; ++i) {
// if (!used[i] && dis[i] > dis[cur] + d[cur][i]) {
// dis[i] = dis[cur] + d[cur][i];
// }
// }
// int idx = -1;
// for(int i = 0; i < N; ++i) {
// if (!used[i] && (idx == -1 || dis[idx] > dis[i])) {
// idx = i;
// }
// }
// if (idx == -1) break;
// used[idx] = true;
// cur = idx;
// }
// out.println(dis[1]);
out.close();
}
// private boolean between(double x1, double y1, double x2, double y2, double x, double y) {
// if (equal(x1, y1, x, y) || equal(x2, y2, x, y)) return true;
// double minx = Math.min(x1, x2), maxx = Math.max(x1, x2);
// double miny = Math.min(y1, y2), maxy = Math.max(y1, y2);
// return minx <= x && x <= maxx && miny <= y && y <= maxy;
// }
// private boolean equal(double x1, double y1, double x2, double y2) {
// return Math.abs(x1 - x2) < EPS && Math.abs(y1 - y2) < EPS;
// }
private boolean intersect(long a1, long b1, long c1, long a2, long b2, long c2, long x1, long y1, long x2, long y2) {
long cross = a1 * b2 - a2 * b1;
if (cross == 0) return false;
long xi = -(c1 * b2 - b1 * c2);
long yi = -(a1 * c2 - a2 * c1);
x1 *= cross;
y1 *= cross;
x2 *= cross;
y2 *= cross;
return Math.min(x1, x2) <= xi && xi <= Math.max(x1, x2) && Math.min(y1, y2) <= yi && yi <= Math.max(y1, y2);
}
public static void main(String args[]) throws Exception{
new Main().run();
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 5;
const long long INF = 1e9 + 9;
const long long MOD = 1000000007;
long long a[500], b[500], c[500];
long long x1_, y1_;
long long x2_, y2_;
bool intersect(int i) {
long long q, w;
q = a[i] * x1_ + b[i] * y1_ + c[i];
w = a[i] * x2_ + b[i] * y2_ + c[i];
if (q < 0 && w > 0) return true;
if (q > 0 && w < 0) return true;
return false;
}
int main() {
cin >> x1_ >> y1_ >> x2_ >> y2_;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i] >> c[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
if (intersect(i)) ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x1, y1;
int x2, y2;
int a, b, c;
int n;
cin >> x1 >> y1;
cin >> x2 >> y2;
cin >> n;
int result = 0;
for (int i = 0; i < n; i++) {
int x, y;
int y12, y22;
cin >> a >> b >> c;
if (b == 0) {
x = (-1.0 * c / a < x1);
y = (-1.0 * c / a < x2);
result += (x != y);
} else {
y12 = -(1.0 * a / b) * x1 - 1.0 * c / b;
y22 = -(1.0 * a / b) * x2 - 1.0 * c / b;
x = (y12 < y1);
y = (y22 < y2);
result += (x != y);
}
}
cout << result << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
struct P {
double x;
double y;
P(double x, double y) : x(x), y(y) {}
P() {}
bool operator<(P other) const {
if (fabs(x - other.x) > 1e-6) {
return x < other.x;
} else {
return y < other.y;
}
}
bool operator==(P other) const {
return fabs(x - other.x) < 1e-6 && fabs(y - other.y) < 1e-6;
}
};
struct Line {
double a, b, c;
Line(double a, double b, double c) : a(a), b(b), c(c) {}
double getX(double y) { return (-b * y - c) / a; }
double getY(double x) { return (-a * x - c) / b; }
};
struct Vec {
double x, y;
Vec(double x, double y) : x(x), y(y) {}
Vec(P A, P B) {
x = B.x - A.x;
y = B.y - A.y;
}
double operator*(Vec &other) { return x * other.y - y * other.x; }
};
int differentSides(Line l, P A, P B) {
if (fabs(l.a) < 1e-6) {
double y = l.getY(0);
if (fabs(A.y - y) < 1e-6 || fabs(B.y - y) < 1e-6) return 0;
if ((A.y - y) * (B.y - y) < 0)
return 1;
else
return 0;
} else if (fabs(l.b) < 1e-6) {
double x = l.getX(0);
if (fabs(A.x - x) < 1e-6 || fabs(B.x - x) < 1e-6) return 0;
if ((A.x - x) * (B.x - x) < 0)
return 1;
else
return 0;
}
P M(-1000, l.getY(-1000));
P N(1000, l.getY(1000));
Vec v1(M, N);
Vec v2(M, A);
Vec v3(M, B);
double a = v1 * v2, b = v1 * v3;
if (fabs(a) < 1e-6 || fabs(b) < 1e-6) return 0;
double t = a * b;
if (t < 0.0) {
return 1;
} else if (t > 0.0) {
return -1;
}
}
int main() {
double x, y, a, b, c;
int sum = 0;
P A, B;
cin >> x >> y;
A = P(x, y);
cin >> x >> y;
B = P(x, y);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
Line l(a, b, c);
if (differentSides(l, A, B) == 1) {
sum++;
}
}
cout << sum << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.util.*;
public class AB {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x1 = in.nextInt();
int y1 = in.nextInt();
int x2 = in.nextInt();
int y2 = in.nextInt();
int n = in.nextInt();
int A = 0;
for (int i = 0; i < n; i++) {
long a = in.nextLong();
long b = in.nextLong();
long c = in.nextLong();
if ((a * x1 + b * y1 + c) < 0 != (a * x2 + b * y2 + c) < 0)
A++;
}
System.out.println(A);
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | x1, y1 = map(int, input().split())
x2, y2 = map(int, input().split())
n = int(input())
d = [[int(i) for i in input().split()] for j in range(n)]
count = 0
if x1 != x2:
k = (y2 - y1) / (x2 - x1)
b = (y1 * x2 - y2 * x1) / (x2 - x1)
for i in range(n):
if -1 * d[i][0] != d[i][1] * k:
x3 = -1 * (d[i][1] * b + d[i][2]) / (d[i][0] + d[i][1] * k)
y3 = k * x3 + b
if max(x1, x2) >= x3 >= min(x1, x2) and max(y1, y2) >= y3 >= min(y1, y2):
count += 1
else:
a = 1
b = 0
c = -1 * x1
for i in range(n):
if d[i][1] != 0:
x3 = x1
y3 = -1 * (d[i][2] + d[i][0] * x1) / d[i][1]
if max(x1, x2) >= x3 >= min(x1, x2) and max(y1, y2) >= y3 >= min(y1, y2):
count += 1
print(count) | PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.PrintStream;
import java.util.Scanner;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintStream out = System.out;
long xH = in.nextLong(), yH = in.nextLong();
long xU = in.nextLong(), yU = in.nextLong();
int count = 0;
int n = in.nextInt();
for (int i = 0; i < n; i++) {
long a = in.nextLong(), b = in.nextLong(), c = in.nextLong();
long sH = a*xH + b*yH + c;
long sU = a*xU + b*yU + c;
if ((sH < 0 && sU > 0) || (sH > 0 && sU < 0)) {
count++;
}
}
out.println(count);
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
public class c284{
public static void main(String[] args) {
MyScanner obj = new MyScanner();
int x1=obj.nextInt();
int y1=obj.nextInt();
int x2=obj.nextInt();
int y2=obj.nextInt();
int n=obj.nextInt();
long l[][]=new long[n][3];
long ans[][]=new long[n][2];
long sum=0;
for(int i=0;i<n;i++)
{
l[i][0]=obj.nextLong();
l[i][1]=obj.nextLong();
l[i][2]=obj.nextLong();
ans[i][0]=(l[i][0]*x1)+(l[i][1]*y1)+l[i][2];
ans[i][1]=(l[i][0]*x2)+(l[i][1]*y2)+l[i][2];
// System.out.println(ans[i][0]+" "+ans[i][1]);
if((ans[i][0]<0&&ans[i][1]>0)||(ans[i][0]>0&&ans[i][1]<0))
sum+=1;
}
System.out.println(sum);
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 |
//package que_b;
import java.io.*;
import java.util.*;
import java.math.*;
public class utkarsh {
InputStream is;
PrintWriter out;
long mod = (long) (1e9 + 7);
boolean SHOW_TIME;
void solve() {
//Enter code here utkarsh
//SHOW_TIME = true;
long x1 = nl(), y1 = nl(), x2 = nl(), y2 = nl();
int n = ni();
int ans = 0;
for(int i = 0; i < n; i++) {
long a = nl(), b = nl(), c = nl();
long u = a*x1 + b*y1 + c;
long v = a*x2 + b*y2 + c;
if(u > 0 && v < 0 || u < 0 && v > 0) ans++;
}
out.println(ans);
}
//---------- I/O Template ----------
public static void main(String[] args) { new utkarsh().run(); }
void run() {
is = System.in;
out = new PrintWriter(System.out);
long start = System.currentTimeMillis();
solve();
long end = System.currentTimeMillis();
if(SHOW_TIME) out.println("\n" + (end - start) + " ms");
out.flush();
}
byte input[] = new byte[1024];
int len = 0, ptr = 0;
int readByte() {
if(ptr >= len) { ptr = 0;
try { len = is.read(input); }
catch(IOException e) { throw new InputMismatchException(); }
if(len <= 0) { return -1; }
} return input[ptr++];
}
boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
int skip() {
int b = readByte();
while(b != -1 && isSpaceChar(b)) { b = readByte(); }
return b;
}
char nc() { return (char)skip(); }
String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
String nLine() {
int b = skip();
StringBuilder sb = new StringBuilder();
while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
int ni() {
int n = 0, b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
if(b == -1) { return -1; } //no input
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
long nl() {
long n = 0L; int b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
double nd() { return Double.parseDouble(ns()); }
float nf() { return Float.parseFloat(ns()); }
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) { a[i] = ni(); }
return a;
}
char[] ns(int n) {
char c[] = new char[n];
int i, b = skip();
for(i = 0; i < n; i++) {
if(isSpaceChar(b)) { break; }
c[i] = (char)b; b = readByte();
} return i == n ? c : Arrays.copyOf(c,i);
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author An Almost Retired Guy
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
Point P1 = new Point(in.nextInt(), in.nextInt()), P2 = new Point(in.nextInt(), in.nextInt());
int ans = 0, n = in.nextInt();
for (int i = 0; i < n; i++) {
Line line = new Line(in.nextInt(), in.nextInt(), in.nextInt());
if (GeometryUtils.sign(P1.signedDistance(line)) * GeometryUtils.sign(P2.signedDistance(line)) == -1)
ans++;
}
out.println(ans);
}
}
static class OutputWriter extends PrintWriter {
public OutputWriter(OutputStream outputStream) {
super(new BufferedOutputStream(outputStream));
}
public OutputWriter(Writer writer) {
super(writer);
}
public void close() {
super.close();
}
}
static class Line {
public double a;
public double b;
public double c;
public Line(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
public Line(Point p1, Point p2) {
a = +(p1.y - p2.y);
b = -(p1.x - p2.x);
c = p1.x * p2.y - p2.x * p1.y;
}
}
static class Vector2d {
public double x;
public double y;
public Vector2d(double x, double y) {
this.x = x;
this.y = y;
}
public Vector2d(Point A, Point B) {
this(B.x - A.x, B.y - A.y);
}
public String toString() {
return "Vector2d [x=" + x + ", y=" + y + "]";
}
}
static class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(InputStream inputStream) {
br = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
static class GeometryUtils {
public static final double EPS = 1e-10;
public static int sign(double a) {
return a < -EPS ? -1 : a > EPS ? 1 : 0;
}
public static double fastHypot(double x, double y) {
return Math.sqrt(x * x + y * y);
}
}
static class Point implements Comparable<Point> {
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public Point(Vector2d vector) {
this(vector.x, vector.y);
}
public int compareTo(Point that) {
if (Math.abs(this.x - that.x) > GeometryUtils.EPS) return this.x < that.x ? -1 : 1;
if (Math.abs(this.y - that.y) > GeometryUtils.EPS) return this.y < that.y ? -1 : 1;
return 0;
}
public boolean equals(Object obj) {
Point that = (Point) obj;
return Math.abs(this.x - that.x) < GeometryUtils.EPS && Math.abs(this.y - that.y) < GeometryUtils.EPS;
}
public double signedDistance(Line line) {
Point p = this;
return (line.a * p.x + line.b * p.y + line.c) / GeometryUtils.fastHypot(line.a, line.b);
}
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.math.*;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Input in = new Input(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, in, out);
out.close();
}
}
class Task {
static final long MOD = 1000000007;
int[] primes;
public void solve(int testNumber, Input in, PrintWriter out) {
long x1 = in.nextLong();
long y1 = in.nextLong();
long x2 = in.nextLong();
long y2 = in.nextLong();
long n = in.nextLong();
long a = 0;
long b = 0;
long c = 0;
long ans = 0;
for (int i = 0; i < n; i++) {
a = in.nextLong();
b = in.nextLong();
c = in.nextLong();
long p1 = a * x1 + b * y1 + c;
long p2 = a * x2 + b * y2 + c;
if ((p1 < 0 && p2 > 0) || (p1 > 0 && p2 < 0))
ans++;
}
out.println(ans);
}
// didn't test
static class BIT {
int[] MAX;
int[] MIN;
int[] SUM;
int n;
public BIT(int n) {
MAX = new int[n + 1];
MIN = new int[n + 1];
SUM = new int[n + 1];
this.n = n;
}
public void setSum(int pos, int val) {
for (int i = pos; i < n; i += (i & -i)) {
SUM[i] += val;
}
}
public int getSum(int to) {
int sum = 0;
for (int i = to; i > 0; i -= (i & -i)) {
sum += SUM[i];
}
return sum;
}
public int getSum(int from, int to) {
return getSum(to) - getSum(from);
}
public void setMax(int pos, int val) {
for (int i = pos; i < n; i += (i & -i)) {
MAX[i] = Math.max(val, MAX[i]);
}
}
public int getMax(int to) {
int max = 0;
for (int i = to; i > 0; i -= (i & -i)) {
max = Math.max(max, MAX[i]);
}
return max;
}
public void setMin(int pos, int val) {
for (int i = pos; i < n; i += (i & -i)) {
MIN[i] = Math.min(val, MIN[i]);
}
}
public int getMin(int to) {
int min = 0;
for (int i = to; i > 0; i -= (i & -i)) {
min = Math.min(min, MIN[i]);
}
return min;
}
}
// didn't test
static class DisjointSet {
int[] set;
public DisjointSet(int n) {
set = new int[n];
for (int i = 0; i < n; i++) {
set[i] = i;
}
}
public int findSet(int a) {
if (set[a] == a)
return a;
return findSet(set[a]);
}
public void unionSet(int a, int b) {
set[findSet(a)] = findSet(b);
}
public boolean isSameSet(int a, int b) {
return findSet(a) == findSet(b);
}
}
static class SegmentTree {
int[] MAX;
int[] MIN;
int[] SUM;
int[] data;
int n;
public SegmentTree(int[] data) {
this.n = data.length;
this.data = data;
MAX = new int[4 * n + 10];
MIN = new int[4 * n + 10];
SUM = new int[4 * n + 10];
buildTree(1, 0, n - 1);
}
public int left(int root) {
return root << 1;
}
public int right(int root) {
return (root << 1) + 1;
}
private void buildTree(int root, int rl, int rr) {
if (rr == rl) {
MAX[root] = MIN[root] = SUM[root] = data[rr];
} else {
int rm = (rl + rr) >> 1;
buildTree(left(root), rl, rm);
buildTree(right(root), rm + 1, rr);
MAX[root] = Math.max(MAX[left(root)], MAX[right(root)]);
MIN[root] = Math.min(MIN[left(root)], MIN[right(root)]);
SUM[root] = SUM[left(root)] + SUM[right(root)];
}
}
public int getMax(int from, int to) {
return getMax(1, 0, n - 1, from, to);
}
private int getMax(int root, int rl, int rr, int from, int to) {
if (from > rr || to < rl || rl > rr) {
return -1;
}
if (from == rl && to == rr) {
return MAX[root];
}
int rm = (rr + rl) >> 1;
int m1 = getMax(left(root), rl, rm, from, Math.min(to, rm));
int m2 = getMax(right(root), rm + 1, rr, Math.max(from, rm + 1), to);
return Math.max(m1, m2);
}
public int getMin(int from, int to) {
return getMin(1, 0, n - 1, from, to);
}
private int getMin(int root, int rl, int rr, int from, int to) {
if (from > rr || to < rl || rl > rr) {
return Integer.MAX_VALUE;
}
if (from == rl && to == rr) {
return MIN[root];
}
int rm = (rr + rl) >> 1;
int m1 = getMin(left(root), rl, rm, from, Math.min(to, rm));
int m2 = getMin(right(root), rm + 1, rr, Math.max(from, rm + 1), to);
return Math.min(m1, m2);
}
public int getSum(int from, int to) {
return getSum(1, 0, n - 1, from, to);
}
private int getSum(int root, int rl, int rr, int from, int to) {
if (from > rr || to < rl || rl > rr) {
return 0;
}
if (from == rl && to == rr) {
return SUM[root];
}
int rm = (rr + rl) >> 1;
int m1 = getSum(left(root), rl, rm, from, Math.min(to, rm));
int m2 = getSum(right(root), rm + 1, rr, Math.max(from, rm + 1), to);
return m1 + m2;
}
}
private String lcs(String s1, String s2) {
int n = s1.length();
int m = s2.length();
int[][] dp = new int[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1] + 1;
} else {
dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
StringBuilder sb = new StringBuilder();
int i = n;
int j = m;
while (i > 0 && j > 0) {
if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
sb.append(s1.charAt(i - 1));
i--;
j--;
} else {
if (dp[i - 1][j] > dp[i][j - 1]) {
i--;
} else {
j--;
}
}
}
return sb.reverse().toString();
}
private boolean isPrime(int n) {
if (n <= 3)
return n > 1;
else if (n % 3 == 0 || n % 2 == 0)
return false;
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
private int[] getPrimes(int n) {
int len = n + 1;
boolean[] isPrimes = new boolean[len];
int[] temps = new int[len];
int index = 0;
Arrays.fill(isPrimes, true);
isPrimes[0] = isPrimes[1] = false;
for (int i = 2; i * i < len; i++) {
if (isPrimes[i]) {
for (int j = i * i; j < len; j += i) {
isPrimes[j] = false;
}
}
}
for (int i = 0; i < len; i++) {
if (isPrimes[i])
temps[index++] = i;
}
int[] primes = Arrays.copyOf(temps, index);
return primes;
}
static void swap(char[] arrays, int pos1, int pos2) {
char tmp = arrays[pos1];
arrays[pos1] = arrays[pos2];
arrays[pos2] = tmp;
}
static void swap(int[] arrays, int pos1, int pos2) {
int tmp = arrays[pos1];
arrays[pos1] = arrays[pos2];
arrays[pos2] = tmp;
}
static long modPow(long n, long ex) {
long res = 1;
while (ex > 0) {
if ((ex & 1) == 1)
res = res * n % MOD;
n = n * n % MOD;
ex >>= 1;
}
return res;
}
static long ModInverse(long n) {
return modPow(n, MOD - 2);
}
static long nCr(int n, int r) {
if (r > n)
return -1;
// return (f[n] * ModInverse((f[r] * f[n - r]) % MOD)) % MOD;
return 0;
}
}
class Input {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Input(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
st = new StringTokenizer("");
sb = new StringBuilder();
double homeX = nxtDbl();
double homeY = nxtDbl();
double uniX = nxtDbl();
double uniY = nxtDbl();
int n = nxtInt();
double[][] plane = new double[n][3];
for( int i = 0;i < n;i++){
plane[i][0] = nxtDbl();
plane[i][1] = nxtDbl();
plane[i][2] = nxtDbl();
}
double m = (uniY - homeY) / ( uniX - homeX );
double a1 = -1*m;
double b1 = 1;
double c1 = m*homeX - homeY;
int counter = 0;
for( int i = 0; i < n;i++){
double a2 = plane[i][0];
double b2 = plane[i][1];
double c2 = plane[i][2];
if( uniX == homeX ){
if( b2 != 0){
double y = ( -c2 - a2*homeX) / b2;
if( (y >= homeY && y <= uniY ) || (y <= homeY && y >= uniY) ){
counter++;
}
}
}else{
double x = (b1*c2 - b2*c1)/(b2*a1 - b1*a2);
double y = (a1*c2 - c1*a2)/(b1*a2 - b2*a1);
if( (x >= homeX && x <= uniX ) || (x <= homeX && x >= uniX) ){
if( (y >= homeY && y <= uniY ) || (y <= homeY && y >= uniY) ){
counter++;
}
}
}//end else.
}//end for.
System.out.println(counter);
}//end main.
static BufferedReader br;
static StringTokenizer st;
static StringBuilder sb;
static PrintWriter out;
static String nxtTok() throws IOException {
while (!st.hasMoreTokens()) {
String s = br.readLine();
if (s == null)
return null;
st = new StringTokenizer(s.trim());
}
return st.nextToken();
}
static int nxtInt() throws IOException {
return Integer.parseInt(nxtTok());
}
static long nxtLng() throws IOException {
return Long.parseLong(nxtTok());
}
static double nxtDbl() throws IOException {
return Double.parseDouble(nxtTok());
}
static int[] nxtIntArr(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nxtInt();
return a;
}
static long[] nxtLngArr(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nxtLng();
return a;
}
static char[] nxtCharArr() throws IOException {
return nxtTok().toCharArray();
}
}//end class. | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.util.*;
import java.io.*;
import java.math.*;
public class Euler {
public static void main(String[] args){
FastReader in = new FastReader();
PrintWriter o = new PrintWriter(System.out);
double x1 = in.nextInt();
double y1 = in.nextInt();
double x2 = in.nextInt();
double y2 = in.nextInt();
if (x2 < x1) {
double temp1 = x1;
double temp2 = y1;
x1 = x2;
y1 = y2;
x2 = temp1;
y2 = temp2;
}
int ans = 0;
int n = in.nextInt();
for (int i = 0; i < n; i++) {
double a2 = in.nextInt();
double b2 = in.nextInt();
double c2 = in.nextInt();
double p = (a2 * x1) + (b2 * y1) + c2;
double q = (a2 * x2) + (b2 * y2) + c2;
if (p * q < 0) ans++;
}
System.out.println(ans);
return;
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long x, y;
long long x1, y1;
scanf("%lld%lld%lld%lld", &x, &y, &x1, &y1);
int n;
scanf("%d", &n);
long long cnt = 0;
while (n--) {
long long a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
if (a * x + b * y + c > 0) {
if (a * x1 + b * y1 + c < 0) ++cnt;
} else {
if (a * x1 + b * y1 + c > 0) ++cnt;
}
}
printf("%lld\n", cnt);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
bool zero(double x) { return fabs(x) <= 1e-8; }
int sign(double x) {
if (zero(x)) return 0;
if (x > 0) return 1;
return -1;
}
struct point {
double x, y;
double tht;
point() {}
point(double xx, double yy) {
x = xx;
y = yy;
}
void output() { printf("(%f,%f)\n", x, y); }
void input() { scanf("%lf%lf", &x, &y); }
point operator-(const point &b) { return point(x - b.x, y - b.y); }
point operator+(const point &b) { return point(x + b.x, y + b.y); }
double operator^(const point &b) { return x * b.y - y * b.x; }
double operator*(const point &b) { return x * b.x + y * b.y; }
};
point pa, pb;
int main() {
pa.input();
pb.input();
int n;
int cnt = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
long long a, b, c;
scanf("%I64d%I64d%I64d", &a, &b, &c);
if (sign(pa.x * a + pa.y * b + c) * sign(pb.x * a + pb.y * b + c) == -1)
cnt++;
}
printf("%d\n", cnt);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
while (b ^= a ^= b ^= a %= b)
;
return a;
}
const int maxn = 2e5 + 9;
const int mod = 1e9 + 7;
double x1, x2, y2, y11;
bool ch(double a, double b, double c) {
double k = a * x1 + b * y11 + c;
double kk = a * x2 + b * y2 + c;
if (k * kk > 0) return 0;
return 1;
}
int main() {
cin >> x1 >> y11 >> x2 >> y2;
int n;
cin >> n;
int ans = 0;
for (int i = 0; i < n; i++) {
double a, b, c;
cin >> a >> b >> c;
if (ch(a, b, c)) ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 100100;
void amir(int x1, int y1, int x2, int y2) {
if (y1 < y2) swap(y1, y2);
int ans = 0;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
if (b == 0) continue;
double tmp = (-1) * (x1 * a + c) / b;
if (y1 > tmp && tmp > y2) ans++;
}
cout << ans << endl;
}
void amir1(int x1, int y1, int x2, int y2) {
if (x1 < x2) swap(x1, x2);
int ans = 0;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
if (a == 0) continue;
double tmp = (-1) * (y1 * b + c) / a;
if (x1 > tmp && tmp > x2) ans++;
}
cout << ans << endl;
}
int main() {
int x1, y1, x2, y2, n;
cin >> x1 >> y1 >> x2 >> y2;
if (x1 == x2) {
amir(x1, y1, x2, y2);
return 0;
}
if (y1 == y2) {
amir1(x1, y1, x2, y2);
return 0;
}
double q1 = (y1 - y2), q2 = (x1 - x2);
double m = q1 / q2;
double z = y1 - m * x1;
int ans = 0;
if (x1 < x2) swap(x1, x2);
cin >> n;
for (int i = 0; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
if (a + b * m == 0) continue;
double tmp = (-1) * (b * z + c) / (a + b * m);
if (x1 > tmp && tmp > x2) ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import sys
x1, y1 = map(int,sys.stdin.readline().split())
x2, y2 = map(int,sys.stdin.readline().split())
n = int(sys.stdin.readline())
lines = []
for i in range(n):
lines.append(map(int,sys.stdin.readline().split()))
res = 0
for a,b,c in lines:
v1 = x1*a + y1*b + c
v2 = x2*a + y2*b + c
if (v1 < 0 and v2 > 0) or (v1 > 0 and v2 < 0):
res += 1
print res
| PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.util.*;
import java.io.*;
public class prb{
static int gcd(int a, int b){
return b == 0 ? a : gcd(b, a%b);
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
long x1 = in.nextLong();
long y1 = in.nextLong();
long x2 = in.nextLong();
long y2 = in.nextLong();
int n = in.nextInt();
int ct = 0;
for(int i=0; i<n; i++){
long a = in.nextInt(), b = in.nextInt(), c = in.nextInt();
int o = 0, u = 0;
if(a*x1+b*y1 + c > 0 || a*x2 + b*y2 + c > 0) o++;
if(a*x1+b*y1+c < 0 || a*x2 + b*y2 + c < 0) u++;
if(o+u==2) ct++;
}
System.out.println(ct);
}
static class slope implements Comparable<slope>{
int n, d;
public slope(int nn, int dd){
n = nn;
d = dd;
}
public int compareTo(slope s){
if(n==s.n && d == s.d) return 0;
if(n-s.n != 0) return n-s.n;
return d - s.d;
}
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | def isX(a,b,c):
det = a*bb-aa*b
if det == 0: return False
x = (b*cc-bb*c)*1.0/det
y = -(a*cc-aa*c)*1.0/det
if min(x1,x2) <= x <= max(x1,x2) and min(y1,y2) <= y <= max(y1,y2):
return True
else:
False
x1,y1 = map(int,raw_input().split())
x2,y2 = map(int,raw_input().split())
aa,bb,cc = y2-y1,-(x2-x1),(x2-x1)*y2-(y2-y1)*x2
n = int(raw_input())
ans = 0
for loop in range(n):
a,b,c = map(int,raw_input().split())
if isX(a,b,c):
ans += 1
print ans | PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
long long int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int n;
cin >> n;
int count = 0;
for (int i = 0; i < n; i++) {
long long int a, b, c;
cin >> a >> b >> c;
long long int pe1 = x1 * a + y1 * b;
long long int pe2 = x2 * a + y2 * b;
if ((pe1 < -c and pe2 > -c) or (pe2 < -c and pe1 > -c)) count++;
}
cout << count << endl;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class A {
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter out;
public void solve() throws IOException {
long X1 = nextLong();
long Y1 = nextLong();
long X2 = nextLong();
long Y2 = nextLong();
int N = nextInt();
int ans = 0;
for (int i = 0; i < N; i++) {
long A = nextLong();
long B = nextLong();
long C = nextLong();
long sign1 = A * X1 + B * Y1 + C;
long sign2 = A * X2 + B * Y2 + C;
boolean pos1 = sign1 >= 0;
boolean pos2 = sign2 >= 0;
if (pos1 != pos2) ans++;
}
out.println(ans);
}
/**
* @param args
*/
public static void main(String[] args) {
new A().run();
}
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
out = new PrintWriter(System.out);
solve();
reader.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main() {
int x1, y1;
int x2, y2;
int n, a, b, c, sol = 0;
cin >> x1 >> y1 >> x2 >> y2 >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
long long t1 = 1LL * a * x1 + 1LL * b * y1 + c;
long long t2 = 1LL * a * x2 + 1LL * b * y2 + c;
if ((t1 > 0 && t2 < 0) || (t1 < 0 && t2 > 0)) sol++;
}
cout << sol << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | R=lambda:map(int,raw_input().split())
x,y=R()
u,v=R()
t=0
for _ in range(R()[0]):
a,b,c=R()
t+=(a*x+b*y+c>0)!=(a*u+b*v+c>0)
print t
| PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
bool chomeeck(long long a, long long b, long long c, long long x1, long long x2,
long long y1, long long y2) {
long long p1 = a * x1 + b * y1;
long long p2 = a * x2 + b * y2;
return (p1 > (-c)) ^ (p2 > (-c));
}
int main() {
long long homex, homey, ux, uy, n;
long long ans = 0;
cin >> homex >> homey >> ux >> uy;
cin >> n;
long long a, b, c;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
if (chomeeck(a, b, c, homex, ux, homey, uy)) {
ans++;
}
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.awt.geom.Line2D;
import java.util.*;
public class CrazyTown {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Point ME = new Point(scan.nextInt(), scan.nextInt());
Point U = new Point(scan.nextInt(), scan.nextInt());
Line2D.Double path = new Line2D.Double(ME.x, ME.y, U.x, U.y);
int n = scan.nextInt();
Line2D.Double[] arr = new Line2D.Double[n];
for(int i = 0; i < n; i++){
double a = scan.nextDouble();
double b = scan.nextDouble();
double c = scan.nextDouble();
arr[i] = lineToSeg(a, b, c);
}
int count = 0;
for(int i = 0; i < n; i++){
if(path.intersectsLine(arr[i])){
count++;
}
}
System.out.println(count);
}
static Line2D.Double lineToSeg(double a, double b, double c){
if(b == 0){
if(a == 0) return new Line2D.Double(c, 1e9, c, -1e9);
return new Line2D.Double(c/-a, 1e9, c/-a, -1e9);
}
double x = 1e9;
double y = (-a*x-c)/b;
double x2 = -1e9;
double y2 = (-a*x2-c)/b;
return new Line2D.Double(x, y, x2, y2);
}
static class Point{
int x;
int y;
public Point(int x, int y){
this.x = x;
this.y = y;
}
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
struct line {
double a, b, c;
line() {}
line(double a, double b, double c) {
this->a = a;
this->b = b;
this->c = c;
}
};
double Hx, Hy, Ox, Oy;
pair<bool, pair<double, double> > intersect(line l1, line l2) {
double det = l1.a * l2.b - l2.a * l1.b;
if (det == 0) return make_pair(false, make_pair(-1, -1));
double x = (l2.b * l1.c - l1.b * l2.c) / det;
double y = (l1.a * l2.c - l2.a * l1.c) / det;
if (x <= max(Hx, Ox) + 0.0000001 && x >= min(Hx, Ox) - 0.0000001 &&
y <= max(Hy, Oy) + 0.0000001 && y >= min(Hy, Oy) - 0.0000001)
return make_pair(true, make_pair(x, y));
else
return make_pair(false, make_pair(-1, -1));
}
int main() {
cin >> Hx >> Hy >> Ox >> Oy;
int N;
cin >> N;
vector<line> data(N);
for (int i = 0; i < N; i++) {
cin >> data[i].a >> data[i].b >> data[i].c;
data[i].c = -data[i].c;
}
double A = (Oy - Hy), B = (Hx - Ox), C = A * Hx + B * Hy;
line l(A, B, C);
vector<pair<double, double> > pts;
for (int i = 0; i < data.size(); i++) {
pair<bool, pair<double, double> > ret = intersect(data[i], l);
if (ret.first) {
pts.push_back(ret.second);
}
}
int total = pts.size();
if (total == 0) goto here;
if ((Hx == pts[0].first && Hy == pts[0].second) ||
(Hx == pts[pts.size() - 1].first && Hy == pts[pts.size() - 1].second)) {
total -= 1;
}
if ((Ox == pts[0].first && Oy == pts[0].second) ||
(Ox == pts[pts.size() - 1].first && Oy == pts[pts.size() - 1].second)) {
total -= 1;
}
here:;
cout << pts.size() << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 100;
const int maxn = 2000000;
long long xx1, yy1, xx2, yy2, a, b, c, c1, c2;
int n, ans = 0;
int main() {
cin >> xx1 >> yy1 >> xx2 >> yy2;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> a >> b >> c;
c1 = a * xx1 + b * yy1 + c;
c2 = a * xx2 + b * yy2 + c;
if (c1 < 0 && c2 > 0)
ans++;
else if (c1 > 0 && c2 < 0)
ans++;
}
cout << ans << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | x1,y1 = map(int, raw_input().split())
x2,y2 = map(int, raw_input().split())
n = int(raw_input())
total = 0
for _ in xrange(n):
a,b,c = map(int, raw_input().split())
if (a*x1+b*y1+c)*(a*x2+b*y2+c) < 0:
total += 1
print total | PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int inf = ~0U >> 1;
const long long INF = ~0ULL >> 1;
;
const double pi = acos(-1.0);
template <class T>
inline T sqr(T a) {
return a * a;
}
template <class T>
inline void read(T &n) {
char c;
for (c = getchar(); !(c >= '0' && c <= '9'); c = getchar())
;
n = c - '0';
for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0';
}
int Pow(int base, int n, int mo) {
if (n == 0) return 1;
if (n == 1) return base;
int tmp = Pow(base, n >> 1, mo);
tmp = (long long)tmp * tmp % mo;
if (n & 1) tmp = (long long)tmp * base % mo;
return tmp;
}
struct point {
int x, y;
point() {}
point(int _x, int _y) : x(_x), y(_y) {}
};
int ans, m;
int main() {
long long x1, y1, x2, y2;
scanf("%lld%lld", &x1, &y1);
scanf("%lld%lld", &x2, &y2);
scanf("%d", &m);
for (int i = (1); i <= (m); ++i) {
long long a, b, c;
scanf("%lld%lld%lld", &a, &b, &c);
long long p = x1 * a + y1 * b + c;
long long q = x2 * a + y2 * b + c;
if (p > 0 && q < 0 || p < 0 && q > 0) ans++;
}
printf("%d\n", ans);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | get = lambda: map(int, raw_input().split())
x1, y1 = get()
x2, y2 = get()
n, = get()
lines = [get() for i in range(n)]
count = 0
for a,b,c in lines:
f1 = a * x1 + b * y1 + c
f2 = a * x2 + b * y2 + c
if (f1 < 0 and f2 > 0) or (f1 > 0 and f2 < 0):
count += 1
print count | PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | h=list(map(int,input().strip().split()))
u=list(map(int,input().strip().split()))
n=int(input())
count=0
for i in range(n):
a,b,c=list(map(int,input().strip().split()))
t1=a*h[0]+b*h[1]+c
t2=a*u[0]+b*u[1]+c
#print(">",t1,t2)
if ((t1>0) != (t2>0)):
count+=1
print(count)
| PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.awt.geom.Line2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class CF284C {
private FastScanner in;
private PrintWriter out;
public void solve() throws IOException {
long x1 = in.nextLong();
long y1 = in.nextLong();
long x2 = in.nextLong();
long y2 = in.nextLong();
int n = in.nextInt();
Line2D l1 = new Line2D.Double(x1, y1, x2, y2);
int cnt = 0;
for (int i = 0; i < n; i++){
int a1 = in.nextInt();
int b1 = in.nextInt();
int c1 = in.nextInt();
long v1 = 1L*a1*x1 + 1L*b1*y1 + c1;
long v2 = 1L*a1*x2 + 1L*b1*y2 + c1;
if (v1 > 0 && v2 < 0 || v1 < 0 && v2 > 0){
cnt++;
}
}
out.println(cnt);
}
public static void main(String[] args) {
new CF284C().run();
}
public void run() {
try {
in = new FastScanner();
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private class FastScanner {
private BufferedReader br;
private StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
bool getP(int x, int y, long long a, long long b, long long c) {
return a * x + b * y + c > 0;
}
void solve() {
int x0, y0, X1, Y1;
scanf("%d%d%d%d", &x0, &y0, &X1, &Y1);
int n;
scanf("%d", &n);
int ans(0);
for (int i(0), _l((int)(n)-1); i <= _l; ++i) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
ans += getP(x0, y0, a, b, c) ^ getP(X1, Y1, a, b, c);
}
printf("%d\n", ans);
}
int main() {
solve();
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long sgn(long long a) { return a < 0 ? -1 : 1; }
int main() {
int x1, y1, x2, y2, n;
scanf("%d %d", &x1, &y1);
scanf("%d %d", &x2, &y2);
scanf("%d", &n);
int ans = 0;
for (int i = 0; i < n; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if (sgn(1ll * a * x1 + 1ll * b * y1 + c) !=
sgn(1ll * a * x2 + 1ll * b * y2 + c)) {
ans++;
}
}
printf("%d", ans);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int n, ans;
double xa, xb, ya, yb, a, b, c;
int main() {
scanf("%lf%lf%lf%lf%d", &xa, &ya, &xb, &yb, &n);
for (int i = 1; i <= n; i++) {
scanf("%lf%lf%lf", &a, &b, &c);
if (!b) {
if (c * -1 / a >= min(xa, xb) && c * -1 / a <= max(xa, xb)) ans++;
} else if (((a * xa + c) / (-1 * b) > ya) ^ ((a * xb + c) / (-1 * b) > yb))
ans++;
}
printf("%d\n", ans);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import static java.lang.Math.abs;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class Main {
static class Point {
double x;
double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
}
static class Line {
double a;
double b;
double c;
public Line(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
}
void solve() throws IOException {
Point p1 = new Point(nextInt(), nextInt());
Point p2 = new Point(nextInt(), nextInt());
int n = nextInt();
int[] a = new int[n];
int[] b = new int[n];
int[] c = new int[n];
int cnt = 0;
for (int i = 0; i < n; i++) {
a[i] = nextInt();
b[i] = nextInt();
c[i] = nextInt();
cnt += cross_segment_line(p1, p2, new Line(a[i], b[i], c[i]));
}
System.out.println(cnt);
}
static double eps = 0.00000001;
Line toline (Point p1, Point p2)
{
double a = p2.y - p1.y;
double b = p1.x - p2.x;
return new Line(a, b, - a * p1.x - b * p1.y);
}
int cross_segment_line (Point p1, Point p2, Line l) {
Point p = new Point(0, 0);
Line t = toline (p1, p2);
int flag = cross_line (l, t, p);
if (flag == 0) return 0;
if (point_in_box (p, p1, p2)) return 1;
return 0;
}
int cross_line (Line l1, Line l2, Point p)
{
if (is_equal_line (l1, l2)) return 0;
if (is_parallel_line (l1, l2)) return 0;
p.x = (l2.b * l1.c - l1.b * l2.c) / (l2.a * l1.b - l1.a * l2.b);
p.y = (l1.b != 0 ? (- l1.c - l1.a * p.x) / l1.b : (- l2.c - l2.a * p.x) / l2.b);
return 1;
}
boolean point_in_box (Point t, Point p1, Point p2)
{
return (abs (t.x - min(p1.x, p2.x)) <= eps || min(p1.x, p2.x) <= t.x) &&
(abs (max(p1.x, p2.x) - t.x) <= eps || max(p1.x, p2.x) >= t.x) &&
(abs (t.y - min(p1.y, p2.y)) <= eps || min(p1.y, p2.y) <= t.y) &&
(abs (max(p1.y, p2.y) - t.y) <= eps || max(p1.y, p2.y) >= t.y);
}
boolean is_parallel_line (Line l1, Line l2) {
return abs(l1.a * l2.b - l2.a * l1.b) <= eps;
}
boolean is_equal_line (Line l1, Line l2) {
return abs(l1.a * l2.b - l2.a * l1.b) <= eps &&
abs (l1.a * l2.c - l2.a * l1.c) <= eps &&
abs (l1.b * l2.c - l2.b * l1.c) <= eps;
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
br.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(239);
}
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = br.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
public static void main(String[] args) {
new Main().run();
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
@Override
public void run() {
try {
init();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
long time = System.currentTimeMillis();
try {
solve();
} catch (Exception e) {
e.printStackTrace();
}
out.close();
System.err.println(System.currentTimeMillis() - time);
}
private void init() throws FileNotFoundException {
String file = "";
if (!file.equals("")) {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} else {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
}
public static void main(String[] args) {
new Thread(new Solution()).start();
}
private String readString() {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return tok.nextToken();
}
private int readInt() {
return Integer.parseInt(readString());
}
private long readLong() {
return Long.parseLong(readString());
}
private void solve() {
long x1 = readInt();
long y1 = readInt();
long x2 = readInt();
long y2 = readInt();
int n = readInt();
int ans = 0;
for (int i = 0; i < n; i++) {
long a = readInt(), b = readInt(), c = readInt();
long temp1 = a * x1 + b * y1 + c;
long temp2 = a * x2 + b * y2 + c;
if((temp1 < 0 && temp2 > 0) || (temp1 > 0 && temp2 < 0)) {
ans++;
}
}
out.println(ans);
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
int main(void) {
long long n, x[2], y[2];
cin >> x[0] >> y[0] >> x[1] >> y[1];
cin >> n;
int answer = 0;
for (int i = (0); i < (n); ++i) {
long long a, b, c;
cin >> a >> b >> c;
if (a * x[0] + b * y[0] + c < 0 && a * x[1] + b * y[1] + c > 0) answer++;
if (a * x[0] + b * y[0] + c > 0 && a * x[1] + b * y[1] + c < 0) answer++;
}
cout << answer << endl;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | home = map(int, raw_input().split())
univ = map(int, raw_input().split())
line = None
if home[0] == univ[0]:
line = [1,0,-home[0]]
elif home[1] == univ[1]:
line = [0,1,-home[1]]
else:
slope = (univ[1]-home[1])/(univ[0]-home[0])
line = [slope, -1, univ[1]-slope*univ[0]]
n = int(raw_input())
road = None
count = 0
for i in xrange(n):
road = map(int, raw_input().split())
if (road[0]*home[0] + road[1]*home[1] + road[2])*(road[0]*univ[0] + road[1]*univ[1] + road[2]) < 0:
if (line[0] == 0 and line[1] != 0) or (line[0] != 0 and line[1] == 0):
count += 1
elif road[0]*line[1] != road[1]*line[0]:
count += 1
print count
| PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const double Pi = acos(-1.0);
const long double eps = 1e-10;
const double inf = 10e30;
double R_to_Angle(double rad) { return 180 / Pi * rad; }
double Angle_to_R(double angle) { return Pi / 180 * angle; }
int sgn(long double x) {
if (fabs(x) < eps) return 0;
if (x < 0) return -1;
return 1;
}
struct Point {
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
};
Point operator+(Point A, Point B) { return Point(A.x + B.x, A.y + B.y); }
Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); }
Point operator*(Point A, double p) { return Point(A.x * p, A.y * p); }
Point operator*(double p, Point A) { return Point(p * A.x, p * A.y); }
Point operator/(Point A, double p) { return Point(A.x / p, A.y / p); }
double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; }
double Cross(Point A, Point B) { return A.x * B.y - B.x * A.y; }
double distance(Point A) { return sqrt(Dot(A, A)); }
double dis(Point A, Point B) {
return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y));
}
Point Format(Point A) {
double L = distance(A);
return Point(A.x / L, A.y / L);
}
Point Normal(Point A) {
double L = distance(A);
return Point(-A.y / L, A.x / L);
}
struct Line {
Point v;
Point p;
Point p2;
double A, B, C;
double deg;
Point get_point_in_line(double t) { return p + v * t; }
Line() {}
Line(Point p, Point v) : p(p), v(v) { deg = atan2(v.y, v.x); }
bool operator<(const Line &L) const { return deg < L.deg; }
};
bool ToLeftTest(Point A, Point B, Point C) { return Cross(B - A, C - B) > 0; }
double Area(Point A, Point B, Point C) { return Cross(B - A, C - A) / 2.0; }
Point Rotate(Point A, double rad) {
return Point(A.x * cos(rad) - A.y * sin(rad),
A.x * sin(rad) + A.y * cos(rad));
}
Point B_dis_A(Point A, Point B) {
Point S;
S.x = 2 * A.x - B.x;
S.y = 2 * A.y - B.y;
return S;
}
Line getLineABC(Point P1, Point P2) {
Line L;
L.A = P2.y - P1.y;
L.B = P1.x - P2.x;
L.C = P2.x * P1.y - P1.x * P2.y;
return L;
}
int relation(Point A, Point B, Point P) {
int c = sgn(Cross((B - A), (P - A)));
if (c < 0)
return 1;
else if (c > 0)
return -1;
return 0;
}
double Distance_point_to_line(Point A, Point B, Point P) {
Point v1 = B - A, v2 = P - A;
return fabs(Cross(v1, v2) / distance(v1));
}
bool OnSegment(Point A, Point B, Point P) {
return sgn(Cross(A - P, B - P)) == 0 && sgn(Dot(A - P, B - P)) < 0;
}
double Distance_point_to_segment(Point A, Point B, Point P) {
if (sgn(A.x - B.x) == 0 && sgn(A.y - B.y)) return distance(P - A);
Point v1 = B - A, v2 = P - A, v3 = P - B;
if (sgn(Dot(v1, v2)) < 0) return distance(v2);
if (sgn(Dot(v1, v3)) > 0) return distance(v3);
return fabs(Cross(v1, v2) / distance(v1));
}
Point FootPoint(Point A, Point B, Point P) {
Point x = P - A, y = P - B, z = B - A;
double len1 = Dot(x, z) / distance(z), len2 = -1.0 * Dot(y, z) / distance(z);
return A + z * (len1 / (len1 + len2));
}
Point Symmetry_PL(Point A, Point B, Point P) {
return P + (FootPoint(A, B, P) - P) * 2;
}
Point Get_line_intersection(Point A, Point v, Point B, Point w) {
Point u = A - B;
double t = Cross(w, u) / Cross(v, w);
return A + v * t;
}
Point Get_intersection(Line a, Line b) {
Point u = a.p - b.p;
double t = Cross(b.v, u) / Cross(a.v, b.v);
return a.p + a.v * t;
}
bool seg_cross_seg(Point a1, Point a2, Point b1, Point b2) {
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return (sgn(c1) * sgn(c2) < 0) && (sgn(c3) * sgn(c4) < 0) &&
min(a1.x, a2.x) <= max(b1.x, b2.x) &&
min(b1.x, b2.x) <= max(a1.x, a2.x) &&
min(a1.x, a2.x) <= max(b1.x, b2.x) &&
min(b1.x, b2.x) <= max(a1.x, a2.x);
}
Point triangle_gravity(Point A, Point B, Point C) { return (A + B + C) / 3.0; }
struct Circle {
Point c;
double r;
Circle(Point c = Point(), double r = 0) : c(c), r(r) {}
inline Point point(double rad) {
return Point(c.x + cos(rad) * r, c.y + sin(rad) * r);
}
};
Circle getcircle(Point A, Point B, Point C) {
Point P1 = (A + B) * 0.5, P2 = (A + C) * 0.5;
Point O = Get_line_intersection(P1, Normal(B - A), P2, Normal(C - A));
return Circle(O, distance(A - O));
}
int intersection_line_circle(Line L, Circle C, vector<Point> &sol) {
double t1, t2;
double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
double e = a * a + c * c, f = 2 * (a * b + c * d),
g = b * b + d * d - C.r * C.r;
double delta = f * f - 4 * e * g;
if (sgn(delta) < 0) return 0;
if (sgn(delta) == 0) {
t1 = -f / (2 * e);
t2 = -f / (2 * e);
sol.push_back(L.get_point_in_line(t1));
return 1;
}
t1 = (-f - sqrt(delta)) / (2 * e);
sol.push_back(L.get_point_in_line(t1));
t2 = (-f + sqrt(delta)) / (2 * e);
sol.push_back(L.get_point_in_line(t2));
cout << t1 << " " << t2 << endl;
return 2;
}
int get_circle_circle_intersection(Circle C1, Circle C2, vector<Point> &sol) {
double d = distance(C1.c - C2.c);
if (sgn(d) == 0) {
if (sgn(C1.r - C2.r) == 0) return -1;
return 0;
}
if (sgn(C1.r + C2.r - d) < 0) return 0;
if (sgn(fabs(C1.r - C2.r) - d) > 0) return 0;
double a = atan2(C2.c.y - C1.c.y, C2.c.x - C1.c.x);
double da = acos((C1.r * C1.r + d * d - C2.r * C2.r) / (2 * C1.r * d));
Point p1 = C1.point(a - da), p2 = C1.point(a + da);
sol.push_back(p1);
if (sgn(p1.x - p2.x) == 0 && sgn(p1.y - p2.y) == 0) return 1;
sol.push_back(p2);
return 2;
}
Point P[25], G[25];
int n, tot, m, T;
struct cmp {
Point p;
cmp(const Point &p0) { p = p0; }
bool operator()(Point &p1, Point &p2) {
double tmp = Cross(p1 - p, p2 - p);
if (tmp > 0) return 1;
if (tmp == 0 && dis(p, p1) < dis(p, p2)) return 1;
return 0;
}
};
void Graham(int n) {
int k = 0;
for (int i = 0; i < n; i++) {
if (P[i].x < P[k].x || (P[i].x == P[k].x && P[i].y < P[k].y)) k = i;
}
swap(P[0], P[k]);
sort(P + 1, P + n, cmp(P[0]));
tot = 2, G[0] = P[0], G[1] = P[1];
for (int i = 2; i < n; i++) {
while (tot > 1 && sgn(Cross(G[tot - 2] - P[i], G[tot - 1] - P[i])) <= 0)
tot--;
G[tot++] = P[i];
}
G[tot] = G[0];
}
int IsPointInPolygon(Point P, Point *G) {
int l = 1, r = tot - 2, mid;
while (l <= r) {
mid = (l + r) >> 1;
int t1 = sgn(Cross(G[mid] - G[0], P - G[0]));
int t2 = sgn(Cross(G[mid + 1] - G[0], P - G[0]));
if (t1 >= 0 && t2 <= 0) {
int t3 = sgn(Cross(G[mid] - P, G[mid + 1] - P));
if (t3 < 0)
return 0;
else if ((t1 == 0 && mid == 1) || (t2 == 0 && mid + 1 == n - 1) ||
t3 == 0)
return -1;
return 1;
} else if (t1 < 0)
r = mid - 1;
else
l = mid + 1;
}
return 0;
}
double RotateCalipers(int n) {
int q = 1;
double ans = 0;
G[tot] = G[0];
for (int i = 0; i < tot; i++) {
while (Cross(G[i] - G[q + 1], G[i + 1] - G[q + 1]) >
Cross(G[i] - G[q], G[i + 1] - G[q]))
q = (q + 1) % tot;
ans =
max(ans, max(dis(G[i], G[q]), dis(G[(i + 1) % tot], G[(q + 1) % tot])));
}
return ans;
}
bool cmpxy(Point A, Point B) {
if (A.x != B.x) return A.x < B.x;
return A.y < B.y;
}
bool mpt_cmpy(const int &a, const int &b) { return P[a].y < P[b].y; }
int mpt[25];
double Closest_Pair(int l, int r) {
double d = inf;
if (l == r) return d;
if (l + 1 == r) return d;
int mid = (l + r) >> 1;
double d1 = Closest_Pair(l, mid);
double d2 = Closest_Pair(mid + 1, r);
d = min(d1, d2);
int cnt = 0;
for (int i = l; i <= r; i++) {
if (fabs(P[mid].x - P[i].x) <= d) mpt[cnt++] = i;
}
sort(mpt, mpt + cnt, mpt_cmpy);
for (int i = 0; i < cnt; i++) {
for (int j = i + 1; j < cnt && P[mpt[j]].y - P[mpt[i]].y < d; j++) {
double d3 = dis(P[mpt[i]], P[mpt[j]]);
if (d > d3) d = d3;
}
}
return d;
}
Line L[100005];
int cnt = 0;
bool on_left(Line L, Point P) { return Cross(L.v, P - L.p) > 0; }
int half_plane_intersection(int n) {
sort(L, L + n);
int first = 0, last = 0;
Point *p = new Point[n];
Line *q = new Line[n];
q[0] = L[0];
for (int i = 1; i < n; ++i) {
while (first < last && !on_left(L[i], p[last - 1])) last--;
while (first < last && !on_left(L[i], p[first])) first++;
q[++last] = L[i];
if (fabs(Cross(q[last].v, q[last - 1].v)) < eps) {
last--;
if (on_left(q[last], L[i].p)) q[last] = L[i];
}
if (first < last) p[last - 1] = Get_intersection(q[last - 1], q[last]);
}
while (first < last && !on_left(q[first], p[last - 1])) last--;
if (last - first <= 1) return 0;
p[last] = Get_intersection(q[last], q[first]);
for (int i = first; i <= last; i++) G[cnt++] = p[i];
return cnt;
}
Point P1, P2;
int sum;
int main() {
ios::sync_with_stdio(false);
cin >> P1.x >> P1.y;
cin >> P2.x >> P2.y;
cin >> n;
for (int i = 1; i <= n; i++) {
double a, b, c;
cin >> a >> b >> c;
if (a * P1.x + b * P1.y + c > 0 && a * P2.x + b * P2.y + c < 0) {
sum++;
continue;
}
if (a * P1.x + b * P1.y + c < 0 && a * P2.x + b * P2.y + c > 0) {
sum++;
}
}
printf("%d", sum);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
#pragma warning(disable : 4996)
int n, i, ans;
long long x1, y, x2, y2, t1, t2, a, b, c;
int main() {
cin >> x1 >> y >> x2 >> y2 >> n;
for (i = 1; i <= n; i++) {
cin >> a >> b >> c;
t1 = a * x1 + b * y + c;
t2 = a * x2 + b * y2 + c;
if ((t1 < 0 && t2 > 0) || (t1 > 0 && t2 < 0)) ans++;
}
cout << ans;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | def s(xx1,yy1,xx2,yy2,x1,y1):
return (xx1-x1)*(yy2-y1)-(yy1-y1)*(xx2-x1)
x1, y1 = map(int,input().split())
x2, y2 = map(int,input().split())
n = int(input())
l = [0] * n
for i in range(n):
l[i] = tuple(map(int,input().split()))
res = 0
for i in range(n):
a,b,c = l[i]
xx1 = 0
xx2 = 1000
yy1, yy2 = 0,0
if b == 0:
yy1 = 0
yy2 = 1000
xx1 = -c / a
xx2 = -c / a
else:
yy1 = (-a*xx1 - c) / b
yy2 = (-a*xx2 - c) / b
#print(xx1, yy1, xx2, yy2)
if s(xx1,yy1,xx2,yy2,x1,y1) * s(xx1,yy1,xx2,yy2,x2,y2) < 0: res += 1
print(res)
| PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long xa, ya, xb, yb;
long long a, b, c, sum = 0, n;
int main() {
cin >> xa >> ya >> xb >> yb;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a >> b >> c;
if (((a * xa + b * ya + c) < 0 && (a * xb + b * yb + c) > 0) ||
((a * xa + b * ya + c) > 0 && (a * xb + b * yb + c) < 0))
sum++;
}
cout << sum;
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long i, j, k, l, x_1, x_2, y_1, y_2, x, y, z, m, n, a, b, c, ans;
int main() {
scanf("%I64d %I64d %I64d %I64d %I64d", &x_1, &y_1, &x_2, &y_2, &n);
for (i = 1; i <= n; i++) {
scanf("%I64d %I64d %I64d", &a, &b, &c);
x = a * x_1 + b * y_1 + c;
y = a * x_2 + b * y_2 + c;
if ((x < 0 && y > 0) || (x > 0 && y < 0)) ans++;
}
printf("%I64d", ans);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
const double PI = 2 * acos(0.0);
const int MAXN = 1e6 + 5;
int n;
int main() {
long long xa, ya, xb, yb, ans = 0, a[303], b[303], c[303];
cin >> xa >> ya;
cin >> xb >> yb;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i];
for (int i = 1; i <= n; i++) {
long long u = (a[i] * xa + b[i] * ya + c[i]);
long long v = (a[i] * xb + b[i] * yb + c[i]);
if ((u > 0 && v < 0) || (u < 0 && v > 0)) ans++;
}
cout << ans;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #!/usr/bin/env python3
x, y = map(int, input().split())
u, v = map(int, input().split())
n = int(input())
s = 0
for i in range(n):
a, b, c = map(int, input().split())
s += (a * x + b * y + c > 0) ^ (a * u + b * v + c > 0)
print(s)
| PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
solve();
}
private static void solve() {
int x1 = IO.readInt();
int y1 = IO.readInt();
int x2 = IO.readInt();
int y2 = IO.readInt();
int n = IO.readInt();
int count = 0;
long a,b,c;
while (n-- > 0) {
a = IO.readInt();
b = IO.readInt();
c = IO.readInt();
boolean b1 = a * x1 + b * y1 + c > 0;
boolean b2 = a * x2 + b * y2 + c > 0;
if (b1 ^ b2) {
count++;
}
}
IO.print(count);
}
private static class IO {
static {
setUpScanner();
}
private static Scanner scanner;
private static void setUpScanner() {
scanner = new Scanner(new java.io.BufferedInputStream(System.in));
};
public static boolean isEmpty() {
return !scanner.hasNext();
}
public static boolean hasNextLine() {
return scanner.hasNextLine();
}
public static String readLine() {
String line;
try {
line = scanner.nextLine();
} catch (Exception e) {
line = null;
}
return line;
}
public static String readString() {
return scanner.next();
}
public static int readInt() {
return scanner.nextInt();
}
public static double readDouble() {
return scanner.nextDouble();
}
public static float readFloat() {
return scanner.nextFloat();
}
public static long readLong() {
return scanner.nextLong();
}
public static void print(Object o) {
System.out.print(o.toString());
}
public static void println(Object o) {
System.out.println(o);
}
}
private static class Algo {
private static int binSearch(int[] array, int n, int lo, int hi) {
if (lo > hi) {
return lo;
}
int mid = lo + (hi - lo) / 2;
int cmp = (int) Math.signum(n - array[mid]);
if (cmp < 0) {
return binSearch(array, n, lo, mid - 1);
} else if (cmp == 0) {
return mid;
} else {
return binSearch(array, n, mid + 1, hi);
}
}
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | x1,y1=map(int,raw_input().split())
x2,y2=map(int,raw_input().split())
n=int(input())
c=0
for _ in xrange(n):
a1,b1,c1=map(int,raw_input().split())
if((a1*x1+b1*y1+c1)*(a1*x2+b1*y2+c1)<0):
c+=1
print c | PYTHON |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.awt.geom.Line2D;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double intx1 = input.nextInt();
double inty1 = input.nextInt();
double intx2 = input.nextInt();
double inty2 = input.nextInt();
double XX = Math.min(intx1, intx2) - 1;
double XY = Math.max(intx1, intx2) + 1;
double YX = Math.min(inty1, inty2) - 1;
double YY = Math.max(inty1, inty2) + 1;
int intLength = input.nextInt();
Line2D[] camino = new Line2D[intLength];
int[][][] intMat = new int[intLength][intLength][intLength];
for (int i = 0; i < intLength; i++) {
//intMat[i][0][0]=input.nextInt();
//intMat[i][0][1]= input.nextInt();
//intMat[i][0][2]= input.nextInt();
int int1 = input.nextInt();
int int2 = input.nextInt();
int int3 = input.nextInt();
if (int2 == 0) {
camino[i] = new Line2D.Double(-int3 / (double) int1, YX, -int3 / (double) int1, YY);
//camino[i] = new Line2D.Double(-int3 / (double) int1, YX, -int3 / (double) int1, maxy);
} else {
camino[i] = new Line2D.Double(XX, (-int1 * XX - int3) / int2, XY, (-int1 * XY - int3) / int2);
//camino[i] = new Line2D.Double(XX, (-int1 * XX - int3) / int2, XY, (-int1 * XY - int3) / int2);
}
}
Line2D mapa = new Line2D.Double(intx1, inty1, intx2, inty2);
int intIndice = 0;
for (int i = 0; i < intLength; i++)
if (mapa.intersectsLine(camino[i]))
intIndice++;
System.out.println(intIndice);
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.io.*;
import java.util.*;
//public final class code
class code
{
static void solve()throws IOException
{
long x1=nextLong();
long y1=nextLong();
long x2=nextLong();
long y2=nextLong();
int n=nextInt();
int count=0;
while(n-->0)
{
long a=nextLong();
long b=nextLong();
long c=nextLong();
long first=a*x1+b*y1+c;
long second=a*x2+b*y2+c;
if(first>0 && second<0 || first<0 && second>0)
count++;
}
out.println(count);
}
///////////////////////////////////////////////////////////
static final long mod=(long)(1e9+7);
static final int inf=(int)(1e9);
static class Pair
{
long first,second;
Pair(int a,int b)
{
first=a;
second=b;
}
// public int compareTo(Pair p)
// {
// return this.second-p.second;
// }
}
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
static String nextToken()throws IOException
{
while(st==null || !st.hasMoreTokens())
st=new StringTokenizer(br.readLine());
return st.nextToken();
}
static String nextLine()throws IOException
{
return br.readLine();
}
static int nextInt()throws IOException
{
return Integer.parseInt(nextToken());
}
static long nextLong()throws IOException
{
return Long.parseLong(nextToken());
}
static double nextDouble()throws IOException
{
return Double.parseDouble(nextToken());
}
public static void main(String args[])throws IOException
{
br=new BufferedReader(new InputStreamReader(System.in));
out=new PrintWriter(new BufferedOutputStream(System.out));
solve();
out.close();
}
} | JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | import java.util.Scanner;
public class T499C {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int i,n,res=0;
long A,B,C,x1,y1,x2,y2,res1,res2;
x1=in.nextLong();
y1=in.nextLong();
x2=in.nextLong();
y2=in.nextLong();
n=in.nextInt();
long[]a=new long[n],b=new long[n],c=new long[n];
for(i=0;i<n;i++){
a[i]=in.nextLong();
b[i]=in.nextLong();
c[i]=in.nextLong();
}
in.close();;
for(i=0;i<n;i++){
res1=a[i]*x1+b[i]*y1+c[i];
res2=a[i]*x2+b[i]*y2+c[i];
if(Long.signum(res1)!=Long.signum(res2))
res++;
}
System.out.println(res);
}
}
| JAVA |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
long long power(long x, long n) {
if (n == 0) return 1;
if (n == 1) return x;
long n2 = n / 2;
long long po = power(x, n2);
if (n % 2) return po * po * x;
return po * po;
}
long long gcd(long long a, long long b) {
if (b == 0) return a;
a %= b;
return gcd(b, a);
}
class CCrazyTown {
public:
int sign(long long x) { return x > 0 ? 1 : -1; }
void solve(std::istream &cin, std::ostream &cout) {
long long X[2], Y[2], n, i, ans = 0, a, b, c;
cin >> X[0] >> Y[0] >> X[1] >> Y[1];
cin >> n;
for (i = 0; i < n; i++) {
cin >> a >> b >> c;
if (sign(a * X[0] + b * Y[0] + c) != sign(a * X[1] + b * Y[1] + c)) ans++;
}
cout << ans << '\n';
}
};
int main() {
ios_base::sync_with_stdio(false), cin.tie(0);
CCrazyTown solver;
std::istream &in(std::cin);
std::ostream &out(std::cout);
solver.solve(in, out);
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | h1, h2 = list(map(int,input().split()))
u1, u2 = list(map(int,input().split()))
steps = 0
for i in int(input())*'_':
a, b, c= list(map(int,input().split()))
if (a * h1 + b * h2 + c) * (a * u1 + b * u2 + c) < 0:
steps+=1
print(steps) | PYTHON3 |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
int n;
long long a, b, c;
long long x1, yy, x2, y2;
int main() {
while (~scanf("%I64d%I64d%I64d%I64d", &x1, &yy, &x2, &y2)) {
scanf("%d", &n);
int num = 0;
for (int i = 1; i <= n; i++) {
scanf("%I64d%I64d%I64d", &a, &b, &c);
if ((a * x1 + b * yy + c < 0 && a * x2 + b * y2 + c > 0) ||
(a * x1 + b * yy + c > 0 && a * x2 + b * y2 + c < 0))
num++;
}
printf("%d\n", num);
}
return 0;
}
| CPP |
498_A. Crazy Town | Crazy Town is a plane on which there are n infinite line roads. Each road is defined by the equation aix + biy + ci = 0, where ai and bi are not both equal to the zero. The roads divide the plane into connected regions, possibly of infinite space. Let's call each such region a block. We define an intersection as the point where at least two different roads intersect.
Your home is located in one of the blocks. Today you need to get to the University, also located in some block. In one step you can move from one block to another, if the length of their common border is nonzero (in particular, this means that if the blocks are adjacent to one intersection, but have no shared nonzero boundary segment, then it are not allowed to move from one to another one in one step).
Determine what is the minimum number of steps you have to perform to get to the block containing the university. It is guaranteed that neither your home nor the university is located on the road.
Input
The first line contains two space-separated integers x1, y1 ( - 106 β€ x1, y1 β€ 106) β the coordinates of your home.
The second line contains two integers separated by a space x2, y2 ( - 106 β€ x2, y2 β€ 106) β the coordinates of the university you are studying at.
The third line contains an integer n (1 β€ n β€ 300) β the number of roads in the city. The following n lines contain 3 space-separated integers ( - 106 β€ ai, bi, ci β€ 106; |ai| + |bi| > 0) β the coefficients of the line aix + biy + ci = 0, defining the i-th road. It is guaranteed that no two roads are the same. In addition, neither your home nor the university lie on the road (i.e. they do not belong to any one of the lines).
Output
Output the answer to the problem.
Examples
Input
1 1
-1 -1
2
0 1 0
1 0 0
Output
2
Input
1 1
-1 -1
3
1 0 0
0 1 0
1 1 -3
Output
2
Note
Pictures to the samples are presented below (A is the point representing the house; B is the point representing the university, different blocks are filled with different colors):
<image> <image> | 2 | 7 | #include <bits/stdc++.h>
const double eps = 1e-8;
int dcmp(double x) {
if (fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
int n, X1, Y1, X2, Y2, xmin, xmax, ymin, ymax;
int a, b, c;
double K, B;
bool f;
int chk() {
if (f) {
if (b == 0) return 0;
double y = (-c - a * X1) * 1.0 / b;
if (dcmp(y - ymin) >= 0 && dcmp(y - ymax) <= 0) return 1;
return 0;
}
double x = -(B * b + c) / (a + b * K);
if (dcmp(x - xmin) >= 0 && dcmp(x - xmax) <= 0) return 1;
return 0;
}
int main() {
while (~scanf("%d %d", &X1, &Y1)) {
scanf("%d %d", &X2, &Y2);
scanf("%d", &n);
ymin = Y1 < Y2 ? Y1 : Y2;
ymax = Y1 + Y2 - ymin;
xmin = X1 < X2 ? X1 : X2;
xmax = X1 + X2 - xmin;
if (X1 == X2) {
f = 1;
} else {
K = (Y1 - Y2) * 1.0 / (X1 - X2);
B = Y1 - K * X1;
f = 0;
}
int ans = 0;
while (n--) {
scanf("%d %d %d", &a, &b, &c);
ans += chk();
}
printf("%d\n", ans);
}
return 0;
}
| CPP |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.