結果

問題 No.777 再帰的ケーキ
コンテスト
ユーザー tenten
提出日時 2020-12-16 18:34:59
言語 Java
(openjdk 25.0.2)
コンパイル:
javac -encoding UTF8 _filename_
実行:
java -ea -Xmx700m -Xss256M -DONLINE_JUDGE=true _class_
結果
AC  
実行時間 1,939 ms / 2,000 ms
コード長 1,819 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,391 ms
コンパイル使用メモリ 85,172 KB
実行使用メモリ 114,288 KB
最終ジャッジ日時 2026-04-08 16:56:14
合計ジャッジ時間 22,557 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		TreeMap<Integer, TreeMap<Integer, Integer>> cakes = new TreeMap<>();
		TreeMap<Integer, Integer> compress = new TreeMap<>();
		for (int i = 0; i < n; i++) {
		    int a = sc.nextInt();
		    int b = sc.nextInt();
		    int c = sc.nextInt();
		    if (!cakes.containsKey(a)) {
		        cakes.put(a, new TreeMap<>());
		    }
		    if (!cakes.get(a).containsKey(-b) || cakes.get(a).get(-b) < c) {
		        cakes.get(a).put(-b, c);
		        compress.put(b, 0);
		    }
		}
		int size = 1;
		for (int x : compress.keySet()) {
		    compress.put(x, size);
		    size++;
		}
		long max = 0;
		BinaryIndexedTree bit = new BinaryIndexedTree(size);
		for (TreeMap<Integer, Integer> one : cakes.values()) {
		    for (int x : one.keySet()) {
		        int idx = compress.get(-x);
		        long next = bit.getMax(idx - 1) + one.get(x);
		        max = Math.max(max, next);
		        bit.set(idx, next);
		    }
		}
		System.out.println(max);
	}
}

class BinaryIndexedTree {
    int size;
    long[] tree;
    
    public BinaryIndexedTree(int size) {
        this.size = size;
        tree = new long[size];
    }
    
    public void set(int idx, long value) {
        int mask = 1;
        while (idx < size) {
            if ((idx & mask) != 0) {
                tree[idx] = Math.max(tree[idx], value);
                idx += mask;
            }
            mask <<= 1;
        }
    }
    
    public long getMax(int x) {
        int mask = 1;
        long ans = 0;
        while (x > 0) {
            if ((x & mask) != 0) {
                ans = Math.max(ans, tree[x]);
                x -= mask;
            }
            mask <<= 1;
        }
        return ans;
    }
}
0