codeforces 343D - Water Tree

题意:题意:给你一颗以点1为根的数,有两种操作,一种是把x及其子树的所有点都灌满水,一种是把x及其所有祖先都放空水,一种是询问,问某个点里有没有水?

思路:线段树维护最晚浇水时间和最晚清空时间。

#include <bits/stdc++.h>
#define lc l,mid,x<<1
#define rc mid+1,r,x<<1|1
#define ls x<<1
#define rs x<<1|1
using namespace std;
const int maxn = 500005;
const int maxm = 1000005;
int ver[maxm],he[maxn],ne[maxm],tot;
void add( int x,int y ){
    ver[++tot] = y;
    ne[tot] = he[x];
    he[x] = tot;
}
int num = 0,st[maxn],ed[maxn];
void dfs( int x,int f ){
    st[x] = ++num;
    for( int cure = he[x];cure;cure = ne[cure] ){
        int y = ver[cure];
        if( y == f ) continue;
        dfs( y,x );
    }
    ed[x] = num;
}
int tree[2][4*maxn],laz[2][4*maxn];
void push_up(int x){
    tree[0][x] = max( tree[0][ls],tree[0][rs] );
    tree[1][x] = max( tree[1][ls],tree[0][rs] );
}
void push_down( int x ){
    if( laz[0][x] ){
        tree[0][ls] = tree[0][rs] = laz[0][x];
        laz[0][ls] = laz[0][rs] = laz[0][x];
        laz[0][x] = 0;
    }
    if( laz[1][x] ){
        tree[1][ls] = tree[1][rs] = laz[1][x];
        laz[1][ls] = laz[1][rs] = laz[1][x];
        laz[1][x] = 0;
    }
}
void update( int left,int right,int v,int p,int l,int r,int x ){
    if( left <= l && right >= r ){
        tree[p][x] = laz[p][x] = v;
        return;
    }
    push_down(x);
    int mid = l+r>>1;
    if( left <= mid ) update( left,right,v,p,lc );
    if( right > mid ) update( left,right,v,p,rc );
    push_up(x);
}
int query( int left,int right,int p,int l,int r,int x ){
    if( left <= l && right >= r ){
        return tree[p][x];
    }
    push_down(x);
    int mid = l+r>>1;
    int res = 0;
    if( left <= mid ) res = max( res,query( left,right,p,lc ) );
    if( right > mid ) res = max( res,query( left,right,p,rc ) );
    push_up(x);
    return res;
}
int main(){
    int n,x,y;
    scanf("%d",&n);
    for( int i = 1; i < n;i++ ){
        scanf("%d%d",&x,&y);
        add( x,y );add( y,x );
    }
    dfs(1,0);
    int m;
    scanf("%d",&m);
    for( int i = 1;i <= m;i++ ){
        int op,x;
        scanf("%d%d",&op,&x);
        if( op == 1 ){
            update( st[x],ed[x],i,1,1,n,1 );
        }else if( op == 2 ){
            update( st[x],st[x],i,0,1,n,1 );
        }else{
            int v0 = query( st[x],ed[x],0,1,n,1 );
            int v1 = query( st[x],st[x],1,1,n,1 );
            if( v1 > v0 ){
                puts("1");
            }else puts("0");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值