SPOJ QTREE3 Query on a tree again!(树链剖分)

本文介绍了一种巧妙地将树链剖分与线段树相结合的方法,用于解决树上的节点状态查询和更新问题。通过以特定节点为根进行树链剖分,并利用线段树快速定位和更新节点状态,实现高效查询离指定节点最近的特定状态节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

一颗树上,每个节点只有黑和白两种状态,最初都是白,现在有两种操作,一种问[1,x]中离1最近的黑节点编号,另一种把x节点取反

思路:

有点巧妙的树链剖分
我们知道,树链剖分是从根向下进行标号的,那么我们就可以以1作为根,这样我们在向上爬的过程中,每次从线段树中拿到的标号必然是最优的(线段树优先向左,这一点和我们在树链剖分里面的标记方式正好吻合),这样我们不断更新ans即可

错误及反思:

我在代码里面有一段注释,是一个可能会写错的坑点,我wa了好多遍才想到的

代码:

#include<bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
using namespace std;
const int N =100010;

int segtree[N*4];//-1 for both
pair<int,int> e[N*2],b[N];
int tot,tid;
int top[N],si[N],fa[N],fi[N],son[N],depth[N],id[N],rnk[N],q,n;

void addedge(int x,int y){
    e[tot].fi=y;
    e[tot].se=fi[x];
    fi[x]=tot++;
    e[tot].fi=x;
    e[tot].se=fi[y];
    fi[y]=tot++;
}
void dfs1(int now,int bef,int dep){
    fa[now]=bef;
    depth[now]=dep;
    si[now]=1;
    for(int i=fi[now];i!=-1;i=e[i].se)
        if(e[i].fi!=bef)
        {
            dfs1(e[i].fi,now,dep+1);
            si[now]+=si[e[i].fi];
            if(son[now]==-1) son[now]=e[i].fi;
            else son[now]=si[e[i].fi]>si[son[now]]?e[i].fi:son[now];
        }
}
void dfs2(int now,int tp){
    top[now]=tp;
    id[now]=tid++;
    if(son[now]!=-1) dfs2(son[now],tp);
    for(int i=fi[now];i!=-1;i=e[i].se)
        if(e[i].fi!=fa[now]&&e[i].fi!=son[now])
            dfs2(e[i].fi,e[i].fi);
}
void init(){
    memset(segtree,0,sizeof(segtree));
    tot=0; tid=1;
    memset(first,-1,sizeof(first));
    memset(son,-1,sizeof(son));
}
int query(int L,int R,int l,int r,int rt){
    if(segtree[rt]==0) return -1;
    if(L<=l&&R>=r&&segtree[rt]==1) return rnk[l];
    int m=(l+r)/2;
    int ans=-1;
    if(m>=L) ans=query(L,R,lson);
    if(ans!=-1) return ans;
    if(m<R) ans=query(L,R,rson);
    return ans;
    /*
    这里必须这样判断,不能这样
    if(m>=L&&segtree[rt<<1]) ans=query(L,R,lson);
    else if(m<R&&segtree[rt<<1|1]) ans=query(L,R,rson);
    注释里的写法会忽略一个问题,我左边是有1,但是并不一定在[L,R]的区间
    */
}
int modify(int L,int R){
    int f1=top[L],f2=top[R];
    int ans=-1;
    while(f1!=f2)
    {
        if(depth[f1]<depth[f2])
        {
            swap(f1,f2);
            swap(L,R);
        }
        int ta=query(id[f1],id[L],1,tid-1,1);
        ans=ta==-1?ans:ta;
        L=fa[f1];
        f1=top[L];
    }
    if(depth[L]>depth[R]) swap(L,R);
    int ta=query(id[L],id[R],1,tid-1,1);
    ans=ta==-1?ans:ta;
    return ans;
}
void update(int p,int l,int r,int rt){
    if(l==r){
        segtree[rt]^=1;
        return ;
    }
    int m=(l+r)/2;
    if(m>=p) update(p,lson);
    else update(p,rson);
    if(segtree[rt<<1]==segtree[rt<<1|1]) segtree[rt]=segtree[rt<<1];
    else segtree[rt]=-1;
}
int main(){
    init();
    scanf("%d%d",&n,&q);
    for(int i=0,u,v;i<n-1;i++){
        scanf("%d%d",&u,&v);
        addedge(u,v);
    }
    dfs1(1,1,1);
    dfs2(1,1);
    for(int i=1;i<=n;i++)
        rnk[id[i]]=i;
    while(q--){
        int op,ta;
        scanf("%d%d",&op,&ta);
        if(op)
            printf("%d\n",modify(1,ta));
        else update(id[ta],1,tid-1,1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值