描述
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

题意

有N个雇员,然后有N-1行每行u,v代表v是u的老板,当老板有任务时,他会让他的下属一起做这个任务

有M个操作,操作C代表询问雇员X当前的任务是什么,没有输出-1,操作T代表给雇员X一个任务Y

题解

首先可以看出一个关系树,我们知道dfs时间戳可以知道每个节点为根开始访问的第一个节点(本身)s和最后一个节点e

然后我们用线段树去维护

对于操作C,我们直接单点s[x]查询

对于操作T,我们通过dfs序,区间[s[x],e[x]]修改延迟标记

代码

 #include<stdio.h>
#include<string.h>
#include<vector>
using namespace std; const int N=5e4+; int n,ans;
int s[N],e[N],vis[N],tot;
int lazy[N<<];
vector<int>G[N]; void PushDown(int rt)
{
if(lazy[rt]!=-)
{
lazy[rt<<]=lazy[rt<<|]=lazy[rt];
lazy[rt]=-;
}
} void Update(int L,int R,int task,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
lazy[rt]=task;
return;
}
int mid=(l+r)>>;
PushDown(rt);
if(L<=mid)Update(L,R,task,l,mid,rt<<);
if(R>mid)Update(L,R,task,mid+,r,rt<<|);
} void Query(int L,int l,int r,int rt)
{
if(L==l&&L==r)
{
ans=lazy[rt];
return;
}
int mid=(l+r)>>;
PushDown(rt);
if(L<=mid)Query(L,l,mid,rt<<);
else Query(L,mid+,r,rt<<|);
} void dfs(int u)
{
tot++;
s[u]=tot;
for(auto X:G[u])dfs(X);
e[u]=tot;
} int main()
{
int t,q,u,v,x,y,o=;
char op[];
scanf("%d",&t);
while(t--)
{
printf("Case #%d:\n",o++);
memset(vis,,sizeof vis);
memset(lazy,-,sizeof lazy);
scanf("%d",&n);
for(int i=;i<=n;i++)G[i].clear();
for(int i=;i<n;i++)
{
scanf("%d%d",&u,&v);
vis[u]=;
G[v].push_back(u);
}
///dfs序
for(int i=;i<=n;i++)
if(!vis[i]){tot=;dfs(i);break;}
scanf("%d",&q);
for(int i=;i<q;i++)
{
scanf("%s",op);
if(op[]=='C')
{
scanf("%d",&x);
Query(s[x],,n,);
printf("%d\n",ans);
}
if(op[]=='T')
{
scanf("%d%d",&x,&y);
Update(s[x],e[x],y,,n,);
}
}
}
return ;
}

HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)的更多相关文章

  1. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

  2. HDU 3974 Assign the task(dfs建树+线段树)

    题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...

  3. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  4. [Assign the task][dfs序+线段树]

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...

  5. 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 ...

  6. HDU 3974 Assign the task(简单线段树)

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

  7. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

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

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

  9. Assign the task-HDU3974 dfs序+线段树

    题意: 一个公司有n个员工,每个员工都有一个上司,一个人下属的下属也是这个人的下属,因此可将他们的关系看成一棵树, 然后给定两种操作,C操作是查询当前员工的工作,T操作是将y工作分配给x员工,当一个人 ...

随机推荐

  1. scala 爬虫 去除不能存储的特殊字符

    scala 爬虫 去除不能存储的特殊字符 /** * 去除不能存储的特殊字符 */ def zifuChange(str: String): String = { var bo = true var ...

  2. Leetcode 题解 Trapping Rain Water

    题目链接:https://leetcode.com/problems/trapping-rain-water/description/ 思路: 1.先找到最左侧第一个高度不为0的柱子i. 2.从i+1 ...

  3. javascript:控制一个元素高度始终等于浏览器高度

    window.onresize = function(){ this.opHtight()} //给浏览器添加窗口大小改变事件window.onresize = function(){ this.op ...

  4. AS_简单的开始

    1.注释   单行注释  //           多行注释  /* src */ 2.变量   变量名,可以包含字母.数字.下划线.$.但不以数字开头.   变量类型,是严格数据类型.AS有静态类型 ...

  5. C++复习:异常

    异常处理机制专题 前言 1)异常是一种程序控制机制,与函数机制独立和互补     函数是一种以栈结构展开的上下函数衔接的程序控制系统,异常是另一种控制结构,它依附于栈结构,却可以同时设置多个异常类型作 ...

  6. C++操作oracle数据库

    数据库操作方式:可以采用ADO方式,也可以采用oracle本身提供的Proc*C/C++或者是OCCI方式操作数据库.  连接方式:可以是客户端连接.也可以是服务器端连接.  数据库配置:无论是何种连 ...

  7. OPENWRT路由3G拔号实验

    以下摘自:http://www.right.com.cn/forum/thread-155168-1-1.html 首先下载 Barrier Breaker 14.07 固件 配置好网络,可以访问到i ...

  8. 吴裕雄 02-mysql PHP语法

    mysqli_connect($connect);mysqli_query($connect,"SQL 语句");mysqli_fetch_array()mysqli_close( ...

  9. 历届试题 大臣的旅费-(树的直径+dfs)

    问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首 ...

  10. ADO.Net 数据库 删除

    删除数据库里的信息和之前增加,修改大同小异,其写法更加简单,也是把SQL语句写为删除语句 删除一条数据,只需要获取并接收到这条数据唯一的能够代表这条数据的信息,比如主键 代码演示: using Sys ...