結果

問題 No.1144 Triangles
ユーザー 37zigen37zigen
提出日時 2020-08-04 18:11:15
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 5,118 bytes
コンパイル時間 2,564 ms
コンパイル使用メモリ 81,980 KB
実行使用メモリ 60,148 KB
最終ジャッジ日時 2023-10-12 21:22:40
合計ジャッジ時間 45,106 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
50,020 KB
testcase_01 AC 44 ms
49,744 KB
testcase_02 AC 44 ms
49,656 KB
testcase_03 AC 44 ms
49,712 KB
testcase_04 TLE -
testcase_05 AC 2,918 ms
59,304 KB
testcase_06 TLE -
testcase_07 AC 2,900 ms
59,328 KB
testcase_08 AC 2,959 ms
58,632 KB
testcase_09 AC 44 ms
49,716 KB
testcase_10 AC 44 ms
50,184 KB
testcase_11 AC 44 ms
49,760 KB
testcase_12 AC 44 ms
49,664 KB
testcase_13 AC 43 ms
49,708 KB
testcase_14 AC 2,881 ms
59,456 KB
testcase_15 AC 2,833 ms
59,376 KB
testcase_16 TLE -
testcase_17 AC 2,956 ms
59,704 KB
testcase_18 TLE -
testcase_19 AC 332 ms
57,580 KB
testcase_20 AC 94 ms
53,016 KB
testcase_21 AC 95 ms
53,232 KB
testcase_22 AC 2,846 ms
59,340 KB
testcase_23 AC 408 ms
57,892 KB
testcase_24 AC 2,971 ms
59,652 KB
testcase_25 AC 1,712 ms
59,252 KB
testcase_26 AC 254 ms
57,688 KB
testcase_27 AC 99 ms
53,080 KB
testcase_28 AC 1,815 ms
59,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.awt.geom.Line2D;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Random;

public class Main {
	
	public static void main(String[] args) {
		new Main().run();
	}
	
	final long MOD=(long)1e9+7;

	long pow(long a,long n) {
		return n!=0?pow(a*a%MOD,n/2)*(n%2==1?a:1)%MOD:1;
	}
	
	long inv(long a) {
		return pow(a,MOD-2);
	}
	
	long ADD(long a,long b) {
		long ret=a+b;
		if (ret<0) ret+=MOD;
		return ret<MOD?ret:ret-MOD;
	}
	
	int quadrant(long x,long y) {
		if (x==0 && y==0) return -1;
		if (y>=0 && x> 0) return 0;
		if (y> 0 && x<=0) return 1;
		if (y<=0 && x< 0) return 2;
		if (y< 0 && x>=0) return 3;
		throw new AssertionError();
	}
	
	
	void run() {
		FastScanner sc=new FastScanner();
		int n=sc.nextInt();
		long[][] a=new long[n][2];
		for (int i=0;i<n;++i) {
			a[i][0]=sc.nextLong();
			a[i][1]=sc.nextLong();
		}
		System.out.println(solve(n, a));
//		rnd();
	}
	
	void rnd() {
		while (true) {
			Random seed=new Random();
			int n=10;
			long[][] a=new long[n][2];
			for (int i=0;i<n;++i) {
				a[i][0]=seed.nextInt(10)-5;
				a[i][1]=seed.nextInt(10)-5;
			}
			long ans1=solve(n,a);
			long ans2=brute(n,a);
			if (ans1!=ans2) {
				System.out.println("ans1 "+ans1);
				System.out.println("ans2 "+ans2);
				tr(a);
				return;
			}
		}
	}
	
	long brute(int n,long[][] a) {
		long ret=0;
		for (int i=0;i<n;++i) {
			for (int j=i+1;j<n;++j) {
				for (int k=j+1;k<n;++k) {
					long x1=a[j][0]-a[i][0];
					long y1=a[j][1]-a[i][1];
					long x2=a[k][0]-a[i][0];
					long y2=a[k][1]-a[i][1];
					long add=x1*y2-x2*y1;
					if (add<0) add=MOD-add%MOD;
					add%=MOD;
					ret=(ret+add)%MOD;
				}
			}
		}
		return ret;
	}
	
	long solve(int n,long[][] a) {
		long[] sumx=new long[2*(n-1)+1];
		long[] sumy=new long[2*(n-1)+1];
		long ans=0;
		for (int i=0;i<n;++i) {
			int zero=0;
			for (int j=0;j<n;++j) if (a[j][0]-a[i][0]==0&&a[j][1]-a[i][1]==0) ++zero;
			long[][] p=new long[n-zero][2];
			int sz=0;
			{
				for (int j=0;j<n;++j) {
					long dx=a[j][0]-a[i][0];
					long dy=a[j][1]-a[i][1];
					if (dx==0 && dy==0) continue;
					p[sz][0]=dx;
					p[sz][1]=dy;
					++sz;
				}
			}
			Arrays.sort(p,new Comparator<long[]>() {
				@Override
				public int compare(long[] o1, long[] o2) {
					if (quadrant(o1[0], o1[1])==quadrant(o2[0], o2[1])) {
						return Line2D.relativeCCW(0, 0, o1[0], o1[1], o2[0], o2[1]);
					} else {
						return Integer.compare(quadrant(o1[0], o1[1]), quadrant(o2[0], o2[1]));
					}
				}
			});
			
			for (int j=1;j<=2*p.length;++j) {
				sumx[j]=ADD(p[(j-1)%sz][0],sumx[j-1]);
				sumy[j]=ADD(p[(j-1)%sz][1],sumy[j-1]);
			}
			int t=0;
			for (int j=0;j<sz;++j) {
				t=Math.max(t, j+1);
				long x0=p[j][0];
				long y0=p[j][1];
				assert(x0!=0||y0!=0);
				while (t!=j+sz) {
					if ((p[j][0]*p[t%sz][1]==p[j][1]*p[t%sz][0])) {
						if (p[j][0]*p[(t+sz-1)%sz][1]==p[j][1]*p[(t+sz-1)%sz][0]) {
							++t;
						}
						else break;
					} else if (Line2D.relativeCCW(0, 0, p[j][0], p[j][1], p[t%sz][0], p[t%sz][1])<=0) {
						++t;					
					} else {
						break;
					}
				}
				long dsx=sumx[t]-sumx[j+1];
				long dsy=sumy[t]-sumy[j+1];
				long add=x0*dsy%MOD-y0*dsx%MOD;
				if (add<0) add+=MOD;
				ans=ADD(ans,add);
			}
		}
		return ans*inv(3)%MOD;
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	class FastScanner {
		private final InputStream in = System.in;
		private final byte[] buffer = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;

		private boolean hasNextByte() {
			if (ptr < buflen) {
				return true;
			} else {
				ptr = 0;
				try {
					buflen = in.read(buffer);
				} catch (IOException e) {
					e.printStackTrace();
				}
				if (buflen <= 0) {
					return false;
				}
			}
			return true;
		}

		private int readByte() {
			if (hasNextByte())
				return buffer[ptr++];
			else
				return -1;
		}

		private boolean isPrintableChar(int c) {
			return 33 <= c && c <= 126;
		}

		private void skipUnprintable() {
			while (hasNextByte() && !isPrintableChar(buffer[ptr]))
				ptr++;
		}

		public boolean hasNext() {
			skipUnprintable();
			return hasNextByte();
		}

		public String next() {
			if (!hasNext())
				throw new NoSuchElementException();
			StringBuilder sb = new StringBuilder();
			int b = readByte();
			while (isPrintableChar(b)) {
				sb.appendCodePoint(b);
				b = readByte();
			}
			return sb.toString();
		}

		public long nextLong() {
			if (!hasNext())
				throw new NoSuchElementException();
			long n = 0;
			boolean minus = false;
			int b = readByte();
			if (b == '-') {
				minus = true;
				b = readByte();
			}
			if (b < '0' || '9' < b) {
				throw new NumberFormatException();
			}
			while (true) {
				if ('0' <= b && b <= '9') {
					n *= 10;
					n += b - '0';
				} else if (b == -1 || !isPrintableChar(b)) {
					return minus ? -n : n;
				} else {
					throw new NumberFormatException();
				}
				b = readByte();
			}
		}

		public int nextInt() {
			return (int) nextLong();
		}
	}
}
0