Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class J2 {
- FastScanner in;
- PrintWriter out;
- class Obj implements Comparable<Obj> {
- int id;
- double value;
- public Obj(int id, double value) {
- super();
- this.id = id;
- this.value = value;
- }
- @Override
- public int compareTo(Obj o) {
- return Double.compare(value, o.value);
- }
- }
- void solve() {
- int tc = in.nextInt();
- Random rnd = new Random(123);
- for (int t = 0; t < tc; t++) {
- int n = in.nextInt();
- int[] x = new int[n];
- int[] y = new int[n];
- for (int i = 0; i < n; i++) {
- x[i] = in.nextInt();
- y[i] = in.nextInt();
- }
- double x1 = rnd.nextDouble(), y1 = rnd.nextDouble();
- double d = Math.hypot(x1, y1);
- x1 /= d;
- y1 /= d;
- Obj[] a = new Obj[n];
- for (int i = 0; i < n; i++) {
- a[i] = new Obj(i, x1 * y[i] - y1 * x[i]);
- }
- Arrays.sort(a);
- long[] answer = new long[n];
- for (int i = 0; i < n; i++) {
- int id = a[i].id;
- answer[id] = Long.MAX_VALUE;
- for (int dx1 = -1; dx1 < 2; dx1 += 2)
- for (int j = i + dx1; j >= 0 && j < n; j += dx1) {
- double dd = Math.abs(a[i].value - a[j].value);
- if (dd * dd - 5 > answer[id]) {
- break;
- }
- long dx = x[id] - x[a[j].id], dy = y[id] - y[a[j].id];
- answer[id] = Math.min(answer[id], dx * dx + dy * dy);
- }
- }
- for (int i = 0; i < n; i++) {
- out.println(answer[i]);
- }
- }
- }
- void run() {
- try {
- in = new FastScanner(new File("j.in"));
- out = new PrintWriter(new File("j.out"));
- solve();
- out.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- void runIO() {
- in = new FastScanner(System.in);
- out = new PrintWriter(System.out);
- solve();
- out.close();
- }
- class FastScanner {
- BufferedReader br;
- StringTokenizer st;
- public FastScanner(File f) {
- try {
- br = new BufferedReader(new FileReader(f));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public FastScanner(InputStream f) {
- br = new BufferedReader(new InputStreamReader(f));
- }
- String next() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return null;
- st = new StringTokenizer(s);
- }
- return st.nextToken();
- }
- boolean hasMoreTokens() {
- while (st == null || !st.hasMoreTokens()) {
- String s = null;
- try {
- s = br.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (s == null)
- return false;
- st = new StringTokenizer(s);
- }
- return true;
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- long nextLong() {
- return Long.parseLong(next());
- }
- double nextDouble() {
- return Double.parseDouble(next());
- }
- }
- public static void main(String[] args) {
- new J2().runIO();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment