HDU3974(dfs+线段树)
Assign the task
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2008 Accepted Submission(s): 895
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 <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
}es[MAXN+MAXN];
int head[MAXN],tot;
int n,m;
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
}
struct Node{
int lazy,l,r;
}a[MAXN*];
void pushUp(int rt)
{
if(a[rt<<].lazy==a[(rt<<)|].lazy) a[rt].lazy=a[rt<<].lazy;
else a[rt].lazy=-;
}
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].lazy=-;
if(l==r)
{
return ;
}
int mid=(a[rt].l+a[rt].r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
pushUp(rt);
}
void pushDown(int rt)
{
a[rt<<].lazy=a[rt].lazy;
a[(rt<<)|].lazy=a[rt].lazy;
a[rt].lazy=-;
}
void update(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].lazy=val;
return ;
}
if(a[rt].lazy!=-)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
update(rt<<,l,r,val);
}
else if(mid<l)
{
update((rt<<)|,l,r,val);
}
else
{
update(rt<<,l,mid,val);
update((rt<<)|,mid+,r,val);
}
pushUp(rt);
}
int query(int rt,int pos)
{
if(a[rt].l==pos&&a[rt].r==pos)
{
return a[rt].lazy;
}
if(a[rt].lazy!=-)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(pos<=mid)
{
return query(rt<<,pos);
}
else
{
return query((rt<<)|,pos);
}
}
int l[MAXN],r[MAXN],key,deg[MAXN],root;
void dfs(int u)
{
l[u]=++key;
for(int i=head[u];i!=-;i=es[i].net)
{
dfs(es[i].to);
}
r[u]=key;
}
int main()
{
int T;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
memset(head,-,sizeof(head));
tot=;
key=;
memset(deg,,sizeof(deg));
scanf("%d",&n);
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(v,u);
deg[u]++;
}
for(int i=;i<=n;i++)
{
if(deg[i]==)
{
root=i;
}
}
dfs(root);
build(,,key);
scanf("%d",&m);
printf("Case #%d:\n",cas);
for(int i=;i<m;i++)
{
scanf("%*c");
char op;
scanf("%c",&op);
if(op=='C')
{
int x;
scanf("%d",&x);
int res=query(,l[x]);
printf("%d\n",res);
}
else
{
int x,y;
scanf("%d%d",&x,&y);
update(,l[x],r[x],y);
}
}
}
return ;
}
HDU3974(dfs+线段树)的更多相关文章
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线
Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...
- dfs+线段树 zhrt的数据结构课
zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...
- 【Codeforces-707D】Persistent Bookcase DFS + 线段树
D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
- 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树
原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...
- hdu 5692(dfs+线段树) Snacks
题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- hdu-2586 How far away ?(lca+bfs+dfs+线段树)
题目链接: How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- 【WPF学习笔记】之如何传递第一个登录界面的值到下一个页面显示:动画系列之(三)
... ... 承接系列(二) 在之前的登录后台已设置发送到主界面: 在主界面接收传递的值: using System; using System.Collections.Generic; using ...
- 02 redis通用命令操作
set hi hello 设置值 get hi 获取值 keys * 查询出所有的key memcached 不能查询出所有的key keys *h 模糊查找key keys h[ie] 模糊查找 k ...
- 进程间通信(IPC)+进程加锁解锁
[0]README 0.1) source code and text description are from orange's implemention of a os: 0.2) for com ...
- 【Atheros】如何禁用载波侦听CSMA和BACKOFF
无线网络上发包之前都要进行载波侦听,如果未收到ACK,就指数级退避之后重传. 有时候我们为了观察网卡在全力发送数据包时的性能,要禁用CSMA和ACK,这一篇文章先来讨论怎么禁用CSMA,这里提供两种思 ...
- 关于TextView 的属性
一.设置不同的字体和颜色值:questionDesTextView=(TextView)findViewById(R.id.question_des); SpannableStringBuilder ...
- net上传文件的三种方法
ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法. 方法一:用Web控件FileUpload,上传到网站根目录. Test.aspx关 ...
- 九度OJ 1005:Graduate Admission (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5646 解决:1632 题目描述: It is said that in 2011, there are about 100 graduat ...
- 如果数据需要被多个应用程序消费的话,推荐使用 Kafka,如果数据只是面向 Hadoop 的,可以使用 Flume
https://www.ibm.com/developerworks/cn/opensource/os-cn-kafka/index.html Kafka 与 Flume 很多功能确实是重复的.以下是 ...
- mysql系列之8.mysql高可用 (keepalived)
环境: centos6.5_x64 准备: 两台mysql机器 主1 master: 192.168.32.130 主2 backup: 192.168.32.131 VIP: 192.168.3 ...
- 前端面试题(一)JS篇
内置类型 JS 中分为七种内置类型,七种内置类型又分为两大类型:基本类型和对象(Object). 基本类型有六种: null,undefined,boolean,number,string,symbo ...