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. 可能是一篇(抄来的)min25学习笔记

    可能是一篇(抄来的)min25学习笔记 一个要求很多的积性函数 我们考虑有一个积性函数,这个函数满足可以快速计算质数处的值 且质数可以写成一个多项式的形式--而且这个多项式如果强行套在合数上,满足积性 ...

  2. Vue CLI 3开发中屏蔽的EsLint错误 (.eslintrc.js 在vue3+中 修改这个)

    1.关闭eslint校验有了eslint的校验,可以来规范开发人员的代码,是挺好的.但是有些像缩进.空格.空白行之类的规范,在开发过程中一直报错,未免太过于苛刻了.所以,我还是会选择关闭eslint校 ...

  3. java实现带过期时间的缓存

    private static ScheduledExecutorService swapExpiredPool = new ScheduledThreadPoolExecutor(10); priva ...

  4. pause的作用

    重要概念:Pod内的容器都是平等的关系,共享Network Namespace.共享文件 pause容器的最主要的作用:创建共享的网络名称空间,以便于其它容器以平等的关系加入此网络名称空间 pause ...

  5. (转)从0移植uboot (二) _uboot启动流程分析

    ref:https://www.cnblogs.com/xiaojiang1025/p/6496704.html 经过了上一篇的配置,我们已经执行make就可以编译出一个uboot.bin,但这还不够 ...

  6. js:把字符串转为变量使用; js下将字符串当函数去执行的方法

    1 把字符串当变量使用 通过计算 string 得到的值(如果有的话).该方法只接受原始字符串作为参数 demo: var type = "car"; var newStr = & ...

  7. List 集合的常用方法总结

    @org.junit.Test public void testListToCompare() { List<String> list1 = new ArrayList<>() ...

  8. C#获取客户端Ip工具类

    string pcname = Dns.GetHostName(); string ip = Dns.GetHostAddresses(pcname).First().ToString(); usin ...

  9. (三)Spring框架之事务管理

    一.编程式事务管理 Spring事务管理器的接口是org.springframework.transaction.PlatformTransactionManager,事务管理器接口PlatformT ...

  10. (五)Maven中的聚合和继承

    一.为什么要聚合? 定义:我们在开发过程中,创建了2个以上的模块,每个模块都是一个独立的maven project,在开始的时候我们可以独立的编译和测试运行每个模块,但是随着项目的不断变大和复杂化,我 ...