谢谢

P3384 【模板】重链剖分/树链剖分

123wbl @ 2024-08-22 21:41:59

调崩溃了,据说这样会来一些大佬,玄关 目前看来大概率是线段树写挂了,但不知道哪里挂了 样例第二个输出23,

#include<iostream>
#include<cstdio>
#include<cmath>
#define len tree[p].r-tree[p].l+1
using namespace std;
struct edge
{
    int to,next;
}e[2000001];
struct node
{
    int l,r,res,add;
}tree[2000001];
int n,m,root,mod,a[200001],tot,head[200001],b[200001];
int son[200001],dep[200001],cnt,siz[200001],top[200001],id[200001],fa[200001];
inline void add(int u,int v)
{
    e[++tot].to=v;
    e[tot].next=head[u];
    head[u]=tot;
}
inline void pushdown(int p,int lenn)
{
    tree[p*2].add+=tree[p].add;
    tree[p*2+1].add+=tree[p].add;
    tree[p*2].res+=tree[p].add*(lenn-(lenn>>1));
    tree[p*2+1].res+=tree[p].add*(lenn>>1);
    tree[p*2].res%=mod;
    tree[p*2+1].res%=mod;
    tree[p].add=0;
}
inline void build(int p,int l,int r)
{
    tree[p].l=l,tree[p].r=r;
    if(l==r)
    {
        tree[p].res=b[l]%mod;
        return;
    }
    int mid=(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    tree[p].res=(tree[p*2].res+tree[p*2+1].res)%mod;
}
inline int query(int p,int x,int y)
{
    int ans=0;
    if(x<=tree[p].l&&tree[p].r<=y)
        return tree[p].res;
    else
    {
        pushdown(p,len);
        int mid=(tree[p].l+tree[p].r)>>1;
        if(x<=mid) ans+=query(p*2,x,y);
        if(y>mid) ans+=query(p*2+1,x,y);
        return ans%mod;
    }
}
inline void update(int p,int x,int y,int k)
{
    if(x<=tree[p].l&&tree[p].r<=y)
        tree[p].add+=k,tree[p].res+=k*len;
    else
    {
        pushdown(p,len);
        int mid=(tree[p].l+tree[p].r)>>1;
        if(x<=mid) update(p*2,x,y,k);
        if(y>mid) update(p*2+1,x,y,k);
        tree[p].res=(tree[p*2].res+tree[p*2+1].res)%mod;
    }
}
inline int qrange(int x,int y)
{
    int ans=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        ans+=query(1,id[top[x]],id[x]);
        ans%=mod;
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    ans+=query(1,id[x],id[y]);
    return ans%mod;
}
inline void uprange(int x,int y,int k)
{
    k%=mod;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        update(1,id[top[x]],id[x],k);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    update(1,id[x],id[y],k);
}
inline int qson(int x)
{
    return query(1,id[x],id[x]+siz[x]-1);
}
inline void upson(int x,int k)
{
    update(1,id[x],id[x]+siz[x]-1,k);
}
inline void dfs1(int u,int f,int deep)
{
    dep[u]=deep;
    fa[u]=f;
    siz[u]=1;
    int maxson=-1;
    for(int i=head[u];i;i=e[i].next)
    {
        int v=e[i].to;
        if(v==f) continue;
        dfs1(v,u,deep+1);
        siz[u]+=siz[v];
        if(siz[v]>maxson) son[u]=v,maxson=siz[v];
    }
}
inline void dfs2(int u,int topf)
{
    id[u]=++cnt;
    b[cnt]=a[u];
    top[u]=topf;
    if(!son[u]) return;
    dfs2(son[u],topf);
    for(int i=head[u];i;i=e[i].next)
    {
        int v=e[i].to;
        if(v==son[u]||v==fa[u]) continue;
        dfs2(v,v);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m>>root>>mod;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n-1;i++)
    {
        int x,y;
        cin>>x>>y;
        add(x,y);
        add(y,x);
    }
    dfs1(root,0,1);
    dfs2(root,root);
    build(1,1,n);
    while(m--)
    {
        int op,x,y,z;
        cin>>op;
        if(op==1)
            cin>>x>>y>>z,uprange(x,y,z);
        if(op==2)
            cin>>x>>y,cout<<qrange(x,y)<<'\n';
        if(op==3)
            cin>>x>>y,upson(x,y);
        if(op==4)
            cin>>x,cout<<qson(x)<<'\n'; 
    }
}

by 123wbl @ 2024-10-02 09:43:04

define len没加括号,此贴结,我是个**


|