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
read_line = lambda: [int(i) for i in input().split()] x0, y0 = read_line() x1, y1 = read_line() print(sum((a * x0 + b * y0 + c) * (a * x1 + b * y1 + c) < 0 for a, b, c in (read_line() for i in range(int(input()))))) # Made By Mostafa_Khaled
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.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * 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); OutputWriter out = new OutputWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int x1 = in.readInt(); int y1 = in.readInt(); int x2 = in.readInt(); int y2 = in.readInt(); int res = 0; int n = in.readInt(); while (n-- > 0) { long a = in.readInt(); long b = in.readInt(); long c = in.readInt(); long v1 = a * x1 + b * y1 + c; long v2 = a * x2 + b * y2 + c; if ((v1 ^ v2) < 0) { res++; } } out.printLine(res); } } 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 read() { 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 readInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.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; signed main() { ios_base::sync_with_stdio(false), cin.tie(NULL); long long x1, y1; cin >> x1 >> y1; long long x2, y2; cin >> x2 >> y2; long long n; cin >> n; long long ans = 0; for (long long i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; long long x = a * x1 + b * y1 + c; long long y = a * x2 + b * y2 + c; if ((x > 0 && y < 0) || (x < 0 && y > 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; long long x, y, z, w, a, b, c, n, ans; int main() { cin >> x >> y >> z >> w >> n; while (n--) { cin >> a >> b >> c; if (a * x + b * y + c > 0 != a * z + b * w + 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; int main() { long long ans = 0; long long homex, homey; long long schoolx, schooly; cin >> homex >> homey; cin >> schoolx >> schooly; long long n; cin >> n; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; long long num1 = (a * homex) + (b * homey) + c; long long num2 = (a * schoolx) + (b * schooly) + c; num1 = (num1 < 0) ? 1 : 0; num2 = (num2 < 0) ? 1 : 0; ans += (num1 != num2); } cout << ans << 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.*; import java.util.*; /** * Created by Dimon on 14.12.2014. */ public class Solution { public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(System.in); double x1 = scanner.nextDouble(); double y1 = scanner.nextDouble(); double x2 = scanner.nextDouble(); double y2 = scanner.nextDouble(); int n = scanner.nextInt(); int ans = 0; for (int i = 0; i < n; i++){ double a, b, c; a = scanner.nextDouble(); b = scanner.nextDouble(); c = scanner.nextDouble(); double d1 = (a * x1 + b * y1 + c); double d2 = (a * x2 + b * y2 + c); if (d1 * d2 < 0) ans++; } System.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
#input x1,y1=map(int,input().split()) x2,y2=map(int,input().split()) n=int(input()) r=[] for i in range(n): r.append([int(x) for x in input().split()]) #variables def f(a,b,c,x,y): if b*y>-c-a*x: return 'UP' else: return 'DOWN' s=n #main for i in range(n): if f(r[i][0],r[i][1],r[i][2],x1,y1)==f(r[i][0],r[i][1],r[i][2],x2,y2): s-=1 #output 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.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CrazyTown { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); Point home = new Point(sc.nextInt(), sc.nextInt()); Point uni = new Point(sc.nextInt(), sc.nextInt()); Line way = new Line(home, uni); int result = 0; int n= sc.nextInt(); for(int i=0; i<n; i++){ int a= sc.nextInt(); int b= sc.nextInt(); int c= sc.nextInt(); Line road; if(b==0) road= new Line(1,0,c/a); else road= new Line(1.0*a/b,1,1.0*c/b); Point inter= way.intersect(road); if(inter!=null && inter.between(uni, home)) result++; } System.out.println(result); } static 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(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } Line(Point p, double m) { a = -m; b = 1; c = -(a * p.x + p.y); } 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); } } static public class Point implements Comparable<Point> { static final double EPS = 1e-9; double x, y; Point(double a, double b) { x = a; y = b; } public int compareTo(Point p) { if (Math.abs(x - p.x) > EPS) return x > p.x ? 1 : -1; if (Math.abs(y - p.y) > EPS) return y > p.y ? 1 : -1; return 0; } public double dist(Point p) { return Math.sqrt(sq(x - p.x) + sq(y - p.y)); } static double sq(double x) { return x * x; } boolean between(Point p, Point q) { return x < Math.max(p.x, q.x) + EPS && x + EPS > Math.min(p.x, q.x) && y < Math.max(p.y, q.y) + EPS && y + EPS > Math.min(p.y, q.y); } // returns true if it is on the line defined by a and b } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 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 { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
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 n, m, x1, y11, x2, y2, aa, bb, cc; double ans1, ans2, a, b, c; int ans = 0; int main() { scanf("%d%d%d%d", &x1, &y11, &x2, &y2); scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &aa, &bb, &cc); c = -cc; a = aa; b = bb; if (b != 0) { ans1 = (c - a * x1) / b; ans1 -= y11; ans2 = (c - a * x2) / b; ans2 -= y2; if (ans1 * ans2 < 0) ans++; } else { ans1 = (c - b * y11) / a; ans1 -= x1; ans2 = (c - b * y2) / a; ans2 -= x2; if (ans1 * ans2 < 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
p = tuple(map(int,input().split())) q = tuple(map(int,input().split())) n = int(input()) lines = [ tuple(map(int,input().split()) ) for _ in range(n) ] def cal(a,b,c,p,q): ux = -b uy = a if b==0: y=0 x=-c/a else: x=0 y=-c/b px,py = p qx,qy = q s1 = (px-x)*uy-(py-y)*ux s2 = (qx-x)*uy-(qy-y)*ux return 1 if s1*s2<0 else 0 res = 0 for L in lines: res += cal(*L,p,q) print(res) # C:\Users\Usuario\HOME2\Programacion\ACM
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() { long long int A[2][2], n, a, b, c, d, e, i, ans = 0; cin >> A[0][0] >> A[0][1] >> A[1][0] >> A[1][1]; cin >> n; for (i = 0; i < n; i++) { cin >> a >> b >> c; d = (a * A[0][0] + b * A[0][1] + c); e = (a * A[1][0] + b * A[1][1] + c); if ((d > 0 && e < 0) || (d < 0 && e > 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; int a[2], b[2]; bool isOppo(vector<int> line) { long long first = (long long)a[0] * line[0] + (long long)a[1] * line[1] + (long long)line[2]; long long second = (long long)b[0] * line[0] + (long long)b[1] * line[1] + (long long)line[2]; if (first > second) swap(first, second); if (first < 0 && second > 0) return true; return false; } void solve() { cin >> a[0] >> a[1]; cin >> b[0] >> b[1]; cin >> n; vector<vector<int>> v(n, vector<int>(3)); for (int i = 0; i < n; ++i) for (int j = 0; j < 3; ++j) cin >> v[i][j]; int cnt = 0; for (int i = 0; i < n; ++i) { if (isOppo(v[i])) ++cnt; } cout << cnt << endl; } int main() { ios_base ::sync_with_stdio(false), cin.tie(nullptr); ; int tt; tt = 1; for (int i = 1; i <= tt; ++i) { 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; int main() { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; long long n; cin >> n; bool home[500]; bool uni[500]; long long A[500], B[500], C[500]; for (int i = 0; i < n; i++) { cin >> A[i] >> B[i] >> C[i]; } for (int i = 0; i < n; i++) { if (A[i] == 0) { if (y1 < (double)(C[i]) / (double)B[i] * -1) { home[i] = false; } else { home[i] = true; } if (y2 < (double)C[i] / (double)B[i] * -1) { uni[i] = false; } else { uni[i] = true; } } else if (B[i] == 0) { if (x1 < (double)C[i] / (double)A[i] * -1) { home[i] = false; } else { home[i] = true; } if (x2 < (double)C[i] / (double)A[i] * -1) { uni[i] = false; } else { uni[i] = true; } } else { long long temp = A[i] * x1 + C[i]; if (temp > y1 * B[i] * -1) { home[i] = false; } else { home[i] = true; } temp = A[i] * x2 + C[i]; if (temp > y2 * B[i] * -1) { uni[i] = false; } else { uni[i] = true; } } } long long counter = 0; for (int i = 0; i < n; i++) { if (home[i] != uni[i]) { counter++; } } cout << counter << 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.Arrays; import java.util.Hashtable; import java.util.Scanner; public class Solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long x1 = sc.nextLong(); long y1 = sc.nextLong(); long x2 = sc.nextLong(); long y2 = sc.nextLong(); long n = sc.nextLong(); long r = 0; for(long i=0;i<n;i++){ long a = sc.nextLong(); long b = sc.nextLong(); long c = sc.nextLong(); if((a*x1+b*y1+c)/100000000000.0*(a*x2+b*y2+c)<0) r++; } System.out.println(r); } }
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 codeforces; import java.io.*; import java.util.*; public class A { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(System.out); A() throws IOException { // reader = new BufferedReader(new FileReader("input.txt")); // writer = new PrintWriter(new FileWriter("output.txt")); } StringTokenizer stringTokenizer; String next() throws IOException { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } void solve() throws IOException { int xh = nextInt(), yh = nextInt(); int xu = nextInt(), yu = nextInt(); int n = nextInt(); int ans = 0; for(int i = 0; i < n; i++) { int a = nextInt(), b = nextInt(), c = nextInt(); if(Long.signum(1l * a * xh + 1l * b * yh + c) * Long.signum(1l * a * xu + 1l * b * yu + c) < 0) { ans++; } } writer.println(ans); writer.close(); } public static void main(String[] args) throws IOException { new A().solve(); } }
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> int main() { long long int a, b, c, d; scanf("%lld%lld%lld%lld", &a, &b, &c, &d); int e; scanf("%d", &e); int count = 0; for (int i = 1; i <= e; i++) { long long int f, g, h; scanf("%lld%lld%lld", &f, &g, &h); long long int s1 = f * a + g * b + h; long long int s2 = f * c + g * d + h; if (s1 < 0 && s2 > 0) count++; if (s1 > 0 && s2 < 0) count++; } printf("%d\n", count); }
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 pwr(long long base, long long p) { long long ans = 1; while (p) { if (p & 1) ans = (ans * base) % (10000007LL); base = (base * base) % (10000007LL); p >>= 1; } return ans; } long long modInv(long long n, long long mod) { return pwr(n, mod - 2); } int bitCount(int n) { int c = 0; while (n) { c++; n &= n - 1; } return c; } struct Line { long long a, b, c; }; Line temp; int n; Line arr[400]; long long xx1, yy1, xx2, yy2; bool oppositeSides(int i) { long long sign1 = xx1 * arr[i].a + yy1 * arr[i].b + arr[i].c; long long sign2 = xx2 * arr[i].a + yy2 * arr[i].b + arr[i].c; if (sign1 < 0 && sign2 > 0) return true; if (sign1 > 0 && sign2 < 0) return true; return false; } int main() { cin >> xx1 >> yy1 >> xx2 >> yy2; int i; cin >> n; for (i = 0; i < n; i++) cin >> arr[i].a >> arr[i].b >> arr[i].c; int ans = 0; for (i = 0; i < n; i++) { if (oppositeSides(i)) 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; template <class T> inline T poww(T b, T p) { long long a = 1; while (p) { if (p & 1) { a = (a * b); } p >>= 1; b = (b * b); } return a; } template <class T> inline T poww2(T b, int p) { T a = 1; while (p) { if (p & 1) { a = (a * b); } p >>= 1; b = (b * b); } return a; } template <class T> inline T modpoww(T b, T p, T mmod) { long long a = 1; while (p) { if (p & 1) { a = (a * b) % mmod; } p >>= 1; b = (b * b) % mmod; } return a % mmod; } template <class T> inline T gcd(T a, T b) { if (b > a) return gcd(b, a); return ((b == 0) ? a : gcd(b, a % b)); } template <class T> inline void scan(vector<T>& a, int n) { T b; int i; for ((i) = 0; (i) < (n); (i) += 1) { cin >> b; a.push_back(b); } } inline void scand(vector<int>& a, int n) { int b; int i; for ((i) = 0; (i) < (n); (i) += 1) { scanf("%d", &(b)); a.push_back(b); } } int main() { int i, j, k, t; int n; long long cod[4]; for ((i) = 0; (i) < (4); (i) += 1) cin >> cod[i]; cin >> n; long long a, b, c; int ans = 0; for ((i) = 0; (i) < (n); (i) += 1) { cin >> a >> b >> c; long long s1 = (a * cod[2] + b * cod[3] + c); long long s2 = (a * cod[0] + b * cod[1] + c); if (s1 > 0 && s2 > 0) continue; else if (s1 < 0 && s2 < 0) continue; ans++; } cout << ans << "\n"; }
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 = 1000000000ll; const long long inf64 = inf * inf; const long long base = inf / 10ll; bool solve() { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; vector<pair<long long, pair<long long, long long> > > p(n); for (int i(0); i < n; i++) cin >> p[i].first >> p[i].second.first >> p[i].second.second; int ans = 0; for (int i(0); i < n; i++) { long long a, b, c; a = p[i].first; b = p[i].second.first; c = p[i].second.second; if (a * x1 + b * y1 + c > 0 && a * x2 + b * y2 + c < 0) ans++; else if (a * x1 + b * y1 + c < 0 && a * x2 + b * y2 + c > 0) ans++; } cout << ans << '\n'; return true; } 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
__author__ = 'Rakshak.R.Hegde' """ Created on Dec 24 2014 PM 10:42 @author: Rakshak.R.Hegde """ x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) n = int(input()) cost = 0 for i in range(n): a, b, c = map(int, input().split()) d1 = a * x1 + b * y1 + c d2 = a * x2 + b * y2 + c if d1 * d2 < 0: cost += 1 print(cost)
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 = map(int, input().split()) xU, yU = map(int, input().split()) n = int(input()) def cross_product(a, b, c): v1 = a * xH + b * yH + c v2 = a * xU + b * yU + c return v1 < 0 and v2 > 0 or v1 > 0 and v2 < 0 res = 0 for _ in range(n): a, b, c = map(int, input().split()) if cross_product(a, b ,c): 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
import java.io.*; import java.util.*; public class C { void solve() throws IOException { double x1 = nextDouble(), y1 = nextDouble(); double x2 = nextDouble(), y2 = nextDouble(); long counter = 0; for (int t = nextInt(); t > 0; --t) { double a = nextDouble(), b = nextDouble(), c = nextDouble(); double v1 = a * x1 + b * y1 + c; double v2 = a * x2 + b * y2 + c; if (v1 * v2 < 0) { counter++; } } out.println(counter); } void run() throws IOException { reader = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(new OutputStreamWriter(System.out)); // reader = new BufferedReader(new FileReader("file.in")); // out = new PrintWriter(new FileWriter("file.out")); tokenizer = null; solve(); reader.close(); out.flush(); } public static void main(String[] args) throws IOException { new C().run(); } BufferedReader reader; StringTokenizer tokenizer; 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 (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
hx, hy = map(int, input().split()) ux, uy = map(int, input().split()) n = int(input()) count = 0 for i in range(n): a,b,c = map(int, input().split()) h = a*hx + b*hy + c u = a*ux + b*uy + c if h*u<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.io.InputStream; import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.BufferedReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStreamReader; import java.io.IOException; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Alejandro Lopez */ 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); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { long x1 = in.nextLong(); long y1 = in.nextLong(); long x2 = in.nextLong(); long y2 = in.nextLong(); int ans = 0; int n = in.nextInt(); for (int i = 0; i < n; ++i) { long a = in.nextLong(); long b = in.nextLong(); long c = in.nextLong(); long rp1 = relPos(x1, y1, a, b, c); long rp2 = relPos(x2, y2, a, b, c); // out.println("rp1 = " + rp1); // out.println("rp2 = " + rp2); if (rp1 != rp2) { ++ans; } } out.println(ans); } private int relPos(long x, long y, long a, long b, long c) { if (b == 0) { return (a * x + c > 0) ? 1 : 2; } if ((a * x + b * y + c > 0 && b > 0) || (a * x + b * y + c < 0 && b < 0)) return 1; if ((a * x + b * y + c < 0 && b > 0) || (a * x + b * y + c < 0 && b > 0)) return 2; return 3; } } static class InputReader { BufferedReader in; StringTokenizer tokenizer = null; public InputReader(InputStream inputStream) { in = new BufferedReader(new InputStreamReader(inputStream)); } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(in.readLine()); } return tokenizer.nextToken(); } catch (IOException e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(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.util.Scanner; public class crazytown { public static void main(String[] args) { Scanner s = new Scanner(System.in); int hx = s.nextInt(); int hy = s.nextInt(); int ux = s.nextInt(); int uy = s.nextInt(); int co = s.nextInt(); int steps =0; while(co-- >0){ long a = s.nextLong(); long b = s.nextLong(); long c = s.nextLong(); long h = (long) (a*hx + b*hy + c); long u =(long) (a*ux + b*uy + c); if((h >0 && u <0) || (h <0 && u >0)) steps++; } System.out.println(steps); } }
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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; /** * * @author Reza */ public class C2 { static ArrayList<MNode> alist; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); alist = new ArrayList<MNode>(); StringTokenizer stt1 = new StringTokenizer(br.readLine()); StringTokenizer stt2 = new StringTokenizer(br.readLine()); double x1 = Integer.parseInt(stt1.nextToken()); double y1 = Integer.parseInt(stt1.nextToken()); double x2 = Integer.parseInt(stt2.nextToken()); double y2 = Integer.parseInt(stt2.nextToken()); double mm; double mb; if (x1 == x2) { mm = Double.MAX_VALUE; mb = x1; } else { mm = (y2 - y1) / (x2 - x1); mb = y1 - mm * x1; } int n = Integer.parseInt(br.readLine()); for (int i = 0; i < n; i++) { StringTokenizer stt = new StringTokenizer(br.readLine()); double ia = Integer.parseInt(stt.nextToken()); double ib = Integer.parseInt(stt.nextToken()); double ic = Integer.parseInt(stt.nextToken()); double m; double b; if (ib == 0) { m = Double.MAX_VALUE; b = -ic / ia; } else { m = -ia / ib; b = -ic / ib; } MNode node = new MNode(m, b); alist.add(node); } int sum = 0; for (int i = 0; i < n; i++) { if (mm != alist.get(i).m) { double xc; double yc; if (mm == Double.MAX_VALUE) { xc = mb; yc = alist.get(i).m * xc + alist.get(i).b; } else if (alist.get(i).m == Double.MAX_VALUE) { xc = alist.get(i).b; yc = mm * xc + mb; } else { xc = (alist.get(i).b - mb) / (mm - alist.get(i).m); yc = mm * xc + mb; } if (xc <= Math.max(x1, x2) && xc >= Math.min(x1, x2) && yc <= Math.max(y1, y2) && yc >= Math.min(y1, y2)) { sum++; } } } System.out.println(sum); } } class MNode { double m; double b; public MNode(double m, double b) { this.m = m; this.b = 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
x1, y1 = [int(x) for x in input().split()] x2, y2 = [int(x) for x in input().split()] n = int(input()) ans = 0 for i in range(n): a, b, c = [int(x) for x in input().split()] x = x1 * a + y1 * b + c x //= abs(x) y = x2 * a + y2 * b + c y //= abs(y) if x != y: 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; const int maxn = 1e6 + 100; const long long inf = 0x3f3f3f3f; inline long long rd() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } long long ax, ay, bx, by; long long x, y, z, n; int main() { ax = rd(); ay = rd(); bx = rd(); by = rd(); n = rd(); int sum = 0; while (n--) { x = rd(); y = rd(); z = rd(); long long xian1 = ax * x + ay * y + z; long long xian2 = bx * x + by * y + z; if ((xian1 > 0 && xian2 < 0) || (xian1 < 0 && xian2 > 0)) 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
#include <bits/stdc++.h> using namespace std; long long x[2], y[2], dis[2]; long long a, b, c; int n; int main() { int ans = 0; for (int i = 0; i < 2; i++) { scanf("%lld%lld", &x[i], &y[i]); } scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld%lld%lld", &a, &b, &c); for (int j = 0; j < 2; j++) { dis[j] = a * x[j] + b * y[j] + c; dis[j] /= llabs(dis[j]); } if (dis[0] != dis[1]) 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.util.StringTokenizer; public class Main { static long xu, xh, yu, yh, a, b, c; public static boolean intersect(){ return ((a*xu + b*yu + c > 0L) != (a*xh + b*yh + c) > 0L) ; } public static void main(String[] args) throws IOException{ BufferedReader buf = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(buf.readLine()); xh = Long.parseLong(st.nextToken()); yh = Long.parseLong(st.nextToken()); st = new StringTokenizer(buf.readLine()); xu = Long.parseLong(st.nextToken()); yu = Long.parseLong(st.nextToken()); int n = Integer.parseInt(buf.readLine().trim()); int cant = 0; for(int i = 0; i < n; i++){ st = new StringTokenizer(buf.readLine()); a = Long.parseLong(st.nextToken()); b = Long.parseLong(st.nextToken()); c = Long.parseLong(st.nextToken()); if( intersect()){ cant++; } } System.out.println(cant); } }
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 int x1, x2, y1, y2; scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2); long long int n, a, b, c; scanf("%lld", &n); long long int ans = 0; while (n--) { scanf("%lld %lld %lld", &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++; } } printf("%lld\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; int main() { long long x1, x2, y1, y2; long long a, b, c; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; int ans = 0; while (n--) { scanf("%lld%lld%lld", &a, &b, &c); if (((a * x1 + b * y1 + c) / abs(a * x1 + b * y1 + c)) * ((a * x2 + b * y2 + c) / abs(a * x2 + b * y2 + c)) < 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 toInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; } long long toInt64(string s) { long long r = 0; istringstream sin(s); sin >> r; return r; } double toDouble(string s) { double r = 0; istringstream sin(s); sin >> r; return r; } string toString(long long n) { string s, s1; while (n / 10 > 0) { s += (char)((n % 10) + 48); n /= 10; } s += (char)((n % 10) + 48); n /= 10; s1 = s; for (int i = 0; i < s.length(); i++) s1[(s.length() - 1) - i] = s[i]; return s1; } bool isUpperCase(char c) { return c >= 'A' && c <= 'Z'; } bool isLowerCase(char c) { return c >= 'a' && c <= 'z'; } bool isLetter(char c) { return c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z'; } bool isDigit(char c) { return c >= '0' && c <= '9'; } char toLowerCase(char c) { return (isUpperCase(c)) ? (c + 32) : c; } char toUpperCase(char c) { return (isLowerCase(c)) ? (c - 32) : c; } long long gcd(long long a, long long b) { if (!a) return b; return gcd(b % a, a); } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; long long x1, y2, x2, y1; cin >> x1 >> y1; cin >> x2 >> y2; cin >> n; long long cnt = 0; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; long long pos1 = (a * x1) + (b * y1) + c; long long pos2 = (a * x2) + (b * y2) + c; if (((pos1 > 0 && pos2 < 0) || (pos1 < 0 && pos2 > 0))) cnt++; } cout << cnt << 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.awt.geom.*; import java.io.*; import java.math.*; import java.util.*; import java.util.regex.*; import static java.lang.Math.*; public class A { public A() throws Exception { long x1 = in.nextInt(); long y1 = in.nextInt(); long x2 = in.nextInt(); long y2 = in.nextInt(); int n = in.nextInt(); int ans = 0; for (int i=0; i<n; i++) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); boolean b1 = a*x1+b*y1+c > 0; boolean b2 = a*x2+b*y2+c > 0; if (b1!=b2) ans++; } buf.append(ans).append('\n'); } public static Scanner in = new Scanner(System.in); public static StringBuilder buf = new StringBuilder(); public static void debug(Object... arr) { // {{{ System.err.println(Arrays.deepToString(arr)); } // }}} public static void main(String[] args) throws Exception { // {{{ new A(); System.out.print(buf); } // }}} public static class Scanner { // {{{ BufferedReader br; String line; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public boolean hasNext() throws IOException { while ((st==null||!st.hasMoreTokens())&&(line=br.readLine())!=null) st = new StringTokenizer(line); return st.hasMoreTokens(); } public String next() throws IOException { if (hasNext()) return st.nextToken(); throw new NoSuchElementException(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { 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.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class CF2 { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int x1 = sc.nextInt(); int y1 = sc.nextInt(); int x2 = sc.nextInt(); int y2 = sc.nextInt(); Line original = new Line(new Point(x1,y1) , new Point(x2,y2)); int n=sc.nextInt(); int count=0; for(int i=0;i<n;i++) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); Line l = new Line(a,b,c); Point point = l.intersect(original); //System.out.println(point); if(point!=null && point.between(new Point(x1,y1), new Point(x2,y2))) count++; } System.out.println(count); } } class Point implements Comparable<Point>{ static final double EPS = 1e-9; double x, y; Point(double a, double b) { x = a; y = b; } public String toString() { return x+" "+y; } public int compareTo(Point p) { if(Math.abs(x - p.x) > EPS) return x > p.x ? 1 : -1; if(Math.abs(y - p.y) > EPS) return y > p.y ? 1 : -1; return 0; } public double dist(Point p) { return Math.sqrt(sq(x - p.x) + sq(y - p.y)); } static double sq(double x) { return x * x; } Point rotate(double angle) //rotate around (0,0) { double c = Math.cos(angle), s = Math.sin(angle); return new Point(x * c - y * s, x * s + y * c); } // for integer points and rotation by 90 (counterclockwise) : swap x and y, negate x Point rotate(double theta, Point p) //rotate around p { Vector v = new Vector(p, new Point(0, 0)); return translate(v).rotate(theta).translate(v.reverse()); } Point translate(Vector v) { return new Point(x + v.x , y + v.y); } Point reflectionPoint(Line l) //reflection point of p on line l { Point p = l.closestPoint(this); Vector v = new Vector(this, p); return this.translate(v).translate(v); } boolean between(Point p, Point q) //it means that it is inside the rectangle created by these points , not on their line { return x <= Math.max(p.x, q.x) + EPS && x + EPS >= Math.min(p.x, q.x) && y <= Math.max(p.y, q.y) + EPS && y + EPS >= Math.min(p.y, q.y); } //returns true if it is on the line defined by a and b boolean onLine(Point a, Point b) { if(a.compareTo(b) == 0) return compareTo(a) == 0; return Math.abs(new Vector(a, b).cross(new Vector(a, this))) < EPS; } boolean onSegment(Point a, Point b) { if(a.compareTo(b) == 0) return compareTo(a) == 0; return onRay(a, b) && onRay(b, a); } //returns true if it is on the ray whose start point is a and passes through b boolean onRay(Point a, Point b) { if(a.compareTo(b) == 0) return compareTo(a) == 0; return new Vector(a, b).normalize().equals(new Vector(a, this).normalize()); } // returns true if r is on the left side of line pq (the order is of course importanrt) //if true then p->q->r in counterclockwise direction //if false then p->q->r in clockwise direction // add EPS to LHS to accepts collinear points (means >=) static boolean ccw(Point p, Point q, Point r) //positive cross product means counter-clockwise { return new Vector(p, q).cross(new Vector(p, r)) > 0; } static boolean collinear(Point p, Point q, Point r) { return Math.abs(new Vector(p, q).cross(new Vector(p, r))) < EPS; //cross and dot are on vectors only } static double angle(Point a, Point o, Point b) // angle AOB { Vector oa = new Vector(o, a), ob = new Vector(o, b); return Math.acos(oa.dot(ob) / Math.sqrt(oa.norm2() * ob.norm2())); // it returns a positive angle } static double distToLine(Point p, Point a, Point b) //distance between point p and a line defined by points a, b (a != b) { if(a.compareTo(b) == 0) return p.dist(a); // formula: c = a + u * ab Vector ap = new Vector(a, p), ab = new Vector(a, b); double u = ap.dot(ab) / ab.norm2(); //u is a unit vector // note here we divide by the square of ab (norm2) // to generate a ratio of the projection to the ab // hna momkn el u tkon negative aw >1 3ady f kol 7aga hatzbot Point c = a.translate(ab.scale(u)); return p.dist(c); } // Another way: find closest point and calculate the distance between it and p static double distToLineSegment(Point p, Point a, Point b) { Vector ap = new Vector(a, p), ab = new Vector(a, b); double u = ap.dot(ab) / ab.norm2(); if (u < 0.0) return p.dist(a); if (u > 1.0) return p.dist(b); //8eer kda bat3aml m3ah ka2eno line 3ady 5las ! return distToLine(p, a, b); } // Another way: find closest point and calculate the distance between it and p } 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; } //only returns norm squared Vector reverse() { return new Vector(-x, -y); } Vector normalize() //get the unit vector of this vector { double d = Math.sqrt(norm2()); return scale(1 / d); } } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} 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 { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } class LineSegment implements Comparable { Point p, q ; double transp; LineSegment(Point a, Point b) { p = a; q = b; } LineSegment(Point a, Point b , double x) { p = a; q = b; transp=x;} 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) ; } Point rightPoint() { if(p.x>q.x) return p; else return q; } Point leftPoint() { if(p.x<q.x) return p; else return q; } @Override public int compareTo(Object o) { LineSegment line = (LineSegment) o; if(Math.abs(line. leftPoint().y-leftPoint().y)<0) { //return 0; if(line.rightPoint().y>leftPoint().y) return -1; if(line.rightPoint().y<leftPoint().y) return 1; return 0; } if(line. leftPoint().y> leftPoint().y) return -1; else if(line. leftPoint().y< leftPoint().y) return 1; return 0; } } class Polygon { // Cases to handle: collinear points, polygons with n < 3 static final double EPS = 1e-9; Point[] g; //first point = last point, counter-clockwise representation Polygon(Point[] o) { g = o; } //hya da5la bel a5era elzyada double perimeter() { double sum = 0.0; for(int i = 0; i < g.length - 1; ++i) sum += g[i].dist(g[i+1]); return sum; } double area() //clockwise/anti-clockwise check, for convex/concave polygons { //the method that sums areas of trapezoids under each line i.e summation( y[i]*x[i+1] - x[i]*y[i+1] ) from i=0 to i=n-1 double area = 0.0; for(int i = 0; i < g.length - 1; ++i) area += g[i].x * g[i+1].y - g[i].y * g[i+1].x; return Math.abs(area) / 2.0; //negative value in case of clockwise } boolean isConvex() { if(g.length <= 3) // point or line return false; boolean ccw = Point.ccw(g[g.length - 2], g[0], g[1]); //edit ccw check to accept collinear points for(int i = 1; i < g.length - 1; ++i) if(Point.ccw(g[i-1], g[i], g[i+1]) != ccw) return false; return true; } boolean inside(Point p) //for convex/concave polygons - winding number algorithm { double sum = 0.0; for(int i = 0; i < g.length - 1; ++i) { double angle = Point.angle(g[i], p, g[i+1]); //always positive //if angle is zero or 180 -> it lies on the polygon so return true if((Math.abs(angle) < EPS || Math.abs(angle - Math.PI) < EPS) && p.between(g[i], g[i+1])) return true; if(Point.ccw(p, g[i], g[i+1])) //if it is clockwise angle -> add else -> subtract sum += angle; else sum -= angle; } return Math.abs(2 * Math.PI - Math.abs(sum)) < EPS; //abs makes it work for clockwise } /* * Another way if the polygon is convex * 1. Triangulate the poylgon through p * 2. Check if sum areas == poygon area * 3. Handle empty polygon */ Polygon cutPolygon(Point a, Point b) //returns the left part of the polygon, swap a & b for the right part { Point[] ans = new Point[g.length<<1]; Line l = new Line(a, b); Vector v = new Vector(a, b); int size = 0; for(int i = 0; i < g.length; ++i) { double left1 = v.cross(new Vector(a, g[i])); double left2 = i == g.length - 1 ? 0 : v.cross(new Vector(a, g[i+1])); //ba5leh b zero fela5er 3shan mayd5olsh elcondition elly ta7t if(left1 + EPS > 0) ans[size++] = g[i]; if(left1 * left2 + EPS < 0) //this condition succeeds if left1 and left2 have opposite signs 3shan ya5odhom ray7 rag3 !! ans[size++] = l.intersect(new Line(g[i], g[i+1])); } if(size != 0 && ans[0] != ans[size-1]) //necessary in case g[0] is not in the new polygon ans[size++] = ans[0]; return new Polygon(Arrays.copyOf(ans, size)); } static Polygon convexHull(Point[] points) //all points are unique, remove duplicates, edit ccw to accept collinear points { int n = points.length; Arrays.sort(points); Point[] ans = new Point[n<<1]; int size = 0, start = 0; for(int i = 0; i < n; i++) { Point p = points[i]; while(size - start >= 2 && !Point.ccw(ans[size-2], ans[size-1], p)) --size; ans[size++] = p; } start = --size; for(int i = n-1 ; i >= 0 ; i--) { Point p = points[i]; while(size - start >= 2 && !Point.ccw(ans[size-2], ans[size-1], p)) --size; ans[size++] = p; } // if(size < 0) size = 0 for empty set of points return new Polygon(Arrays.copyOf(ans, size)); } Point centroid() //center of mass { double cx = 0.0, cy = 0.0; for(int i = 0; i < g.length - 1; i++) { double x1 = g[i].x, y1 = g[i].y; double x2 = g[i+1].x, y2 = g[i+1].y; double f = x1 * y2 - x2 * y1; cx += (x1 + x2) * f; cy += (y1 + y2) * f; } double area = area(); //remove abs cx /= 6.0 * area; cy /= 6.0 * area; return new Point(cx, cy); } static double polygonArea (int numberOfSides , double SideLength) { return (SideLength*SideLength*numberOfSides)/ ( 4* Math.tan(Math.PI/numberOfSides) ); } } class Line { static final double INF = 1e9, EPS = 1e-9; //we always try to make b=1 , if it is 0 then we make a=1 //herein this representation a =-slope double a, b, c; Line(Point p, Point q) //two options : a=1 , b=0 (vertical) OR a=constant , b=1 { if(Math.abs(p.x - q.x) < EPS) { a = 1; b = 0; c = -p.x; } // x-s are equal --> the line is vertical ( a*x + 0*y + c = 0 ) else { a = (p.y - q.y) / (q.x - p.x); //slope = -a/b (ax+by+c=0) b = 1.0; c = -(a * p.x + p.y); } } Line(double hh , double w , double e) { if(w==0) { a=1;b=0;c=e/hh; } else { a=hh*1.0/w;b=1;c=e*1.0/w; } } //constructing a line using a point and a slope Line(Point p, double m) { a = -m; b = 1; c = -(a * p.x + p.y); } 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); //using crammer double y; if(Math.abs(b) < EPS) //check whether if the first is vertical or not y = -l.a * x - l.c; else y = -a * x - c; return new Point(x, y); } boolean intersectWithSegment(LineSegment l) { Point point = intersect(new Line(l.p,l.q)); return (point!=null &&point.onSegment(l.p, l.q)); } 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)); } }
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 i64 = long long; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int x1, y1, x2, y2; std::cin >> x1 >> y1 >> x2 >> y2; int ans = 0; int n; std::cin >> n; for (int i = 0; i < n; i++) { int a, b, c; std::cin >> a >> b >> c; i64 A = i64(a) * x1 + i64(b) * y1 + c; i64 B = i64(a) * x2 + i64(b) * y2 + c; if ((A > 0) != (B > 0)) { ans++; } } std::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
import java.util.Arrays; import java.io.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.math.BigInteger; import java.io.PrintStream; 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 * @author Orchid */ 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) { long hx,hy,ux,uy; hx=in.nextLong(); hy=in.nextLong(); ux=in.nextLong(); uy=in.nextLong(); int m=in.nextInt(); long a,b,c,ans=0; for(int i=0;i<m;i++) { a=in.nextLong(); b=in.nextLong(); c=in.nextLong(); long v1=a*hx+b*hy+c; long v2=a*ux+b*uy+c; if((v1<0 && v2 >0) || (v1>0 && v2<0)) { ans++; } } out.println(ans); } } 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 long nextLong() { return Long.parseLong(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; const double eps = 1e-6; int dcmp(double x) { if (fabs(x) < eps) return 0; return (x < 0) ? -1 : 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/(Point A, double p) { return Point(A.x / p, A.y / p); } bool operator<(const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator==(const Point& a, const Point& b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } double Length(Point A) { return sqrt(Dot(A, A)); } double Angle(Point A, Point B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Angle(Point v) { return atan2(v.y, v.x); } double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; } Point Horunit(Point x) { return x / Length(x); } Point Verunit(Point x) { return Point(-x.y, x.x) / Length(x); } Point Rotate(Point A, double rad) { return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); } double Area2(const Point A, const Point B, const Point C) { return Cross(B - A, C - A); } void getLineGeneralEquation(const Point& p1, const Point& p2, double& a, double& b, double& c) { a = p2.y - p1.y; b = p1.x - p2.x; c = -a * p1.x - b * p1.y; } Point GetLineIntersection(Point P, Point v, Point Q, Point w) { Point u = P - Q; double t = Cross(w, u) / Cross(v, w); return P + v * t; } double DistanceToLine(Point P, Point A, Point B) { Point v1 = B - A, v2 = P - A; return fabs(Cross(v1, v2)) / Length(v1); } double DistanceToSegment(Point P, Point A, Point B) { if (A == B) return Length(P - A); Point v1 = B - A, v2 = P - A, v3 = P - B; if (dcmp(Dot(v1, v2)) < 0) return Length(v2); else if (dcmp(Dot(v1, v3)) > 0) return Length(v3); else return fabs(Cross(v1, v2)) / Length(v1); } Point GetLineProjection(Point P, Point A, Point B) { Point v = B - A; return A + v * (Dot(v, P - A) / Dot(v, v)); } bool SegmentProperIntersection(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 dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0; } bool OnSegment(Point p, Point a1, Point a2) { return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0; } double PolygonArea(Point* p, int n) { double area = 0; for (int i = 1; i < n - 1; i++) area += Cross(p[i] - p[0], p[i + 1] - p[0]); return area / 2; } struct Line { Point p; Point v; double ang; Line(Point _p, Point _v) : p(_p), v(_v) { ang = atan2(v.y, v.x); } Line(double a, double b, double c) { v.x = -b; v.y = a; if (dcmp(a) != 0) { p.x = -c / a; p.y = 0.; } else if (dcmp(b) != 0) { p.x = 0.; p.y = -c / b; } } Point point(double a) { return p + (v * a); } bool operator<(const Line& L) const { return ang < L.ang; } void print() { printf("p:%lf %lf v:%lf %lf\n", p.x, p.y, v.x, v.y); } }; Line LineTransHor(Line l, int d) { Point vl = Verunit(l.v); Point p1 = l.p + vl * d; Line ll = Line(p1, l.v); return ll; } Point GetLineIntersection(Line a, Line b) { return GetLineIntersection(a.p, a.v, b.p, b.v); } bool OnLeft(const Line& L, const Point& p) { return Cross(L.v, p - L.p) >= 0; } const double pi = acos(-1.0); struct Circle { Point c; double r; Circle(Point _c = 0, double _r = 0) : c(_c), r(_r) {} Point point(double a) { return Point(c.x + cos(a) * r, c.y + sin(a) * r); } }; double D(Point a, Point b, Circle C) { double ang1, ang2; Point v1, v2; v1 = a - C.c; v2 = b - C.c; ang1 = atan2(v1.y, v1.x); ang2 = atan2(v2.y, v2.x); if (ang2 < ang1) ang2 += 2 * pi; return C.r * (ang2 - ang1); } int getLineCircleIntersection(Line L, Circle C, double& t1, double& t2, vector<Point>& sol) { 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 (dcmp(delta) < 0) return 0; if (dcmp(delta) == 0) { t1 = t2 = -f / (2. * e); sol.push_back(L.point(t1)); return 1; } t1 = (-f - sqrt(delta)) / (2. * e); sol.push_back(L.point(t1)); t2 = (-f + sqrt(delta)) / (2. * e); sol.push_back(L.point(t2)); return 2; } int getCircleCircleIntersection(Circle C1, Circle C2, vector<Point>& Sol) { double d = Length(C1.c - C2.c); if (dcmp(d) == 0) { if (dcmp(C1.r - C2.r) == 0) return -1; return 0; } if (dcmp(C1.r + C2.r - d) < 0) return 0; if (dcmp(fabs(C1.r - C2.r) - d) > 0) return 0; double a = Angle(C2.c - C1.c); 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 (p1 == p2) return 1; Sol.push_back(p2); return 2; } int getTangents(Point p, Circle C, Point* v) { Point u = C.c - p; double dist = Length(u); if (dist < C.r) return 0; else if (dcmp(dist - C.r) == 0) { v[0] = Rotate(u, pi / 2); return 1; } else { double ang = asin(C.r / dist); v[0] = Rotate(u, -ang); v[1] = Rotate(u, ang); return 2; } } int getTengents(Circle A, Circle B, Point* a, Point* b) { int cnt = 0; if (A.r < B.r) { swap(A, B); swap(a, b); } int d2 = (A.c.x - B.c.x) * (A.c.x - B.c.x) + (A.c.y - B.c.y) * (A.c.y - B.c.y); int rdiff = A.r - B.r; int rsum = A.r + B.r; if (d2 < rdiff * rdiff) return 0; double base = atan2(B.c.y - A.c.y, B.c.x - A.c.x); if (d2 == 0 && A.r == B.r) return -1; if (d2 == rdiff * rdiff) { a[cnt] = A.point(base); b[cnt] = B.point(base); cnt++; return 1; } double ang = acos((A.r - B.r) / sqrt(d2)); a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++; a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++; if (d2 == rsum * rsum) { a[cnt] = A.point(base); b[cnt] = B.point(pi + base); cnt++; } else if (d2 > rsum * rsum) { double ang = acos((A.r - B.r) / sqrt(d2)); a[cnt] = A.point(base + ang); b[cnt] = B.point(pi + base + ang); cnt++; a[cnt] = A.point(base - ang); b[cnt] = B.point(pi + base - ang); cnt++; } return cnt; } Circle CircumscribedCircle(Point p1, Point p2, Point p3) { double Bx = p2.x - p1.x, By = p2.y - p1.y; double Cx = p3.x - p1.x, Cy = p3.y - p1.y; double D = 2 * (Bx * Cy - By * Cx); double cx = (Cy * (Bx * Bx + By * By) - By * (Cx * Cx + Cy * Cy)) / D + p1.x; double cy = (Bx * (Cx * Cx + Cy * Cy) - Cx * (Bx * Bx + By * By)) / D + p1.y; Point p = Point(cx, cy); return Circle(p, Length(p1 - p)); } Circle InscribedCircle(Point p1, Point p2, Point p3) { double a = Length(p2 - p3); double b = Length(p3 - p1); double c = Length(p1 - p2); Point p = (p1 * a + p2 * b + p3 * c) / (a + b + c); return Circle(p, DistanceToLine(p, p1, p2)); } double RtoDegree(double x) { return x / pi * 180.; } double DegreetoR(double degree) { return degree / 180. * pi; } int ConvexHull(Point* p, int n, Point* ch) { sort(p, p + n); int m = 0; for (int i = 0; i < n; i++) { while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } int k = m; for (int i = n - 2; i >= 0; i--) { while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0) m--; ch[m++] = p[i]; } if (n > 1) m--; return m; } int kase; int main() { Point A, B; scanf("%lf %lf", &A.x, &A.y); scanf("%lf %lf", &B.x, &B.y); if (A.x > B.x) { Point t = A; A = B; B = t; } int n, ans = 0; Line L(A, B); scanf("%d", &n); for (int i = 0; i < n; i++) { double a, b, c; scanf("%lf%lf%lf", &a, &b, &c); if ((a * A.x + b * A.y + c) * (a * B.x + b * B.y + c) < 0) ans++; } printf("%d\n", 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> const int N = 310; const double eps = 1e-12; using namespace std; int sgn(double x) { if (fabs(x) < eps) return 0; if (x > 0) return 1; else return -1; } int ans = 0; struct Point { double x, y; Point() {} Point(double _x, double _y) { x = _x, y = _y; } bool operator<(Point b) const { return (sgn(x - b.x) == 0 ? sgn(y - b.y) < 0 : x < b.x); } Point operator-(const Point &b) const { return Point(x - b.x, y - b.y); } double operator^(const Point &b) const { return x * b.y - y * b.x; } double operator*(const Point &b) const { return x * b.x + y * b.y; } }; map<Point, int> H; struct Line { Point s, e; Line() {} Line(Point x, Point y) { s = x, e = y; } int check(double x, double y, double z) { double temp; if (sgn(s.x - e.x) == 0) temp = -(x * s.x + z) / y; else temp = -(y * s.y + z) / x; if (temp >= -1e6 && temp <= 1e6) return 1; return 0; } Point cal(double x, double y, double z) { if (sgn(s.x - e.x) == 0) return Point(e.x, -(x * s.x + z) / y); else return Point(-(y * s.y + z) / x, e.y); } Point crosspoint(Line v) { double a1 = (v.e - v.s) ^ (s - v.s); double a2 = (v.e - v.s) ^ (e - v.s); return Point((s.x * a2 - e.x * a1) / (a2 - a1), (s.y * a2 - e.y * a1) / (a2 - a1)); } int nei(Point x) { if (sgn((x - s) * (x - e)) <= 0) return 1; return 0; } void linetoline(Line t) { if (sgn((s - e) ^ (t.s - t.e)) == 0) return; Point temp = crosspoint(t); if (nei(temp) && t.nei(temp)) ans++; } }; int main() { Point a, b; scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y); Line l1 = Line(a, b); int n; scanf("%d", &n); Line t1 = Line(Point(-1e6, 1e6), Point(1e6, 1e6)), t2 = Line(Point(-1e6, -1e6), Point(-1e6, 1e6)); Line t3 = Line(Point(1e6, -1e6), Point(1e6, 1e6)), t4 = Line(Point(1e6, -1e6), Point(-1e6, -1e6)); H.clear(); for (int i = 0; i < (n); i++) { double x, y, z; scanf("%lf%lf%lf", &x, &y, &z); Line l2; if (sgn(x) == 0) { z = -z / y; l2 = Line(Point(-1e6, z), Point(1e6, z)); } else { if (sgn(y) == 0) { z = -z / x; l2 = Line(Point(z, -1e6), Point(z, 1e6)); } else { vector<Point> v; v.clear(); if (t1.check(x, y, z)) v.push_back(t1.cal(x, y, z)); if (t2.check(x, y, z)) v.push_back(t2.cal(x, y, z)); if (t3.check(x, y, z)) v.push_back(t3.cal(x, y, z)); if (t4.check(x, y, z)) v.push_back(t4.cal(x, y, z)); sort(v.begin(), v.end()); int j; for (j = 1; j < v.size(); j++) if (v[j] < v[0] || v[0] < v[j]) break; l2 = Line(v[0], v[j]); } } l1.linetoline(l2); } 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; public class Main implements Runnable { private void solve(QuickScanner in, PrintWriter out) { double x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt(); int n = in.nextInt(); int ans = 0; for (int i = 0; i < n; i++) { int a = in.nextInt(), b = in.nextInt(), c = in.nextInt(); // ax + by + c = 0 double x3 = 0, y3 = 0, x4 = 0, y4 = 0; if (b == 0) { x3 = x4 = -1.0 * c / a; y3 = 0; y4 = 1; } else { x3 = 0; x4 = 1; y3 = -1.0 * c / b; y4 = -1.0 * (c + a) / b; } if (test(x1, y1, x3, y3, x4, y4) * test(x2, y2, x3, y3, x4, y4) < 0) { ans++; } } out.println(ans); } private int test(double x1, double y1, double x2, double y2, double x3, double y3) { x3 -= x2; y3 -= y2; x1 -= x2; y1 -= y2; return signum(x1 * y3 - x3 * y1); } private static final double EPS = 1E-8; private static int signum(double val) { if (Math.abs(val) < EPS) { return 0; } return val > 0 ? 1 : -1; } private class QuickScanner { private BufferedReader bufferedReader = null; private StringTokenizer stringTokenizer = null; private String nextHolder = null; QuickScanner() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } String next() { // If called hasNext before, should return string in nextHolder. if (nextHolder != null) { String next = nextHolder; nextHolder = null; return next; } try { while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { String newLine = bufferedReader.readLine(); if (newLine != null) { stringTokenizer = new StringTokenizer(newLine); } else { return null; } } return stringTokenizer.nextToken(); } catch (IOException e) { e.printStackTrace(); return null; } } int nextInt() { return Integer.parseInt(next()); } } // Tedious code. public Main() {} @Override public void run() { QuickScanner in = new QuickScanner(); PrintWriter out = new PrintWriter(System.out); try { solve(in, out); } catch (Exception e) { e.printStackTrace(); } finally { out.flush(); } } 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.IOException; import java.util.Arrays; import java.util.LinkedList; import java.util.Map; import java.util.Queue; import java.util.Scanner; import java.util.TreeMap; import java.util.TreeSet; public class Main { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); Point p1 = new Point(sc.nextInt(), sc.nextInt()); Point p2 = new Point(sc.nextInt(), sc.nextInt()); int n = sc.nextInt(); int cnt = 0; while (n -- > 0) { int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); Point p3 ; Point p4 ; if (a == 0) { p3 = new Point (0 , -c / (double)b); p4 = new Point (1 , (-c - a) / (double)b); } else { p3 = new Point (-c / (double) a , 0 ); p4 = new Point ( (-c - b) / (double)a , 1); } if (ccw(p3, p4, p1) != ccw(p3, p4, p2)) ++ cnt; } System.out.println(cnt); } static class Point { double x ; double y ; Point (double xx , double yy){ x = xx; y = yy; } } static boolean ccw(Point p, Point q, Point r) { return new Vector(p, q).cross(new Vector(p, r)) > 0; } static 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); } double cross(Vector v) { return x * v.y - y * v.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
import java.io.*; import java.math.*; import java.util.*; /** * * @author Saju * */ public class Main { private static int dx[] = { 1, 0, -1, 0 }; private static int dy[] = { 0, -1, 0, 1 }; private static final long INF = (long) Math.pow(10, 16); private static final int INT_INF = Integer.MAX_VALUE; private static final long NEG_INF = Long.MIN_VALUE; private static final int NEG_INT_INF = Integer.MIN_VALUE; private static final double EPSILON = 1e-10; private static final long MAX = (long) 1e12; private static final long MOD = 1000000007; private static final int MAXN = 300005; private static final int MAXA = 1000007; private static final int MAXLOG = 22; private static final double PI = Math.acos(-1); public static void main(String[] args) throws IOException { InputReader in = new InputReader(System.in); // Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); // InputReader in = new InputReader(new FileInputStream("src/test.in")); // PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("src/test.out"))); /* */ double x1 = in.nextDouble(); double y1 = in.nextDouble(); double x2 = in.nextDouble(); double y2 = in.nextDouble(); int cnt = 0; int n = in.nextInt(); for(int i = 0; i < n; i++) { double a = in.nextDouble(); double b = in.nextDouble(); double c = in.nextDouble(); double val = (a * x1) + (b * y1) + c; double val1 = (a * x2) + (b * y2) + c; if((val > 0 && val1 < 0) || (val < 0 && val1 > 0)) { cnt++; } } out.println(cnt); in.close(); out.flush(); out.close(); System.exit(0); } /* * return the number of elements in list that are less than or equal to the val */ private static long upperBound(List<Long> list, long val) { int start = 0; int len = list.size(); int end = len - 1; int mid = 0; while (true) { if (start > end) { break; } mid = (start + end) / 2; long v = list.get(mid); if (v == val) { start = mid; while(start < end) { mid = (start + end) / 2; if(list.get(mid) == val) { if(mid + 1 < len && list.get(mid + 1) == val) { start = mid + 1; } else { return mid + 1; } } else { end = mid - 1; } } return start + 1; } if (v > val) { end = mid - 1; } else { start = mid + 1; } } if (list.get(mid) < val) { return mid + 1; } return mid; } private static boolean isPalindrome(String str) { StringBuilder sb = new StringBuilder(); sb.append(str); String str1 = sb.reverse().toString(); return str.equals(str1); } private static String getBinaryStr(long n, int j) { String str = Long.toBinaryString(n); int k = str.length(); for (int i = 1; i <= j - k; i++) { str = "0" + str; } return str; } private static long modInverse(long r) { return bigMod(r, MOD - 2, MOD); } private static long bigMod(long n, long k, long m) { long ans = 1; while (k > 0) { if ((k & 1) == 1) { ans = (ans * n) % m; } n = (n * n) % m; k >>= 1; } return ans; } private static long ceil(long n, long x) { long div = n / x; if(div * x != n) { div++; } return div; } private static int ceil(int n, int x) { int div = n / x; if(div * x != n) { div++; } return div; } private static int abs(int x) { if (x < 0) { return -x; } return x; } private static double abs(double x) { if (x < 0) { return -x; } return x; } private static long abs(long x) { if(x < 0) { return -x; } return x; } private static long lcm(long a, long b) { return (a * b) / gcd(a, b); } private static int lcm(int a, int b) { return (a * b) / gcd(a, b); } private static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } private static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } private static int log(long x, int base) { return (int) (Math.log(x) / Math.log(base)); } private static int log(long x, long base) { return (int) (Math.log(x) / Math.log(base)); } private static long min(long a, long b) { if (a < b) { return a; } return b; } private static int min(int a, int b) { if (a < b) { return a; } return b; } private static long max(long a, long b) { if (a < b) { return b; } return a; } private static int max(int a, int b) { if (a < b) { return b; } return a; } private static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException e) { return null; } return tokenizer.nextToken(); } public String nextLine() { String line = null; try { tokenizer = null; line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public boolean hasNext() { try { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (Exception e) { return false; } return true; } public int[] nextIntArr(int n) { int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArr(int n) { long arr[] = new long[n]; for(int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public int[] nextIntArr1(int n) { int arr[] = new int[n + 1]; for(int i = 1; i <= n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArr1(int n) { long arr[] = new long[n + 1]; for(int i = 1; i <= n; i++) { arr[i] = nextLong(); } return arr; } public void close() { try { if(reader != null) { reader.close(); } } catch(Exception e) { } } } }
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 CrazyTown { public static void main (String[] args) throws IOException{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter o=new PrintWriter(System.out); StringTokenizer st = new StringTokenizer(in.readLine()); long x1 = Long.parseLong(st.nextToken()); long y1 = Long.parseLong(st.nextToken()); st = new StringTokenizer(in.readLine()); long x2 = Long.parseLong(st.nextToken()); long y2 = Long.parseLong(st.nextToken()); int n = Integer.parseInt(in.readLine()); int count=0; for(int i=0;i<n;i++){ st = new StringTokenizer(in.readLine()); int a = Integer.parseInt(st.nextToken()); int b = Integer.parseInt(st.nextToken()); int c = Integer.parseInt(st.nextToken()); long pos1 = a*x1+b*y1+c; long pos2 = a*x2+b*y2+c; if((pos1 > 0 && pos2 < 0) || (pos1 < 0 && pos2 > 0)){ count++; } } o.println(count); o.flush(); o.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() { long long xh, yh, xu, yu, at, bt, ct, ans = 0, n; cin >> xh >> yh >> xu >> yu >> n; while (n--) { cin >> at >> bt >> ct; ans += (at * xh + bt * yh < -ct) ^ (at * xu + bt * yu < -ct); } 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
import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.io.*; import java.math.*; import java.text.*; import java.util.*; import java.util.regex.*; /* br = new BufferedReader(new FileReader("input.txt")); pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); */ public class Main { private static BufferedReader br; private static StringTokenizer st; private static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //int qq = 1; int qq = Integer.MAX_VALUE; //int qq = readInt(); for(int casenum = 1; casenum <= qq; casenum++) { long ax = readInt(); long ay = readInt(); long bx = readInt(); long by = readInt(); int n = readInt(); int ret = 0; while(n-- > 0) { long a = readLong(); long b = readLong(); long c = readLong(); if((a*ax+b*ay < -c) != (a*bx+b*by < -c)) { ret++; } } pw.println(ret); } exitImmediately(); } private static void exitImmediately() { pw.close(); System.exit(0); } private static long readLong() throws IOException { return Long.parseLong(nextToken()); } private static double readDouble() throws IOException { return Double.parseDouble(nextToken()); } private static int readInt() throws IOException { return Integer.parseInt(nextToken()); } private static String nextLine() throws IOException { if(!br.ready()) { exitImmediately(); } st = null; return br.readLine(); } private static String nextToken() throws IOException { while(st == null || !st.hasMoreTokens()) { if(!br.ready()) { exitImmediately(); } st = new StringTokenizer(br.readLine().trim()); } return st.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() { long long line_a; long long line_b; long long line_c; int nlines; int cross = 0; long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2 >> nlines; for (int i = 0; i < nlines; i++) { cin >> line_a >> line_b >> line_c; if ((line_a * x1 + line_b * y1 + line_c) < 0 != (line_a * x2 + line_b * y2 + line_c) < 0) cross++; } cout << cross; }
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
xh, yh = map(int, raw_input().split()) xu, yu = map(int, raw_input().split()) def order(a, b, c, x, y): if a*x + b*y + c > 0: return 1 else: return 0 n = input() lines = [map(int, raw_input().split()) for _ in range(n)] order_h = map(lambda l: order(l[0], l[1], l[2], xh, yh), lines) order_u = map(lambda l: order(l[0], l[1], l[2], xu, yu), lines) diff = map(lambda i, j: (1 if i != j else 0), order_h, order_u) print sum(diff)
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
hx,hy = map(int, raw_input().strip().split()) ux, uy = map(int, raw_input().strip().split()) ans = 0 n = input() for i in range(n): a,b,c = map(int, raw_input().strip().split()) x = a*hx + b*hy + c y = a*ux + b*uy + c if abs(x) + abs(y) != abs(x+y): 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 x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; int cnt = 0; while (n--) { long long int a, b, c; cin >> a >> b >> c; long long int s1 = ((a * x1 + b * y1 + c) > 0); long long int s2 = ((a * x2 + b * y2 + c) > 0); if ((s1 + s2) == 1) cnt++; } cout << cnt << 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
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package javaapplication33; import java.awt.Point; import java.io.IOException; import java.util.Scanner; /** * * @author HIMANSHU */ public class main34 { static class data{ double a,b,c; data(double a, double b, double c){ this.a = a; this.b = b; this.c = c; } } static class Hold{ data x; public Hold(data x){ this.x = x; } } static class Point{ double x; double y; Point(double x, double y){ this.x = x; this.y = y;} } static Point p1; static Point p2; static int n; static Hold[] v; static Scanner s=new Scanner(System.in); public static void main(String args[]) throws IOException { double x1,y1,x2,y2; x1 = s.nextDouble(); y1 = s.nextDouble(); x2 = s.nextDouble(); y2 = s.nextDouble(); p1 = new Point(x1,y1); p2 = new Point(x2,y2); n = s.nextInt(); v = new Hold[n]; for(int i=0;i<n;++i){ double a,b,c; a = s.nextDouble(); b = s.nextDouble(); c = s.nextDouble(); data ll = new data(0,0,0); ll.a = a; ll.b = b; ll.c = c; v[i] = new Hold(ll); } int ans = 0; for(int i=0;i<n;++i){ int s1,s2; s1 = s2 = 0; double r1 = v[i].x.a*p1.x + v[i].x.b*p1.y+v[i].x.c; double r2 = v[i].x.a*p2.x + v[i].x.b*p2.y+v[i].x.c; s1 = (r1<0.) ? 1:0; s2 = (r2<0.) ? 1:0; ans += (s1+s2==1) ? 1:0; } System.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
a , b = map(int, input("").split(" ")) c , d = map(int, input("").split(" ")) dumb = int(input("")) lines = [] for value in range(dumb): lines.append(input("").split(" ")) count = 0 check1 = 0 check2 = 0 for value in lines: if (int(value[0])*a + int(value[1])*b+int(value[2])>= 0): check1 = 1 else: check1 = -1 if(int(value[0])*c+int(value[1])*d+int(value[2])>=0): check2 = 1 else: check2 = -1 if check2 != check1: count = 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
#include <bits/stdc++.h> using namespace std; long long int a, b, c, d; long long int sgn(long long int k) { if (k > 0) return 1; if (k < 0) return -1; return 0; } void sol() { scanf("%I64d %I64d", &a, &b); scanf("%I64d %I64d", &c, &d); int n; scanf("%d", &n); int ans = 0; for (int i = 0; i < n; i++) { long long int x, y, z; scanf("%I64d %I64d %I64d", &x, &y, &z); if (sgn(x * a + y * b + z) * sgn(x * c + y * d + z) < 0) ans++; } printf("%d\n", ans); } int main() { while (1) { sol(); break; } 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
x, y = map(int, raw_input().split()) X, Y = map(int, raw_input().split()) ans = 0 n = int(raw_input()) for _ in xrange(n): a, b, c = map(int, raw_input().split()) if (a * x + b * y + c) * (a * X + b * Y + c) < 0: 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
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CrazyTown { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); Point p = new Point(sc.nextDouble(), sc.nextDouble()); Point q = new Point(sc.nextDouble(), sc.nextDouble()); LineSegment l = new LineSegment(p, q); int n = sc.nextInt(); long ans = 0; for (int i = 0; i < n; i++) { Line t = new Line(sc.nextDouble(), sc.nextDouble(), sc.nextDouble()); if (l.intersects(t) != null) ans++; } System.out.println(ans); } static class LineSegment { Point p, q; static final double EPS = 1e-9; public LineSegment(Point a, Point b) { p = a; q = b; } public Point intersects(Line l) { Line temp = new Line(p, q); if (temp.isParallel(l)) return null; Point c = temp.intersects(l); if ( ((c.x + EPS > p.x && c.x < q.x + EPS) || (c.x + EPS > q.x && c.x < p.x + EPS)) && ((c.y + EPS > p.y && c.y < q.y + EPS) || (c.y + EPS > q.y && c.y < p.y + EPS))) return c; else return null; } public Point lower() { return p.y < q.y ? p : q; } } static class Line { double a, b, c; static final double EPS = 1e-9; public Line(Point p, Point q) { if (Math.abs(p.x - q.x) < EPS) { a = 1.0; b = 0.0; c = -p.x; } else { a = (p.y - q.y)/(q.x - p.x); b = 1.0; c = -(a * p.x) - p.y; } } public Line(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } public boolean isParallel(Line l) { return Math.abs(a - l.a) < EPS && Math.abs(b - l.b) < EPS; } public boolean isSame(Line l) { return isParallel(l) && Math.abs(c - l.c) < EPS; } public Point intersects(Line l) { if (isParallel(l)) return null; double x = (l.b * c - l.c * b) / (b * l.a - a * l.b); double y; if (Math.abs(b) < EPS) y = (-l.c - l.a*x) / l.b; else y = (-c - a*x) / b; return new Point(x, y); } } static class Point { double x, y; static final double EPS = 1e-9; public Point(double a, double b) { x = a; y = b; } public double distance(Point p) { return Math.sqrt(sq(x - p.x) + sq(y - p.y)); } public double sq(double x) { return x * x; } public Point rotate(double theta) { // must be in rads. rotate around origin only. return new Point(x * Math.cos(theta) - y * Math.sin(theta), x * Math.sin(theta) + y * Math.cos(theta)); } @Override public String toString() { return "(" + x + ", " + y + ")"; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(FileReader f) { br = new BufferedReader(f); } public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean Ready() throws IOException { return br.ready(); } public void waitForInput(long time) { long ct = System.currentTimeMillis(); while(System.currentTimeMillis() - ct < time) {}; } } }
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.InputStream; import java.io.InputStreamReader; import java.io.BufferedReader; import java.math.BigInteger; 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 * @author shu_mj @ http://shu-mj.com */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); solver.solve(1, in, out); out.close(); } } class TaskA { Scanner in; PrintWriter out; public void solve(int testNumber, Scanner in, PrintWriter out) { this.in = in; this.out = out; run(); } void run() { int xa = in.nextInt(); int ya = in.nextInt(); int xb = in.nextInt(); int yb = in.nextInt(); int n = in.nextInt(); int res = 0; for (int i = 0; i < n; i++) { long a = in.nextInt(); long b = in.nextInt(); long c = in.nextInt(); if ((a * xa + b * ya + c > 0) ^ (a * xb + b * yb + c > 0)) res++; } out.println(res); } } class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); eat(""); } private void eat(String s) { st = new StringTokenizer(s); } public String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } public boolean hasNext() { while (!st.hasMoreTokens()) { String s = nextLine(); if (s == null) return false; eat(s); } return true; } public String next() { hasNext(); return st.nextToken(); } 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
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0 && a == 0) return 1; if (b == 0) return a; if (a == 0) return b; return gcd(b, a % b); } void reduce(long long &a, long long &b) { long long g = gcd(a, b); if (g == 0) return; a /= g; b /= g; } bool isless(long long p, long long q, long long x) { if (q > 0) return p <= (q * x); else return p >= (q * x); } bool isgreat(long long p, long long q, long long x) { if (q > 0) return p >= (q * x); return p <= (q * x); } int main() { long long h1, k1; cin >> h1 >> k1; long long h2, k2; cin >> h2 >> k2; int n; cin >> n; long long a, b, c; b = h1 - h2; a = k2 - k1; reduce(a, b); c = -(a * h1 + b * k1); int cnt = 0; if (h1 > h2) swap(h1, h2); if (k1 > k2) swap(k1, k2); while (n--) { long long a1, b1, c1; cin >> a1 >> b1 >> c1; long long p1 = (c1 * b - c * b1); long long p2 = (a1 * c - a * c1); long long q2 = (a * b1 - a1 * b); long long q1 = q2; reduce(p1, q1); reduce(p2, q2); if (q1 && isless(p1, q1, h2) && isgreat(p1, q1, h1) && isless(p2, q2, k2) && isgreat(p2, q2, k1)) { cnt++; } } cout << cnt; }
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 = 0x7fffffff; const int MINF = 0x80000000; const long long mod = 1000000007; const int cons = 100001; long long fn(long long a, long long b, long long c, long long x, long long y) { return a * x + b * y + c; } int main() { long long x[2], y[2]; int n; cin >> x[0] >> y[0] >> x[1] >> y[1]; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; long long fvl = fn(a, b, c, x[0], y[0]); long long fvl2 = fn(a, b, c, x[1], y[1]); fvl /= abs(fvl); fvl2 /= abs(fvl2); if (fvl * fvl2 < 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 main() { long long x1, x2, y1, y2; scanf("%lld%lld", &x1, &y1); scanf("%lld%lld", &x2, &y2); int t; int ans = 0; scanf("%d", &t); for (int i = 0; i < t; i++) { long long a, b, c; scanf("%lld%lld%lld", &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++; } 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
/** * ******* Created on 30/12/19 5:23 AM******* */ import java.io.*; import java.util.*; public class A498 implements Runnable { private static final int MAX = (int) (1E5 + 5); private static final int MOD = (int) (1E9 + 7); private static final long Inf = (long) (1E14 + 10); private void solve() throws IOException { int x1 = reader.nextInt(); int y1 = reader.nextInt(); int x2 = reader.nextInt(); int y2 = reader.nextInt(); double ma = y1 - y2; double mb = x2 - x1; double mc = ma*(x1) + mb*(y1); int a,b,c; int n = reader.nextInt(); int res = 0; double eps = 1e-9; for(int i =0;i<n;i++){ a = reader.nextInt(); b = reader.nextInt(); c = reader.nextInt(); c= -c; double delta = ma*b - mb*a; //writer.println(ma +" "+mb +" "+mc +" "+delta); double x = (b*mc - mb*c) / delta; double y = (ma*c - a*mc) / delta; res += (Math.min(x1,x2)<=x+eps && Math.max(x1,x2)+eps >=x && Math.min(y1, y2) <=y+eps && Math.max(y1, y2)+eps>= y)?1:0; } writer.println(res); } public static void main(String[] args) throws IOException { try (Input reader = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) { new A498().run(); } } StandardInput reader; PrintWriter writer; @Override public void run() { try { reader = new StandardInput(); writer = new PrintWriter(System.out); solve(); reader.close(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } interface Input extends Closeable { String next() throws IOException; default int nextInt() throws IOException { return Integer.parseInt(next()); } default long nextLong() throws IOException { return Long.parseLong(next()); } default double nextDouble() throws IOException { return Double.parseDouble(next()); } default int[] readIntArray() throws IOException { return readIntArray(nextInt()); } default int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i = 0; i < array.length; i++) { array[i] = nextInt(); } return array; } default long[] readLongArray(int size) throws IOException { long[] array = new long[size]; for (int i = 0; i < array.length; i++) { array[i] = nextLong(); } return array; } } private static class StandardInput implements Input { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer stringTokenizer; @Override public void close() throws IOException { reader.close(); } @Override public String next() throws IOException { if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.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
import java.io.*; import java.util.*; public class A { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; void solve() throws IOException { long x1 = nextLong(); long y1 = nextLong(); long x2 = nextLong(); long y2 = nextLong(); int n = nextInt(); int ret = 0; for (int i = 0; i < n; i++) { long a = nextLong(); long b = nextLong(); long c = nextLong(); if (Long.signum(a * x1 + b * y1 + c) != Long.signum(a * x2 + b * y2 + c)) { ret++; } } out.println(ret); } A() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new A(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(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
/** * Created by watson on 24.12.14. */ import java.util.*; import java.io.*; public class A { FastScanner in; PrintWriter out; public void solve() throws IOException { long x1 = in.nextInt(); long y1 = in.nextInt(); long x2 = in.nextInt(); long y2 = in.nextInt(); int n = in.nextInt(); long[]a = new long[n]; long[]b = new long[n]; long[]c = new long[n]; long ans = 0; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); b[i] = in.nextInt(); c[i] = in.nextInt(); if (isIntersect(x1, y1, x2, y2, a[i], b[i], c[i])) { ans++; } } System.out.println(ans); } public boolean isIntersect(long x1, long y1, long x2, long y2, long a, long b, long c) { boolean t1 = (a * x1 + b * y1 + c) > 0; boolean t2 = (a * x2 + b * y2 + c) > 0; return t1 != t2; } public void run() { try { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] arg) { new A().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.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CrazyTown { public static void main(String[]args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); Long x1=Long.parseLong(st.nextToken()); Long y1=Long.parseLong(st.nextToken()); st = new StringTokenizer(br.readLine()); Long x2=Long.parseLong(st.nextToken()); Long y2=Long.parseLong(st.nextToken()); int x=Integer.parseInt(br.readLine()); int y=0; for(int i=0;i<x;i++){ st = new StringTokenizer(br.readLine()); Long a = Long.parseLong(st.nextToken()); Long b = Long.parseLong(st.nextToken()); Long c = Long.parseLong(st.nextToken()); 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)){ y++; } } System.out.println(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; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; int ans = 0; int n; cin >> n; for (int i = 0; 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) ans++; else if (f > 0 && s < 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.awt.*; import java.awt.geom.Area; import java.io.*; import java.math.*; import java.util.*; import java.util.Map.Entry; import javax.management.RuntimeErrorException; import static java.lang.Math.*; public class Solution implements Runnable { final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); String delim = " "; String readString() throws IOException { try { while(!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(delim); } catch (Exception e){ return null; } } int readInt() throws IOException{ return Integer.parseInt(readString()); } long readLong() throws IOException{ return Long.parseLong(readString()); } double readDouble() throws IOException{ return Double.parseDouble(readString()); } Point readPoint() throws IOException { return new Point(readInt(), readInt()); } char readChar() throws IOException{ int x = 0; while ((x = in.read()) == '\n' || x == '\r' || x == ' '){ } return (char)x; } //---------------------------------- public class Pair<T1,T2> { public T1 x1; public T2 x2; public Pair(){} public Pair(T1 x1, T2 x2){ this.x1 = x1; this.x2 = x2; } public String toString(){ return x1 + ":" + x2; } } public class Three implements Comparable<Three> { public long a, b, c; public Three(){} public Three(long a, long b, long c){this.a = a;this.b=b;this.c=c;} public String toString(){ return a + ":" + b + ":" + c; } @Override public int compareTo(Three arg0) { return 1; } } //---------------------------------------- TreeMap<String, String> tm = new TreeMap<String, String>(); void solve() throws NumberFormatException, IOException { int x1 = readInt(); int y1 = readInt(); int x2 = readInt(); int y2 = readInt(); int n = readInt(); long A = y1 - y2; long B = x2 - x1; long C = -A*x1 - B*y1; Three own = new Three(A, B, C); int ans = 0; for (int i = 0; i < n; i++){ int x = readInt(); int y = readInt(); int z = readInt(); Three aa = new Three(x, y, z); if (isInter(own, aa, min(x1, x2), max(x1, x2), min(y1, y2), max(y1, y2))){ ans++; } } out.println(ans); } boolean isInter(Three k1, Three k2, long x1, long x2, long y1, long y2){ if (k1.a*k2.b == k2.a*k1.b) return false; double x = - (k1.c*k2.b - k2.c*k1.b)*1.0/(k1.a*k2.b - k2.a*k1.b); double y = - (k1.a*k2.c - k2.a*k1.c)*1.0/(k1.a*k2.b - k2.a*k1.b); return x1 <= x && x <= x2 && y1 <= y && y <= y2; } //---------------------------------------- public static void main(String[] args){ new Thread(null, new Solution(), "", 256 * (1L << 20)).start(); } public void run(){ try{ timeBegin = System.currentTimeMillis(); try{ if (ONLINE_JUDGE){ 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"); } }catch(Throwable e){ in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } solve(); out.close(); timeEnd = System.currentTimeMillis(); System.err.println("Time = " + (timeEnd - timeBegin)); }catch (Exception e){ e.printStackTrace(System.err); System.exit(-1); } } long timeBegin, timeEnd; }
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 sign(long long x) { return x < 0 ? -1 : 1; } int main() { ios::sync_with_stdio(0); long long x1, y1, x2, y2, n; cin >> x1 >> y1 >> x2 >> y2 >> n; int res = 0; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; if (sign(a * x1 + b * y1 + c) != sign(a * x2 + b * y2 + c)) res++; } cout << res << '\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 x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; int r = 0; for (int i = 0; i < n; i++) { long long a, b, c; cin >> a >> b >> c; long long p = a * x1 + b * y1 + c; long long q = a * x2 + b * y2 + c; if (p > 0 && q < 0 || p < 0 && q > 0) ++r; } cout << r << 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.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProbC { public static void main(String args[]) { InputStream inputStream = System.in; OutputStream outputStream = System.out; SolverC.InputReader in = new SolverC.InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); SolverC solver = new SolverC(); int testCount = 1; for (int i = 1; i <= testCount; i++) { solver.solve(in, out); } out.close(); } } class SolverC { public void solve(InputReader in, PrintWriter out) { long x1 = in.nextlong(); long y1 = in.nextlong(); long x2 = in.nextlong(); long y2 = in.nextlong(); long n = in.nextlong(); int ct = 0; for (long i=0; i<n; i++) { long a = in.nextlong(); long b = in.nextlong(); long c = in.nextlong(); long v1 = a * x1 + b * y1 + c; long v2 = a * x2 + b * y2 + c; if (sign(v1) * sign(v2) == -1) { ct++; } } out.println(ct); } private int sign(final long v2) { return (int) (v2 / Math.abs(v2)); } static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } 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()); } } }
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
x, y = map(int, input().split()) u, v = map(int, input().split()) s = 0 for i in range(int(input())): 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
#include <bits/stdc++.h> using namespace std; void doin() { cin.tie(); cout.tie(); ios::sync_with_stdio(0); } float X, Y, X1, Y1, a, b, c, xt, xx; int ans = 0, n; int main() { doin(); cin >> X >> Y >> X1 >> Y1; cin >> n; while (n--) { cin >> a >> b >> c; ans += ((a * X + b * Y + c < 0) ^ (a * X1 + b * Y1 + c < 0)); } 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CrazyTown { static final double EPS = 1e-9; static final double oo = 1e9; static class Point implements Comparable<Point> { // Global Variables static final double EPS = 1e-9; static final double oo = 1e9; double x, y; public Point() { x = 0.0; y = 0.0; } public Point(double xx, double yy) { x = xx; y = yy; } public double dist(Point p) { return hyp(x - p.x, y - p.y); } public double hyp(double dx, double dy) { return Math.sqrt(dx * dx + dy * dy); } public boolean between(Point p, Point q) { return x < Math.max(p.x, q.x) + EPS && x + EPS > Math.min(p.x, q.x) && y < Math.max(p.y, q.y) + EPS && y + EPS > Math.min(p.y, q.y); } public boolean higher(Point p) { return y > p.y; } public boolean toLeft(Point p) { return x < p.x; } @Override public int compareTo(Point p) { if (Math.abs(x - p.x) > EPS) return Double.compare(x, p.x); if (Math.abs(y - p.y) > EPS) return Double.compare(y, p.y); return 0; } public boolean equals(Object o) { Point p = (Point) o; return this.compareTo(p) == 0; } public String toString() { return x + " " + y; } } static class Line { double a, b, c; public Line(double aa, double bb, double cc) { a = aa; b = bb; c = cc; } public Line(Point p, Point q) { if (Math.abs(p.x - q.x) < EPS) { a = 1.0; b = 0.0; c = -p.x; } else { a = -(p.y - q.y) / (p.x - q.x); b = 1.0; c = -((p.x * a) + p.y); } } public Line(Point p, double m) { a = -m; b = 1; c = -(a * p.x + p.y); } public Point intersect(Line l) { if (AreParallel(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); } public 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 double closestDistance(Point p) { return p.dist(closestPoint(p)); } public boolean AreParallel(Line l) { return Math.abs(a - l.a) < EPS && Math.abs(b - l.b) < EPS; } public boolean SameLine(Line l) { return AreParallel(l) && Math.abs(c - l.c) < EPS; } } public static void main(String[] args) throws IOException { MyScanner sc = new MyScanner(System.in); Point home = new Point(sc.nextInt(), sc.nextInt()); Point uni = new Point(sc.nextInt(), sc.nextInt()); if (home.equals(uni)) { System.out.println(0); return; } int n = sc.nextInt(); int count = 0; for (int i = 0; i < n; i++) { double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble(); double f = a * home.x + b * home.y + c; double s = a * uni.x + b * uni.y + c; if (Math.signum(f) != Math.signum(s)) count++; } System.out.println(count); } static class MyScanner { StringTokenizer st; BufferedReader br; public MyScanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 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 { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
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, ans = 0, r1, r2; cin >> x1 >> y1 >> x2 >> y2; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b >> c; r1 = (a * x1) + (b * y1) + c; r2 = (a * x2) + (b * y2) + c; if ((r1 > 0 and r2 < 0) or (r1 < 0 and r2 > 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.Scanner; import java.util.*; public class contest1 { public static void main(String[] args) { // TODO Auto-generated method stub Scanner s=new Scanner(System.in); int mx=s.nextInt(); int my=s.nextInt(); int rx=s.nextInt(); int ry=s.nextInt(); int lines=s.nextInt(); long[][] p=new long[lines][3]; for(int i=0;i<lines;i++){ p[i][0]=s.nextLong(); p[i][1]=s.nextLong(); p[i][2]=s.nextLong(); } int out=0; for(int i=0;i<lines;i++){ int x=checkSign(p,i,mx,my); int y=checkSign(p,i,rx,ry); if(x!=y) out+=1; } System.out.println(out); } public static int checkSign(long[][] p, int i, int x, int y){ long a=p[i][0]*x+p[i][1]*y+p[i][2]; if(a>0) return 1; else return 0; } }
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
#!/usr/bin/env python3 import collections, itertools, functools, math def value(a, b, c, x, y): return a*x + b*y + c def sign(x): if x < 0: return -1 if x == 0: return 0 if x > 0: return 1 def solve(): x1, y1 = map(int, input().split()) x2, y2 = map(int, input().split()) n = int(input()) r = 0; for i in range(n): a, b, c = map(int, input().split()) s1 = value(a, b, c, x1, y1) s2 = value(a, b, c, x2, y2) if sign(s1) != sign(s2): r += 1 return r if __name__ == '__main__': print(solve())
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; vector<bool> sieve(long long n) { vector<bool> prime(n + 1, true); prime[0] = prime[1] = false; for (long long i = 2; i <= n; ++i) { if (prime[i] && (i * i) <= n) for (long long j = i * i; j <= n; j += i) prime[j] = false; } return prime; } long long power(long long a, long long b, long long m = 1000000007) { a %= m; long long res = 1; while (b > 0) { if (b & 1) res = res * a % m; a = a * a % m; b >>= 1; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; long long n; cin >> n; long long res = 0; for (long long i = 0; i < (long long)n; ++i) { long long a, b, c; cin >> a >> b >> c; long long val1 = (a * x1 + b * y1 + c) > 0 ? 1 : 0; long long val2 = (a * x2 + b * y2 + c) > 0 ? 1 : 0; if (val1 != val2) res++; } cout << res << '\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
import java.util.Arrays; import java.util.Scanner; public class Main { MyScanner sc = new MyScanner(); Scanner sc2 = new Scanner(System.in); long start = System.currentTimeMillis(); long fin = System.currentTimeMillis(); final int MOD = 1000000007; int[] dx = { 1, 0, 0, -1 }; int[] dy = { 0, 1, -1, 0 }; final int INF = 10000000; void run() { Point from = new Point(sc.nextDouble(), sc.nextDouble()); Point to = new Point(sc.nextDouble(), sc.nextDouble()); int n = sc.nextInt(); int cnt = 0; for (int i = 0; i < n; i++) { double a = sc.nextDouble(); double b = sc.nextDouble(); double c = sc.nextDouble(); Point p1 = new Point(0, 0); Point p2 = new Point(0, 0); if (a == 0) { // y = -c p1.x = -INF; p1.y = -c / b; p2.x = INF; p2.y = -c / b; } else if (b == 0) { // x = -c p1.x = -c / a; p1.y = -INF; p2.x = -c / a; p2.y = INF; } else { // y = -ax/b - c/b p1.x = INF; p1.y = -a * INF / b - c / b; p2.x = -INF; p2.y = a * INF / b - c / b; } if (lineCross(from, to, p1, p2)) cnt++; } System.out.println(cnt); } double EPS = 1e-8; class Point implements Comparable<Point> { double x; double y; public Point(double x, double y) { super(); this.x = x; this.y = y; } @Override public int compareTo(Point arg0) { if (this.x == arg0.x) return this.y - arg0.y > 0 ? 1 : -1; if (this.x - arg0.x > 0) return 1; else return -1; } } // 線分p1p2と線分p3p4が交差しているかを判定 true->交差(=含みで接する含む) false->交差せず boolean lineCross(Point p1, Point p2, Point p3, Point p4) { double a = (p1.x - p2.x) * (p3.y - p1.y) + (p1.y - p2.y) * (p1.x - p3.x); double b = (p1.x - p2.x) * (p4.y - p1.y) + (p1.y - p2.y) * (p1.x - p4.x); double c = (p3.x - p4.x) * (p1.y - p3.y) + (p3.y - p4.y) * (p3.x - p1.x); double d = (p3.x - p4.x) * (p2.y - p3.y) + (p3.y - p4.y) * (p3.x - p2.x); return a * b <= 0 && c * d <= 0; } public static void main(String[] args) { new Main().run(); } void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } void debug2(int[][] array) { for (int i = 0; i < array.length; i++) { for (int j = 0; j < array[i].length; j++) { System.out.print(array[i][j]); } System.out.println(); } } boolean inner(int h, int w, int limH, int limW) { return 0 <= h && h < limH && 0 <= w && w < limW; } void swap(int[] x, int a, int b) { int tmp = x[a]; x[a] = x[b]; x[b] = tmp; } // find minimum i (a[i] >= border) int lower_bound(int a[], int border) { int l = -1; int r = a.length; while (r - l > 1) { int mid = (l + r) / 2; if (border <= a[mid]) { r = mid; } else { l = mid; } } // r = l + 1 return r; } boolean palindrome(String s) { for (int i = 0; i < s.length() / 2; i++) { if (s.charAt(i) != s.charAt(s.length() - 1 - i)) { return false; } } return true; } class MyScanner { int nextInt() { try { int c = System.in.read(); while (c != '-' && (c < '0' || '9' < c)) c = System.in.read(); if (c == '-') return -nextInt(); int res = 0; do { res *= 10; res += c - '0'; c = System.in.read(); } while ('0' <= c && c <= '9'); return res; } catch (Exception e) { return -1; } } double nextDouble() { return Double.parseDouble(next()); } long nextLong() { return Long.parseLong(next()); } String next() { try { StringBuilder res = new StringBuilder(""); int c = System.in.read(); while (Character.isWhitespace(c)) c = System.in.read(); do { res.append((char) c); } while (!Character.isWhitespace(c = System.in.read())); return res.toString(); } catch (Exception e) { return null; } } int[] nextIntArray(int n) { int[] in = new int[n]; for (int i = 0; i < n; i++) { in[i] = nextInt(); } return in; } int[][] nextInt2dArray(int n, int m) { int[][] in = new int[n][m]; for (int i = 0; i < n; i++) { in[i] = nextIntArray(m); } return in; } double[] nextDoubleArray(int n) { double[] in = new double[n]; for (int i = 0; i < n; i++) { in[i] = nextDouble(); } return in; } long[] nextLongArray(int n) { long[] in = new long[n]; for (int i = 0; i < n; i++) { in[i] = nextLong(); } return in; } char[][] nextCharField(int n, int m) { char[][] in = new char[n][m]; for (int i = 0; i < n; i++) { String s = sc.next(); for (int j = 0; j < m; j++) { in[i][j] = s.charAt(j); } } return in; } } }
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> #pragma comment(linker, "/STACK:256000000") using namespace std; const long double PI = 3.14159265358979323846; const long double gammama = 0.57721566490153286060; const long double eps = 1e-5; const int INF = 1000 * 1000 * 1000 + 1; const long long LINF = (long long)1000 * 1000 * 1000 * 1000 * 1000 * 1000; const long long mod = 1000 * 1000 * 1000 + 7; const long long N = 1001; void solve() { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; int res = 0; for (int i = 0; i < n; ++i) { long long a, b, c; cin >> a >> b >> c; long long z1 = a * x1 + b * y1 + c; long long z2 = a * x2 + b * y2 + c; if ((z1 > 0) && (z2 < 0)) ++res; if ((z1 < 0) && (z2 > 0)) ++res; } cout << res << endl; } 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> template <class T> inline T gcd(T x, T y) { if (!y) return x; return gcd(y, x % y); } template <class T> inline void read(T& x) { bool fu = 0; char c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == '-') fu = 1, c = getchar(); for (x = 0; c > 32; c = getchar()) x = x * 10 + c - '0'; if (fu) x = -x; }; template <class T> inline void read(T& x, T& y) { read(x); read(y); } template <class T> inline void read(T& x, T& y, T& z) { read(x); read(y); read(z); } using namespace std; int main() { long long x1, x2, y1, y2, a, b, c, x, y; int n; read(x1, y1); read(x2, y2); read(n); long long ans = 0; for (int i = 0; i < n; ++i) { read(a, b, c); x = a * x1 + b * y1 + c; y = a * x2 + b * y2 + c; if (x < 0 and y > 0) ans++; else if (x > 0 and y < 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Codeforces499C { public static double crossProduct(double x1, double y1, double a, double b, double c) { return a*x1 + b*y1 + c; } public static void main(String[] args) { try { BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(f.readLine()); double x1 = Double.parseDouble(st.nextToken()); double y1 = Double.parseDouble(st.nextToken()); st = new StringTokenizer(f.readLine()); double x2 = Double.parseDouble(st.nextToken()); double y2 = Double.parseDouble(st.nextToken()); int n = Integer.parseInt(f.readLine()); int crossings = 0; for(int i = 0; i < n; i++) { st = new StringTokenizer(f.readLine()); double a = Double.parseDouble(st.nextToken()); double b = Double.parseDouble(st.nextToken()); double c = Double.parseDouble(st.nextToken()); double crossHome = crossProduct(x1, y1, a, b, c); double crossDestination = crossProduct(x2, y2, a, b, c); if((crossHome > 0 && crossDestination < 0) || (crossHome < 0 && crossDestination > 0)) crossings++; } System.out.println(crossings); } catch(IOException e) { System.out.println(e); } } }
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> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; template <class T> inline bool read(T &n) { T x = 0, tmp = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if (c == EOF) return false; if (c == '-') c = getchar(), tmp = -1; while (c >= '0' && c <= '9') x *= 10, x += (c - '0'), c = getchar(); n = x * tmp; return true; } template <class T> inline void write(T n) { if (n < 0) { putchar('-'); n = -n; } int len = 0, data[20]; while (n) { data[len++] = n % 10; n /= 10; } if (!len) data[len++] = 0; while (len--) putchar(data[len] + 48); } int main() { long long x, y, a, b, c; long long xx, yy, n, ans = 0; read(x), read(y), read(xx), read(yy); read(n); for (int i = 1; i <= n; i++) { read(a), read(b), read(c); bool t = a * x + b * y + c > 0; bool tt = a * xx + b * yy + c > 0; if (tt != t) ans++; } write(ans), putchar('\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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class CF_498_A_CRAZY_TOWN { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); Point home = new Point(sc.nextDouble(), sc.nextDouble()); Point uni = new Point(sc.nextDouble(), sc.nextDouble()); LineSegment homeToUni = new LineSegment(home, uni); int n = sc.nextInt(); int ans = 0; while (n-- > 0) { double a = sc.nextDouble(); double b = sc.nextDouble(); double c = sc.nextDouble(); if (b == 0) { c /= a; a = 1; } else { a /= b; c /= b; b = 1; } if (homeToUni.intersect(new Line(a, b, c))) ans++; } System.out.println(ans); } static 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(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } Line(Point p, double m) { a = -m; b = 1; c = -(a * p.x + p.y); } 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); } } static class LineSegment { Point p, q; LineSegment(Point a, Point b) { p = a; q = b; } boolean intersect(Line l2) { Line l1 = new Line(p, q); if (l1.parallel(l2)) { if (l1.same(l2)) return true; return false; } Point c = l1.intersect(l2); return c.between(p, q); } } static class Point { static final double EPS = 1e-9; double x, y; 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.x, q.x) && y <= Math.max(p.y, q.y) + EPS && y + EPS >= Math.min(p.y, q.y); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 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 { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } public boolean ready() throws IOException { return br.ready(); } } }
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 ms = 1e5 + 20; int a[ms]; int main() { long long x1, x2, y1, y2; cin >> x1 >> y1 >> x2 >> y2; int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { long long a, b, c; scanf("%lld %lld %lld", &a, &b, &c); long long me = a * x1 + b * y1 + c; long long uni = a * x2 + b * y2 + c; me = me / abs(me); uni = uni / abs(uni); if (me != uni) ans++; } cout << ans << 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
#include <bits/stdc++.h> using namespace std; int main() { long long x1, y1, x2, y2; scanf("%lld %lld %lld %lld", &x1, &y1, &x2, &y2); int n, ans(0); long long a, b, c; scanf("%d", &n); while (n--) { scanf("%lld %lld %lld", &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; } 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
def get(): return map(int,raw_input().split()) A = get() B = get() n = int(raw_input()) lines = [get() for _ in xrange(n)] def subs(l1,p): return l1[0] * p[0] + l1[1]*p[1] + l1[2] def check(l1): a = subs(l1,A) b = subs(l1,B) return (a>0 and b < 0) or (a < 0 and b>0) print len(filter(check,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
import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class CF_284C { static Reader sc = new Reader(); public static void main(String[] args) throws IOException { Point a = new Point(sc.nextDouble(), sc.nextDouble()); Point b = new Point(sc.nextDouble(), sc.nextDouble()); if(a.eq(b)){ System.out.println(0); return; } int count = 0; int n = sc.nextInt(); for (int i = 0; i < n; i++) { Line act = new Line(sc.nextDouble(), sc.nextDouble(), sc.nextDouble()); Point p = intersect(a, b, act); if(p != null) { count++; } } System.out.println(count); } static final double EPS = 1e-8; static double deg2rad(double deg){ return deg * Math.PI / 180.0; } static double rad2deg(double rad){ return rad * 180.0 / Math.PI; } static int cmp(double a, double b) { if (eqZero(a - b)) return 0; return Double.compare(a, b); } static boolean eqZero(double d) { return Math.abs(d) < EPS; } // Intersection of 2 segments static Point intersect(Point a, Point b, Line B) { Line A = new Line(a, b); Point res = A.intersect(B); if (res == null) return null; if (isInside(res.x, a.x, b.x) && isInside(res.y, a.y, b.y)) return res; return null; } static boolean isInside(double val, double x, double y) { if (x > y) { double tmp = x; x = y; y = tmp; } return cmp(val, x) >= 0 && cmp(val, y) <= 0; } static class Point implements Comparable<Point>{ double x, y; public static Point readPoint(Scanner in) { double x = in.nextDouble(); double y = in.nextDouble(); return new Point(x, y); } public boolean eq(Point p) { return Math.abs(x - p.x) < EPS && Math.abs(y - p.y) < EPS; } public Point(double x, double y) { this.x = x; this.y = y; } public double dist(Point b){ double dx = b.x - this.x; double dy = b.y - this.y; return Math.sqrt(dx*dx + dy*dy); } @Override public int compareTo(Point o) { if (cmp(y, o.y) != 0) return cmp(y, o.y); return cmp(x, o.x); } public Point sub(Point a){ return new Point(x - a.x,y - a.y); } public Point add(Point a){ return new Point(x + a.x,y + a.y); } public double cross(Point a){ return x*a.y - y*a.x; } public Point multbyscalar(double ua) { return new Point(ua*x,ua*y); } public Line line(Point other) { if (equals(other)) return null; double a = other.y - y; double b = x - other.x; double c = -a * x - b * y; return new Line(a, b, c); } @Override public String toString() { return "(" + x + ", " + y + ")"; } } static class Line { double A, B, C; boolean normed = false; public Line(Point P, Point Q) { A = Q.y - P.y; B = P.x - Q.x; C = -A * P.x - B * P.y; } public Line(double a, double b, double c) { double h = Math.sqrt(a * a + b * b); /*if (a < -GeometryUtils.epsilon) { a = -a; b = -b; c = -c; } else if (a < GeometryUtils.epsilon && b < -GeometryUtils.epsilon) { b = -b; c = -c; }*/ this.A = a / h; this.B = b / h; this.C = c / h; } Point intersect(Line p) { double D = A * p.B - B * p.A; if (eqZero(D)) return null; double x = C * p.B - B * p.C; double y = A * p.C - C * p.A; return new Point(-x / D, -y / D); } void norm() { double z = Math.sqrt(A * A + B * B); A /= z; B /= z; C /= z; normed = true; } double dist(Point p) { if (!normed) norm(); return Math.abs(A * p.x + B * p.y + C); } public boolean parallel(Line other) { return Math.abs(A * other.B - B * other.A) < EPS; } public Line perpendicular(Point point) { return new Line(-B, B, B * point.x - A * point.y); } } static class Reader { final private int BUFFER_SIZE = 1 << 16;private byte[] buffer;private int bufferPointer, bytesRead; public Reader(){buffer=new byte[BUFFER_SIZE];bufferPointer=bytesRead=0; }private void fillBuffer() throws IOException{bytesRead=System.in.read(buffer,bufferPointer=0,BUFFER_SIZE);if(bytesRead==-1)buffer[0]=-1; }private byte read() throws IOException{if(bufferPointer==bytesRead)fillBuffer();return buffer[bufferPointer++]; }public String next() throws IOException{StringBuilder sb = new StringBuilder();byte c;while((c=read())<=' '){if(c==-1) return null;};do{sb.append((char)c);}while((c=read())>' ');if (sb.length()==0) return null;return sb.toString(); }public String nextLine() throws IOException{StringBuilder sb = new StringBuilder();byte c;boolean read = false;while((c=read())!=-1){if(c=='\n') {read = true;break;}if(c>=' ')sb.append((char)c);}if (!read) return null;return sb.toString(); }public int nextInt() throws IOException{int ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret; }public long nextLong() throws IOException{long ret=0;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c=read();do{ret=ret*10L+c-'0';}while((c=read())>='0'&&c<='9');if(neg)return -ret;return ret; }public double nextDouble() throws IOException{double ret=0,div=1;byte c=read();while(c<=' ')c=read();boolean neg=(c=='-');if(neg)c = read();do {ret=ret*10+c-'0';}while((c=read())>='0'&&c<='9');if(c=='.')while((c=read())>='0'&&c<='9')ret+=(c-'0')/(div*=10);if(neg)return -ret;return ret; }public int[] na(int n) throws IOException{int[] a = new int[n];for(int i = 0;i < n;i++)a[i] = nextInt();return a; }public int[][] nm(int n, int m) throws IOException{int[][] map = new int[n][m];for(int i = 0;i < n;i++)map[i] = na(m);return map; }public void close() throws IOException{if(System.in==null) return;System.in.close();} } /* long s = System.currentTimeMillis(); debug(System.currentTimeMillis()-s+"ms"); */ }
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 n, a[1 << 21], s; int main() { long long int a, b, x, y, m, t, o, c = 0, h, u; cin >> a >> b >> x >> y >> n; while (n--) { cin >> m >> t >> o; h = (a * m + b * t + o > 0) ? 1 : -1; u = (x * m + y * t + o > 0) ? 1 : -1; if (h * u == -1) c++; } cout << c; 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()) ans = 0 n = input() for i in range(n): a, b, c = map(int, raw_input().split()) d1 = a * x1 + b * y1 + c d2 = a * x2 + b * y2 + c if d1 * d2 < 0: 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
import java.awt.Point; import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.*; public class A498 implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer tok = new StringTokenizer(""); public static void main(String[] args) { new Thread(null, new A498(), "", 256 * (1L << 20)).start(); } public void run() { try { long t1 = System.currentTimeMillis(); if (System.getProperty("ONLINE_JUDGE") != null) { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } Locale.setDefault(Locale.US); solve(); in.close(); out.close(); long t2 = System.currentTimeMillis(); System.err.println("Time = " + (t2 - t1)); } catch (Throwable t) { t.printStackTrace(System.err); System.exit(-1); } } String readString() throws IOException { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } int readInt() throws IOException { return Integer.parseInt(readString()); } long readLong() throws IOException { return Long.parseLong(readString()); } double readDouble() throws IOException { return Double.parseDouble(readString()); } // solution boolean sign(long a, long b){ return Math.abs(a+b)<(Math.abs(a)+Math.abs(b)); } void solve() throws IOException { long x1 = readLong(); long y1 = readLong(); long x2 = readLong(); long y2 = readLong(); int n = readInt(); long[] a = new long[n]; long[] b = new long[n]; long[] c = new long[n]; for(int i =0; i<n; i++){ a[i] = readLong(); b[i] = readLong(); c[i] = readLong(); } long count =0; for(int i =0; i<n; i++){ if(sign(a[i]*x1+b[i]*y1+c[i], a[i]*x2+b[i]*y2+c[i])){ 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.*; import java.text.*; import java.math.*; import java.util.regex.*; public class sou { public static void main(String[]args) { Scanner sc=new Scanner(System.in); int x1=sc.nextInt(); int y1=sc.nextInt(); int x2=sc.nextInt(); int y2=sc.nextInt(); int n=sc.nextInt(); int sum=0; while(n-->0) { int a=sc.nextInt(); int b=sc.nextInt(); int c=-sc.nextInt(); boolean f1=1l*a*x1+1l*b*y1*1l<=1l*c; boolean f2=1l*a*x2*1l+1l*b*y2*1l<=c*1l; if(f1^f2)sum++; } System.out.println(sum);} } 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); } 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)); } } class Point { double x;double y; Point(double x,double y){ this.x=x;this.y=y; } public boolean between(Point p1,Point p2) { return x<=Math.max(p1.x, p2.x)+1e-9&&x+1e-9>=Math.min(p1.x, p2.x)&& y<=Math.max(p1.y, p2.y)+1e-9&&y>=Math.min(p1.y, p2.y); } } 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); } }
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, x2, y1, y2, a, b, c, n, r; scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2); scanf("%lld", &n); for (r = 0; n--;) { scanf("%lld%lld%lld", &a, &b, &c); long long t = a * x1 + b * y1 + c; long long v = a * x2 + b * y2 + c; if (t < 0 && v > 0) r++; else if (t > 0 && v < 0) r++; } printf("%lld\n", r); 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 node { long long int a, b, c; } node1[305]; int main(void) { long long int x1, y1, x2, y2, n, a, b, c; while (scanf("%lld%lld", &x1, &y1) != EOF) { scanf("%lld%lld", &x2, &y2); scanf("%lld", &n); int i, ans = 0; for (i = 0; i < n; i++) { scanf("%lld%lld%lld", &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++; } } 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; int main() { int x, y; scanf("%d", &x); scanf("%d", &y); int x2, y2; scanf("%d", &x2); scanf("%d", &y2); int n, a, b, c; scanf("%d", &n); long long sign1, sign2; int ans = 0; for (int i = 0; i < n; i++) { scanf("%d", &a); scanf("%d", &b); scanf("%d", &c); sign1 = (long long)a * x + (long long)b * y + (long long)c; sign2 = (long long)a * x2 + (long long)b * y2 + (long long)c; if ((sign1 < 0 && sign2 > 0) || (sign1 > 0 && sign2 < 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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class algorithms{ public static void main(String[] args) throws IOException { MyScanner in = new MyScanner(); Task284C task = new Task284C(); task.slove(in); } public static class Task284A { public void slove(MyScanner in) { int n = in.nextInt(); int x = in.nextInt(); int l, r, count = 0, current = 1, b2; for (int i = 0; i < n;i++) { l = in.nextInt(); r = in.nextInt(); b2 = (l - current)/x; if (b2 >= 1) { current += x*b2; } count += r - current + 1; current = r+1; } System.out.println(count); } } public static class Task284B { public void slove(MyScanner in) { int n = in.nextInt(); int m = in.nextInt(); Map<String, String> map = new HashMap<String, String>(); String s, answer = ""; for (int i = 0; i < m;i++) { s = in.nextLine(); String arr [] = s.split(" "); if (arr[1].length() < arr[0].length()) { map.put(arr[0], arr[1]); } else { map.put(arr[0], arr[0]); } } s = in.nextLine(); for (String retval: s.split(" ")){ answer += map.get(retval) + " "; } System.out.println(answer); } } public static class Task284C { public void slove(MyScanner in) { long x = in.nextInt(); long y = in.nextInt(); Point pa = new Point(x, y); x = in.nextInt(); y = in.nextInt(); Point pb = new Point(x, y); int count = 0; int n = in.nextInt(); long a, b, c; for (int i = 0; i < n;i++) { a = in.nextInt(); b = in.nextInt(); c = in.nextInt(); if (pa.onOneSide(pb, a, b, c) == false) { count++; } } System.out.println(count); } } public static class Point { public long x; public long y; public Point(long x, long y) { this.x = x; this.y = y; } public boolean onOneSide(Point p, long a, long b, long c) { long d2 = a*p.x + b*p.y + c; long d1 = a*this.x + b*this.y + c; // if (d2 == 0 || d1 == 0) { // System.out.println(d2); // } // System.out.println(d1); // System.out.println(d2); if ((d2 > 0 && d1 < 0) || (d1 > 0 && d2 < 0)) { return false; } return true; } } public static class TaskA { public void slove(MyScanner in) { int n = in.nextInt(); int prev = 0, prevprev = 0, diff, answer; int min = 1000, max = 0; int imin = 1; int arr [] = new int [n]; for (int i = 0; i < n;i++) { arr[i] = in.nextInt(); if (i != 0) { if (i > 1) { diff = arr[i] - prevprev; if (diff < min) { min = diff; imin = i; } } if ((arr[i] - prev) > max) { max = arr[i] - prev; } } prevprev = prev; prev = arr[i]; } diff = arr[imin] - arr[imin-2]; if (diff > max) { answer = diff; } else { answer = max; } System.out.println(answer); } } public static class TaskB { public int arr []; public int n; public void slove(MyScanner in) { n = in.nextInt(); arr = new int [n]; int count[] = new int [10]; String s = in.nextLine(); String min = s, num; for (int i = 0; i < n;i++) { arr[i] = Character.getNumericValue(s.charAt(i)); count[arr[i]] += 1; } for (int i = 0; i < 10;i++) { if (count[i] == 0) { continue; } num = getSmallestInt(10 - i); for (int j = 0; j < num.length();j++) { if (num.charAt(j) < min.charAt(j)) { min = num; } if (num.charAt(j) != min.charAt(j)) { break; } } } String answer = ""; answer += min; System.out.println(answer); } public String getSmallestInt(int offset) { StringBuffer tmp = new StringBuffer(); long newvalue; ArrayList<Integer> al = new ArrayList<Integer>(); for (int i = 0; i < n;i++) { newvalue = arr[i] + offset; if (newvalue >= 10) { newvalue -= 10; if (newvalue == 0) { al.add(i); } } tmp.append((int)newvalue); } String str = tmp.toString(); String newstr; String min = String.format("%0" + n + "d", 0).replace("0","9"); for(int i = 0; i < al.size(); i++) { newstr = str.substring(al.get(i)) + str.substring(0, al.get(i)); for (int j = 0; j < newstr.length();j++) { if (newstr.charAt(j) < min.charAt(j)) { min = newstr; } if (newstr.charAt(j) != min.charAt(j)) { break; } } } return min; } } public static class TaskC { public void slove(MyScanner in) { int n = in.nextInt(); int m = in.nextInt(); String arr [] = new String [n]; char prev = ' '; int count = 0; for (int i = 0; i < n;i++) { arr[i] = in.nextLine(); } char c; ArrayList<Integer> ignore = new ArrayList<Integer>(); ArrayList<Integer> ignorenext = new ArrayList<Integer>(); for (int i = 0; i < m;i++) { prev = ' '; for (int j = 0; j < n;j++) { c = arr[j].charAt(i); if (ignore.contains(j)) { prev = c; continue; } if (c > prev && prev != ' ') { if (ignorenext.contains(j) == false) { ignorenext.add(j); } } if (c < prev) { count++; ignorenext.clear(); break; } prev = c; } ignore.addAll(ignorenext); ignorenext.clear(); } System.out.println(count); } } public static class TaskD { public void slove(MyScanner in) { int n = in.nextInt(); int s = 0, t = 0; int count1 = 0, count2 = 0; int max = 0; int arr[] = new int [n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); if (arr[i] == 1) { count1++; } else { count2++; } } if (count1 != count2) { if (count1 > count2) { max = count1; } else { max = count2; } int x = 1, y = max; while (max/x >= 1) { if (max%x == 0) { y = max/x; System.out.println(x + " " + y); } x++; } } System.out.println(s + " " + t); } } public static class Pair implements Comparable<Pair> { public final Comparator<Pair> SLOPE_ORDER = new Comparator<Pair>() { // compare points by slope to this point public int compare(Pair p1, Pair p2) { return p1.compareTo(p2); } }; private long min; private long max; private int count; private int num; public Pair(long min, long max, int count, int num) { this.min = min; this.max = max; this.count = count; this.num = num; } public void decrease() { count--; } public int count() { return count; } public int num() { return num; } public int compareTo(Pair that) { if ((this.min == that.min) && (this.max == that.max)) { return 0; } if ((this.min < that.min) || (this.min == that.min && this.max < that.max)) { return -1; } return 1; } public int contain(Pair that) { // System.out.println(this.min); // System.out.println(this.max); // System.out.println(that.min); // System.out.println(that.max); if ((this.min <= that.min) && (this.max >= that.max)) { return 1; } if (this.min <= that.min) { return 0; } return -1; } } public static class TaskE { public void slove(MyScanner in) { int n = in.nextInt(); Pair parties[] = new Pair [n]; int p_a[] = new int [n]; long min, max; int count; for (int i = 0; i< n;i++) { min = in.nextLong(); max = in.nextLong(); parties[i] = new Pair(min, max, 0, i+1); } Arrays.sort(parties); // for (int i = 0; i< n;i++) { // System.out.println(parties[i].num()); // } int m = in.nextInt(); Pair actors[] = new Pair [m]; for (int i = 0; i< m;i++) { min = in.nextLong(); max = in.nextLong(); count = in.nextInt(); actors[i] = new Pair(min, max, count, i+1); } Arrays.sort(actors); // for (int i = 0; i< m;i++) { // System.out.println(actors[i].num()); // } int j = 0; Pair partie; String answer = "YES\n"; for (int i = 0; i< n;i++) { partie = parties[i]; // actor = actors[j]; // System.out.println(actor.count()); if (actors[j].count() <= 0) { if (j == (m)) { answer = "NO"; break; } j++; // actor = actors[j]; } // System.out.println(actor.contain(partie)); for(int k = j; k< m; k++) { if (actors[k].count() <= 0) { continue; } int contain = actors[k].contain(partie); if (contain == 1) { actors[k].decrease(); // System.out.println(actors[k].count()); // System.out.println(partie.num()); // System.out.println(actors[k].num()); p_a[partie.num()-1] = actors[k].num(); break; } else if (contain == -1){ answer = "NO"; break; } } if (answer == "NO") { break; } } if (answer != "NO") { for (int i = 0; i< n;i++) { if (p_a[i] == 0) { answer = "NO"; break; } answer += p_a[i] + " "; } } System.out.println(answer); } } public static class TaskDold { public void slove(MyScanner in) { int n = in.nextInt(); Boolean ok = true; int arr [][] = new int [n][n]; int minj [] = new int [n]; int min = 1000000000; int min_j = 0; for (int i =0; i< n;i++) { min = 1000000001; min_j = 0; for (int j=0; j< n; j++) { arr[i][j] = in.nextInt(); if (arr[i][j] < min && (i != j)) { min = arr[i][j]; min_j = j; } if ((i == j) && (arr[i][j] != 0)) { ok = false; } if ((i > j) && (arr[i][j] != arr[j][i])) { ok = false; } if ((i != j) && (arr[i][j] == 0)) { ok = false; } } minj[i] = min_j; for (int j =0; j< i;j++) { if ((j == min_j) && (minj[j] == i)) { } } } if (!ok) { System.out.println("NO"); return; } int j; for (int i =0; i< n;i++) { min_j = minj[i]; // System.out.println(min_j); for (j =0; j< i;j++) { if ((j == min_j) && (minj[j] == i)) { } if (minj[j] == min_j) { // System.out.println(arr[i][j]); // System.out.println(arr[j][minj[j]]); // System.out.println(arr[i][min_j]); if (arr[i][j] != (arr[j][minj[j]] + arr[i][min_j])) { System.out.println("NO"); return; } } } } System.out.println("YES"); } } 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
#include <bits/stdc++.h> using namespace std; int main() { double x, y, z, a1, b1, a2, b2; scanf("%lf%lf%lf%lf", &a1, &b1, &a2, &b2); int n, c = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lf%lf%lf", &x, &y, &z); if ((a1 * x + b1 * y + z) * (a2 * x + b2 * y + z) < 0) { c++; } } printf("%d\n", c); }
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()) n = int(input()) ans = 0 for i in range(n): a, b, c = map(int, input().split()) f1 = a * x1 + b * y1 + c f2 = a * x2 + b * y2 + c if f1 * f2 < 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
#include <bits/stdc++.h> using namespace std; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; else return a.first < b.first; } long long exp(long long a, long long b) { long long ans = 1; while (b != 0) { if (b % 2) ans = ans * a; a = a * a; b /= 2; } return ans; } int main() { long long a, b, c, d; scanf("%lld %lld %lld %lld", &a, &b, &c, &d); long long n; scanf("%lld", &n); long long ans = 0; for (long long i = (long long)0; i < (long long)n; i++) { long long e, f, g; scanf("%lld %lld %lld", &e, &f, &g); long long val1 = e * a + f * b + g; long long val2 = e * c + f * d + g; if (val1 < 0 && val2 > 0 || (val1 > 0 && val2 < 0)) { ans++; } } printf("%lld\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
x1,y1 = map(int,input().split()) x2,y2 = map(int,input().split()) n = int(input()) s = 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): s = s + 1 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
#include <bits/stdc++.h> using namespace std; int x, y, xx, yy; int ans, n; int main() { scanf("%d %d", &x, &y); scanf("%d %d", &xx, &yy); scanf("%d", &n); for (int i = 1; i <= n; i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); if (1.0 * a * x + 1.0 * b * y + c > 0.0 && 1.0 * a * xx + 1.0 * b * yy + c < 0.0) ans++; else if (1.0 * a * x + 1.0 * b * y + c < 0.0 && 1.0 * a * xx + 1.0 * b * yy + c > 0.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; vector<bool> prime; vector<long long> fact, inv, primes, factors; void factorize(long long a) { fact.clear(); for (long long i = 1; i * i <= a; i++) { if (i * i == a) factors.push_back(i); else if (a % i == 0) { factors.push_back(i); factors.push_back(a / i); } } sort(factors.begin(), factors.end()); } long long power(long long a, long long b) { if (a == 1 || b == 0) return 1; long long c = power(a, b / 2); long long res = 1; res = c * c; if (res >= 1000000007) res %= 1000000007; if (b % 2) res *= a; if (res >= 1000000007) res %= 1000000007; return res; } long long modInv(long long a) { return power(a, 1000000007 - 2); } void factorial(long long n) { fact.resize(n + 1); fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i; if (fact[i] >= 1000000007) fact[i] %= 1000000007; } } void InvFactorial(long long n) { inv.resize(n + 1); inv[0] = 1; for (long long i = 1; i <= n; i++) inv[i] = modInv(fact[i]); } long long ncr(long long n, long long r) { if (n < r || n < 0 || r < 0) return 0; long long b = inv[n - r]; long long c = inv[r]; long long a = fact[n] * b; if (a >= 1000000007) a %= 1000000007; a *= c; if (a >= 1000000007) a %= 1000000007; return a; } void remove_duplicates(vector<pair<long long, long long>> &v) { sort(v.begin(), v.end()); long long _size = unique(v.begin(), v.end()) - v.begin(); v.resize(_size); } unsigned long long gcd(unsigned long long u, unsigned long long v) { if (u == 0 || v == 0) return max(u, v); unsigned long long shift = __builtin_ctz(u | v); u >>= __builtin_ctz(u); do { v >>= __builtin_ctz(v); if (u > v) swap(u, v); v -= u; } while (v != 0); return u << shift; } void sieve(long long n) { prime.assign(n + 1, 1); prime[1] = false; for (long long p = 2; p * p <= n; p++) if (prime[p]) for (long long i = p * p; i <= n; i += p) prime[i] = false; for (long long i = 2; i < n + 1; i++) if (prime[i]) primes.push_back(i); } void solve() { long long x1, y1; cin >> x1 >> y1; long long x2, y2; cin >> x2 >> y2; long long n; cin >> n; long long count = 0; while (n--) { long long a, b, c; cin >> a >> b >> c; long long z1 = a * x1 + b * y1 + c; long long z2 = a * x2 + b * y2 + c; if ((z1 > 0 && z2 < 0) || (z1 < 0 && z2 > 0)) count++; } cout << count; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long T = 1; for (long long i = 1; i <= T; i++) solve(); }
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 = 250, MAXBS = 1000000; const double PI = acos(-1); struct dot { long long x, y; dot(long long x = 0, long long y = 0) : x(x), y(y) {} }; struct line { long long a, b, c; bool side(dot p) { return (a * p.x + b * p.y > -c); } }; int n, total; dot A, B; line l; int main() { while (cin >> A.x >> A.y >> B.x >> B.y) { cin >> n; total = 0; for (int i = (0); i < (int)(n); i++) { cin >> l.a >> l.b >> l.c; if (l.side(A) != l.side(B)) total++; } cout << total << 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() { ios::sync_with_stdio(0); cin.tie(0); long long x1, y1, x2, y2; cin >> x1 >> y1; cin >> x2 >> y2; long long n; cin >> n; long long ans = 0; for (int i = 0; i < n; i++) { long a, b, c; cin >> a >> b >> c; if ((a * x1 + b * y1 + c) > 0 && (a * x2 + b * y2 + c < 0)) ans++; else if ((a * x1 + b * y1 + c) < 0 && (a * x2 + b * y2 + c > 0)) ans++; } cout << ans; return 0; }
CPP