题目
There is a company that has N employees(numbered from 1 to N),every employee in the company has a immediate boss (except for the leader of whole company).If you are the immediate boss of someone,that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody’s boss, then you have no subordinates,the employee who has no immediate boss is the leader of whole company.So it means the N employees form a tree.
The company usually assigns some tasks to some employees to finish.When a task is assigned to someone,He/She will assigned it to all his/her subordinates.In other words,the person and all his/her subordinates received a task in the same time. Furthermore,whenever a employee received a task,he/she will stop the current task(if he/she has) and start the new one.
Write a program that will help in figuring out some employee’s current task after the company assign some tasks to some employee.
Input
The first line contains a single positive integer T( T <= 10 ), indicates the number of test cases.
For each test case:
The first line contains an integer N (N ≤ 50,000) , which is the number of the employees.
The following N - 1 lines each contain two integers u and v, which means the employee v is the immediate boss of employee u(1<=u,v<=N).
The next line contains an integer M (M ≤ 50,000).
The following M lines each contain a message which is either
“C x” which means an inquiry for the current task of employee x
or
"T x y"which means the company assign task y to employee x.
(1<=x<=N,0<=y<=10^9)
Output
For each test case, print the test case number (beginning with 1) in the first line and then for every inquiry, output the correspond answer per line.
Sample Input
1
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3
Sample Output
Case #1:
-1
1
2
题意
题给一棵树,上面的父亲节点是下面节点的领导。存在两个操作,C x是查询x的任务 T x y 是给x节点布置任务y。此处存在一个要求,领导的任务会被同时发放给他的下属,下属的当前任务以最近被分配到的任务为优先。
解释
题给的数和线段树相联系却缺乏了一个桥梁,由于题给树的每个节点的编号和跨度和线段树并不相同。为了转化,我们借助dfs给每个节点标记上相对应等效的线段左右区间值。每个节点对应着一条线段,举个小例子按照题给例子3的下属是4,1。3的线段是【2,4】,3是【3,3】,1是【4,4】,所以修改节点3的时候只要去线段树中修改【2,4】(注意【2,4】在线段树中可能不是一个节点,是多个节点拼凑的线段)上所有包含的节点区间。
查询就转变为在线段树上查询节点3的线段左区间值2,写个列子,深入理解dfs后会发现每个题给数编号在线段树上对应的是自己的线段区间的左值(多动手)。所以查询就变成查询l[x]的在线段区间的值query(l[x],1,n,1)。也可以查询query(l[x],r[x],1,n,1)因为领导区间的值,和领导自己对应的叶子节点值相同。
修改就变成update(l[x],r[x],y,1,n,1)。
#include <cstdio>
#include <vector>
#include <cstring>
#define lson l,m,ret<<1
#define rson m+1, r, ret<<1|1
using namespace std;
vector<int> G[50005];
int l[50005], r[50005], num[50005*4], lazy[50005*4];
int pre[50005];
int id;
void dfs(int pos){
l[pos] = ++id;
for (int i = 0; i < G[pos].size(); i++){
dfs(G[pos][i]);
}
r[pos] = id;
}
void bulid(int l, int r, int ret){
num[ret] = -1;
lazy[ret] = -1;
if(l == r){
return ;
}
int m = (l+r)/2;
bulid(lson);
bulid(rson);
}
void pushdown(int ret){
if(lazy[ret] != -1){
num[ret<<1] = num[ret<<1|1] = lazy[ret];
lazy[ret<<1] = lazy[ret<<1|1] = lazy[ret];
lazy[ret] = -1;
}
}
void update(int L, int R, int y, int l, int r, int ret){
int m = (l+r)/2;
if(L <= l && r <= R){
num[ret] = y;
lazy[ret] = y;
return ;
}
pushdown(ret);
if(m >= L)
update(L, R, y, lson);
if(m < R)
update(L, R, y, rson);
}
int query(int L,int R,int l,int r,int ret)
{
if(L<=l&&r<=R)
{
return num[ret];
}
pushdown(ret);
int m=(l+r)>>1;
if(L<=m) return query(L,R,lson);
if(R>m) return query(L,R,rson);
}
/*
int query(int x,int l, int r, int ret){
if(l == r){
return num[ret];
}
pushdown(ret);
int m =(l+r)/2;
if(m >=x)
return query(x, lson);
else
return query(x, rson);
}*/
int main(){
int t;
int n, m;
int u, v;
int a, b;
int cont = 1;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
for(int i = 1; i <= n; i++){
G[i].clear();
pre[i] = 0;
}
for (int i = 0; i < n-1; i++){
scanf("%d %d", &u, &v);
G[v].push_back(u);
pre[u]++;
}
id = 0;
for(int st = 1; st <= n; st++)
if(pre[st] == 0)
dfs(st);
scanf("%d", &m);
char s[5];
bulid(1, n, 1);
printf("Case #%d:\n", cont++);
for (int i = 0; i < m; i++){
scanf("%s", s);
if(s[0] == 'C'){
scanf("%d", &a);
printf("%d\n", query(l[a],r[a], 1, n,1));
}
else{
scanf("%d %d", &a, &b);
update(l[a],r[a],b, 1,n,1);
}
}
}
return 0;
}