牛客寒训营2 G(复习LCA了)

题目
题意: 给定n个点,每个点高度为ai。用n-1条边连接这n个点构成一棵树,规定如果从高度高的点跳向高度低的点,花费为0;从高度低的点跳向高度高的点,花费为高度的差值。现在有m次询问,从地面(地面高度为0)跳向点u,再从u跳向点v,求最小花费。
思路: 求LCA,发现光求个dist不够用,因为从下往上跳和从上往下跳不一样。分别预处理从两个方向跳到根的距离。 这样,我们就可以先让u向上跳到LCA处,之后再从LCA跳到v,距离用dist[] - dist[LCA]得到.
时间复杂度: O((n+m)logn)
代码:

// Problem: 小沙的身法
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/23477/G
// Memory Limit: 524288 MB
// Time Limit: 6000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
int a[M];
int h[M],e[M<<1],ne[M<<1],w[M<<1],idx = 0;
int lg[M];
int fa[M][23];
int depth[M];
ll dist[M],dist2[M];
void add(int a,int b)
{
	e[idx] = b,ne[idx] = h[a],h[a] = idx++;
}
void dfs(int cur,int father)
{
	fa[cur][0] = father; 
	depth[cur] = depth[father]+1;
	
	for(int i=1;i<=lg[depth[cur]];++i)
	{
		fa[cur][i] = fa[fa[cur][i-1]][i-1];
	}
    for(int i=h[cur];~i;i=ne[i])
    {
    	int j = e[i];
    	if(j == father) continue;
        dist[j] = dist[cur] + max(0,a[cur] - a[j]) ; //i跳到根
        dist2[j] = dist2[cur] + max(0,a[j] - a[cur]); //根跳到i
    	dfs(j,cur);
    }
}
int LCA(int x,int y)
{
	if(depth[x] < depth[y]) swap(x,y);
	while(depth[x] > depth[y])
	{
		x = fa[x][lg[depth[x]-depth[y]]-1];
	}
	if(x == y) return x;
	for(int k=lg[depth[x]]-1;k>=0;--k)
	{
		if(fa[x][k] != fa[y][k])
		{
			x = fa[x][k],y = fa[y][k];
		}
	}
	return fa[x][0];
}
void solve()
{
   mem(h,-1); idx = 0;
   read(n); read(m);
   for(int i=1;i<=n;++i) read(a[i]),lg[i] = lg[i>>1] + 1;
   for(int i=0;i<n-1;++i)
   {
   	 int x,y; read(x),read(y);
   	 add(x,y); add(y,x);
   }
   dfs(1,0);
   // for(int i=1;i<=n;++i) cout<<i<<":"<<dist[i]<<endl;
   // for(int i=1;i<=n;++i) cout<<i<<":"<<dist2[i]<<endl;
   while(m--)
   {
   	 int x,y; read(x),read(y);
   	 ll ans = a[x];
   	 int tmp = LCA(x,y);
   	 ans += dist[x] - dist[tmp] + dist2[y] - dist2[tmp]; //先跳到LCA,再跳到目标点.
   	 write(ans); puts("");
   }
}
signed main(void)
{  
   T = 1;
   // OldTomato; cin>>T;
   // read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值