結果

問題 No.325 マンハッタン距離2
ユーザー uwiuwi
提出日時 2015-12-18 01:07:57
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,165 bytes
コンパイル時間 4,287 ms
コンパイル使用メモリ 86,124 KB
実行使用メモリ 50,492 KB
最終ジャッジ日時 2024-09-16 08:18:47
合計ジャッジ時間 6,271 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
37,120 KB
testcase_01 AC 52 ms
36,820 KB
testcase_02 AC 50 ms
37,052 KB
testcase_03 AC 52 ms
36,724 KB
testcase_04 AC 52 ms
36,748 KB
testcase_05 AC 52 ms
37,000 KB
testcase_06 AC 52 ms
36,792 KB
testcase_07 AC 52 ms
37,052 KB
testcase_08 AC 52 ms
36,916 KB
testcase_09 AC 53 ms
36,648 KB
testcase_10 AC 52 ms
37,072 KB
testcase_11 AC 53 ms
36,712 KB
testcase_12 AC 53 ms
36,936 KB
testcase_13 AC 54 ms
36,924 KB
testcase_14 AC 52 ms
36,844 KB
testcase_15 AC 54 ms
36,772 KB
testcase_16 AC 52 ms
37,052 KB
testcase_17 AC 53 ms
36,744 KB
testcase_18 AC 56 ms
36,888 KB
testcase_19 AC 51 ms
36,596 KB
testcase_20 AC 50 ms
37,120 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 52 ms
37,096 KB
testcase_24 AC 53 ms
37,120 KB
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

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();
		int 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]);
			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 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