QTREE3 - Query on a tree again! 树链

树链剖分应用题,看到一群大佬各种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;
    }
}

posted @ 2020-02-05 12:31  潇湘风夜  阅读(191)  评论(0编辑  收藏  举报