RE 0pts求助
  • 板块P1001 A+B Problem
  • 楼主lfxxx_
  • 当前回复24
  • 已保存回复24
  • 发布时间2024/7/25 12:42
  • 上次更新2024/7/31 17:10:55
查看原帖
RE 0pts求助
795344
lfxxx_楼主2024/7/25 12:42

rt

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n=2;
int tr[N<<2];
void pushup(int p){tr[p]=tr[p<<1]+tr[p<<1|1];}
void update(int p,int pl,int pr,int x,int d)
{
	if(pl==x&&pr==x)
	{
		tr[p]+=d;
		return ;
	}
	int mid=(pl+pr)>>1;
	update(p<<1,pl,mid,x,d);
	update(p<<1|1,mid+1,pr,x,d);
}
int query(int p,int pl,int pr,int L,int R)
{
	if(L<=pl&&pr<=R)
		return tr[p];
	if(R<pl||pr<L)
		return 0;
	int mid=(pl+pr)>>1;
	return query(p<<1,pl,mid,L,R)+query(p<<1|1,mid+1,pr,L,R);
}
vector<int>edge[N];
int son[N],sz[N],fa[N],dep[N];
int dfn[N],rnk[N],top[N],id;
void dfs1(int u,int ft)
{
	sz[u]=1;
	fa[u]=ft;
	dep[u]=dep[ft]+1;
	son[u]=-1;
	for(auto &v:edge[u])
	{
		if(v==ft)
			continue;
		dfs1(v,u);
		sz[u]+=sz[v];
		if(sz[v]>sz[son[u]])
			son[u]=v;
	}
}
void dfs2(int u,int t)
{
	top[u]=t;
	dfn[u]=++id;
	rnk[id]=u;
	dfs2(son[u],t);
	for(auto &v:edge[u])
	{
		if(v==fa[u])
			continue;
		dfs2(v,v);
	}
}
int qrange(int x,int y)
{
	int res=0;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]])
			swap(x,y);
		res+=query(1,1,n,dfn[top[x]],dfn[x]);
		x=fa[top[x]];
	}
	return res+query(1,1,n,dfn[x],dfn[y]);
}
signed main()
{
	int x,y;
	cin>>x>>y;
	edge[1].emplace_back(2);
	edge[2].emplace_back(1);
	dfs1(1,0);
	dfs2(1,1);
	update(1,1,n,dfn[1],x);
	update(1,1,n,dfn[2],y);
	cout<<qrange(1,2);
}
2024/7/25 12:42
加载中...