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 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序+线段树单点查询,区间修改)的更多相关文章
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- HDU 3974 Assign the task(dfs建树+线段树)
题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- [Assign the task][dfs序+线段树]
http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...
- 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 ...
- HDU 3974 Assign the task(简单线段树)
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- HDU3974 Assign the task —— dfs时间戳 + 线段树
题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...
- Assign the task-HDU3974 dfs序+线段树
题意: 一个公司有n个员工,每个员工都有一个上司,一个人下属的下属也是这个人的下属,因此可将他们的关系看成一棵树, 然后给定两种操作,C操作是查询当前员工的工作,T操作是将y工作分配给x员工,当一个人 ...
随机推荐
- day24-抽象类与接口类
接口类 1.继承有两种用途:一:继承基类的方法,并且做出自己的改变或者扩展(代码重用) 二:声明某个子类兼容于某基类,定义一个接口类,接口类中定义了一些接口名(就是函数名)且并未实现接口的功能,子类继 ...
- day13-文件操作
1.打开与关闭 1.1.open() close()我们使用 open() 函数打开文件.这个函数将返回一个文件对象,我们对文件的读写都将使用这个对象.open() 函数需要三个参数,第一个参数是文件 ...
- ncnn 源码学习-Mat.h Mat.c
纯小白记录下腾讯的ncnn框架源码的学习.纯粹写给自己看的,不保证正确性. Mat 类似于 caffe中的blob,是一个张量的存储结构体. 一.数据成员: 1.void * data 多维数据按一位 ...
- python反汇编函数字节码
使用dis模块 >>> def test(): ... print(1) ... a = 1 ... print(a) ... >>> from dis impor ...
- Delphi的子类化控件消息, 消息子类化
所谓的子类化,网上有很多说明,我就说我个人的随意理解,可能有误,请列位看官斟酌理解. 所谓子类化,个人理解就是拦截某个控件的消息以及样式,来进行自己的特定处理以达到特殊的功能需求.这个子类化,可以有子 ...
- linux目录结构详解(以suse linux 10为例)
一.文件系统结构 位于Linux系统的最顶端即根目录是/.Linux的文件系统的入口就是/,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者. 它之下的子目录有: ...
- ANg-别人家的笔记
http://blog.csdn.net/scruelt/article/details/78997697 https://github.com/fengdu78/Coursera-ML-Andrew ...
- Java学习01 (第一遍)
java se - 桌面 java ee - 网页 Jdk :Jre 区别Jdk-开发环境必要Jre-运行环境需要Jdk包含Jre,安装完Jdk就可以了 Javac Demo1.java-Javac ...
- Android文档 学习目录
Building Your First App After you've installed the Android SDK, start with this class to learn the b ...
- VMwear安装Centos7超详细过程
本篇文章主要介绍了VMware安装Centos7超详细过程(图文),具有一定的参考价值,感兴趣的小伙伴们可以参考一下 1.软硬件准备 软件:推荐使用VMwear,我用的是VMwear 12 镜像:Ce ...