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+线段树)的更多相关文章

  1. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  2. Codeforces1110F Nearest Leaf dfs + 线段树 + 询问离线

    Codeforces1110F dfs + 线段树 + 询问离线 F. Nearest Leaf Description: Let's define the Eulerian traversal of ...

  3. dfs+线段树 zhrt的数据结构课

    zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...

  4. 【Codeforces-707D】Persistent Bookcase DFS + 线段树

    D. Persistent Bookcase Recently in school Alina has learned what are the persistent data structures: ...

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

  6. 2014 Super Training #9 F A Simple Tree Problem --DFS+线段树

    原题: ZOJ 3686 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3686 这题本来是一个比较水的线段树,结果一个ma ...

  7. hdu 5692(dfs+线段树) Snacks

    题目http://acm.hdu.edu.cn/showproblem.php?pid=5692 题目说每个点至多经过一次,那么就是只能一条路线走到底的意思,看到这题的格式, 多个询问多个更新, 自然 ...

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

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

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

随机推荐

  1. jdbc 链接池

    package cn.itcast.jdbc.datasourse; import java.sql.Connection;import java.sql.DriverManager;import j ...

  2. Android Calendar的学习与运用

    [java]mport java.text.DateFormat; import java.text.ParsePosition; import java.text.SimpleDateFormat; ...

  3. struct timeval 和 struct timespec

    struct timeval { time_t tv_sec; suseconds_t tv_usec; }; 測试代码例如以下: #include <stdio.h> #include ...

  4. 【设计模式】C++单例模式的几种写法——Java自动加载内部类对象,C++怎么破?

    单例模式是最简单的设计模式,就让我像玩简单的游戏一样写下去吧. v1: 简单模式 和这个版本有过一面之缘,但不敢苟同. class Singleton { private: Singleton() { ...

  5. PowerBuilder -- Tab控件

    在tab中关闭窗口 Close(tab_1.getparent()) 调整tab中的控件的tab oder 鼠标右键tabpage_1,选择 Tab Order菜单.

  6. 多媒体开发之---h264 高度和宽度获取

    ( School of Computer Science & Technology, Soochow University,SuZhou 215006:) Abstract: H.264 is ...

  7. Jmeter文章索引贴

    一.基础部分: 使用Jmeter进行http接口测试 Jmeter之Http Cookie Manager Jmeter之HTTP Request Defaults Jmeter之逻辑控制器(Logi ...

  8. TVirtualStringTree的Minimal例子学习

    预步骤第一步,定义数据结构type PMyRec = ^TMyRec; TMyRec = record Caption: WideString; end;预步骤第二步,规定取得节点数据时候的大小pro ...

  9. 怎样拆分View Controller进而实现轻量级的View Controller[UIKit]

    參考文章来自objcio站点   为什么要编写轻量级的View Controller??   1.作为iOS项目中最大的文件,ViewControllers中的代码复用率差点儿是最低的 2.重量级的V ...

  10. (转)扫盲--JavaScript的立即执行函数

    看过jQuery源码的人应该知道,jQuery开篇用的就是立即执行函数.立即执行函数常用于第三方库,好处在于隔离作用域,任何一个第三方库都会存在大量的变量和函数,为了避免变量污染(命名冲突),开发者们 ...