Assign the task

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
题目大意:一个工厂有n员工,员工之间有不同的下属关系,现在有n-1个关系将n个人构成一棵关系树,给某个人分配任务,这个人会将自己当前的任务分配给他的所有属下。给定m次操作,一种操作是询问x员工的当前任务,另一种操作是给x员工分配y任务。
思路:由于现在是一棵关系树,我们可以将按子节点的个数划分区间,对于某个节点,从根节点遍历到当前节点的num数为该节点的左区间,遍历完当前节点所有子节点之后的num为该节点的右区间,那么当前的区间就包含了当前节点的所有子节点。 我觉得难点就是在这里吧!根据树划分区间,之后的都是线段树的基本操作了
 
 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio> using namespace std;
const int maxn = ;
struct node{
int to,next;
}e[maxn];
int a[maxn<<],add[maxn<<];
int f[maxn],head[maxn];
int ls[maxn],rs[maxn];
int n,m,T,cnt,num;
void ade(int u,int v)//加边 便于dfs搜索
{
e[cnt].to = u;
e[cnt].next = head[v];
head[v] = cnt++;
}
int Find(int x){//并查集
if(x!=f[x])
f[x] = Find(f[x]);
return f[x];
}
void dfs(int rt)//求出当前节点覆盖的区间左值和右值
{
int x = head[rt];
ls[rt] = ++num;
while(x!=-){
dfs(e[x].to);
x = e[x].next;
}
rs[rt] = num;
}
void pushdown(int rt)
{
if(add[rt]){
a[rt<<] = a[rt];
a[rt<<|] = a[rt];
add[rt<<] = add[rt];
add[rt<<|] = add[rt];
add[rt] = ;
}
}
void build(int l,int r,int rt)
{
a[rt] = -;//
add[rt] = ;//此处初始化所有点线段树节点都为-1和0;
if(l==r)return;
int mid = (l+r)>>;
build(l,mid,rt<<);
build(mid+,r,rt<<|);
}
void update(int L,int R,int C,int l,int r,int rt)
{
if(L<=l&&r<=R){
a[rt] = C;
add[rt] = C;
return;
}
pushdown(rt);//下推懒惰标记
int mid = (l+r)>>;
if(L<=mid)update(L,R,C,l,mid,rt<<);
if(R>mid)update(L,R,C,mid+,r,rt<<|);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)return a[rt];
pushdown(rt);//下推懒惰标记
int mid = (l+r)>>;
int ans = ;
if(L<=mid) ans += query(L,R,l,mid,rt<<);
if(R>mid) ans += query(L,R,mid+,r,rt<<|);
return ans;
}
int main()
{
scanf("%d",&T);
for(int t=;t<=T;t++){
scanf("%d",&n);
cnt = ,num = ;
for(int i=;i<=n;i++){
f[i]=i;head[i]=-;
ls[i]=,rs[i]=;
}
for(int l,r,i=;i<n;i++){
scanf("%d%d",&l,&r);
f[l]=r;
ade(l,r);
}
int root = Find();//利用并查集的思想求出根节点
dfs(root);//根据根节点 求出每个节点覆盖的区间 区间左值在ls中,右值在rs中
// cout<<ls[root]<<" "<<rs[root]<<endl;
// for(int i=1;i<=n;i++)
// cout<<ls[i]<<" "<<rs[i]<<endl;
// getchar();
build(,n,);
scanf("%d",&m);
char op;int x,y;
printf("Case #%d:\n",t);
while(m--){
scanf(" %c",&op);
if(op=='C'){
scanf("%d",&x);
printf("%d\n",query(ls[x],ls[x],,n,));//区间的单点查询
}else if(op=='T'){
scanf("%d%d",&x,&y);
update(ls[x],rs[x],y,,n,);//区间更新当前的任务
}
}
}
return ;
}

HDU 3974 Assign the task的更多相关文章

  1. HDU 3974 Assign the task 并查集/图论/线段树

    Assign the task Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  2. HDU 3974 Assign the task 暴力/线段树

    题目链接: 题目 Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...

  3. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

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

  6. HDU 3974 Assign the task 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 题目大意: 一个公司有N个员工,对于每个员工,如果他们有下属,那么他们下属的下属也是他的下属. 公司会给员 ...

  7. hdu 3974 Assign the task(线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=3974 题意:给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更 ...

  8. hdu 3974 Assign the task (线段树+树的遍历)

    Description There is a company that has N employees(numbered from 1 to N),every employee in the comp ...

  9. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

随机推荐

  1. mac上SVN的图形工具 SmartSVN注册

    mac上SVN的图形工具 SmartSVN注册 打开smartsvn,选中license注册 选中文件smartsvn.license,下一步下一步就ok了 smartsvn.license Name ...

  2. ELK学习之jdk和jre的区别

    JRE: Java Runtime EnvironmentJDK:Java Development KitJRE顾名思义是java运行时环境,包含了java虚拟机,java基础类库.是使用java语言 ...

  3. Spring → 03:核心机制

    一.控制反转 1.1.控制反转的概念 (1).Inverse of Controller被称为控制反转或反向控制,其实真正体现的是“控制转移”.(2).所谓的控制指的是负责对象关系的指定.对象创建.初 ...

  4. 全球首个百万IOPS云盘即将商业化 阿里云推出超高性能云盘ESSD

    近日,在经过近半年的上线公测后,阿里云全球首个跨入IOPS百万时代的云盘——ESSD即将迎来商业化,单盘IOPS高达100万,这是阿里云迄今为止性能最强的企业级块存储服务. 搭配ECS云服务器使用, ...

  5. css 的float和inline-block区别

    CSS布局创建网站,浮动绝对占据了很大的比例.大块区域如主内容及侧边栏,以及在其中的小块区域,都可以看到浮动的影子.这里浮动是唯一的解决方案吗? 浮动通常表现正常,但有时候搞起来会很纠结.特别是处理内 ...

  6. Python学习之路5☞文件处理

    一.文件处理流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 正趣果上果 Interesting fruit fruit 词:郭婞 曲:陈粒 编曲/混音/和声:燕池 萧: ...

  7. linux查看用户组所有成员

    1.grep 'user1' /etc/group //找出用户组的gid user1:x:1004://得出gid=1004 2. awk -F":" '{print $1&qu ...

  8. COGS-2638 区间与,异或,询问max

    本篇题解参考了这个博客 题目链接 我们利用线段树来维护区间第最大值,考虑如何修改 每一次进行与操作时只有z的二进制为0的位会产生影响 每一次进行或操作时只有z的二进制为1的位会产生影响 所以只要该区间 ...

  9. 【NS2】各种TCP版本 之 TCP Tahoe 和 TCP Reno(转载)

    实验目的 学习TCP的拥塞控制机制,并了解TCP Tahoe 和 TCP Reno的运行方式. 基础知识回顾 TCP/IP (Transmission Control Protocol/Interne ...

  10. Java 调用restful webservice & jackson

    package com.bullshit.webcrawler.client.impl; import java.io.BufferedReader; import java.io.IOExcepti ...