描述
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. IIS asp 401.1错误

    asp程序使用非匿名帐户运行时因用户名前带了计算机名会导致出现401.1错误,只要直接输入用户名即可,不要带计算机名.

  2. oracle 修改字符集 为ZHS16GBK

    一.oracle server 端 字符集查询 select userenv('language') from dual 其中NLS_CHARACTERSET 为server端字符集 NLS_LANG ...

  3. java swing 制作一个登陆界面,亲测有效

    一.介绍 Swing 是一个为Java设计的GUI工具包. Swing是JAVA基础类的一部分. Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表. Swing提供许多比AWT ...

  4. 关于vector变量的size,是一个无符号数引发的bug。LeetCode 3 sum

    class Solution { public: vector<vector<int>> threeSum(vector<int>& a) { vector ...

  5. CSS中clear属性的both、left和right浅析

    前端开发中,我们知道clear属性有none.both.left和right四个值. 它们的具体含义如下: none:允许两边都可以有浮动对象: both:不允许有浮动对象; left:不允许左边有浮 ...

  6. U3D中可以直接使用GL!!!

    https://blog.csdn.net/u013172864/article/details/78860624

  7. 转: jquery.qrcode.js生成二维码插件&转成图片格式

    原文地址: https://blog.csdn.net/u011127019/article/details/51226104 1.qrcode其实是通过使用jQuery实现图形渲染,画图,支持can ...

  8. Python操作远程服务器paramiko模块介绍

    paramiko模块是基于Python实现的SSH远程安全连接,可以提供在远程服务器上执行命令.上传文件到服务器或者从指定服务器下载文件的功能. paramiko模块安装方法 paramiko模块不是 ...

  9. 如何配置windows定时任务

    Windows上配置任务定时执行有两种方法.一是通过控制面板中的界面配置,另外一种是通过schtasks命令配置.如果是简单的定时任务配置(比如每天单次执行)建议选择界面的方式,简洁.直观.易上手.如 ...

  10. ASP.NET 身份验证机制

    ASP.NET提供了3种认证方式:windows身份验证:IIS根据应用程序的设置执行身份验证.要使用这种验证方式,在IIS中必须禁用匿名访问.Forms验证          :用Cookie来保存 ...