[Assign the task][dfs序+线段树]
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
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.
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)
5
4 3
3 2
1 3
5 2
5
C 3
T 2 1
C 3
T 3 2
C 3
-1
1
2
#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序+线段树]的更多相关文章
- 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 ...
- 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 ...
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- HDU3974 Assign the task —— dfs时间戳 + 线段树
题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...
- Assign the task-HDU3974 dfs序+线段树
题意: 一个公司有n个员工,每个员工都有一个上司,一个人下属的下属也是这个人的下属,因此可将他们的关系看成一棵树, 然后给定两种操作,C操作是查询当前员工的工作,T操作是将y工作分配给x员工,当一个人 ...
- HDU 3974 Assign the task(dfs建树+线段树)
题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...
- 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 ...
- 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 ...
- Educational Codeforces Round 6 E dfs序+线段树
题意:给出一颗有根树的构造和一开始每个点的颜色 有两种操作 1 : 给定点的子树群体涂色 2 : 求给定点的子树中有多少种颜色 比较容易想到dfs序+线段树去做 dfs序是很久以前看的bilibili ...
随机推荐
- C#中输入法全角转换半角
一般情况下,我们都是使用英文半角的来进行编程,包括输入框和密码框的设定一般也是英文半角,但往往有些人使用全角输入,登陆不进去还以为你系统错误,现整理了几种全角切换半角和设定输入法的几种方法. 方法一: ...
- NotePad++ 正则表达式 转
https://gerardnico.com/ide/notepad/replace https://notepad-plus-plus.org/community/topic/16787/find- ...
- SrpingBoot入门到入坟03-基于idea快速创建SpringBoot应用
先前先创建Maven项目然后依照官方文档再然后编写主程序写业务逻辑代码才建立好SpringBoot项目,这样太过麻烦,IDE都支持快速创建,下面基于idea: 使用Spring Initializer ...
- js — 字符串
目录 1. 拼接字符串 2. 获取字符的方法 3. 字符串操作方法(切片) 4. 字符串位置方法 - 索引 5. trim()方法 6. 字符串大小写转换方法 字符串 typeof 用于校验当前变量的 ...
- 【判环】Perpetuum Mobile
Perpetuum Mobile 题目描述 The year is 1902. Albert Einstein is working in the patent office in Bern. Many ...
- sqlserver 聚集索引 非聚集索引
聚集索引是一种对磁盘上实际数据重新组织以按指定的一列或者多列值排序.像我们用到的汉语字典,就是一个聚集索引.换句话说就是聚集索引会改变数据库表中数据的存放顺序.非聚集索引不会重新组织表中的数据,而是对 ...
- IDEA报错: Clone failed: Authentication failed for 'http://10.70.XXXXXXXXXXXXXXXXX'
今天从git上导入公司的项目,总是报错Clone failed: Authentication failed for 'http://10.70.XXXXXXXXXXXXXX' 在网上百度了一下,大致 ...
- 创建新表,自动授权trigger
需求 一个用户下三个表,开发人员不定时进行rename表名称,create原表名称 as old_table 插入少量数据,另一个业务用户需要访问该表,由于表名称rename导致经常需要手工授权. 需 ...
- (十)springmvc之文件的处理
一.同步上传文件 导入common-fileupload这个jar包. 配置 springmvc-servlet.xml <?xml version="1.0" en ...
- 如何使 JavaScript 更高效?
传统的 Web 页面不会包含很多脚本,至少不会太影响 Web 页面的性能.然而,Web 页面变得越来越像应用程序,脚本对其的影响也越来越大.随着越来越多的应用采用 Web 技术开发,脚本性能的提升就变 ...