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. Error Retries and Exponential Backoff in AWS

    Error Retries and Exponential Backoff in AWS https://docs.aws.amazon.com/general/latest/gr/api-retri ...

  2. Win10 自定义鼠标右键菜单

    1. 点击文件鼠标右键显示软件 1.1 步骤 win+R输入regedit进入注册表 定位到HKEY_CLASSES_ROOT\*\shell下 在shell创建一个你想要的右键文件 例如:Kinok ...

  3. LC 3. Longest Substring Without Repeating Characters

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

  4. IDEA中通过Maven插件使用MyBatis Generator

    这样做更简单,参考: IDEA集成MyBatis Generator 插件 详解

  5. MySQL 5.7 多源复制实践

    多源复制使用场景 数据分析部门会需要各个业务部门的部分数据做数据分析,这个时候就可以用到多源复制把各个主数据库的数据复制到统一的数据库中. 在从服务器进行数据汇总,如果我们的主服务器进行了分库分表的操 ...

  6. Linux自动运维工具Ansible的使用

    Linux自动运维工具Ansible的使用 我们熟悉这个工具后, 可以很轻松的安装k8s. 一.介绍 ansible - run a task on a target host(s) Ansible是 ...

  7. 【爬虫集合】Python爬虫

    一.爬虫学习教程 1. https://www.jianshu.com/u/c32d557edfa3 2. WebMagic是一个简单灵活的Java爬虫框架.基于WebMagic,你可以快速开发出一个 ...

  8. 数据库及MYSQL基础(3)-JDBC

    教学视频链接:https://edu.aliyun.com/course/1694?spm=5176.11400004.0.0.29254768sg2H5P 程序文件链接:https://pan.ba ...

  9. MySql8.0 安装重要的两步。

    1.去官网下载mysql社区版 windows安装包. 2.在安装包 安装的过程中,有一步就是启动mysql 会失败: 然后修改服务后,再次回到安装界面点击:execute,就会成功了. 先去配置一下 ...

  10. Plugin 免费CSS生成器CssCollector

    下载: 百度云 自己在做Web开发的时候,页面里会有很多样式类,一个个复制到样式表里总感觉很麻烦 网上也没有找到合适的工具,可以一键生成样式表 所以,干脆自己做一个咯~ 案例展示 花了一天时间,CSS ...