Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.OutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.io.BufferedWriter;
- import java.io.Writer;
- import java.io.OutputStreamWriter;
- import java.util.InputMismatchException;
- import java.io.IOException;
- import java.io.InputStream;
- /**
- * Built using CHelper plug-in
- * Actual solution is at the top
- *
- * @author ilyakor
- */
- 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);
- TaskI solver = new TaskI();
- solver.solve(1, in, out);
- out.close();
- }
- static class TaskI {
- int x1;
- int y1;
- int x2;
- int y2;
- int cnt;
- final int MAX = 505;
- final double eps = 1e-6;
- int[][] used;
- int[][] rad;
- public double pointToSegmentDistance(int x, int y) {
- int dx = x2 - x1;
- int dy = y2 - y1;
- int px = x - x1;
- int py = y - y1;
- int squaredLength = dx * dx + dy * dy;
- double k = dx * px + dy * py;
- if (k < 0 || squaredLength == 0) {
- return sqHypot(px, py);
- }
- if (k > squaredLength) {
- return sqHypot(px - dx, py - dy);
- }
- k /= squaredLength;
- return sqHypot(px - k * dx, py - k * dy);
- }
- private double sqHypot(double px, double py) {
- return px * px + py * py;
- }
- public void solve(int testNumber, InputReader in, OutputWriter out) {
- int n = in.nextInt();
- used = new int[MAX][MAX];
- rad = new int[MAX][MAX];
- for (int x = 0; x < MAX; ++x)
- for (int y = 0; y < MAX; ++y)
- used[x][y] = -1;
- for (int i = 0; i < n; ++i) {
- int x = in.nextInt(), y = in.nextInt(), r = (int) (in.nextDouble() * 10);
- used[x][y] = 0;
- rad[x][y] = r * r;
- }
- int q = in.nextInt();
- for (int it = 1; it <= q; ++it) {
- x1 = in.nextInt();
- y1 = in.nextInt();
- x2 = in.nextInt();
- y2 = in.nextInt();
- cnt = 0;
- int a = y2 - y1, b = x1 - x2, c = -a * x1 - b * y1;
- int curx = x1, cury = y1, dirx = (int) Math.signum(x2 - x1), diry = (int) Math.signum(y2 - y1);
- while ((curx != x2) || (cury != y2)) {
- check(curx, cury, it);
- check(curx + dirx, cury, it);
- check(curx, cury + diry, it);
- if ((diry != 0) && (getD(a, b, c, curx, cury + diry) * getD(a, b, c, curx + dirx, cury + diry) <= 0))
- cury += diry;
- else
- curx += dirx;
- }
- check(x2, y2, it);
- out.printLine(cnt);
- }
- }
- private void check(int x, int y, int it) {
- if ((x < 0) || (x >= MAX) || (y < 0) || (y >= MAX) || (used[x][y] < 0) || (used[x][y] == it)) return;
- used[x][y] = it;
- if (pointToSegmentDistance(x, y) * 100 <= rad[x][y] + eps)
- cnt++;
- }
- private int getD(int a, int b, int c, int x, int y) {
- int d = a * x + b * y + c;
- if (d < 0) return -1;
- if (d > 0) return 1;
- return 0;
- }
- }
- static class OutputWriter {
- private final PrintWriter writer;
- public OutputWriter(OutputStream outputStream) {
- writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
- }
- public OutputWriter(Writer writer) {
- this.writer = new PrintWriter(writer);
- }
- 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();
- }
- }
- static class InputReader {
- private InputStream stream;
- private byte[] buffer = new byte[10000];
- private int cur;
- private int count;
- public InputReader(InputStream stream) {
- this.stream = stream;
- }
- public static boolean isSpace(int c) {
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
- public int read() {
- if (count == -1) {
- throw new InputMismatchException();
- }
- try {
- if (cur >= count) {
- cur = 0;
- count = stream.read(buffer);
- if (count <= 0)
- return -1;
- }
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- return buffer[cur++];
- }
- public int readSkipSpace() {
- int c;
- do {
- c = read();
- } while (isSpace(c));
- return c;
- }
- public int nextInt() {
- int sgn = 1;
- int c = readSkipSpace();
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- int res = 0;
- do {
- if (c < '0' || c > '9') {
- throw new InputMismatchException();
- }
- res = res * 10 + c - '0';
- c = read();
- } while (!isSpace(c));
- res *= sgn;
- return res;
- }
- public double nextDouble() {
- double sgn = 1;
- int c = readSkipSpace();
- if (c == '-') {
- sgn = -1;
- c = read();
- }
- double res = 0;
- while (!isSpace(c) && c != '.') {
- if (c == 'e' || c == 'E') {
- return res * Math.pow(10, nextInt());
- } else if (c < '0' || c > '9') {
- throw new InputMismatchException();
- }
- res = res * 10 + c - '0';
- c = read();
- }
- if (c == '.') {
- c = read();
- double m = 1;
- while (!isSpace(c)) {
- if (c == 'e' || c == 'E') {
- return res * Math.pow(10, nextInt());
- } else if (c < '0' || c > '9') {
- throw new InputMismatchException();
- }
- m /= 10;
- res += (c - '0') * m;
- c = read();
- }
- }
- res *= sgn;
- return res;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement