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. feed流拉取,读扩散,究竟是啥?

    from:https://mp.weixin.qq.com/s?__biz=MjM5ODYxMDA5OQ==&mid=2651961214&idx=1&sn=5e80ad6f2 ...

  2. 解决Android 6.0(api 23) SDK,不再提供org.apache.http.*

    Eclipse 解决办法 libs中加入 org.apache.http.legacy.jar 上面的jar包在:**\android-sdk\platforms\android-23\optiona ...

  3. UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multib

    [问题] 在执行代码时,提示上述错误,源码如下: # 下载小说... def download_stoy(crawl_list,header): # 创建文件流,将各个章节读入内存 with open ...

  4. Python学习笔记(15)- os\os.path 操作文件

    程序1 编写一个程序,统计当前目录下每个文件类型的文件数,程序实现如图: import os def countfile(path): dict1 = {} # 定义一个字典 all_files = ...

  5. PHP代码审计笔记--任意文件下载漏洞

    在文件下载操作中,文件名及路径由客户端传入的参数控制,并且未进行有效的过滤,导致用户可恶意下载任意文件.  0x01 客户端下载 常见于系统中存在文件(附件/文档等资源)下载的地方. 漏洞示例代码: ...

  6. Explaining Delegates in C# - Part 1 (Callback and Multicast delegates)

    I hear a lot of confusion around Delegates in C#, and today I am going to give it shot of explaining ...

  7. Splash plugins_enabled 属性

    plugins_enabled属性可以控制浏览器插件(如 Flash 插件)是否开启.默认情况下,此属性是 false ,表示不开启. function main(splash, args) spla ...

  8. Selenium 节点交互

    Selenium 可以驱动浏览器来执行一些操作,也就是说可以让浏览器模拟执行一些动作 常见方法:输入文字时用 send_keys() 方法,清空文字时用 clear() 方法,点击按钮时用 click ...

  9. osg内置shader变量

    uniform int osg_FrameNumber:当前OSG程序运行的帧数: uniform float osg_FrameTime:当前OSG程序的运行总时间: uniform float o ...

  10. Nginx 72万连接性能测试(一)

    转自:http://my.oschina.net/chenzhuo/blog/150200?p=2#comments 根据系统内存64G估算单台tengine做反向代理最高支持72万连接.为了验证达到 ...