• QTREE3


    树链剖分应用题,看到一群大佬各种LCT,Splay,瑟瑟发抖ing

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int N = 2e5;
    
    int fa[N],lv[N],siz[N],son[N],top[N],dfn[N],ptn[N],cnt=0;
    vector<int> G[N];
    int n,seg[N<<2];
    
    void build(int l,int r,int p) {
        if(l==r) {
            seg[p]=INT_MAX;
            return;
        }
        int m=(l+r)/2;
        build(l,m,p<<1);
        build(m+1,r,p<<1|1);
        seg[p]=min(seg[p<<1],seg[p<<1|1]);
    }
    
    int query(int s,int t,int l,int r,int p) {
        if(s<=l&&r<=t) return seg[p];
        int m=(l+r)/2;
        if(t<=m) return query(s,t,l,m,p<<1);
        if(m+1<=s) return query(s,t,m+1,r,p<<1|1);
        return min(query(s,t,l,m,p<<1),query(s,t,m+1,r,p<<1|1));
    }
    
    void update(int pos,int l,int r,int p){
        if(l==r) {
            if(seg[p]==INT_MAX) seg[p]=pos;
            else seg[p] = INT_MAX;
            return;
        }
        int m=(l+r)/2;
        if(pos<=m) update(pos,l,m,p<<1);
        else update(pos,m+1,r,p<<1|1);
        seg[p]=min(seg[p<<1],seg[p<<1|1]);
    }
    
    int dfs1(int x,int fx) {
        son[x]=-1;
        siz[x]=1;
        fa[x]=fx;
        lv[x]=lv[fx]+1;
        for(auto p:G[x]) {
            int to = p;
            if(to==fx) continue;
            siz[x] += dfs1(to,x);
            if(son[x] == -1 || siz[to] > siz[son[x]]) son[x] = to;
        }
        return siz[x];
    }
    
    void dfs2(int x,int root) {
        top[x]=root;
        dfn[x]=++cnt;
        ptn[dfn[x]]=x;
        if(son[x]==-1) return;
        dfs2(son[x],root);
        for(auto p:G[x]) {
            int to = p;
            if(to == son[x] || to == fa[x]) continue;
            dfs2(to,to);
        }
    }
    
    int ask(int x,int y) {
        int ans=INT_MAX;
        while(top[x]!=top[y]) {
            if(lv[top[x]]>lv[top[y]]) swap(x,y);
            ans=min(ans, query(dfn[top[y]], dfn[y],1,n,1));
            y=fa[top[y]];
        }
        if(dfn[x]!=dfn[y]) {
            if(dfn[x]>dfn[y]) swap(x,y);
            ans = min(ans, query(dfn[x],dfn[y],1,n,1));
        }
        if(ans == INT_MAX) return -1;
        return ptn[ans];
    }
    
    int a,b,c,q,x,o;
    
    int main(){
        memset(seg,0,sizeof(seg));
        cnt = 0;
        scanf("%d %d",&n,&q);
        for(int i=1;i<=n;i++) G[i].clear();
        for(int i=1;i<n;i++){
            scanf("%d %d",&a,&b);
            G[a].push_back(b);
            G[b].push_back(a);
        }
        dfs1(1,0);
        dfs2(1,1);
        build(1,n,1);
        while(q--) {
            scanf("%d %d",&o,&x);
            if(o==0) update(dfn[x],1,n,1);
            else cout<<ask(1,x)<<endl;
        }
    }
    
    
  • 相关阅读:
    PYTHON简介
    zabbix4.0搭建2
    zabbix4.0搭建1
    zabbix监控
    Linux中vim编辑命令
    零基础逆向工程25_C++_02_类的成员权限_虚函数_模板
    零基础逆向工程24_C++_01_类_this指针_继承本质_多层继承
    零基础逆向工程23_PE结构07_重定位表_IAT表(待补充)
    零基础逆向工程22_PE结构06_导入表
    零基础逆向工程21_PE结构05_数据目录表_导出表
  • 原文地址:https://www.cnblogs.com/xxfy1/p/12263212.html
Copyright © 2020-2023  润新知