結果

問題 No.777 再帰的ケーキ
ユーザー 37zigen37zigen
提出日時 2018-10-07 20:51:01
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,880 ms / 2,000 ms
コード長 4,628 bytes
コンパイル時間 3,894 ms
コンパイル使用メモリ 80,084 KB
実行使用メモリ 65,664 KB
最終ジャッジ日時 2024-04-29 21:22:22
合計ジャッジ時間 18,173 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
36,908 KB
testcase_01 AC 53 ms
36,652 KB
testcase_02 AC 53 ms
36,852 KB
testcase_03 AC 54 ms
36,768 KB
testcase_04 AC 53 ms
36,828 KB
testcase_05 AC 53 ms
36,496 KB
testcase_06 AC 55 ms
36,888 KB
testcase_07 AC 54 ms
36,992 KB
testcase_08 AC 54 ms
36,620 KB
testcase_09 AC 53 ms
36,600 KB
testcase_10 AC 55 ms
37,004 KB
testcase_11 AC 54 ms
36,776 KB
testcase_12 AC 53 ms
36,548 KB
testcase_13 AC 55 ms
36,532 KB
testcase_14 AC 58 ms
36,852 KB
testcase_15 AC 53 ms
36,552 KB
testcase_16 AC 53 ms
36,832 KB
testcase_17 AC 54 ms
36,772 KB
testcase_18 AC 53 ms
36,852 KB
testcase_19 AC 54 ms
36,992 KB
testcase_20 AC 56 ms
37,012 KB
testcase_21 AC 90 ms
38,164 KB
testcase_22 AC 86 ms
38,012 KB
testcase_23 AC 64 ms
37,360 KB
testcase_24 AC 60 ms
36,980 KB
testcase_25 AC 100 ms
38,292 KB
testcase_26 AC 99 ms
37,900 KB
testcase_27 AC 69 ms
37,036 KB
testcase_28 AC 1,880 ms
65,284 KB
testcase_29 AC 1,856 ms
64,052 KB
testcase_30 AC 1,812 ms
64,736 KB
testcase_31 AC 1,826 ms
65,504 KB
testcase_32 AC 386 ms
61,268 KB
testcase_33 AC 319 ms
56,328 KB
testcase_34 AC 334 ms
61,188 KB
testcase_35 AC 1,836 ms
65,664 KB
testcase_36 AC 412 ms
61,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Scanner;

class Main {

	void run() {
		Scanner sc = new Scanner();
		int N = sc.nextInt();
		if (!(1 <= N && N <= 200000))
			throw new AssertionError();
		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();
			for (int j = 0; j < 3; ++j)
				if (!(1 <= v[i][j] && v[i][j] <= 1e9))
					throw new AssertionError();
			--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();
	}

	class Scanner {
		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