結果

問題 No.325 マンハッタン距離2
ユーザー GrenacheGrenache
提出日時 2015-12-19 20:34:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 59 ms / 1,000 ms
コード長 3,107 bytes
コンパイル時間 4,099 ms
コンパイル使用メモリ 79,708 KB
実行使用メモリ 53,820 KB
最終ジャッジ日時 2023-10-17 12:42:46
合計ジャッジ時間 6,547 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 58 ms
53,800 KB
testcase_01 AC 57 ms
53,804 KB
testcase_02 AC 57 ms
53,792 KB
testcase_03 AC 57 ms
53,792 KB
testcase_04 AC 57 ms
53,804 KB
testcase_05 AC 57 ms
53,800 KB
testcase_06 AC 56 ms
53,792 KB
testcase_07 AC 56 ms
53,804 KB
testcase_08 AC 56 ms
53,820 KB
testcase_09 AC 57 ms
53,804 KB
testcase_10 AC 57 ms
53,800 KB
testcase_11 AC 56 ms
52,684 KB
testcase_12 AC 57 ms
53,796 KB
testcase_13 AC 58 ms
52,696 KB
testcase_14 AC 58 ms
53,800 KB
testcase_15 AC 58 ms
52,764 KB
testcase_16 AC 58 ms
53,800 KB
testcase_17 AC 57 ms
53,792 KB
testcase_18 AC 57 ms
53,804 KB
testcase_19 AC 56 ms
53,792 KB
testcase_20 AC 56 ms
53,800 KB
testcase_21 AC 59 ms
53,804 KB
testcase_22 AC 56 ms
53,800 KB
testcase_23 AC 56 ms
53,792 KB
testcase_24 AC 58 ms
53,808 KB
testcase_25 AC 57 ms
52,672 KB
testcase_26 AC 58 ms
53,796 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Iterator;


public class Main_yukicoder325 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        long x1 = sc.nextInt();
        long y1 = sc.nextInt();
        long x2 = sc.nextInt();
        long y2 = sc.nextInt();
        long d = sc.nextInt();

        if (x1 > 0) {
        	x2 -= x1;
        	d -= x1;
        	x1 -= x1;
        }
        if (y1 > 0) {
        	y2 -= y1;
        	d -= y1;
        	y1 -= y1;
        }
        if (x2 < 0) {
        	x1 -= x2;
        	d -= Math.abs(x2);
        	x2 -= x2;
        }
        if (y2 < 0) {
        	y1 -= y2;
        	d -= Math.abs(y2);
        	y2 -= y2;
        }

        if (d < 0) {
        	pr.println(0);

        	pr.close();
        	sc.close();
        	return;
        }

        long ret = 0;
        ret += getArea(0, 0, Math.abs(x2), Math.abs(y2), d);
        ret += getArea(0, 0, Math.abs(x1), Math.abs(y2), d);
        ret += getArea(0, 0, Math.abs(x1), Math.abs(y1), d);
        ret += getArea(0, 0, Math.abs(x2), Math.abs(y1), d);
        ret -= getArea(0, 0, 0, Math.abs(y2), d);
        ret -= getArea(0, 0, Math.abs(x1), 0, d);
        ret -= getArea(0, 0, 0, Math.abs(y1), d);
        ret -= getArea(0, 0, Math.abs(x2), 0, d);

        pr.println(ret + 1);

        pr.close();
        sc.close();
    }

    private static long getArea(long x1, long y1, long x2, long y2, long d) {
    	if (x2 + y2 <= d) {
    		return (x2 + 1) * (y2 + 1);
    	} if (x2 >= d && y2 >= d) {
    		return (d + 1) * (d + 2) / 2;
    	} if (x2 >= d) {
    		return (d + 1) * (d + 2) / 2 - (d - y2) * (d - y2 + 1) / 2;
    	} if (y2 >= d) {
    		return (d + 1) * (d + 2) / 2 - (d - x2) * (d - x2 + 1) / 2;
    	} else {
    		return (x2 + 1) * (y2 + 1) - (x2 + y2 - d) * (x2 + y2 - d + 1) / 2;
    	}
	}

    @SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0