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.InputThe 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)OutputFor 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个人有着上司下属关系【一棵树】。开始每个人的任务是-1,当一个人分到一个任务时,他的下属也同时分到这个任务【即这个节点一下所有节点都被重新赋值为新值】。要求支持两种操作:
C x y  询问x号当前的任务
T x y 将任务y交给x号
题解:
这个问题就是整棵子树赋值的问题。如果暴力赋值会超时,能不能像线段树一样打个懒标记?但我们询问节点不想线段树一样是从上到下询问的,也就是说这个懒标记无法下传。怎么办?
那我们就将树改为序列。
怎么做呢?
考虑一下树的dfs遍历的dfs序:在访问u节点后,它的儿子节点的dfs序一定在它之后并且时连续的,那么我们就可以利用dfs序化树为序列。
在分配任务时,只需将[dfn[u],dfn[u]+siz-1]这个区间赋值就好了【siz是以u为根的子树的大小】
然后就是套线段树模板了。由于只询问单点,所以非叶节点的值大可随意搞搞,但别溢出了= =
【还有。别忘了每次初始化= =】
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=50005,INF=200000000; inline int read(){
int out=0,flag=1;char c=getchar();
while(c<48||c>57) {if(c=='-') flag=-1;c=getchar();}
while(c>=48&&c<=57) {out=out*10+c-48;c=getchar();}
return out*flag;
} int N,M,L,R,num[4*maxn],lazy[4*maxn],index[maxn],root=1,siz=0; class node{
public:
int lson,rb,siz,f;
node() {lson=rb=0;siz=1;f=0;}
}e[maxn]; void preorder(int u){
index[u]=++siz;
for(int k=e[u].lson;k;k=e[k].rb){
preorder(k);
e[u].siz+=e[k].siz;
}
} void init(){
memset(lazy,-1,sizeof(lazy));
memset(num,-1,sizeof(num));
siz=0;
root=1;
for(int i=0;i<=N;i++){e[i].lson=e[i].rb=e[i].f=0;e[i].siz=1;}
int T=N-1,a,f;
while(T--){
a=read();
f=read();
e[a].rb=e[f].lson;
e[a].f=f;
e[f].lson=a;
}
while(e[root].f) root=e[root].f;
preorder(root);
} void pd(int u){
lazy[u<<1]=lazy[u<<1|1]=num[u<<1]=num[u<<1|1]=lazy[u];
lazy[u]=-1;
} void Set(int u,int l,int r,int x){
if(l>=L&&r<=R) num[u]=lazy[u]=x;
else{
if(lazy[u]!=-1) pd(u);
int mid=(l+r)>>1;
if(mid>=L) Set(u<<1,l,mid,x);
if(mid<R) Set(u<<1|1,mid+1,r,x);
num[u]=x;
}
} int Query(int u,int l,int r){
if(l==r) return num[u];
else{
if(lazy[u]!=-1) pd(u);
int mid=(l+r)>>1;
if(mid>=L) return Query(u<<1,l,mid);
else return Query(u<<1|1,mid+1,r);
}
} int main()
{
int T=read(),a,b;
char cmd;
for(int t=1;t<=T;t++){
printf("Case #%d:\n",t);
N=read();
init();
M=read();
while(M--){
cmd=getchar();
while(cmd!='T'&&cmd!='C') cmd=getchar();
if(cmd=='T'){
a=read();
b=read();
L=index[a];
R=L+e[a].siz-1;
Set(1,1,N,b);
}
else{
L=R=index[read()];
printf("%d\n",Query(1,1,N));
}
}
}
return 0;
}

HDU-3974 Assign the task题解报告【dfs序+线段树】的更多相关文章

  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. BZOJ 3252题解(贪心+dfs序+线段树)

    题面 传送门 分析 此题做法很多,树形DP,DFS序+线段树,树链剖分都可以做 这里给出DFS序+线段树的代码 我们用线段树维护到根节点路径上节点权值之和的最大值,以及取到最大值的节点编号x 每次从根 ...

  3. HDU.5692 Snacks ( DFS序 线段树维护最大值 )

    HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...

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

  5. 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树

    题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...

  6. BZOJ1103 [POI2007]大都市meg dfs序 线段树

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...

  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. 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)

    P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...

  9. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  10. [51nod 1681]公共祖先(dfs序+线段树合并)

    [51nod 1681]公共祖先(dfs序+线段树合并) 题面 给出两棵n(n<=100000)个点的树,对于所有点对求它们在两棵树中公共的公共祖先数量之和. 如图,对于点对(2,4),它们在第 ...

随机推荐

  1. javaweb(十一)——使用Cookie进行会话管理

    一.会话的概念 会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话. 有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学曾 ...

  2. CentOS 下 MySQL 5.6 基于 RPM 的下载、安装、配置

    CentOS 下 MySQL 5.6 基于 RPM 的下载.安装.配置 系统: CentOS 7 x86_64 MySQL 版本: 5.6.40 安装方式: RPM 下载 下载地址 操作系统 选择 R ...

  3. windows下Mysql安装启动及常用操作

    1.下载mysql https://dev.mysql.com/downloads/ 2.配置环境变量 变量名:MYSQL_HOME 变量值:E:\MySql\mysql-8.0.15-winx64\ ...

  4. Lua学习笔记(2): 流程控制与循环以及初涉迭代器

    条件判断语句 --if...语句 if (表达式) then --表达式为1时执行的语句 end --if...else语句 if (表达式) then --表达式为1时执行的语句 else --表达 ...

  5. 袋鼠云研发手记 | 开源·数栈-扩展FlinkSQL实现流与维表的join

    作为一家创新驱动的科技公司,袋鼠云每年研发投入达数千万,公司80%员工都是技术人员,袋鼠云产品家族包括企业级一站式数据中台PaaS数栈.交互式数据可视化大屏开发平台Easy[V]等产品也在迅速迭代.在 ...

  6. [T-ARA][HOLIDAY]

    歌词来源:http://music.163.com/#/song?id=22704407 HOLI HOLI DAY [HOLI HOLI DAY] 뚜뚜 뚜루루 [ddu-ddu ddu-lu-lu ...

  7. “Hello World!”团队第六周的第一次会议

    今天是我们团队“Hello World!”团队第六周召开的第一次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.Todo List 六.会议照片 七.燃尽图 一.会议时间 ...

  8. python 中如何计算时间差...

    Q:如何方便的计算两个时间的差,如两个时间相差几天,几小时等 A:使用datetime模块可以很方便的解决这个问题,举例如下: >>> import datetime>> ...

  9. MySQL 备份和恢复 理论知识

    为什么要备份 数据无价   制定备份策略的注意点 1:可容忍丢失多少数据     2:恢复需要在多长时间内完成     3:备份的对象   数据.二进制日志和InnoDB的事务日志.SQL代码(存储过 ...

  10. 团队项目-BUG挖掘

    测试硬件: 华为畅享5 测试平台: 安卓5.1 测试项目Git地址: https://github.com/RABITBABY/We-have-bing 测试Apk来源地址: http://www.a ...