RE 0pts
查看原帖
RE 0pts
805551
scj_juruo楼主2023/1/26 18:59

直接拿板子改的,结果全部RE。

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,r,root,pmod,cnt,val[100001],head[100001],fa[100001],depth[100001],son[100001],sz[100001],top[100001],id[100001],rk[100001];
struct node{int l,r,ls,rs,sum,lazy;}a[800001];
struct fake{int next,to;}e[800001];
void add(int u,int v)
{
	e[++cnt].next=head[u];
	e[cnt].to=v;
	head[u]=cnt;
}
void dfs1(int x)
{
	sz[x]=1;
	depth[x]=depth[fa[x]]+1;
    for(int i=head[x];i;i=e[i].next)
    {
    	int v=e[i].to;
        if(v!=fa[x])
        {
            fa[v]=x;
			dfs1(v);
			sz[x]+=sz[v];
            if(sz[son[x]]<sz[v])
                son[x]=v;
        }
    }
}
void dfs2(int x,int tp)
{
	top[x]=tp;
	id[x]=++cnt;
	rk[cnt]=x;
    if(son[x])
        dfs2(son[x],tp);
	for(int i=head[x];i;i=e[i].next)
	{
		int v=e[i].to;
        if(v!=fa[x]&&v!=son[x])
            dfs2(v,v);
    }
}
int len(int x)
{
	return a[x].r-a[x].l+1;	
}
void up(int x)
{
	a[x].sum=(a[a[x].ls].sum+a[a[x].rs].sum)%pmod;
}
void down(int x)
{
	 if(a[x].lazy)
	 {
        int ls=a[x].ls,rs=a[x].rs,lz=a[x].lazy;
        (a[ls].lazy+=lz)%=pmod;
		(a[rs].lazy+=lz)%=pmod;
        (a[ls].sum+=lz*len(ls))%=pmod;
		(a[rs].sum+=lz*len(rs))%=pmod;
        a[x].lazy=0;
     }
}
void build(int l,int r,int x)
{
	if(l==r)
	{
        a[x].sum=val[rk[l]];
		a[x].l=a[x].r=l;
        return;
	}
    int mid=(l+r)/2;
    a[x].ls=cnt++;
	a[x].rs=cnt++;
	build(l,mid,a[x].ls);
	build(mid+1,r,a[x].rs);
	a[x].l=a[a[x].ls].l;
	a[x].r=a[a[x].rs].r;
    up(x);
}
void update(int l,int r,int z,int x)
{
	if(a[x].l>=l && a[x].r<=r)
	{
        (a[x].lazy+=z)%=pmod;
		(a[x].sum+=len(x)*z)%=pmod;
        return;
    }   
    down(x);
	int mid=(a[x].l+a[x].r)/2;
    if(mid>=l)
        update(l,r,z,a[x].ls);
	if(mid<r)
        update(l,r,z,a[x].rs);
	up(x);
}
void change(int x,int y,int z)
{
	while(top[x]!=top[y])
    {
        if(depth[top[x]]<depth[top[y]])
            swap(x,y);
        update(id[top[x]],id[x],z,root);
        x=fa[top[x]];
    }
    if(id[x]>id[y])
        swap(x,y);   
	update(id[x],id[y],z,root);
}
int query(int l,int r,int x)
{
	if(a[x].l>=l&&a[x].r<=r)
		return a[x].sum;
	down(x);
	int mid=(a[x].l+a[x].r)/2,tot=0;
	if(mid>=l)
		tot+=query(l,r,a[x].ls);
	if(r>mid) 
		tot+=query(l,r,a[x].rs);
	return tot%pmod;
	
}
int sum(int x,int y)
{
	int ans=0;
	while(top[x]!=top[y])
	{
		if(depth[top[x]]<depth[top[y]])
			swap(x,y);
		(ans+=query(id[top[x]],id[x],root))%=pmod;
		x=fa[top[x]]; 
	}
	if(id[x]>id[y])
		swap(x,y);
	return (ans+query(id[x],id[y],root))%pmod;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		add(u,v);
		add(v,u);
	}
	cnt=0;
	dfs1(1);
	dfs2(1,1);
	cnt=0;
	build(1,n,root=cnt++);
	while(m--)
	{
		char op;
		cin>>op;
		if(op=='P')
		{
			int x,y;
			cin>>x>>y;
			change(x,y,1);
		}
		else if(op=='Q')
		{
			int x,y;
			cin>>x>>y;
			cout<<sum(x,y)<<endl;
		}
	}
	return 0;
}
2023/1/26 18:59
加载中...