結果

問題 No.777 再帰的ケーキ
ユーザー 37zigen37zigen
提出日時 2018-10-07 04:26:54
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,704 bytes
コンパイル時間 2,531 ms
コンパイル使用メモリ 76,376 KB
実行使用メモリ 83,588 KB
最終ジャッジ日時 2023-08-12 06:52:18
合計ジャッジ時間 27,428 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
56,128 KB
testcase_01 AC 128 ms
55,864 KB
testcase_02 AC 127 ms
56,040 KB
testcase_03 AC 126 ms
56,016 KB
testcase_04 AC 125 ms
54,640 KB
testcase_05 AC 127 ms
56,176 KB
testcase_06 AC 131 ms
55,876 KB
testcase_07 AC 134 ms
55,940 KB
testcase_08 AC 134 ms
55,960 KB
testcase_09 AC 127 ms
56,192 KB
testcase_10 AC 125 ms
55,768 KB
testcase_11 AC 126 ms
56,116 KB
testcase_12 AC 126 ms
56,816 KB
testcase_13 AC 126 ms
55,800 KB
testcase_14 AC 128 ms
55,900 KB
testcase_15 AC 127 ms
55,840 KB
testcase_16 AC 123 ms
53,796 KB
testcase_17 AC 126 ms
56,108 KB
testcase_18 AC 131 ms
55,864 KB
testcase_19 AC 133 ms
55,880 KB
testcase_20 AC 136 ms
56,056 KB
testcase_21 AC 259 ms
60,560 KB
testcase_22 AC 256 ms
60,192 KB
testcase_23 AC 213 ms
59,080 KB
testcase_24 AC 214 ms
59,156 KB
testcase_25 AC 262 ms
59,980 KB
testcase_26 AC 264 ms
59,932 KB
testcase_27 AC 233 ms
59,444 KB
testcase_28 TLE -
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 1,229 ms
79,140 KB
testcase_33 AC 1,040 ms
72,996 KB
testcase_34 AC 1,151 ms
78,660 KB
testcase_35 TLE -
testcase_36 AC 1,559 ms
81,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		PrintWriter pw = new PrintWriter(System.out);
		int[][] v = new int[N][3];
		for (int i = 0; i < N; ++i) {
			v[i][0] = sc.nextInt();
			v[i][1] = sc.nextInt();
			v[i][2] = sc.nextInt();
			--v[i][0];
			--v[i][1];
		}
		compress(v);
		Arrays.sort(v, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				if (o1[0] != o2[0]) {
					return Integer.compare(o1[0], o2[0]);
				} else {
					return -Integer.compare(o1[1], o2[1]);
				}
			}
		});

		SegmentTree seg = new SegmentTree(N + 1);
		for (int i = 0; i < N; ++i) {
			long val = seg.query(0, v[i][1]);
			seg.update(v[i][1], val + v[i][2]);
		}
		pw.println(seg.query(0, N + 1));
		pw.close();
	}

	void compress(int[][] a) {
		int n = a.length;
		int[][] tmp = new int[n][4];
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < a[i].length; ++j) {
				tmp[i][j] = a[i][j];
			}
			tmp[i][3] = i;
		}
		Arrays.sort(tmp, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[0], o2[0]);
			}
		});

		int p = 0;
		for (int i = 0; i < n; ++i) {
			if (i + 1 < n && tmp[i][0] != tmp[i + 1][0]) {
				tmp[i][0] = p++;
			} else {
				tmp[i][0] = p;
			}
		}
		Arrays.sort(tmp, new Comparator<int[]>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				return Integer.compare(o1[1], o2[1]);
			}
		});
		p = 0;
		for (int i = 0; i < n; ++i) {
			if (i + 1 < n && tmp[i][1] != tmp[i + 1][1]) {
				tmp[i][1] = p++;
			} else {
				tmp[i][1] = p;
			}
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < 3; ++j) {
				a[tmp[i][3]][j] = tmp[i][j];
			}
		}
	}

	class SegmentTree {
		int n;
		long[] v;

		public SegmentTree(int n_) {
			n = 1;
			while (n < n_) {
				n *= 2;
			}
			v = new long[2 * n - 1];
		}

		void update(int k, long val) {
			k += n - 1;
			v[k] = Math.max(v[k], val);
			while (k > 0) {
				k = (k - 1) / 2;
				v[k] = Math.max(v[2 * k + 1], v[2 * k + 2]);
			}
		}

		long query(int a, int b) {
			return query(0, n, a, b, 0);
		}

		long query(int l, int r, int a, int b, int k) {
			if (r <= a || b <= l)
				return 0;
			else if (a <= l && r <= b)
				return v[k];
			else {
				return Math.max(query(l, (l + r) / 2, a, b, 2 * k + 1), query((l + r) / 2, r, a, b, 2 * k + 2));
			}
		}
	}

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

	public static void main(String[] args) throws FileNotFoundException {
		new Main().run();
	}
}
0