結果
問題 | No.325 マンハッタン距離2 |
ユーザー |
|
提出日時 | 2015-12-19 20:34:17 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 55 ms / 1,000 ms |
コード長 | 3,107 bytes |
コンパイル時間 | 3,900 ms |
コンパイル使用メモリ | 79,556 KB |
実行使用メモリ | 50,660 KB |
最終ジャッジ日時 | 2024-09-17 10:47:41 |
合計ジャッジ時間 | 5,664 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
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);}}}