12pts onlyAC #12 求调 玄关
查看原帖
12pts onlyAC #12 求调 玄关
1771965
tuxiaolai楼主2025/8/4 21:59
#include<bits/stdc++.h>
using namespace std;
int n,root=0,m;
string op;
int u,v,w;
struct edge {
    int v,idx,w;
};
vector<edge> adj[200010],son[200010];
vector<int> a;
int b[200010];
bool vis[200010];
int des[200010],deep[200010],dss[200010];
int hld[200010],hfa[200010],hff[200010];
int pr[200010];
int k;
bool cmpdes(edge a,edge b) {
    return des[a.v]>des[b.v];
}
int dfs(int p) {
    int res=1;
    for(auto i:adj[p]) {
        if(vis[i.v]) {
            continue;
        }
        vis[i.v]=1;
        son[p].push_back(i);
        b[i.v]=i.w;
        pr[i.idx]=i.v;
        res+=dfs(i.v);
    }
    return des[p]=res;
}
void HLD(int p,int fa,int d) {
    if(p==root) {
        hff[1]=1;
    }
    hld[p]=++k;
    deep[hld[p]]=d;
    hfa[k]=hld[fa];
    a.push_back(b[p]);
    int res=hld[p];
    sort(son[p].begin(),son[p].end(),cmpdes);
    for(int i=0; i<son[p].size(); i++) {
        if(i==0) {
            HLD(son[p][i].v,fa,d+1);
        } else {
            hff[k+1]=hld[p];
            HLD(son[p][i].v,son[p][i].v,d+1);
        }
        res=max(res,dss[hld[son[p][i].v]]);
    }
    dss[hld[p]]=res;
    return ;
}
//Segtree
struct Segtree {
    int n;
    vector<int> tree,tma,tmi,lazy;
    void init(int size,vector<int> &a) {
        n=size;
        tree.resize(4*n+10);
        tma.resize(4*n+10);
        tmi.resize(4*n+10);
        lazy.resize(4*n+10);
        build(1,1,n,a);
    }
    void pushup(int p) {
        tree[p]=tree[p*2]+tree[p*2+1];
        tma[p]=max(tma[p*2],tma[p*2+1]);
        tmi[p]=min(tmi[p*2],tmi[p*2+1]);
    }
    void pushmo(int p) {
        tree[p]=-tree[p];
        swap(tma[p],tmi[p]);
        tma[p]=-tma[p];
        tmi[p]=-tmi[p];
        lazy[p]^=1;
    }
    void build(int p,int l,int r,vector<int> &a) {
        if(l==r) {
            tree[p]=tma[p]=tmi[p]=a[l-1];
            return;
        }
        int mid=(l+r)/2;
        build(p*2,l,mid,a);
        build(p*2+1,mid+1,r,a);
        pushup(p);
    }
    void pushdown(int p,int l,int r) {
        if(lazy[p]) {
            pushmo(p*2);
            pushmo(p*2+1);
            lazy[p]=0;
        }
    }
    void add(int p,int l,int r,int ql,int qr) {
        if(ql<=l && r<=qr) {
            pushmo(p);
            return;
        }
        pushdown(p,l,r);
        int mid=(l+r)/2;
        if(mid>=ql) {
            add(p*2,l,mid,ql,qr);
        }
        if(mid+1<=qr) {
            add(p*2+1,mid+1,r,ql,qr);
        }
        pushup(p);
    }
    void mof(int p,int l,int r,int q,long long val) {
        if(q==l && r==q) {
            tree[p]=tma[p]=tmi[p]=val;
            return;
        }
        pushdown(p,l,r);
        int mid=(l+r)/2;
        if(mid>=q) {
            mof(p*2,l,mid,q,val);
        }
        if(mid+1<=q) {
            mof(p*2+1,mid+1,r,q,val);
        }
        pushup(p);
    }
    long long query(int p,int l,int r,int ql,int qr) {
        if(ql<=l && r<=qr) {
            return tree[p];
        }
        pushdown(p,l,r);
        int mid=(l+r)/2;
        long long res=0;
        if(mid>=ql) {
            res+=query(p*2,l,mid,ql,qr);
        }
        if(mid+1<=qr) {
            res+=query(p*2+1,mid+1,r,ql,qr);
        }
        return res;
    }
    long long queryma(int p,int l,int r,int ql,int qr) {
        if(ql<=l && r<=qr) {
            return tma[p];
        }
        pushdown(p,l,r);
        int mid=(l+r)/2;
        long long res=LLONG_MIN;
        if(mid>=ql) {
            res=max(res,queryma(p*2,l,mid,ql,qr));
        }
        if(mid+1<=qr) {
            res=max(res,queryma(p*2+1,mid+1,r,ql,qr));
        }
        return res;
    }
    long long querymi(int p,int l,int r,int ql,int qr) {
        if(ql<=l && r<=qr) {
            return tmi[p];
        }
        pushdown(p,l,r);
        int mid=(l+r)/2;
        long long res=LLONG_MAX;
        if(mid>=ql) {
            res=min(res,querymi(p*2,l,mid,ql,qr));
        }
        if(mid+1<=qr) {
            res=min(res,querymi(p*2+1,mid+1,r,ql,qr));
        }
        return res;
    }
} tree;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen("/storage/emulated/0/txlpy/CPPProject/默认目录/Helloworld/src/infile.txt","r",stdin);//t
    cin>>n;
    for(int i=1; i<n; i++) {
        cin>>u>>v>>w;
        adj[u].push_back({v,i,w});
        adj[v].push_back({u,i,w});
    }
    vis[root]=1;
    dfs(root);
    HLD(root,root,1);
    tree.init(n,a);
    cin>>m;
    for(int i=1; i<=m; i++) {
        cin>>op>>u>>v;
        if(op=="C") {
            u=hld[pr[u]];
            tree.mof(1,1,n,u,v);
        }
        if(op=="N") {
            u=hld[u];
            v=hld[v];
            while(hfa[u]!=hfa[v]) {
                if(deep[hfa[u]]<deep[hfa[v]]) {
                    swap(u,v);
                }
                tree.add(1,1,n,hfa[u]+1,u);
                u=hff[hfa[u]];
            }
            tree.add(1,1,n,min(u,v)+1,max(u,v));
        }
        if(op=="SUM") {
            u=hld[u];
            v=hld[v];
            long long res=0;
            while(hfa[u]!=hfa[v]) {
                if(deep[hfa[u]]<deep[hfa[v]]) {
                    swap(u,v);
                }
                res+=tree.query(1,1,n,hfa[u]+1,u);
                u=hff[hfa[u]];
            }
            res+=tree.query(1,1,n,min(u,v)+1,max(u,v));
            cout<<res<<'\n';
        }
        if(op=="MAX") {
            u=hld[u];
            v=hld[v];
            long long res=LLONG_MIN;
            while(hfa[u]!=hfa[v]) {
                if(deep[hfa[u]]<deep[hfa[v]]) {
                    swap(u,v);
                }
                res=max(res,tree.queryma(1,1,n,hfa[u]+1,u));
                u=hff[hfa[u]];
            }
            res=max(res,tree.queryma(1,1,n,min(u,v)+1,max(u,v)));
            cout<<res<<'\n';
        }
        if(op=="MIN") {
            u=hld[u];
            v=hld[v];
            long long res=LLONG_MAX;
            while(hfa[u]!=hfa[v]) {
                if(deep[hfa[u]]<deep[hfa[v]]) {
                    swap(u,v);
                }
                res=min(res,tree.querymi(1,1,n,hfa[u]+1,u));
                u=hff[hfa[u]];
            }
            res=min(res,tree.querymi(1,1,n,min(u,v)+1,max(u,v)));
            cout<<res<<'\n';
        }
    }
    return 0;
}
2025/8/4 21:59
加载中...