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
这题建树有些困难
首先要把所有的员工都重新编号
用used 来标记做过下属的员工 找到最大的boss
之后记录下每一个员工管辖的区间
这样就可以转换为线段树的问题了
单点查询区间更新
实现的时候和普通线段树都一样 函数的调用点的时候有一点差别而已
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std;
const int maxn = 50005;
int t, m, n, tot, cnt;
struct edge{
int to, next;
}edges[maxn];
int start[maxn], ed[maxn], head[maxn];
struct node{
int val, lazy;
int l, r;
}tree[maxn << 2];
void init()
{
tot = 0;
cnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v)
{
edges[tot].to = v;
edges[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u)//给boss和employee重新编号 变成区间
{
++cnt;
start[u] = cnt;
for(int i = head[u]; i != -1; i = edges[i].next){
dfs(edges[i].to);
}
ed[u] = cnt;
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
tree[rt].val = -1;
tree[rt].lazy = 0;
if(l == r) return;
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
}
void updatesame(int rt, int v)
{
if(rt){
tree[rt].val = v;
tree[rt].lazy = 1;
}
}
void pushdown(int rt)
{
if(tree[rt].lazy){
updatesame(rt << 1, tree[rt].val);
updatesame(rt << 1 | 1, tree[rt].val);
tree[rt].lazy = 0;
}
}
void update(int v, int l, int r, int rt)
{
if(tree[rt].l == l && tree[rt].r == r){
updatesame(rt, v);
return;
}
pushdown(rt);
int m = (tree[rt].l + tree[rt].r) >>1;
if(r <= m) update(v, l, r, rt << 1);
else if(l > m) update(v, l, r, rt << 1 | 1);
else{
update(v, l, m, rt << 1);
update(v, m + 1, r, rt << 1 | 1);
}
}
int query(int u, int rt)
{
if(tree[rt].l == u && tree[rt].r == u){
return tree[rt].val;
}
pushdown(rt);
int m = (tree[rt].l + tree[rt].r) / 2;
if(u <= m) return query(u, rt << 1);
else return query(u, rt << 1 | 1);
}
bool used[maxn];
int main()
{
cin>>t;
for(int cas = 1; cas <= t; cas++){
printf("Case #%d:
", cas);
memset(used, 0, sizeof(used));
init();
cin>>n;
for(int i = 1; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
used[u] = true;
addedge(v, u);
}
for(int i = 1; i <= n; i++){
if(!used[i]){
dfs(i);
break;
}
}
build(1, cnt, 1);
char op[10];
scanf("%d", &m);
while(m--){
scanf("%s", op);
int a, b;
if(op[0] == 'C'){
scanf("%d", &a);
printf("%d
", query(start[a], 1));
}
else{
scanf("%d%d", &a, &b);
update(b, start[a], ed[a], 1);
}
}
}
return 0;
}