http://acm.hdu.edu.cn/showproblem.php?pid=3974

Assign the task

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7144    Accepted Submission(s): 2708

Problem Description
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
题意:给一棵树,有单点查询和区间更新两种操作
题解:通过dfs序转化为区间问题,使用线段树维护即可
 #include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
#define debug(x) cout<<"["<<#x<<"]"<<" is "<<x<<endl;
typedef long long ll;
const int maxn=5e4+;
int n,head[maxn],in[maxn],out[maxn],cnt,tim;
bool f[maxn];
struct node{
int l;
int r;
int val;
int lazy;
}N[maxn<<];
struct edge{
int q;
int w;
int nex;
}e[maxn<<];
void pushup(int rt){
N[rt].val=N[rt<<].val+N[(rt<<)|].val;
return;
}
void build(int L,int R,int rt){
N[rt].l=L;
N[rt].r=R;
N[rt].lazy=;
if(L==R){
N[rt].val=-;
return;
}
build(L,(L+R)/,rt<<);
build((L+R)/+,R,(rt<<)|);
}
void pushdown(int rt){
if(N[rt].lazy){
N[rt<<].lazy=N[rt].lazy;
N[(rt<<)|].lazy=N[rt].lazy;
N[rt<<].val=N[(rt<<)|].val=N[rt<<].lazy;
N[rt].lazy=;
}
}
void update(int L,int R,int rt,int L1,int R1,int c){
if(L1<=L&&R1>=R){
N[rt].val=c;
N[rt].lazy=c;
return;
}
int mid=(L+R)/;
pushdown(rt);
if(L1<=mid)update(L,mid,rt<<,L1,R1,c);
if(R1>mid)update(mid+,R,(rt<<)|,L1,R1,c);
}
int query(int L,int R,int rt,int id){
if(L==R){
return N[rt].val;
}
int mid=(L+R)/;
pushdown(rt);
if(id<=mid)return query(L,mid,rt<<,id);
else return query(mid+,R,(rt<<)|,id);
}
void adde(int x,int y){
e[cnt].q=y;
e[cnt].w=x;
e[cnt].nex=head[y];
head[y]=cnt++;
}
void dfs(int u){
in[u]=++tim;
for(int i=head[u];i!=-;i=e[i].nex){
int v=e[i].w;
dfs(v);
}
out[u]=tim;
}
int main(){
int t;
int case1=;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
tim=;
cnt=;
for(int i=;i<=n;i++){f[i]=;head[i]=-;}
build(,n,);
for(int i=;i<n;i++){
int a,b;
scanf("%d%d",&a,&b);
adde(a,b);
f[a]=;
}
for(int i=;i<=n;i++){if(!f[i])dfs(i);}
int k;
scanf("%d",&k);
printf("Case #%d:\n",++case1);
while(k--){
char ch[];
scanf("%s",ch);
if(ch[]=='T'){
int x,xx;
scanf("%d%d",&x,&xx);
update(,n,,in[x],out[x],xx);
}
else{
int x;
scanf("%d",&x);
printf("%d\n",query(,n,,in[x]));
}
}
}
return ;
}

[Assign the task][dfs序+线段树]的更多相关文章

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

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

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

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

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

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

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

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

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

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

  7. Codeforces Round #442 (Div. 2)A,B,C,D,E(STL,dp,贪心,bfs,dfs序+线段树)

    A. Alex and broken contest time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  8. CodeForces 877E Danil and a Part-time Job(dfs序+线段树)

    Danil decided to earn some money, so he had found a part-time job. The interview have went well, so ...

  9. Educational Codeforces Round 6 E dfs序+线段树

    题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...

随机推荐

  1. 小程序使用mpvue框架无缝接入Vant Weapp组件库

    有美团开源出的mpvue以其vue的语法和良好的开发效率再搭配上用户体验良好的UI组件无疑是定制化微信小程序的开发方式,然而由于mpvue是对微信原生开发的再次封装,这也为我们引入UI组件添加了不少麻 ...

  2. PHP切割整数工具,类似微信红包金额分配

    Composer地址:https://packagist.org/packages/werbenhu/php-number-slicing GitHub地址:https://github.com/we ...

  3. ABP的UnitOfWork内部SaveChanges无效

    应用层一个AppService默认是一个工作单元,默认是开启的,默认是事务的.因为应用服务方法应该是原子的且一般都会使用数据库. 但是有些情况需要关闭工作单元 1.AppService有多个操作需要操 ...

  4. PAT-1001 A+B Format (20 分) 注意零的特例

    Calculate a+b and output the sum in standard format -- that is, the digits must be separated into gr ...

  5. 不遮挡人物弹幕是怎么实现的——图片蒙版效果-webkit-mask

    这是一个实验中的功能,用于设置元素上遮罩层的图像. 一.Values none:默认值,透明的黑色图像层,也就是没有遮罩层. <mask-source>:<mask>或CSS图 ...

  6. (五)Activiti之获取流程定义图片和流程定义删除

    一.获取流程定义图片 /** * 通过流程部署ID获取流程图图片 */ @Test public void getImageById()throws Exception{ InputStream in ...

  7. Java MergeSort

    Java MergeSort /** * <html> * <body> * <P> Copyright 1994-2018 JasonInternational ...

  8. 再谈 pm2

    比forever好用,对Ubuntu和centos更友好,性能监控.自动重启.负载均衡,适用于laas场景, 总之,pm2是一个带有负载均衡功能的Node应用的进程管理器,可以实现在云服务器上,对no ...

  9. VS2019打开项目加载失败:无法找到 .NET Core SDK。请检查确保已安装此项且 global.json 中指定的版本(如有)与所安装的版本相匹配。

    问题描述: 用VS2019创建了asp.net core项目,正常运行:过几天后,再次打开,发现无法加载项目,报错无法找到.net core sdk.   分析过程: 首先怀疑环境变量的问题,重新设置 ...

  10. 开发环境,不用每次都ant自动编译

    公司所用ant技术,每次改个java文件,配置文件都需要重新编译一次发布 在实际搭环境的过程发现,ant就是把项目目录下的文件编译成功后的搬移到到 ,Tomcat 运行环境配置的目录下,凡是修改的文件 ...