Submission #85302


Source Code Expand

import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.TreeSet;

public class Main {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	
	static void solve()
	{
		int n = ni();
		IncrementalConvexHull ich = new IncrementalConvexHull();
		for(int i = 0;i < n;i++){
			char c = ns().toCharArray()[0];
			int x = ni(), y = ni();
			if(c == 'T'){
				if(ich.includes(x, y)){
					out.println("DANGER");
				}else{
					out.println("SAFE");
				}
			}else{
				ich.add(x, y);
			}
		}
	}
	
	public static class IncrementalConvexHull {
		public TreeSet<Vertex> hull;
		public Vertex center = null;
		
		public IncrementalConvexHull() {
			hull = new TreeSet<Vertex>(new Comparator<Vertex>(){
				@Override
				public int compare(Vertex a, Vertex b) {
//					if(center == null)return Double.compare(a.arg(0, 0), b.arg(0, 0));
					if(a.x == b.x && a.y == b.y)return 0;
					if(a.x == center.x && a.y == center.y)return -1;
					if(b.x == center.x && b.y == center.y)return 1;
					return Double.compare(a.arg(center.x, center.y), b.arg(center.x, center.y));
				}
			});
		}
		
		public boolean add(double x, double y)
		{
			if(includes(x, y))return false;
			if(hull.size() <= 1){
				// 何も考えずに追加
				if(hull.size() == 1){
					center = new Vertex((hull.first().x+x)/2, (hull.first().y+y)/2);
				}
				hull.add(new Vertex(x, y));
				return true;
			}else if(hull.size() == 2){
				Vertex v = new Vertex(x, y);
				Vertex f = hull.first();
				Vertex c = hull.last();
				if(isACB(center.x, center.y, v.x, v.y, f.x, f.y)){
					hull.remove(f);
					hull.add(v);
					return true;
				}
				if(isACB(center.x, center.y, v.x, v.y, c.x, c.y)){
					hull.remove(c);
					hull.add(v);
					return true;
				}
				hull.add(v);
				return true;
			}else{
				Vertex v = new Vertex(x, y);
				Vertex f = hull.floor(v);
				Vertex c = null;
				if(f == null){
					f = hull.last();
					c = hull.first();
				}else{
					c = hull.higher(f);
					if(c == null)c = hull.first();
				}
				
				
				// 反時計回り側
				while(hull.size() > 1){
					Vertex nc = hull.higher(c);
					if(nc == null)nc = hull.first();
					// ncがv-cの上にあるならcを除く
					double cross = (nc.x-v.x)*(c.y-v.y)-(nc.y-v.y)*(c.x-v.x);
					if(cross >= 0){
						hull.remove(c);
						c = nc;
					}else{
						break;
					}
				}
				
				// 時計回り側
				while(hull.size() > 1){
					Vertex nf = hull.lower(f);
					if(nf == null)nf = hull.last();
//					U.tr("v", v, "c", f, "nc", nf);
					// nfがv-fの下にあるならfを除く
					double cross = (nf.x-v.x)*(f.y-v.y)-(nf.y-v.y)*(f.x-v.x);
					if(cross <= 0){
						hull.remove(f);
						f = nf;
					}else{
						break;
					}
				}
//				double thres = -1E-9;
//				while(!hull.isEmpty()){
//					Vertex nc = hull.higher(c);
//					if(nc == null)nc = hull.first();
//					// ncがv-cの上にあるならcを除く
//					double cross = (nc.x-v.x)*(c.y-v.y)-(nc.y-v.y)*(c.x-v.x);
//					if(cross >= thres){
//						if(cross < 1E-9)cross = 1E-9;
//						hull.remove(c);
//						c = nc;
//					}else{
//						break;
//					}
//				}
//				
//				// 時計回り側
//				thres = 1E-9;
//				while(!hull.isEmpty()){
//					Vertex nf = hull.lower(f);
//					if(nf == null)nf = hull.last();
////					U.tr("v", v, "c", f, "nc", nf);
//					// nfがv-fの下にあるならfを除く
//					double cross = (nf.x-v.x)*(f.y-v.y)-(nf.y-v.y)*(f.x-v.x);
//					if(cross <= thres){
//						if(cross > -1E-9)cross = -1E-9;
//						hull.remove(f);
//						f = nf;
//					}else{
//						break;
//					}
//				}
				hull.add(v);
				return true;
			}
		}
		
		public boolean includes(double x, double y)
		{
			if(hull.isEmpty())return false;
			if(hull.size() == 1)return Point2D.distance(x, y, hull.first().x, hull.first().y) < 1E-9;
			if(hull.size() == 2)return Line2D.ptSegDist(hull.first().x, hull.first().y, hull.last().x, hull.last().y, x, y) < 1E-9;
//			if(Point2D.distance(x, y, center.x, center.y) < 1E-9)return true;
			
			// O(log N)
			Vertex v = new Vertex(x, y);
			Vertex f = hull.floor(v);
			Vertex c;
			if(f == null){
				f = hull.last();
				c = hull.first();
			}else{
				c = hull.higher(f);
				if(c == null)c = hull.first();
			}
			// on
			if(Line2D.ptSegDist(f.x, f.y, c.x, c.y, v.x, v.y) < 1E-9)return true;
//			U.tr(f.x, f.y, c.x, c.y, v.x, v.y, Line2D.relativeCCW(f.x, f.y, c.x, c.y, v.x, v.y));
			if(isACB(center.x, center.y, v.x, v.y, f.x, f.y))return false;
			if(isACB(center.x, center.y, v.x, v.y, c.x, c.y))return false;
			if(Line2D.relativeCCW(f.x, f.y, c.x, c.y, v.x, v.y) == -1)return true;
			return false;
		}
		
		static boolean isACB(double ax, double ay, double bx, double by, double cx, double cy)
		{
			if((ax == cx && ay == cy) || (bx == cx && by == cy))return true;
			return Math.abs(((cx-ax)*(by-ay)-(cy-ay)*(bx-ax))/Point2D.distance(ax, ay, bx, by)/Point2D.distance(ax, ay, cx, cy)) < 1E-9
					&& (cx-ax)*(cx-bx)+(cy-ay)*(cy-by) <= 0;
		}
		
		static class Vertex
		{
			public double x, y;
			
			public Vertex(double x, double y) {
				this.x = x; this.y = y;
			}
			
			public double arg(double gx, double gy)
			{
				return Math.atan2(y-gy, x-gx);
			}
			
			public String toString()
			{
				return String.format("(%.3f,%.3f)", x, y);
			}
		}
	}
	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static boolean eof()
	{
		if(lenbuf == -1)return true;
		int lptr = ptrbuf;
		while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
		
		try {
			is.mark(1000);
			while(true){
				int b = is.read();
				if(b == -1){
					is.reset();
					return true;
				}else if(!isSpaceChar(b)){
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}
	
	private static byte[] inbuf = new byte[1024];
	private static int lenbuf = 0, ptrbuf = 0;
	
	private static int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static double nd() { return Double.parseDouble(ns()); }
	private static char nc() { return (char)skip(); }
	
	private static String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private static char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private static char[][] nm(int n, int m)
	{
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private static int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private static int ni()
	{
		int num = 0, b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}

Submission Info

Submission Time
Task C - 泥棒
User uwi
Language Java (OpenJDK 1.7.0)
Score 300
Code Size 8804 Byte
Status AC
Exec Time 4568 ms
Memory 39980 KB

Judge Result

Set Name Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 50 / 50 50 / 50 50 / 50 150 / 150
Status
AC × 25
AC × 50
AC × 75
AC × 100
Set Name Test Cases
Subtask1 11_rand00.txt, 11_rand01.txt, 11_rand02.txt, 11_rand03.txt, 11_rand04.txt, 11_rand05.txt, 11_rand06.txt, 11_rand07.txt, 11_rand08.txt, 11_rand09.txt, 11_rand10.txt, 11_rand11.txt, 11_rand12.txt, 11_rand13.txt, 11_rand14.txt, 11_rand15.txt, 11_rand16.txt, 11_rand17.txt, 11_rand18.txt, 11_rand19.txt, 11_rand20.txt, 11_rand21.txt, 11_rand22.txt, 11_rand23.txt, 11_rand24.txt
Subtask2 11_rand00.txt, 11_rand01.txt, 11_rand02.txt, 11_rand03.txt, 11_rand04.txt, 11_rand05.txt, 11_rand06.txt, 11_rand07.txt, 11_rand08.txt, 11_rand09.txt, 11_rand10.txt, 11_rand11.txt, 11_rand12.txt, 11_rand13.txt, 11_rand14.txt, 11_rand15.txt, 11_rand16.txt, 11_rand17.txt, 11_rand18.txt, 11_rand19.txt, 11_rand20.txt, 11_rand21.txt, 11_rand22.txt, 11_rand23.txt, 11_rand24.txt, 21_rand00.txt, 21_rand01.txt, 21_rand02.txt, 21_rand03.txt, 21_rand04.txt, 21_rand05.txt, 21_rand06.txt, 21_rand07.txt, 21_rand08.txt, 21_rand09.txt, 21_rand10.txt, 21_rand11.txt, 21_rand12.txt, 21_rand13.txt, 21_rand14.txt, 21_rand15.txt, 21_rand16.txt, 21_rand17.txt, 21_rand18.txt, 21_rand19.txt, 21_rand20.txt, 21_rand21.txt, 21_rand22.txt, 21_rand23.txt, 21_rand24.txt
Subtask3 11_rand00.txt, 11_rand01.txt, 11_rand02.txt, 11_rand03.txt, 11_rand04.txt, 11_rand05.txt, 11_rand06.txt, 11_rand07.txt, 11_rand08.txt, 11_rand09.txt, 11_rand10.txt, 11_rand11.txt, 11_rand12.txt, 11_rand13.txt, 11_rand14.txt, 11_rand15.txt, 11_rand16.txt, 11_rand17.txt, 11_rand18.txt, 11_rand19.txt, 11_rand20.txt, 11_rand21.txt, 11_rand22.txt, 11_rand23.txt, 11_rand24.txt, 21_rand00.txt, 21_rand01.txt, 21_rand02.txt, 21_rand03.txt, 21_rand04.txt, 21_rand05.txt, 21_rand06.txt, 21_rand07.txt, 21_rand08.txt, 21_rand09.txt, 21_rand10.txt, 21_rand11.txt, 21_rand12.txt, 21_rand13.txt, 21_rand14.txt, 21_rand15.txt, 21_rand16.txt, 21_rand17.txt, 21_rand18.txt, 21_rand19.txt, 21_rand20.txt, 21_rand21.txt, 21_rand22.txt, 21_rand23.txt, 21_rand24.txt, 31_rand00.txt, 31_rand01.txt, 31_rand02.txt, 31_rand03.txt, 31_rand04.txt, 31_rand05.txt, 31_rand06.txt, 31_rand07.txt, 31_rand08.txt, 31_rand09.txt, 31_rand10.txt, 31_rand11.txt, 31_rand12.txt, 31_rand13.txt, 31_rand14.txt, 31_rand15.txt, 31_rand16.txt, 31_rand17.txt, 31_rand18.txt, 31_rand19.txt, 31_rand20.txt, 31_rand21.txt, 31_rand22.txt, 31_rand23.txt, 31_rand24.txt
Subtask4 11_rand00.txt, 11_rand01.txt, 11_rand02.txt, 11_rand03.txt, 11_rand04.txt, 11_rand05.txt, 11_rand06.txt, 11_rand07.txt, 11_rand08.txt, 11_rand09.txt, 11_rand10.txt, 11_rand11.txt, 11_rand12.txt, 11_rand13.txt, 11_rand14.txt, 11_rand15.txt, 11_rand16.txt, 11_rand17.txt, 11_rand18.txt, 11_rand19.txt, 11_rand20.txt, 11_rand21.txt, 11_rand22.txt, 11_rand23.txt, 11_rand24.txt, 21_rand00.txt, 21_rand01.txt, 21_rand02.txt, 21_rand03.txt, 21_rand04.txt, 21_rand05.txt, 21_rand06.txt, 21_rand07.txt, 21_rand08.txt, 21_rand09.txt, 21_rand10.txt, 21_rand11.txt, 21_rand12.txt, 21_rand13.txt, 21_rand14.txt, 21_rand15.txt, 21_rand16.txt, 21_rand17.txt, 21_rand18.txt, 21_rand19.txt, 21_rand20.txt, 21_rand21.txt, 21_rand22.txt, 21_rand23.txt, 21_rand24.txt, 31_rand00.txt, 31_rand01.txt, 31_rand02.txt, 31_rand03.txt, 31_rand04.txt, 31_rand05.txt, 31_rand06.txt, 31_rand07.txt, 31_rand08.txt, 31_rand09.txt, 31_rand10.txt, 31_rand11.txt, 31_rand12.txt, 31_rand13.txt, 31_rand14.txt, 31_rand15.txt, 31_rand16.txt, 31_rand17.txt, 31_rand18.txt, 31_rand19.txt, 31_rand20.txt, 31_rand21.txt, 31_rand22.txt, 31_rand23.txt, 31_rand24.txt, 41_rand00.txt, 41_rand01.txt, 41_rand02.txt, 41_rand03.txt, 41_rand04.txt, 41_rand05.txt, 41_rand06.txt, 41_rand07.txt, 41_rand08.txt, 41_rand09.txt, 41_rand10.txt, 41_rand11.txt, 41_rand12.txt, 41_rand13.txt, 41_rand14.txt, 41_rand15.txt, 41_rand16.txt, 41_rand17.txt, 41_rand18.txt, 41_rand19.txt, 41_rand20.txt, 41_rand21.txt, 41_rand22.txt, 41_rand23.txt, 41_rand24.txt
Case Name Status Exec Time Memory
11_rand00.txt AC 1655 ms 19228 KB
11_rand01.txt AC 392 ms 19144 KB
11_rand02.txt AC 390 ms 19144 KB
11_rand03.txt AC 395 ms 19016 KB
11_rand04.txt AC 389 ms 18896 KB
11_rand05.txt AC 398 ms 19012 KB
11_rand06.txt AC 390 ms 18880 KB
11_rand07.txt AC 392 ms 18888 KB
11_rand08.txt AC 387 ms 19064 KB
11_rand09.txt AC 388 ms 19144 KB
11_rand10.txt AC 388 ms 19020 KB
11_rand11.txt AC 396 ms 19140 KB
11_rand12.txt AC 402 ms 19136 KB
11_rand13.txt AC 393 ms 19016 KB
11_rand14.txt AC 450 ms 19144 KB
11_rand15.txt AC 391 ms 19152 KB
11_rand16.txt AC 385 ms 19140 KB
11_rand17.txt AC 393 ms 19032 KB
11_rand18.txt AC 393 ms 19100 KB
11_rand19.txt AC 398 ms 19152 KB
11_rand20.txt AC 390 ms 19148 KB
11_rand21.txt AC 421 ms 19024 KB
11_rand22.txt AC 401 ms 19140 KB
11_rand23.txt AC 398 ms 19136 KB
11_rand24.txt AC 394 ms 19152 KB
21_rand00.txt AC 396 ms 19072 KB
21_rand01.txt AC 390 ms 19144 KB
21_rand02.txt AC 389 ms 19020 KB
21_rand03.txt AC 399 ms 19008 KB
21_rand04.txt AC 400 ms 19016 KB
21_rand05.txt AC 394 ms 19148 KB
21_rand06.txt AC 402 ms 19016 KB
21_rand07.txt AC 395 ms 19148 KB
21_rand08.txt AC 391 ms 19016 KB
21_rand09.txt AC 398 ms 19008 KB
21_rand10.txt AC 399 ms 19148 KB
21_rand11.txt AC 401 ms 19108 KB
21_rand12.txt AC 401 ms 19012 KB
21_rand13.txt AC 404 ms 19148 KB
21_rand14.txt AC 453 ms 19016 KB
21_rand15.txt AC 392 ms 19148 KB
21_rand16.txt AC 409 ms 19024 KB
21_rand17.txt AC 449 ms 19064 KB
21_rand18.txt AC 431 ms 19012 KB
21_rand19.txt AC 411 ms 19084 KB
21_rand20.txt AC 408 ms 19012 KB
21_rand21.txt AC 407 ms 19144 KB
21_rand22.txt AC 413 ms 19028 KB
21_rand23.txt AC 453 ms 19008 KB
21_rand24.txt AC 407 ms 19004 KB
31_rand00.txt AC 424 ms 19912 KB
31_rand01.txt AC 458 ms 21284 KB
31_rand02.txt AC 455 ms 20924 KB
31_rand03.txt AC 455 ms 21180 KB
31_rand04.txt AC 422 ms 19908 KB
31_rand05.txt AC 459 ms 21448 KB
31_rand06.txt AC 484 ms 21052 KB
31_rand07.txt AC 440 ms 20684 KB
31_rand08.txt AC 459 ms 21464 KB
31_rand09.txt AC 456 ms 21236 KB
31_rand10.txt AC 428 ms 19904 KB
31_rand11.txt AC 437 ms 20816 KB
31_rand12.txt AC 439 ms 20924 KB
31_rand13.txt AC 459 ms 21568 KB
31_rand14.txt AC 453 ms 19136 KB
31_rand15.txt AC 451 ms 21312 KB
31_rand16.txt AC 441 ms 20936 KB
31_rand17.txt AC 434 ms 20816 KB
31_rand18.txt AC 433 ms 20812 KB
31_rand19.txt AC 442 ms 20944 KB
31_rand20.txt AC 430 ms 20160 KB
31_rand21.txt AC 425 ms 20088 KB
31_rand22.txt AC 450 ms 21652 KB
31_rand23.txt AC 471 ms 21524 KB
31_rand24.txt AC 438 ms 20300 KB
41_rand00.txt AC 1047 ms 31968 KB
41_rand01.txt AC 838 ms 30884 KB
41_rand02.txt AC 1096 ms 32156 KB
41_rand03.txt AC 972 ms 32596 KB
41_rand04.txt AC 902 ms 32460 KB
41_rand05.txt AC 790 ms 33144 KB
41_rand06.txt AC 501 ms 22676 KB
41_rand07.txt AC 967 ms 32596 KB
41_rand08.txt AC 833 ms 29744 KB
41_rand09.txt AC 949 ms 32188 KB
41_rand10.txt AC 1197 ms 32160 KB
41_rand11.txt AC 1125 ms 32724 KB
41_rand12.txt AC 1095 ms 32984 KB
41_rand13.txt AC 1031 ms 32496 KB
41_rand14.txt AC 793 ms 31752 KB
41_rand15.txt AC 719 ms 32420 KB
41_rand16.txt AC 1038 ms 32136 KB
41_rand17.txt AC 881 ms 33420 KB
41_rand18.txt AC 408 ms 19136 KB
41_rand19.txt AC 433 ms 20688 KB
41_rand20.txt AC 414 ms 19136 KB
41_rand21.txt AC 429 ms 19660 KB
41_rand22.txt AC 471 ms 19136 KB
41_rand23.txt AC 4557 ms 39680 KB
41_rand24.txt AC 4568 ms 39980 KB