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. es查询优化思路

    尽可能的利用内存 将尽可能的索引留在内存,即留更多的堆外内存给es 不查询的字段尽量不要往es插入,节省索引的空间大小(es + hbase) 数据预热 冷热数据分离 文档字段设计 根据查询场景设计字 ...

  2. db2 数据库配置HADR+TSA添加集群节点

    Db2配置HADR高可用+TSA添加集群节点 一.服务器资源 Master IP:10.78.10.1 数据库:dbclassSlave IP:10.78.10.2 数据库:dbclassVIP:10 ...

  3. Windows下jmap命令报错问题

       最近换了笔记本,新的工作环境下jmap命令居然在报错,而jps.jstat.jinfo.jstack都能正常使用,所以初步排除进程号的问题. Attaching to core 17536 fr ...

  4. Bean的三种实例化方式

    在面向对象程序中,如要使用某个对象,就需要先实例化这个对象.同样的,在Spring中,要想使用容器中的Bean,也需要实例化Bean.实例化Bean有三种方式,分别是:构造器实例化.静态工厂实例化.实 ...

  5. Error:Could not find method google() for arguments [] on repository container

    Error:Could not find method google() for arguments [] on repository container. Consult IDE log for m ...

  6. Tomcat与WAS应用中间件差异化分析研究

    --转载 http://blog.chinaunix.net/uid-25723371-id-5759072.html 目前我们在使用的基于JAVA的提供逻辑展现应用中间件有两种,一种是以商用软件WA ...

  7. 轻松搭建CAS 5.x系列(8)-在CAS Server增加双因素认证(DUO版)

    概述说明 为了让系统更加安全,很多登录会加入双因素认证.何为双因素,如果把登陆作为开一扇门的话,那就是在原来的锁上再加一把锁,第二锁用新的钥匙,这样安全系数就更加高了. CAS是通过账号名和密码来认证 ...

  8. js中 this 的指向

    js中 this的指向一共存在3种地方: 1.全局的this; 2.构造函数的this; 3.call/apply; 一.全局的this: function test(){ this.d = 3;// ...

  9. Linux 安装Mysql(图文教程)

    原文:Linux 安装Mysql(图文教程) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net ...

  10. C#基础--AbStract与Interface

         Interface: 接口方法不能用public abstract等修饰.接口内不能有字段变量,构造函数. 接口内可以定义属性,如string color{get;set;}这种. 实现接口 ...