只过了第11个点,请求帮助,谢了
查看原帖
只过了第11个点,请求帮助,谢了
478885
lenlen楼主2022/10/17 19:13
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7232;
int n,m;
string op;
int h[N],cnt;
struct hl{
    int v,nxt,w,id;
}e[N<<1];
void add(int u,int v,int w,int id)
{
    e[++cnt].v=v;e[cnt].w=w;e[cnt].nxt=h[u];h[u]=cnt;e[cnt].id=id;
}
int fa[N],dfn[N],top[N],re[N],dep[N],size[N],son[N],w[N],id[N],num;
int x,y,z;
void dfs1(int x,int fx)
{
    fa[x]=fx;dep[x]=dep[fx]+1;size[x]=1;
    for(int i=h[x];i;i=e[i].nxt)
    {
        if(e[i].v==fx) continue;
        id[e[i].id]=e[i].v;
        w[e[i].v]=e[i].w;
        dfs1(e[i].v,x);
        size[x]+=size[e[i].v];
        if(size[e[i].v]>size[son[x]]) son[x]=e[i].v;
    }
}
void dfs2(int x,int tp)
{
    dfn[x]=++num;top[x]=tp;re[dfn[x]]=x;
    if(!son[x]) return ;
    dfs2(son[x],tp);
    for(int i=h[x];i;i=e[i].nxt)
    {
        if(e[i].v==fa[x]||e[i].v==son[x]) continue;
        dfs2(e[i].v,e[i].v);
    }
}
struct lenlen{
    int l,r,sum,lam,maax,miin;
}t[N*20];
int mx(int x,int y)
{
    return x>y?x:y;
}
int mi(int x,int y)
{
    return x>y?y:x;
}
void build(int p,int l,int r)
{
    t[p].l=l;t[p].r=r;
    if(l==r) 
    {
        if(l==1)
        {
            t[p].sum=0;t[p].maax=0;t[p].miin=1e9;
            return ;
        }
        t[p].sum=w[re[l]];
        t[p].maax=t[p].sum;
        t[p].miin=t[p].sum;
        return ;
    }
    int mid=l+r>>1;
    build(p<<1,l,mid);build(p<<1|1,mid+1,r);
    t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
    t[p].maax=mx(t[p<<1].maax,t[p<<1|1].maax);
    t[p].miin=mi(t[p<<1].miin,t[p<<1|1].miin);
}
void swp(int &x,int &y)
{
    int t=y;y=x;x=t;
}
void down(int p)
{
    t[p<<1].lam=t[p].lam;
    t[p<<1|1].lam=t[p].lam;
    t[p<<1].sum=-t[p<<1].sum;
    t[p<<1|1].sum=-t[p<<1|1].sum;
    swp(t[p<<1].maax,t[p<<1].miin);
    t[p<<1].maax=-t[p<<1].maax;
    t[p<<1].miin=-t[p<<1].miin;
    swp(t[p<<1|1].maax,t[p<<1|1].miin);
    t[p<<1|1].maax=-t[p<<1|1].maax;
    t[p<<1|1].miin=-t[p<<1|1].miin;
    if(t[p].l==t[p].r&&t[p].r==1) t[p].maax=0,t[p].miin=1e9;
    t[p].sum=0;
}
void changeone(int p,int l,int r,int z)
{
    if(t[p].l==l&&t[p].r==r)
    {
        t[p].sum=z;
        t[p].maax=z;t[p].miin=z;
        return ;
    }
    if(t[p].lam&1) down(p);
    int mid=t[p].l+t[p].r>>1;
    if(mid>=l) changeone(p<<1,l,r,z);
    if(mid<r) changeone(p<<1|1,l,r,z);
    t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
    t[p].maax=mx(t[p<<1].maax,t[p<<1|1].maax);
    t[p].miin=mi(t[p<<1].miin,t[p<<1|1].miin);
}
void change(int p,int l,int r)
{
    if(t[p].l>=l&&t[p].r<=r) 
    {
        t[p].lam^=1;
        t[p].sum=-t[p].sum;
        swp(t[p].miin,t[p].maax);
        t[p].miin=-t[p].miin;t[p].maax=-t[p].maax;
        if(t[p].l==t[p].r&&t[p].r==1) t[p].maax=0,t[p].miin=1e9;
        return ;
    }
    if(t[p].lam&1) down(p);
    int mid=t[p].l+t[p].r>>1;
    if(mid>=l) change(p<<1,l,r);
    if(mid<r) change(p<<1|1,l,r);
    t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
    t[p].maax=mx(t[p<<1].maax,t[p<<1|1].maax);
    t[p].miin=mi(t[p<<1].miin,t[p<<1|1].miin);
}
int asksum(int p,int l,int r)
{
    if(t[p].l>=l&&t[p].r<=r) return t[p].sum;
    if(t[p].lam&1) down(p);
    int mid=t[p].l+t[p].r>>1,tmp=0;
    if(mid>=l) tmp+=asksum(p<<1,l,r);
    if(mid<r) tmp+=asksum(p<<1|1,l,r);
    t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
    t[p].maax=mx(t[p<<1].maax,t[p<<1|1].maax);
    t[p].miin=mi(t[p<<1].miin,t[p<<1|1].miin);
    return tmp;
}
int askmax(int p,int l,int r)
{
    if(t[p].l>=l&&t[p].r<=r) return t[p].maax;
    if(t[p].lam&1) down(p);
    int mid=t[p].l+t[p].r>>1,tmp=0;
    if(mid>=l) tmp=mx(tmp,askmax(p<<1,l,r));
    if(mid<r) tmp=mx(tmp,askmax(p<<1|1,l,r));
    t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
    t[p].maax=mx(t[p<<1].maax,t[p<<1|1].maax);
    t[p].miin=mi(t[p<<1].miin,t[p<<1|1].miin);
    return tmp;
}
int askmin(int p,int l,int r)
{
    if(t[p].l>=l&&t[p].r<=r) return t[p].miin;
    if(t[p].lam&1) down(p);
    int mid=t[p].l+t[p].r>>1,tmp=1e9;
    if(mid>=l) tmp=mi(tmp,askmin(p<<1,l,r));
    if(mid<r) tmp=mi(tmp,askmin(p<<1|1,l,r));
    t[p].sum=t[p<<1].sum+t[p<<1|1].sum;
    t[p].maax=mx(t[p<<1].maax,t[p<<1|1].maax);
    t[p].miin=mi(t[p<<1].miin,t[p<<1|1].miin);
    return tmp;
}
int treesum(int x,int y)
{
    int tmp=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swp(x,y);
        tmp+=asksum(1,dfn[top[x]],dfn[x]);
        x=fa[top[x]];
    }
    if(x==y) return tmp;
    if(dep[x]>dep[y]) swp(x,y);
    tmp+=asksum(1,dfn[x]+1,dfn[y]);
    return tmp;
}
void _change(int x,int y)
{
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swp(x,y);
        change(1,dfn[top[x]],dfn[x]);
        x=fa[top[x]];
    }
    if(x==y) return ;
    if(dep[x]>dep[y]) swp(x,y);
    change(1,dfn[x]+1,dfn[y]);
    return ;
}
int treemax(int x,int y)
{
    int tmp=0;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swp(x,y);
        tmp=mx(tmp,askmax(1,dfn[top[x]],dfn[x]));
        x=fa[top[x]];
    }
    if(x==y) return tmp;
    if(dep[x]>dep[y]) swp(x,y);
    tmp=mx(tmp,askmax(1,dfn[x]+1,dfn[y]));
    return tmp;
}
int treemin(int x,int y)
{
    int tmp=1e9;
    while(top[x]!=top[y])
    {
        if(dep[top[x]]<dep[top[y]]) swp(x,y);
        tmp=mi(tmp,askmin(1,dfn[top[x]],dfn[x]));
        x=fa[top[x]];
    }
    if(x==y) return tmp;
    if(dep[x]>dep[y]) swp(x,y);
    tmp=mi(tmp,askmin(1,dfn[x]+1,dfn[y]));
    return tmp;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<n;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        ++x;++y;
        add(x,y,z,i);add(y,x,z,i);
    }
    dfs1(1,1);dfs2(1,1);
    build(1,1,n);
    scanf("%d",&m);
    while(m--)
    {
        //cout<<"\n\n";
        //for(int i=1;i<=10;i++)
        //{
        //    cout<<t[i].l<<" "<<t[i].r<<" "<<t[i].sum<<" "<<t[i].maax<<" "<<t[i].miin<<"\n";
        //}
        //cout<<"\n\n";
        cin>>op;
        scanf("%d%d",&x,&y);
        ++x;++y;
        if(op=="SUM")
        {
            printf("%d\n",treesum(x,y));
        }
        else if(op=="MAX") 
        {
            printf("%d\n",treemax(x,y));
        }
        else if(op=="MIN")
        {
            printf("%d\n",treemin(x,y));
        }
        else if(op=="C")
        {
            --x;--y;
            changeone(1,dfn[id[x]],dfn[id[x]],y);
        }
        else 
        {
            _change(x,y);
        }
    }
}
/*
5
0 1 1 
0 3 2
1 2 3 
1 4 4 
*/
2022/10/17 19:13
加载中...