結果

問題 No.1623 三角形の制作
ユーザー ks2mks2m
提出日時 2021-07-23 21:42:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 677 ms / 2,000 ms
コード長 2,461 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 84,976 KB
実行使用メモリ 75,244 KB
最終ジャッジ日時 2024-07-18 17:21:07
合計ジャッジ時間 12,933 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 161 ms
38,708 KB
testcase_01 AC 158 ms
38,548 KB
testcase_02 AC 494 ms
56,860 KB
testcase_03 AC 497 ms
55,476 KB
testcase_04 AC 483 ms
57,004 KB
testcase_05 AC 624 ms
74,600 KB
testcase_06 AC 295 ms
46,172 KB
testcase_07 AC 499 ms
56,416 KB
testcase_08 AC 313 ms
47,432 KB
testcase_09 AC 491 ms
56,300 KB
testcase_10 AC 559 ms
65,020 KB
testcase_11 AC 536 ms
55,500 KB
testcase_12 AC 363 ms
50,836 KB
testcase_13 AC 440 ms
55,072 KB
testcase_14 AC 391 ms
55,048 KB
testcase_15 AC 640 ms
75,220 KB
testcase_16 AC 636 ms
74,732 KB
testcase_17 AC 616 ms
74,688 KB
testcase_18 AC 677 ms
75,244 KB
testcase_19 AC 491 ms
55,480 KB
testcase_20 AC 163 ms
38,564 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