树剖,但是 TLE(悬赏1个关注)
查看原帖
树剖,但是 TLE(悬赏1个关注)
229366
Fast_IO楼主2022/11/27 20:15
#pragma GCC O(fast)
#include <ctype.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#define N 20005
int n,m,root,Mod;
struct Tu{int to,nxt,val;}e[N];
int hd[N],w[N],pt[N],id[N];
int heavy[N],fa[N],cnt,dep[N],sz[N],top[N],tot;
struct Node{int u,v,val;}g[N];
struct Seg{int l,r,maxn;}tr[N*4];
int max(int a,int b){return a>b?a:b;}
void swap(int a,int b){
    int t=a;
    a=b;
    b=t;
    return ;
}
void addedge(int u,int v,int val){
    e[++cnt].to=v;
    e[cnt].val=val;
    e[cnt].nxt=hd[u];
    hd[u]=cnt;
    return ;
}
void pushup(int p){
    tr[p].maxn=max(tr[p*2].maxn,tr[p*2+1].maxn);
}
void build(int p,int l,int r){
    tr[p].l=l,tr[p].r=r;
    if(l==r){
        tr[p].maxn=pt[l];
        return ;
    }
    int mid=(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    pushup(p);
}
void update(int p,int u,int k){
    if(tr[p].l==tr[p].r){
        tr[p].maxn=k;
        return ;
    }
    int mid=(tr[p].l+tr[p].r)>>1;
    if(u<=mid) update(p*2,u,k);
    else update(p*2+1,u,k);
    pushup(p);
}
int query(int p,int ql,int qr){
    if(ql<=tr[p].l && tr[p].r<=qr){
        return tr[p].maxn;
    }
    int mid=(tr[p].l+tr[p].r)>>1,ans=-1919810;
    if(ql<=mid) ans=max(ans,query(p*2,ql,qr));
    if(qr>mid) ans=max(ans,query(p*2+1,ql,qr));
    return ans;
}

// slpf 

void dfs1(int p,int fat,int depth){
    fa[p]=fat;
    dep[p]=dep[fat]+1;
    sz[p]=1;
    int max_son=-1;
    for(int i=hd[p];i;i=e[i].nxt){
        int v=e[i].to;
        if(v==fat){
            w[p]=e[i].val;
            continue;
        }
        dfs1(v,p,depth+1);
        sz[p]+=sz[v];
        if(sz[v]>max_son){
            max_son=sz[v];
            heavy[p]=v;
        }
    }   
}
void dfs2(int p,int fat){
    top[p]=fat;
    id[p]=++tot;
    pt[tot]=w[p];
    if(!heavy[p]) return ;
    dfs2(heavy[p],fat);
    for(int i=hd[p];i;i=e[i].nxt){
        int v=e[i].to;
        if(v==fa[p] || v==heavy[p]) continue;
        dfs2(v,v);
    }
}
int Query_path(int u,int v){
    int ans=-114514;
    while(top[u]!=top[v]){
        if(dep[top[u]]<dep[top[v]]) swap(u,v);
        ans=max(ans,query(1,id[top[u]],id[u]));
        u=fa[top[u]];
    }
    if(dep[u]>dep[v]) swap(u,v);
    ans=max(ans,query(1,id[u]+1,id[v]));
    return ans;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
/*
struct Tu{int to,nxt,val;}e[N];
int hd[N],w[N],pt[N],id[N];
int heavy[N],fa[N],cnt,dep[N],sz[N],top[N],tot;
struct Node{int u,v,val;}g[N];
struct Seg{int l,r,maxn;}tr[N*4];
*/
        tot=0,cnt=0;
        scanf("%d",&n);
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&g[i].u,&g[i].v,&g[i].val);
            addedge(g[i].u,g[i].v,g[i].val);
            addedge(g[i].v,g[i].u,g[i].val);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        build(1,1,n);
        while(1){
            char s[10]={0};
            int u,v;
            scanf("%s",s);
            if(s[0]=='D') break;
            scanf("%d%d",&u,&v);
            if(s[0]=='Q'){
                if(u==v) printf("0\n");
                else printf("%d\n",Query_path(u,v));
            }else{
                if(dep[g[u].u]>dep[g[u].v]) u=g[u].u;
                else u=g[u].v;
                update(1,id[u],v);
            }
        }
    }
    return 0;
}
2022/11/27 20:15
加载中...