結果

問題 No.325 マンハッタン距離2
ユーザー uwiuwi
提出日時 2015-12-18 01:20:54
言語 Java21
(openjdk 21)
結果
AC  
実行時間 54 ms / 1,000 ms
コード長 5,284 bytes
コンパイル時間 4,055 ms
コンパイル使用メモリ 88,960 KB
実行使用メモリ 37,096 KB
最終ジャッジ日時 2024-09-16 08:19:56
合計ジャッジ時間 6,329 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,052 KB
testcase_01 AC 52 ms
37,088 KB
testcase_02 AC 53 ms
37,096 KB
testcase_03 AC 52 ms
36,832 KB
testcase_04 AC 52 ms
36,684 KB
testcase_05 AC 53 ms
36,908 KB
testcase_06 AC 53 ms
36,920 KB
testcase_07 AC 53 ms
36,880 KB
testcase_08 AC 52 ms
36,556 KB
testcase_09 AC 52 ms
36,780 KB
testcase_10 AC 52 ms
36,576 KB
testcase_11 AC 50 ms
36,852 KB
testcase_12 AC 52 ms
36,912 KB
testcase_13 AC 52 ms
36,996 KB
testcase_14 AC 52 ms
36,712 KB
testcase_15 AC 51 ms
36,764 KB
testcase_16 AC 52 ms
36,852 KB
testcase_17 AC 54 ms
37,024 KB
testcase_18 AC 51 ms
36,916 KB
testcase_19 AC 52 ms
36,796 KB
testcase_20 AC 51 ms
36,916 KB
testcase_21 AC 51 ms
36,888 KB
testcase_22 AC 52 ms
36,764 KB
testcase_23 AC 51 ms
36,880 KB
testcase_24 AC 53 ms
36,968 KB
testcase_25 AC 51 ms
37,096 KB
testcase_26 AC 51 ms
36,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Q477 {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		long x1 = ni(), y1 = ni(), x2 = ni(), y2 = ni();
		long d = ni();
		long[][] c = new long[][]{
				{x1, y1}, {x2, y1}, {x2, y2}, {x1, y2}
		};
		c = convexCut(c, d+1, 1, 0, -d);
		c = convexCut(c, 1, -d-1, -d, 0);
		c = convexCut(c, -d-1, -1, 0, d);
		c = convexCut(c, -1, d+1, d, 0);
		if(c.length == 0){
			out.println(0);
			return;
		}
		
		long S2 = areaPoly2(c);
		long onEdge = onEdge(c);
		long inner = (S2-onEdge+2)/2;
		out.println(inner + onEdge);
	}
	
	public static long onEdge(long[][] co)
	{
		int n = co.length;
		long s = 0;
		for(int i = 0, j = 1;i < n;i++,j++){
			if(j == n)j = 0;
			long dx = Math.abs(co[i][0]-co[j][0]);
			long dy = Math.abs(co[i][1]-co[j][1]);
			if(dx == 0 && dy == 0){
				continue;
			}
			long g = gcd(dx, dy);
			s += g;
		}
		return s;
	}
	
	public static long gcd(long a, long b) {
		while (b > 0) {
			long c = a;
			a = b;
			b = c % b;
		}
		return a;
	}
	
	
	public static long areaPoly2(long[][] co)
	{
		int n = co.length;
		long s = 0;
		for(int i = 0, j = 1;i < n;i++,j++){
			if(j == n)j = 0;
			s += (long)co[i][0]*co[j][1]-(long)co[j][0]*co[i][1];
		}
		return Math.abs(s);
	}

	
	public static long[][] convexCut(long[][] c, long ax, long ay, long bx, long by)
	{
		int n = c.length;
		long[] ccw = new long[n];
		int m = 2;
		for(int i = 0;i < n;i++){
			ccw[i] = (c[i][0]-ax)*(by-ay)-(bx-ax)*(c[i][1]-ay);
			if(ccw[i] >= 0)m++;
		}
		
		long[][] ret = new long[m][];
		int p = 0;
		for(int i = 0, j = 1;i < n;i++, j++){
			if(j == n)j = 0;
			if(ccw[i] >= 0){
				ret[p++] = c[i];
			}
			if(ccw[i] > 0 && ccw[j] < 0 || ccw[i] < 0 && ccw[j] > 0){
				long[] il = interLinesS(ax, ay, bx, by, c[i][0], c[i][1], c[j][0], c[j][1]);
				if(il != null){
					long g = gcd(Math.abs(il[0]), il[2]);
					il[0] /= g; il[2] /= g;
					long cx = ax + il[0] * (bx-ax) / il[2];
					long cy = ay + il[0] * (by-ay) / il[2];
					ret[p++] = new long[]{cx, cy};
				}
			}
		}
		return Arrays.copyOf(ret, p);
	}
	
	public static long[] interLinesS(long ax, long ay, long bx, long by, long cx, long cy, long dx, long dy)
	{
		long aa = bx - ax;
		long cc = by - ay;
		long bb = cx - dx;
		long dd = cy - dy;
		long xx = cx - ax;
		long yy = cy - ay;
		long det = aa * dd - bb * cc;
		if(det == 0)return null;
		long tnum = dd*xx-bb*yy;
		long unum = -cc*xx+aa*yy;
		if(det < 0){
			det = -det;
			tnum = -tnum;
			unum = -unum;
		}
//		if(tnum < 0 || tnum > det || unum < 0 || unum > det)return null;
		return new long[]{
				tnum,
				unum,
				det
		};
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
	}
	
	public static void main(String[] args) throws Exception { new Q477().run(); }
	
	private byte[] inbuf = new byte[1024];
	private int lenbuf = 0, ptrbuf = 0;
	
	private 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 boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private 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 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 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 int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private 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 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) { System.out.println(Arrays.deepToString(o)); }
}
0