結果

問題 No.1623 三角形の制作
ユーザー ks2mks2m
提出日時 2021-07-23 21:42:31
言語 Java19
(openjdk 21)
結果
AC  
実行時間 637 ms / 2,000 ms
コード長 2,461 bytes
コンパイル時間 2,266 ms
コンパイル使用メモリ 75,544 KB
実行使用メモリ 84,952 KB
最終ジャッジ日時 2023-09-25 21:36:32
合計ジャッジ時間 13,111 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
51,320 KB
testcase_01 AC 140 ms
51,336 KB
testcase_02 AC 509 ms
68,696 KB
testcase_03 AC 490 ms
65,880 KB
testcase_04 AC 483 ms
67,556 KB
testcase_05 AC 596 ms
84,952 KB
testcase_06 AC 299 ms
56,460 KB
testcase_07 AC 492 ms
66,964 KB
testcase_08 AC 313 ms
58,916 KB
testcase_09 AC 473 ms
65,780 KB
testcase_10 AC 544 ms
76,160 KB
testcase_11 AC 488 ms
65,784 KB
testcase_12 AC 366 ms
60,732 KB
testcase_13 AC 389 ms
64,748 KB
testcase_14 AC 378 ms
64,160 KB
testcase_15 AC 631 ms
84,560 KB
testcase_16 AC 630 ms
84,280 KB
testcase_17 AC 637 ms
83,832 KB
testcase_18 AC 606 ms
83,604 KB
testcase_19 AC 454 ms
65,392 KB
testcase_20 AC 140 ms
51,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		int m = 3001;
		String[] sa = br.readLine().split(" ");
		int[] r = new int[m];
		for (int i = 0; i < n; i++) {
			int a = Integer.parseInt(sa[i]);
			r[a]++;
		}
		sa = br.readLine().split(" ");
		int[] g = new int[m];
		for (int i = 0; i < n; i++) {
			int a = Integer.parseInt(sa[i]);
			g[a]++;
		}
		sa = br.readLine().split(" ");
		FenwickTree ft = new FenwickTree(m);
		for (int i = 0; i < n; i++) {
			int a = Integer.parseInt(sa[i]);
			ft.add(a, 1);
		}
		br.close();

		long ans = 0;
		for (int i = 1; i < m; i++) {
			long val = 0;
			for (int j = 1; j <= i; j++) {
				long v = ft.sum(i - j + 1, i + 1);
				val += g[j] * v;
			}
			ans += r[i] * val;
		}
		System.out.println(ans);
	}
}

class FenwickTree {
	private int n;
	private long[] data;

	/**
	 * 長さnの配列(a[0]~a[n-1])を作る。初期値は全て0。<br>
	 * O(n)
	 * 
	 * @param n 配列の長さ
	 */
	public FenwickTree(int n) {
		this.n = n;
		data = new long[n];
	}

	/**
	 * 初期データを元にFenwick Treeを構成する。<br>
	 * O(n)
	 * 
	 * @param data 初期データ
	 */
	public FenwickTree(long[] data) {
		this(data.length);
		build(data);
	}

	/**
	 * a[p] += x を行う。<br>
	 * O(log n)
	 * 
	 * @param p 加算位置(0≦p<n)
	 * @param x 加算値
	 */
	void add(int p, long x) {
		assert 0 <= p && p < n : "p=" + p;

		p++;
		while (p <= n) {
			data[p - 1] += x;
			p += p & -p;
		}
	}

	/**
	 * a[l] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param l 開始位置(含む)    (0≦l≦r≦n)
	 * @param r 終了位置(含まない)(0≦l≦r≦n)
	 */
	long sum(int l, int r) {
		assert 0 <= l && l <= r && r <= n : "l=" + l + ", r=" + r;

		return sum(r) - sum(l);
	}

	/**
	 * a[0] + ... + a[r-1] を返す。<br>
	 * O(log n)
	 * 
	 * @param r 終了位置(含まない)(0≦r≦n)
	 */
	long sum(int r) {
		assert 0 <= r && r <= n : "r=" + r;

		long s = 0;
		while (r > 0) {
			s += data[r - 1];
			r -= r & -r;
		}
		return s;
	}

	private void build(long[] dat) {
		System.arraycopy(dat, 0, data, 0, n);
		for (int i = 1; i <= n; i++) {
			int p = i + (i & -i);
			if (p <= n) {
				data[p - 1] += data[i - 1];
			}
		}
	}
}
0