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员工,当一个人 ...
随机推荐
- ---github git clone 加速
https://www.zhihu.com/question/27159393/answer/35528173 git config --global http.postBuffer 52428800 ...
- ABAP-计算器-动态表达式
data:lv_value type string. call function 'EVAL_FORMULA' exporting formula = '90 <= 90' program = ...
- es6新特性之箭头函数
<script> { // es3,es5 var evens = [1, 2, 3, 4, 5]; var odds = evens.map(function (v) { return ...
- Java如何判断当前系统是Windows 还是LInux
- 安装 nginx
一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...
- list<T>中的按特定顺序排序
前段时间有个任务,就是把参数要按特定顺序排序,就是要是在一张大的参数表中,只选取,2,5,12,9,13,10 这几个参数,并按上述顺序进行排序. 假设这个参数在一个类中.例如: 上述参数序列就存在P ...
- 【375】COMP 9021 相关笔记
1. Python 中的逻辑否定用 not 2. 对于下面的代码直邮输入整数才能运行,无论字符串或者浮点型都会报错 int(input('How many games should I simulat ...
- mysql 5.7.3.0-m13安装教程
安装mysql百度经验地址:(默认安装,除了选择不更新和选择保存路径,其它基本是下一步下一步) http://jingyan.baidu.com/article/7e440953d6f0702fc1e ...
- k近邻算法(KNN)
k近邻算法(KNN) 定义:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别. from sklearn.model_selection ...
- kotlin函数api
原 Kotlin学习(4)Lambda 2017年09月26日 21:00:03 gwt0425 阅读数:551 记住Lambda的本质,还是一个对象.和JS,Python等不同的是,Kotlin ...