HDU-3974 Assign the task题解报告【dfs序+线段树】
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
#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序+线段树】的更多相关文章
- HDU 3974 Assign the task 并查集/图论/线段树
Assign the task Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- BZOJ 3252题解(贪心+dfs序+线段树)
题面 传送门 分析 此题做法很多,树形DP,DFS序+线段树,树链剖分都可以做 这里给出DFS序+线段树的代码 我们用线段树维护到根节点路径上节点权值之和的最大值,以及取到最大值的节点编号x 每次从根 ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- 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 ...
- 【XSY2667】摧毁图状树 贪心 堆 DFS序 线段树
题目大意 给你一棵有根树,有\(n\)个点.还有一个参数\(k\).你每次要删除一条长度为\(k\)(\(k\)个点)的祖先-后代链,问你最少几次删完.现在有\(q\)个询问,每次给你一个\(k\), ...
- BZOJ1103 [POI2007]大都市meg dfs序 线段树
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1103 题意概括 一棵树上,一开始所有的边权值为1,我们要支持两种操作: 1. 修改某一条边的权值为 ...
- 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 ...
- 洛谷P3178 [HAOI2015]树上操作(dfs序+线段树)
P3178 [HAOI2015]树上操作 题目链接:https://www.luogu.org/problemnew/show/P3178 题目描述 有一棵点数为 N 的树,以点 1 为根,且树点有边 ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [51nod 1681]公共祖先(dfs序+线段树合并)
[51nod 1681]公共祖先(dfs序+线段树合并) 题面 给出两棵n(n<=100000)个点的树,对于所有点对求它们在两棵树中公共的公共祖先数量之和. 如图,对于点对(2,4),它们在第 ...
随机推荐
- Java构造方法与析构方法实例剖析
Java构造方法 类有一个特殊的成员方法叫作构造方法,它的作用是创建对象并初始化成员变量.在创建对象时,会自动调用类的构造方法. 构造方法定义规则:Java 中的构造方法必须与该类具有相同的名字,并且 ...
- 现有新的iOS更新可用,请从iOS12 beta版进行更新.解决方案
问题描述: ios系统一直弹出“现有新的iOS更新可用,请从iOS12 beta版进行更新”的提示,很烦的. 应该只出现在安装测试版ios12的手机上. 解决方案: 删除描述文件无法解决. 有网友机制 ...
- Unity编辑器扩展 Chapter3--Create Custom Inspector
一.Create Custom Inspector 重绘inspector面板一方面是我们的挂在脚本的窗口变得友好,另一方面可以让其变得更强大,比如添加一些有效性验证. 二.重要说明 1.Editor ...
- 剑指offer-数值的整数方
数值的整数方 一.问题描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 二.算法思路 按照指数Exp的情况进行讨论. Exp> ...
- 20170928-3 四则运算psp
1.本周psp: 2.本周进度条: 3.累计进度图(折线图): 4.psp饼状图:
- Android 中调用本地命令
Android 中调用本地命令 通常来说,在 Android 中调用本地的命令的话,一般有以下 3 种情况: 调用下也就得了,不管输出的信息,比如:echo Hello World.通常来说,这种命令 ...
- 20162316刘诚昊 第八周实验报告:实验二 Java面向对象程序设计
实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验要求 1.没有Linux基础的同学建议先学习<L ...
- OpenCV学习笔记——腐蚀与膨胀
1.膨胀 此操作将图像 与任意形状的内核 (),通常为正方形或圆形,进行卷积. 内核 有一个可定义的 锚点, 通常定义为内核中心点. 进行膨胀操作时,将内核 划过图像,将内核 覆盖区域的最大相素值提取 ...
- 我是IT小小鸟(读后感)
序 1.兴趣,这本书第一个点讲兴趣,可是在中国填鸭式的教育下,有兴趣也被这种教育给泯灭了. 2.他山之石,可以攻玉.但不可照搬.这点我非常赞同作者的看法.别人东西你拿来,一定要在他的基础上进行创 ...
- 小白用Android MVP-初体验(一)
写android以来,一直都是采用MVC的模式,所有的业务逻辑,网络请求等都放在了View中,即Activity或者Fragment中.看了一些mvp文章,很多跨度较大,也因为自己造诣不够,还不能跟上 ...