描述
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. centos查看命令

    1.查看 CPU 物理个数 grep 'physical id' /proc/cpuinfo | sort -u | wc -l 2.查看 CPU 核心数量 grep 'core id' /proc/ ...

  2. cookies_ajax

    views def test_user(request): print('start') if request.method=='POST': print('goon_test_user') user ...

  3. Zookeeper原理架构

    Zookeeper到底是什么!? 学一个东西,不搞明白他是什么东西,哪还有心情学啊!! 首先,Zookeeper是Apache的一个java项目,属于Hadoop系统,扮演管理员的角色. 然后看到官网 ...

  4. Logstash收集nginx日志之使用grok过滤插件解析日志

    grok作为一个logstash的过滤插件,支持根据模式解析文本日志行,拆成字段. nginx日志的配置: log_format main '$remote_addr - $remote_user [ ...

  5. Maven仓库—Nexus环境搭建及使用

    使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包 http://blog.csdn.net/yanjun008/article/details/42084109 Nexus介 ...

  6. PHP 操作Mongodb 实例

    缩略版本<?php //1.连接MongoDB $mongo = new Mongo(); $mongo = new Mongo("mongodb://username:passwor ...

  7. Android事件拦截机制 - 两句话

    模拟情形:ViewGroupA ->ViewGroupB->View False往下走,True就停下.(适用于事件传递和事件处理)

  8. 前端Web安全介绍及规避。。。

    本文转载自:https://jelon.top/posts/web-security/ 如果侵权,请及时告知. 一.跨站脚本攻击 (xss) 反射型跨站脚本攻击 攻击者会通过社会工程学手段,发送一个 ...

  9. 编程四剑客sed-2019.2.20

    sed    [-Options]     [‘Commands’]    filename; sed工具默认处理文本,文本内容输出屏幕已经修改,但是文件内容其实没有修改,需要加-i参数即对文件彻底修 ...

  10. 学JS的心路历程 -物件与原型(二)

    昨天有提到说Object.setPrototypeOf可以指定一个物件为另一个物件的原型,但有想过到底这个原型,也就是[[Prototype]]最终会到何处吗? 答案是Object.prototype ...