結果

問題 No.1637 Easy Tree Query
ユーザー pengin_2000pengin_2000
提出日時 2021-08-06 21:52:28
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,208 bytes
コンパイル時間 1,839 ms
コンパイル使用メモリ 31,684 KB
実行使用メモリ 6,620 KB
最終ジャッジ日時 2023-10-17 05:07:30
合計ジャッジ時間 7,149 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 120 ms
6,620 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 66 ms
5,224 KB
testcase_05 AC 73 ms
4,860 KB
testcase_06 AC 50 ms
4,632 KB
testcase_07 AC 15 ms
4,348 KB
testcase_08 AC 50 ms
4,932 KB
testcase_09 AC 115 ms
5,964 KB
testcase_10 AC 37 ms
4,500 KB
testcase_11 AC 137 ms
6,216 KB
testcase_12 AC 110 ms
5,624 KB
testcase_13 AC 29 ms
4,532 KB
testcase_14 AC 107 ms
6,024 KB
testcase_15 AC 139 ms
6,324 KB
testcase_16 AC 144 ms
6,548 KB
testcase_17 AC 33 ms
4,528 KB
testcase_18 AC 70 ms
4,852 KB
testcase_19 AC 79 ms
5,028 KB
testcase_20 AC 109 ms
5,564 KB
testcase_21 AC 77 ms
5,260 KB
testcase_22 AC 162 ms
6,588 KB
testcase_23 AC 38 ms
4,656 KB
testcase_24 AC 73 ms
5,272 KB
testcase_25 AC 70 ms
4,788 KB
testcase_26 AC 116 ms
5,736 KB
testcase_27 AC 161 ms
6,488 KB
testcase_28 AC 57 ms
4,728 KB
testcase_29 AC 96 ms
5,468 KB
testcase_30 AC 74 ms
5,452 KB
testcase_31 AC 29 ms
4,372 KB
testcase_32 AC 52 ms
4,812 KB
testcase_33 AC 56 ms
4,512 KB
testcase_34 AC 85 ms
6,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<stdio.h>
int a[200005], b[200005];
int p[100005], depth[100005];
int h[200005], l;
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 (depth[h[x]] < depth[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[l] = ne;
	int p = l;
	l++;
	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)
{
	l--;
	swap_h(0, l);
	int p = 0;
	for (;;)
	{
		if (2 * p + 2 < l)
		{
			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 < l)
		{
			if (comp_h(p, 2 * p + 1, z) > 0)
				swap_h(p, 2 * p + 1);
			p = 2 * p + 1;
		}
		else
			break;
	}
	return h[l];
}
int main()
{
	int n, q;
	scanf("%d %d", &n, &q);
	int i;
	for (i = 0; i < n - 1; i++)
	{
		i *= 2;
		scanf("%d %d", &a[i], &b[i]);
		a[i]--;
		b[i]--;
		a[i + 1] = b[i];
		b[i + 1] = a[i];
		i /= 2;
	}
	l = 0;
	for (i = 0; i < 2 * n - 2; i++)
		push(i, 0);
	int c[200005];
	for (i = 0; i < 2 * n - 2; i++)
		c[i] = pop(0);
	c[2 * n - 2] = 2 * n - 2;
	a[2 * n - 2] = -1;
	int min, mid, max;
	for (i = 0; i < n; i++)
		p[i] = -1;
	p[0] = 0;
	depth[0] = 0;
	h[0] = 0;
	l = 1;
	while (l > 0)
	{
		l--;
		i = h[l];
		min = -1;
		max = 2 * n - 2;
		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 (p[b[c[max]]] >= 0)
				continue;
			p[b[c[max]]] = i;
			depth[b[c[max]]] = depth[i] + 1;
			h[l] = b[c[max]];
			l++;
		}
	}
	long long int cnt[100005];
	for (i = 0; i < n; i++)
		cnt[i] = 0;
	l = 0;
	for (i = 0; i < n; i++)
		push(i, 1);
	while (l > 0)
	{
		i = pop(1);
		cnt[i]++;
		if (i > 0)
			cnt[p[i]] += cnt[i];
	}
	long long int pp, xx;
	long long int ans = 0;
	for (i = 0; i < q; i++)
	{
		scanf("%lld %lld", &pp, &xx);
		ans += cnt[pp - 1] * xx;
		printf("%lld\n", ans);
	}
	return 0;
}
0