WA on #2,3,4,5,求调

P3038 [USACO11DEC] Grass Planting G

Cupricion @ 2024-10-02 14:02:17

#include<bits/stdc++.h>
using namespace std;
int n,m;  
#define ls (n<<1)
#define rs (n<<1|1)
int res=0;
int sum[800010],laz[800010];
int to[200010],st=0,nxt[200010],beg[100010],w[100010],wt[100010];
int f[100010],dep[100010],son[100010],id[100010],siz[100010],cnt,top[100010];
void add(int x,int y)
{
    to[++st]=y;
    nxt[st]=beg[x];
    beg[x]=st;
}
void pushup(int n)
{
    sum[n]=(sum[ls]+sum[rs]);
}
void pushdown(int n,int l,int r)
{
    int mid=(l+r)>>1;
    laz[ls]+=laz[n];
    laz[rs]+=laz[n];
    sum[ls]+=laz[n]*(mid-l+1);
    sum[rs]+=laz[n]*(r-mid);
    sum[ls]=sum[ls];
    sum[rs]=sum[rs];
    laz[n]=0;
}
void build(int n,int l,int r)
{
    if(l==r)
    {
        sum[n]=wt[l];
        return;
    }
    int mid=(l+r)>>1;
    build(ls,l,mid);
    build(rs,mid+1,r);
    pushup(n);
}
void change(int n,int l,int r,int ll,int rr,int k)
{
    if(ll<=l&&r<=rr)
    {
        laz[n]=(laz[n]+k);
        sum[n]=(sum[n]+k*(r-l+1));
        return; 
    }
    if(laz[n]) 
        pushdown(n,l,r);
    int mid=(l+r)>>1;
    if(ll<=mid) 
        change(ls,l,mid,ll,rr,k);
    if(rr>mid) 
        change(rs,mid+1,r,ll,rr,k);
    pushup(n);
}
void query(int n,int l,int r,int ll,int rr)
{
    if(ll<=l&&rr>=r)
    {
        res=(res+sum[n]);
        return;
    }
    if(laz[n]) 
        pushdown(n,l,r);
    int mid=(l+r)>>1;
    if(ll<=mid) 
        query(ls,l,mid,ll,rr);
    if(rr>mid) 
        query(rs,mid+1,r,ll,rr);
}
int qr(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]])
            swap(x,y);
        res=0;
        query(1,1,n,id[top[x]],id[x]);
        ans+=res;
        ans=ans;
        x=f[top[x]]; 
    }
    if(dep[x]>dep[y])
        swap(x,y);
    res=0;
    query(1,1,n,id[x]+1,id[y]);
    ans+=res;
    return ans;
} 
void changer(int x,int y,int k)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) 
            swap(x,y);
        change(1,1,n,id[top[x]],id[x],k);
        x=f[top[x]];
    }
    if(dep[x]>dep[y]) 
        swap(x,y);
    change(1,1,n,id[x]+1,id[y],k);
}
void dfs1(int u,int father)
{
    dep[u]=dep[father]+1;
    f[u]=father;
    siz[u]=1;
    for(int i=beg[u];i;i=nxt[i])
    {
        int y=to[i];
        if(y==father) 
            continue;
        wt[y]=w[i];
        dfs1(y,u);
        siz[u]+=siz[y];
        if(siz[son[u]]<siz[y]) 
            son[u] = y; 
    }
}
void dfs2(int u,int tp)
{
    id[u]=++cnt;
    top[u]=tp;
    if(!son[u]) 
        return;
    dfs2(son[u],tp);
    for(int i=beg[u];i;i=nxt[i])
    {
        int y=to[i];
        if(y==f[u]||y==son[u])  
            continue;
        dfs2(y,y);
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//  freopen("P3038_2.in","r",stdin);
//  freopen("P3038.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        add(x,y);
        add(y,x);
    }
    dfs1(1,0);
    dfs2(1,1);
    build(1,1,n);
    while(m--)
    {
        char o;
        cin>>o;
        if(o=='P')
        {
            int x,y,z=1;
            cin>>x>>y;
            changer(x,y,z);
        }
        else if(o=='Q')
        {
            int x,y;
            cin>>x>>y;
            cout<<qr(x,y)<<'\n';
        }
    }
    return 0;
}

by ivyjiao @ 2024-10-02 14:25:32

@Cupricion 这题没让你建树,你把build删了试试

还有 ans=ans; 是啥意思?


by ivyjiao @ 2024-10-02 14:26:05

还有

sum[ls]=sum[ls];
sum[rs]=sum[rs];

by Cupricion @ 2024-10-02 15:13:27

@ivyjiao

因为这个代码是书剖板子改的,以前要取模,现在我把取模删了

另外,已过,拜谢


|