萌新求救
查看原帖
萌新求救
291706
Unordered_OIer楼主2021/7/27 21:43
#include<bits/stdc++.h>
#define N 100005
using namespace std;
typedef long long ll;
inline ll read(){
	ll x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}
inline void write(ll x){
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
inline void writeln(ll x){write(x),putchar('\n');}
struct node{ll l,r,sum,tag;}st[N<<2];
struct edge{ll to,nxt;}es[N<<1];
ll n,m,tot,hd[N],sz[N];
ll son[N],d[N],p[N],top[N];
ll dfn[N],rk[N],cnt;
inline void add(ll u,ll v){es[++tot]=(edge){v,hd[u]},hd[u]=tot;}
inline ll len(ll x){return st[x].r-st[x].l+1;}
inline void build(ll x,ll l,ll r){
	st[x]=(node){l,r,0,0};
	if(l==r)return;
	ll mid=(l+r)>>1;
	build(x<<1,l,mid);
	build(x<<1|1,mid+1,r);
}
inline void pushup(ll x){
	st[x].sum=st[x<<1].sum+st[x<<1|1].sum;
}
inline void pushdown(ll x){
	st[x<<1].tag+=st[x].tag;
	st[x<<1].sum+=len(x)*st[x].tag;
	st[x<<1|1].tag+=st[x].tag;
	st[x<<1|1].sum+=len(x)*st[x].tag;
	st[x].tag=0;
}
inline void update(ll l,ll r,ll k,ll x){
	if(st[x].l>r||st[x].r<l)return;
	if(st[x].l>=l&&st[x].r<=r){
		st[x].tag+=k;
		st[x].sum+=len(x)*k;
		return;
	}
	pushdown(x);
	update(l,r,k,x<<1);
	update(l,r,k,x<<1|1);
	pushup(x);
}
inline ll query(ll k,ll x){
	if(st[x].l==st[x].r)return st[x].sum;
	if(k<=st[x<<1].r)return query(k,x<<1);
	else return query(k,x<<1|1);
}
inline void dfs1(ll u,ll fa){
	d[u]=d[fa]+1,sz[u]=1,p[u]=fa;
	for(ll i=hd[u];i;i=es[i].nxt){
		ll v=es[i].to;
		if(v==fa)continue;
		dfs1(v,u);
		sz[u]+=sz[v];
		if(sz[son[u]]<sz[v])son[u]=v;
	}
}
inline void dfs2(ll u,ll tp){
	top[u]=tp,dfn[u]=++cnt,rk[cnt]=u;
	if(son[u])dfs2(son[u],tp);
	for(ll i=hd[u];i;i=es[i].nxt){
		ll v=es[i].to;
		if(v==son[u]||v==p[u])continue;
		dfs2(v,v);//新重链
	}
}
inline void updPath(ll x,ll y,ll c){
	while(top[x]!=top[y]){
		if(d[top[x]]<d[top[y]])swap(x,y);
		update(dfn[top[x]],dfn[x],c,1);
		x=p[top[x]];
	}
	if(d[x]>d[y])swap(x,y);
	update(dfn[x]+1,dfn[y],c,1);
}
int main(){
	n=read(),m=read();
	for(ll i=1,u,v;i<n;i++){
		u=read(),v=read();
		add(u,v),add(v,u);
	}
	dfs1(1,0),dfs2(1,1);
	build(1,1,n);
	for(ll i=1,u,v;i<=m;i++){
		char op;
		cin>>op;
		u=read(),v=read();
		if(op=='P')updPath(u,v,1);
		else writeln(p[u]==v?query(dfn[u],1):query(dfn[v],1));
	}
	return 0;
}

WA 成 31pts(

/kel

2021/7/27 21:43
加载中...