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. python缓存装饰器,第二种方式(二)

    来个简单的装饰器 def cached_method_result(fun): """方法的结果缓存装饰器""" @wraps(fun) d ...

  2. python打造线程池

    # coding=utf-8 import threading import Queue import time import traceback class ThreadPoolExecutor(o ...

  3. Dubbo -- 系统学习 笔记 -- 依赖

    Dubbo -- 系统学习 笔记 -- 目录 依赖 必需依赖 缺省依赖 可选依赖 依赖 必需依赖 JDK1.5+ 理论上Dubbo可以只依赖JDK,不依赖于任何三方库运行,只需配置使用JDK相关实现策 ...

  4. Jackson JSON Processor

    Jackson提供接口,可以再json和bean之间互相转换 1. 一个例子 public class JsonToJavaBean { public static void main(String[ ...

  5. CM和CDH的安装-准备工作

    估计要分开来两篇博客说明,因为截图较多. 1.三个节点cdh1(主节点),cdh2,cdh3 配置就用一张图来说明,因为资源有限 2.版本依赖说明: CDH-5.9.0-1.cdh5.9.0.p0.2 ...

  6. Linux 添用户报错:useradd:警告:此主目录已经存在

    建立mysql用户.组 groupadd mysql useradd -g mysql mysql 然后删除 userdel mysql 再添用户和组加时,提示: 解决方法:删除用户不用userdel ...

  7. java的对象锁和类锁

    在java编程中,经常需要用到同步,而用得最多的也许是synchronized关键字了,下面看看这个关键字的用法. 因为synchronized关键字涉及到锁的概念,所以先来了解一些相关的锁知识. j ...

  8. IIS6独立用户建立网站的方法,提高网站安全性

    在Windows server 2003系统下,用IIS架设Web服务器,合理的为每个站点配置独立的Internet来宾账号,这样可以限制Internet 来宾账号的访问权限,只允许其可以读取和执行运 ...

  9. samba 服务器

    1.apt-get install smaba 2.安装完成后apt-get install smbclient 然后就是配置那个.conf文件,这个到网上搜下ubuntu下配置smaba服务器就可以 ...

  10. Oralce分析函数

    1 列传行  listagg(city,',')  within GROUP (order by city)    over (partition by nation) rank with temp ...