結果

問題 No.3134 二分探索木
ユーザー ID 21712
提出日時 2025-04-30 02:37:48
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,072 ms / 2,000 ms
コード長 1,811 bytes
コンパイル時間 2,977 ms
コンパイル使用メモリ 81,020 KB
実行使用メモリ 94,160 KB
最終ジャッジ日時 2025-04-30 02:38:04
合計ジャッジ時間 14,390 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 15
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.lang.*;
import java.util.*;

class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		int N = Integer.parseInt(in.readLine());
		
		int[] B = new int[N], C = new int[N];
		
		TreeMap<Key, Item> map = new TreeMap<>();
		
		Item item = new Item(1, N, 0);
		map.put(item.key, item);
		
		String[] nums = in.readLine().split(" ");
		for (int i = 0; i < N; i++) {
			int a = Integer.parseInt(nums[i]);
			item = map.remove(new Key(a, a));
			B[i] = item.depth;
			C[i] = item.key.ub - item.key.lb;
			for (Item tmp : item.split(a)) {
				map.put(tmp.key, tmp);
			}
		}
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintStream out = new PrintStream(baos);
		
		for (int i = 0; i < N; i++) {
			if (i > 0) out.print(" ");
			out.print(B[i]);
		}
		out.println();

		for (int i = 0; i < N; i++) {
			if (i > 0) out.print(" ");
			out.print(C[i]);
		}
		out.println();
		
		System.out.write(baos.toByteArray());
	}
}

class Key implements Comparable<Key> {
	int lb, ub;
	Key(int lb, int ub) {
		this.lb = lb;
		this.ub = ub;
	}
	public int compareTo(Key o) {
		if (this.ub < o.lb) {
			return -1;
		} else if (o.ub < this.lb) {
			return 1;
		} else {
			return 0;
		}
	}
}

class Item {
	Key key;
	int depth;
	Item(int lb, int ub, int depth) {
		this.key = new Key(lb, ub);
		this.depth = depth;
	}
	Item[] split(int a) {
		if (key.lb == a && key.ub == a) {
			return new Item[0];
		} else if (key.lb == a) {
			return new Item[] { new Item(a+1, key.ub, depth+1) };
		} else if (key.ub == a) {
			return new Item[] { new Item(key.lb, a-1, depth+1) };
		} else {
			return new Item[] {
				new Item(key.lb, a-1, depth+1),
				new Item(a+1, key.ub, depth+1)
			};
		}
	}
	
}
0