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:\n", 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\n", query(start[a], 1));
}
else{
scanf("%d%d", &a, &b);
update(b, start[a], ed[a], 1);
}
}
}
return 0;
}

hdu3974 Assign the task【线段树】的更多相关文章

  1. hdu3974 Assign the task线段树 dfs序

    题意: 无序的给编号为1-n的员工安排上下级, 操作一:给一个员工任务C,则该员工以及他的下级任务都更换为任务C 操作二:询问一个员工,返回他的任务   题解: 给一个员工任务,则他所在组都要改变,联 ...

  2. HDU3974 Assign the task —— dfs时间戳 + 线段树

    题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...

  3. hdu3974 Assign the task dfs序+线段树

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  4. HDU-3974 Assign the task题解报告【dfs序+线段树】

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  5. HDU-3974 Assign the task(多叉树DFS时间戳建线段树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Time Limit: 15000/5000 MS (Java/Others)    Memory Lim ...

  6. HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)

    题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...

  7. HDU3974 Assign the task

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

  9. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序

    题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...

随机推荐

  1. kendo-ui的MVVM模式

    摘要: MVVM(Model View ViewModel)是一种帮助开发者将数据从模型分离的设计模式.MVVM的ViewModel负责将数据对象从模型中分离出来,通过这种方式数据就很容易控制数据如何 ...

  2. 马尔科夫链蒙特卡洛(Markov chain Monte Carlo)

    (学习这部分内容大约需要1.3小时) 摘要 马尔科夫链蒙特卡洛(Markov chain Monte Carlo, MCMC) 是一类近似采样算法. 它通过一条拥有稳态分布 \(p\) 的马尔科夫链对 ...

  3. %s %d %f 等等是什么意思

    这个是C语言的格式化输出:%s是字符串.%d是整数.%f代表浮点数. 这些是格式声明,格式声明由“%”和格式字符组成.常用的格式字符有:1)d格式符,用来输出一个有符号的十进制整数:2)c格式符,用来 ...

  4. Android 4.0以上BlurMaskFilter效果无效

    Android MaskFilter的基本使用: MaskFilter类可以为Paint分配边缘效果.        对MaskFilter的扩展可以对一个Paint边缘的alpha通道应用转换.An ...

  5. SpringMVC由浅入深day02_4springmvc校验

    4 springmvc校验 4.1 校验Validation理解 b/s系统中对http请求数据的校验多数在客户端进行,这也是出于简单及用户体验性上考虑,但是在一些安全性要求高的系统中服务端校验是不可 ...

  6. SpringMVC -- 梗概--源码--贰--下载

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  7. error LNK2038: 检测到“_MSC_VER”的不匹配项: 值“1600”不匹配值“1800”

    _MSC_VER 定义编译器的版本.下面是一些编译器版本的_MSC_VER值:MS VC++ 10.0 _MSC_VER = 1600MS VC++ 9.0 _MSC_VER = 1500MS VC+ ...

  8. springboot学习过程笔记

    1.spring-boot-devtools热部署在IDEA中配置后不起作用(Eclipse设置了自动编译,所以不用额外设置) 1).pom.xml添加spring-boot-devtools依赖后 ...

  9. docker in centos error

    centos 7 Docker 启动了一个web服务 但是启动时 报 WARNING: IPv4 forwarding is disabled. Networking will not work. 网 ...

  10. Windows命令行参数的知识(一)

    最近没事的时候,准备研究一下Windows命令行参数的知识,因为每次自己在操作电脑时总是效率太慢,如果能够了解Windows参数的一些知识,绝对能提高效率! 基本外部命令和内部命令 首先是基本的知识, ...