HDU 3974

/*
Assign the task
dfs建立线段树
*/
#include<vector>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson (pos<<1)
#define rson (pos<<1|1)
const int maxn = 50005;
int n,m;
int num[maxn],vis[maxn];
vector<int>G[maxn];
int Hash[maxn],cnt;
int dfs(int u){
    num[u] = 0;
    Hash[u] = cnt;
    cnt ++;
    for(int i = 0; i < G[u].size(); i++){
         num[u] += dfs(G[u][i]);
    }
    return num[u] + 1;
}
int col[maxn << 2];
void build(){
    memset(col,-1,sizeof(col));
}
void pushdown(int pos){
    if(col[pos] != -1){
        col[lson] = col[rson] = col[pos];
        col[pos]  = -1;
    }
}
void update(int L,int R,int l,int r,int pos,int d){
    if(l <= L && R <= r){
        col[pos] = d;
        return;
    }
    pushdown(pos);
    int mid = (L + R) >> 1;
    if(l <= mid)
        update(L,mid,l,r,lson,d);
    if(r  > mid)
        update(mid + 1,R,l,r,rson,d);
}
int query(int L,int R,int m,int pos){
    if(L == R)
        return col[pos];
    pushdown(pos);
    int mid = (L + R) >> 1;
    if(m <= mid)
        return query(L,mid,m,lson);
    else
        return query(mid + 1,R,m,rson);
}
int main(){
    int T,Case = 1;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        cnt = 1;
        memset(vis,0,sizeof(vis));
        for(int i = 1; i <= n;i++) G[i].clear();
        for(int i = 1;i < n; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            G[v].push_back(u);
            vis[u] = 1;
        }
        for(int i = 1; i <= n; i++)
            if(!vis[i]){
                dfs(i);
            }
        build();
        scanf("%d",&m);
        int x,y;
        printf("Case #%d:\n",Case++);
        for(int i = 0; i < m; i++){
            char op[10];
            scanf("%s",op);
            if(op[0] == 'C'){
                scanf("%d",&x);
                printf("%d\n",query(1,n,Hash[x],1));
            }
            else{
                scanf("%d%d",&x,&y);
                int l = Hash[x];
                int r = l + num[x];
                update(1,n,l,r,1,y);
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值