結果

問題 No.2325 Skill Tree
ユーザー pengin_2000pengin_2000
提出日時 2023-05-28 14:25:38
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 262 ms / 3,000 ms
コード長 2,347 bytes
コンパイル時間 1,181 ms
コンパイル使用メモリ 30,220 KB
実行使用メモリ 6,592 KB
最終ジャッジ日時 2023-08-27 09:45:14
合計ジャッジ時間 9,750 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 48 ms
4,376 KB
testcase_08 AC 58 ms
4,760 KB
testcase_09 AC 65 ms
4,376 KB
testcase_10 AC 89 ms
5,156 KB
testcase_11 AC 83 ms
4,932 KB
testcase_12 AC 167 ms
5,748 KB
testcase_13 AC 168 ms
5,748 KB
testcase_14 AC 167 ms
5,688 KB
testcase_15 AC 168 ms
5,692 KB
testcase_16 AC 168 ms
5,732 KB
testcase_17 AC 164 ms
5,688 KB
testcase_18 AC 165 ms
5,800 KB
testcase_19 AC 164 ms
5,824 KB
testcase_20 AC 165 ms
5,824 KB
testcase_21 AC 165 ms
5,732 KB
testcase_22 AC 167 ms
5,728 KB
testcase_23 AC 168 ms
5,728 KB
testcase_24 AC 166 ms
5,732 KB
testcase_25 AC 165 ms
5,812 KB
testcase_26 AC 165 ms
5,696 KB
testcase_27 AC 252 ms
6,524 KB
testcase_28 AC 253 ms
6,528 KB
testcase_29 AC 251 ms
6,512 KB
testcase_30 AC 255 ms
6,540 KB
testcase_31 AC 254 ms
6,572 KB
testcase_32 AC 235 ms
6,416 KB
testcase_33 AC 237 ms
6,592 KB
testcase_34 AC 241 ms
6,588 KB
testcase_35 AC 254 ms
6,404 KB
testcase_36 AC 252 ms
6,524 KB
testcase_37 AC 262 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
int h[200005], hl;
int l[200005], a[200005];
int comp_h(int x, int y,int z)
{
	if (z == 0)
	{
		if (a[h[x]] > a[h[y]])
			return 1;
		else
			return -1;
	}
	else
	{
		if (h[x] > h[y])
			return 1;
		else
			return -1;
	}
}
void swap_h(int x, int y)
{
	int f = h[x];
	h[x] = h[y];
	h[y] = f;
	return;
}
void push(int ne, int z)
{
	h[hl] = ne;
	int p = hl;
	hl++;
	for (; p > 0; p = (p - 1) / 2)
		if (comp_h((p - 1) / 2, p, z) > 0)
			swap_h((p - 1) / 2, p);
	return;
}
int pop(int z)
{
	hl--;
	swap_h(0, hl);
	int p = 0;
	for (;;)
	{
		if (2 * p + 2 < hl)
		{
			if (comp_h(2 * p + 1, 2 * p + 2, z) > 0)
			{
				if (comp_h(p, 2 * p + 2, z) > 0)
					swap_h(p, 2 * p + 2);
				p = 2 * p + 2;
			}
			else
			{
				if (comp_h(p, 2 * p + 1, z) > 0)
					swap_h(p, 2 * p + 1);
				p = 2 * p + 1;
			}
		}
		else if (2 * p + 1 < hl)
		{
			if (comp_h(p, 2 * p + 1, z) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return h[hl];
}
int c[200005];
int maxlevel[200005];
int stack[200005], ss;
int sorted_maxlevel[200005];
int main()
{
	int n;
	scanf("%d", &n);
	int i;
	for (i = 1; i < n; i++)
	{
		scanf("%d %d", &l[i], &a[i]);
		a[i]--;
	}
	l[0] = a[0] = 0;
	hl = 0;
	for (i = 0; i < n; i++)
		push(i, 0);
	for (i = 0; i < n; i++)
		c[i] = pop(0);
	c[n] = n;
	a[n] = -1;
	for (i = 0; i < n; i++)
		maxlevel[i] = -1;
	maxlevel[0] = 0;
	stack[0] = 0;
	ss = 1;
	int min, mid, max;
	while (ss > 0)
	{
		ss--;
		i = stack[ss];
		min = -1;
		max = n;
		while (max - min > 1)
		{
			mid = (max + min) / 2;
			if (a[c[mid]] < i)
				min = mid;
			else
				max = mid;
		}
		for (; a[c[max]] == i; max++)
		{
			if (maxlevel[c[max]] >= 0)
				continue;
			maxlevel[c[max]] = maxlevel[i];
			if (maxlevel[i] < l[c[max]])
				maxlevel[c[max]] = l[c[max]];
			stack[ss] = c[max];
			ss++;
		}
	}
	for (i = 0; i < n; i++)
		push(maxlevel[i], 1);
	ss = 0;
	for (i = 0; i < n; i++)
	{
		sorted_maxlevel[ss] = pop(1);
		if (sorted_maxlevel[ss] >= 0)
			ss++;
	}
	int q;
	scanf("%d", &q);
	int t, x;
	for (; q > 0; q--)
	{
		scanf("%d %d", &t, &x);
		if (t == 1)
		{
			min = -1;
			max = ss;
			while (max - min > 1)
			{
				mid = (max + min) / 2;
				if (sorted_maxlevel[mid] > x)
					max = mid;
				else
					min = mid;
			}
			printf("%d\n", max);
		}
		else
		{
			printf("%d\n", maxlevel[x - 1]);
		}
	}
	return 0;
}
0