結果

問題 No.325 マンハッタン距離2
ユーザー ぴろずぴろず
提出日時 2015-12-20 17:58:50
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,938 bytes
コンパイル時間 2,972 ms
コンパイル使用メモリ 85,716 KB
実行使用メモリ 57,772 KB
最終ジャッジ日時 2023-10-17 14:32:00
合計ジャッジ時間 7,420 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
55,028 KB
testcase_01 AC 132 ms
57,192 KB
testcase_02 AC 132 ms
57,680 KB
testcase_03 AC 130 ms
57,116 KB
testcase_04 AC 133 ms
57,080 KB
testcase_05 WA -
testcase_06 AC 131 ms
57,344 KB
testcase_07 WA -
testcase_08 AC 128 ms
57,608 KB
testcase_09 AC 129 ms
57,232 KB
testcase_10 AC 131 ms
57,672 KB
testcase_11 WA -
testcase_12 AC 132 ms
57,576 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 145 ms
57,740 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 132 ms
57,348 KB
testcase_24 AC 132 ms
57,152 KB
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package no325;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long x1 = sc.nextLong();
		long y1 = sc.nextLong();
		long x2 = sc.nextLong();
		long y2 = sc.nextLong();
		long d = sc.nextLong();
		Vector2[] sq1 = {new Vector2(x1,y1),new Vector2(x2,y1),new Vector2(x2,y2),new Vector2(x1,y2)};
		Vector2[] sq2 = {new Vector2(d,0),new Vector2(0,d),new Vector2(-d,0),new Vector2(0,-d)};
		TreeSet<Long> ee = new TreeSet<>();
		for(int i=0;i<4;i++) {
			for(int j=0;j<4;j++) {
				Vector2 is = Vector2.intersect2(sq1[i], sq1[(i+1)%4], sq2[i], sq2[(i+1)%4]);
				long x = Math.round(is.x);
				if (x1 <= x && x <= x2) {
					ee.add(Math.round(is.x));
				}
			}
		}
		ArrayList<Long> e = new ArrayList<>(ee);
//		System.out.println(e);
		long sum = 0;
		for(int i=0;i<e.size()-1;i++) {
			long xa = e.get(i);
			long xb = e.get(i+1);
			long c1 = count(xa,y1,y2,d);
			long c2 = count(xb,y1,y2,d);
//			System.out.println("[" + xa + "," + xb + "):" + c1 + "->" + c2 + ":" + ((c1 + c2) * (xb - xa + 1) / 2 - c2));
			sum += (c1 + c2) * (xb - xa + 1) / 2 - c2;
		}
		sum += count(e.get(e.size()-1),y1,y2,d);
		System.out.println(sum);
	}
	
	//(a,b) (a=x,y1<=b<=y2) に、原点からマンハッタン距離がdイカの点がいくつ存在するか数える 
	public static long count(long x,long y1,long y2,long d) {
		long dy = d - Math.abs(x);
		if (dy < 0) {
			return 0;
		}
		if (y1 > dy || y2 < -dy) {
			return 0;
		}
		return Math.min(dy, y2) - Math.max(-dy, y1) + 1;
	}

}
class Vector2 {
	public double x;
	public double y;
	public Vector2(double x,double y) {
		this.x = x;
		this.y = y;
	}
	public double dot(Vector2 v) {
		return this.x*v.x+this.y*v.y;
	}
	public double cross(Vector2 v) {
		return this.x*v.y-this.y*v.x;
	}
	public double norm() {
		return Math.sqrt(this.x*this.x+this.y*this.y);
	}
	public Vector2 normalize() {
		return divide(norm());
	}
	public Vector2 add(Vector2 v) {
		return new Vector2(x+v.x,y+v.y);
	}
	public Vector2 subtract(Vector2 v) {
		return new Vector2(x-v.x,y-v.y);
	}
	public Vector2 multiply(double k) {
		return new Vector2(x*k,y*k);
	}
	public Vector2 divide(double k) {
		return new Vector2(x/k,y/k);
	}
	public Vector2 rotate90() {
		return new Vector2(-y,x);
	}
	public Vector2 rotate270() {
		return new Vector2(y,-x);
	}
	public static Vector2 intersect(Vector2 r1,Vector2 d1,Vector2 r2,Vector2 d2) {
		return r1.add(d1.multiply(-d2.cross(r2.subtract(r1)) / d1.cross(d2)));
	}
	public static Vector2 intersect2(Vector2 v11, Vector2 v12, Vector2 v21, Vector2 v22) {
		return Vector2.intersect(v11, v12.subtract(v11), v21, v22.subtract(v21));
	}
	public static double dist(Vector2 r1,Vector2 r2,Vector2 p) {
		return r2.subtract(r1).normalize().cross(p.subtract(r1));
	}
	public String toString() {
		return "(" + this.x + "," + this.y + ")";
	}
}
0