HDU 5266 pog loves szh III

本文深入探讨了游戏开发领域的关键技术,包括游戏引擎、编程语言、硬件优化等,并重点介绍了AI音视频处理在游戏中的应用,如语音识别、图像处理、AR特效等。同时,文章还涉及了自动化测试、性能优化等现代开发流程,旨在为游戏开发者提供全面的技术指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

Pog and Szh are playing games. Firstly Pog draw a tree on the paper. Here we define 1 as the root of the tree. Then Szh choose some nodes from the tree. He wants Pog helps to find the least common ancestor(LCA) of these node. The question is too difficult for Pog. So he decided to simplify the problems. The nodes picked are consecutive numbers from li to ri ([li,ri]).

Hint : You should be careful about stack overflow !

Input

Several groups of data (no more than 3 groups,n10000 or Q10000).

The following line contains ans integers,n(2n300000).

AT The following n1 line, two integers are bi and ci at every line, it shows an edge connecting bi and ci.

The following line contains ans integers,Q(Q300000).

AT The following Q line contains two integers li and ri(1lirin).

Output

For each case,output Q integers means the LCA of [li,ri].

Sample Input
5
1 2
1 3
3 4
4 5
5
1 2
2 3
3 4
3 5
1 5
Sample Output
1
1
3
3
1


Hint

Be careful about stack overflow.

lca+rmq

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<string>
using namespace std;
const int maxn = 300005;
int n, m, x, y, lc[maxn][20], dp[maxn][20], deep[maxn];
vector<int> t[maxn];

void bfs()
{
	queue<int> p;
	p.push(1);	deep[1] = 1;
	while (!p.empty())
	{
		int q = p.front();	p.pop();
		for (int i = 0; i < t[q].size(); i++)
		if (t[q][i] != lc[q][0])
		{
			lc[t[q][i]][0] = q;
			deep[t[q][i]] = deep[q] + 1;
			p.push(t[q][i]);
		}
	}
}

int lca(int a, int b)
{
	int i, j;
	if (deep[a]<deep[b]) swap(a, b);
	for (i = 0; (1 << i) <= deep[a]; i++);
	i--;
	for (j = i; j >= 0; j--)
		if (deep[a] - (1 << j) >= deep[b])
			a = lc[a][j];
	if (a == b)return a;
	for (j = i; j >= 0; j--)
	{
		if (lc[a][j] != -1 && lc[a][j] != lc[b][j])
		{
			a = lc[a][j];
			b = lc[b][j];
		}
	}
	return lc[a][0];
}

int query(int s, int v)
{
	int k = (int)(log((v - s + 1)*1.0) / log(2.0));
	return lca(dp[s][k], dp[v - (1 << k) + 1][k]);
}

int main()
{
	while (cin >> n)
	{
		memset(dp, -1, sizeof(dp));
		memset(lc, -1, sizeof(lc));
		for (int i = 1; i <= n; i++) 
		{ 
			t[i].clear(); 
			dp[i][0] = i; 
			deep[i] = 0;
		}
		for (int i = 1; i < n; i++)
		{
			scanf("%d%d", &x, &y);
			t[x].push_back(y); 
			t[y].push_back(x);
		}
		bfs();
		
		for (int j = 1; (1 << j) <= n; j++)
			for (int i = 1; i <= n; i++)
				if (lc[i][j - 1] != -1) lc[i][j] = lc[lc[i][j - 1]][j - 1];
		
		for (int j = 1; (1 << j) <= n; j++)
			for (int i = 1; i <= n; i++)
			if (i + (1 << j) - 1 <= n)
			{
				dp[i][j] = lca(dp[i][j - 1], dp[i + (1 << (j - 1))][j - 1]);
			}

		cin >> m;
		while (m--)
		{
			scanf("%d%d", &x, &y);
			printf("%d\n", query(x, y));
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值