求助
查看原帖
求助
143925
吴勉之楼主2020/6/21 12:25

调了N久,但还是2RE(#2#10),其他都AC

#include<bits/stdc++.h>
using namespace std;
const int N=100005;
void rd(auto &x)
{
	x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
}
int n,head[N],tot; 
struct edge
{
	int v,to;
}e[N<<1];
void add(int u,int v)
{
	e[++tot].v=v;
	e[tot].to=head[u];
	head[u]=tot;
}
int dfn,dep[N],id[N],top[N],fa[N],son[N],sz[N];
void dfs1(int u,int f,int dp)
{
	fa[u]=f;
	sz[u]=1;
	dep[u]=dp;
	for(int i=head[u];i;i=e[i].to)
	{
		int v=e[i].v;
		dfs1(v,u,dp+1);
		sz[u]+=sz[v];
		if(sz[son[u]]<sz[v])son[u]=v;
	}
}
void dfs2(int u,int tp)
{
	top[u]=tp;
	id[u]=++dfn;
	if(!son[u])return;
	dfs2(son[u],tp);
	for(int i=head[u];i;i=e[i].to)
	{
		int v=e[i].v;
		if(v!=son[u])dfs2(v,v);
	}
}
long long tag[N<<3],sum[N<<3];
int ls(int x){return x<<1;}
int rs(int x){return x<<1|1;}
void pushup(int x){sum[x]=sum[ls(x)]+sum[rs(x)];}
void pushdown(int x,int l,int r)
{
	int len=r-l+1;
	sum[ls(x)]+=tag[x]*(len-(len>>1));
	sum[rs(x)]+=tag[x]*(len>>1);
	tag[ls(x)]+=tag[x];
	tag[rs(x)]+=tag[x];
	tag[x]=0;
}
void upd(int x,int l,int r,int L,int R,int w)
{
	if(L<=l&&r<=R)
	{
		tag[x]+=w;
		sum[x]+=(r-l+1)*w;
		return;
	}
	if(tag[x])pushdown(x,l,r);
	int mid=l+r>>1;
	if(L<=mid)upd(ls(x),l,mid,L,R,w);
	if(R>mid)upd(rs(x),mid+1,r,L,R,w);
	pushup(x);
}
void Qupd(int u,int v,int w)
{
	while(top[u]!=top[v])
	{
		if(dep[id[u]]<dep[id[v]])swap(u,v);
		upd(1,1,n,id[top[u]],id[u],w);
		u=fa[top[u]];
	}
	if(dep[u]>dep[v])swap(u,v);
	upd(1,1,n,id[u],id[v],w);
}
long long query(int x,int l,int r,int L,int R)
{
	if(L<=l&&r<=R)return sum[x];
	int mid=l+r>>1;
	long long res=0;
	if(tag[x])pushdown(x,l,r);
	if(L<=mid)res+=query(ls(x),l,mid,L,R);
	if(R>mid)res+=query(rs(x),mid+1,r,L,R);
	return res;
}
int m,u,v;
long long w;
char c;
int main()
{
	rd(n);
	for(int i=1;i<n;i++)
	{
		rd(u),rd(v);
		add(u+1,v+1);
	}
	dfs1(1,0,1);
	dfs2(1,1);
	//for(int i=1;i<=n;i++)printf("%d %d %d\n",id[i],top[i],dep[i]);
	rd(m);
	while(m--)
	{
		cin>>c;
		rd(u);
		u++;
		if(c=='A')
		{
			rd(v);rd(w);
			Qupd(u,v+1,w);
		}
		else printf("%lld\n",query(1,1,n,id[u],id[u]+sz[u]-1));
		//printf("%c %d\n",c,u);
	}
	return 0;
}
2020/6/21 12:25
加载中...