# Assign the task HDU - 3974 dfs序 +线段树

## 一、内容

 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


## 二、思路

• dfs序
• in[i]代表每个节点在线段树中的标号， out[i] 代表最后一个下属的标号。
• 转化为单点查询和区间修改。 每次直接查询x的任务即可，编号为in[x].
• 区间修改时用tr[]记录任务。若等于-1代表没任务或者2个子区间的任务不相同。 修改的区间为【in[x], out[y]】 代表自己到最后一个下属的区间长度。

## 三、代码

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int N = 5e4 + 5;
struct E {
int v, next;
} e[N];
int t, n, m, x, y, u, v, len, index, root, head[N], in[N], out[N], p[N], tr[N << 2];
char str[2];
void add(int u, int v) {
e[len].v = v;
}
void pushdown(int id) {
if (tr[id] == -1) return; //若没任务 不必下放
tr[id << 1] = tr[id << 1 | 1] = tr[id];
tr[id] = -1;
}
void dfs(int u) {
in[u] = ++index; //记录它代表的下标
for (int j = head[u]; j; j = e[j].next) {
int v = e[j].v;
dfs(v);
}
out[u] = index; //它的最后一个下属的下标
}
void build(int id, int l, int r) {
if (l == r) {
tr[id] = -1;
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
tr[id] = -1;
}
void update(int id, int l, int r, int x, int y, int d) {
if (x <= l && r <= y) {
tr[id] = d;
return;
}
pushdown(id);
int mid = (l + r) >> 1;
if (x <= mid) update(id << 1, l, mid, x, y, d);
if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, d);
}
int query(int id, int l, int r, int x) {
if (tr[id] != -1 && l <= x && x <= r) return tr[id];
if (l == r) return tr[id];
int mid = (l + r) >> 1;
pushdown(id);
if (x <= mid) return query(id << 1, l, mid, x);
else return query(id << 1 | 1, mid + 1, r, x);
}
int main() {
scanf("%d", &t);
for (int ci = 1; ci <= t; ci++) {
printf("Case #%d:\n", ci);
memset(p, -1, sizeof(p));
index = 0;
len = 1;
scanf("%d", &n);
build(1, 1, n);
for (int i = 1; i < n; i++) {
scanf("%d%d", &v, &u);
p[v] = u;
}
for (int i = 1; i <= n; i++) {
if (p[i] == -1) root = i;
}
dfs(root); //搜索一下每个点代表的下标
scanf("%d", &m);
for (int i = 1; i <= m; i++) {
scanf("%s", str);
if (str[0] == 'C') {
scanf("%d", &x);
printf("%d\n", query(1, 1, n, in[x]));
} else {
scanf("%d%d", &x, &y);
update(1, 1, n, in[x], out[x], y);
}
}
}
return 0;
}


©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客