0pts全RE求调
查看原帖
0pts全RE求调
508086
Liuboom楼主2024/4/18 14:02
#include <bits/stdc++.h>
#define ls x<<1
#define rs x<<1|1
using namespace std;
const int N=4e5+5;
int n,m,cnt,tot;
char c[10];
int xx[N],yy[N],ww[N];
int head[N],id[N],fa[N],dfn[N],son[N],top[N],len[N],a[N],wz[N],de[N];
struct Tree{
    int l,r,mi,ma,sum,tag;
}t[N<<2];
struct Edge{
    int to,ne,w;
}edge[N<<1];
void add(int x,int y,int w)
{
    edge[++cnt]=(Edge){y,head[x],w};head[x]=cnt;
}
void init()
{
    t[1].l=1,t[1].r=n;
    for(int i=1;i<=n;i++)
    {
        head[i]=-1;
    }
}
void dfs1(int x,int f)
{
    de[x]=de[f]+1;
    fa[x]=f;
    len[x]=1;
    int maxlen=0;
    //cout<<"head:"<<x<<" "<<head[x]<<endl;
    for(int i=head[x];~i;i=edge[i].ne)
    {
        int to=edge[i].to;
        if(to==fa[x])continue;
        //cout<<"to:"<<x<<"--"<<to<<endl;
        a[to]=edge[i].w;
        dfs1(to,x);
        if(maxlen<len[to])
        {
            son[x]=to,maxlen=len[to];
        }
        len[x]+=len[to];
    }
    //cout<<"son:"<<x<<"=="<<son[x]<<endl;
}
void dfs2(int x,int tp)
{
    //cout<<"top:"<<x<<" "<<tp<<endl;
    dfn[x]=++tot;
    id[tot]=x;
    top[x]=tp;
    if(!son[x])return ;
    dfs2(son[x],tp);
    for(int i=head[x];~i;i=edge[i].ne)
    {
        int to=edge[i].to;
        if(to==fa[x]||to==son[x])continue;
        dfs2(to,to);
    }
}
void up(int x)
{
    t[x].ma=max(t[ls].ma,t[rs].ma);
    t[x].mi=min(t[ls].mi,t[ls].mi);
    t[x].sum=t[ls].sum+t[rs].sum;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void build(int x)
{
    if(t[x].l==t[x].r)
    {
        t[x].sum=t[x].mi=t[x].ma=a[id[t[x].l]];
        //cout<<"a:"<<id[t[x].l]<<"=="<<a[id[t[x].l]]<<endl;
        //cout<<x<<":"<<"sum:"<<t[x].sum<<"\n ma:"<<t[x].ma<<"\n mi:"<<t[x].mi<<"\n";
        return ;
    }
    int mid=t[x].l+t[x].r>>1;
    t[ls].l=t[x].l,t[ls].r=mid,t[rs].l=mid+1,t[rs].r=t[x].r;
    build(ls),build(rs);
    up(x);
    //cout<<x<<":"<<"sum:"<<t[x].sum<<"\n ma:"<<t[x].ma<<"\n mi:"<<t[x].mi<<"\n";
}
inline void pushdown(int x)
{
    if(t[x].tag)
    {
        t[ls].sum*=-1,t[ls].mi*=-1,t[ls].ma*=-1;swap(t[ls].mi,t[ls].ma);
        t[rs].sum*=-1,t[rs].mi*=-1,t[rs].ma*=-1;swap(t[rs].mi,t[rs].ma);
    }
    t[x].tag=0;
    return;
}
void upd(int x,int fi,int w)
{
    //cout<<"upd\n";
    if(t[x].l==t[x].r)
    {
        //cout<<"upd"<<x<<"->"<<fi<<"=="<<t[x].l<<"--"<<t[x].ma<<"to"<<w<<endl;
        t[x].ma=t[x].mi=t[x].sum=w;
        t[x].tag=0;
        return ;
    }
    pushdown(x);
    int mid=t[x].l+t[x].r>>1;
    if(fi<=mid)upd(ls,fi,w);
    else upd(rs,fi,w);
    up(x);
}
void change(int x,int ll,int rr)
{
    //cout<<"chage\n";
    //cout<<"chage"<<x<<"=="<<ll<<"  "<<rr<<endl;
    if(ll<=t[x].l&&t[x].r<=rr)
    {
        t[x].sum*=-1,t[x].mi*=-1,t[x].ma*=-1;swap(t[x].mi,t[x].ma);
        t[x].tag^=1;
        return;
    }
    pushdown(x);
    int mid=t[x].l+t[x].r>>1;
    if(ll<=mid)change(ls,ll,rr);
    if(rr>mid)change(rs,ll,rr);
    up(x);
}
void query(int x,int ll,int rr,Tree &res)
{
    //cout<<"query\n";
    //cout<<"query"<<x<<"=="<<ll<<" "<<rr<<endl;
    if(ll<=t[x].l&&t[x].r<=rr)
    {
        res.sum+=t[x].sum;res.ma=max(t[x].ma,res.ma),res.mi=min(t[x].mi,res.mi);
        //cout<<"sum:"<<t[x].sum<<"\n ma:"<<t[x].ma<<"\n mi:"<<t[x].mi<<"\n";
        return ;
    }
    pushdown(x);
    int mid=t[x].l+t[x].r>>1;
    if(rr<=mid)
    {
        query(ls,ll,rr,res);
    }
    if(ll>mid)
    {
        query(rs,ll,rr,res);
    }
    query(ls,ll,rr,res),query(rs,ll,rr,res);
    up(x);
}
void lca1(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(de[top[x]]<de[top[y]])swap(x,y);
        change(1,dfn[top[x]],dfn[x]);
        //cout<<"chage"<<dfn[top[x]]<<"--"<<dfn[x]<<endl;
        x=fa[top[x]];
    }
    if(de[x]<de[y])swap(x,y);
    //cout<<"chage"<<dfn[y]<<"--"<<dfn[x]<<endl;
    change(1,dfn[y],dfn[x]);
}
Tree lca2(int x,int y)
{
    Tree res=(Tree){0,0,0,0,0,0};
    //cout<<"lca2:"<<x<<" "<<y<<"top:"<<top[x]<<" "<<top[y]<<endl;
    while(top[x]!=top[y])
    {
        if(de[top[x]]<de[top[y]])swap(x,y);
        //cout<<"query1:"<<dfn[top[x]]<<" "<<dfn[x]<<endl;
        query(1,dfn[top[x]],dfn[x],res);
        x=fa[top[x]];
    }
    if(de[x]<de[y])swap(x,y);
    //cout<<"query2:"<<dfn[y]<<" "<<dfn[x]<<endl;
    query(1,dfn[y],dfn[x],res);
    return res;
}
int main()
{
    //freopen("p1505.in","r",stdin);
    //freopen("p1505.out","w",stdout);
    cin>>n;
    cout<<n;
    init();
    for(int i=1;i<=n-1;i++)
    {
        scanf("%d%d%d",&xx[i],&yy[i],&ww[i]);
        xx[i]++,yy[i]++;
        add(xx[i],yy[i],ww[i]),add(yy[i],xx[i],ww[i]);
    }
    dfs1(1,0);
    dfs2(1,1);
    build(1);
    cin>>m;
    for(int i=1,l,r;i<=m;i++)
    {
        scanf("%s",c);
        scanf("%d%d",&l,&r);
        l++,r++;
        if(c[0]=='C')
        {
            l--;r--;
            int x=xx[l],y=yy[l];
            if(de[x]<de[y])swap(x,y);
            //cout<<"upd:"<<l<<" "<<x<<endl;
            upd(1,id[x],r);
        }
        else if(c[0]=='N')
        {
            //cout<<"lca1:"<<l<<"=="<<r<<endl;
            lca1(l,r);
        }
        else
        {
            Tree ans=lca2(l,r);
            if(c[1]=='U')printf("%d\n",ans.sum);
            if(c[1]=='A')printf("%d\n",ans.ma);
            if(c[1]=='I')printf("%d\n",ans.mi);
        }
    }
    return 0;
}

2024/4/18 14:02
加载中...