/*
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;
}
HDU 3974
最新推荐文章于 2024-02-03 23:54:57 发布
1281

被折叠的 条评论
为什么被折叠?



