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 ;
}

最新文章

  1. 如何把select默认的小三角替换成自己的图片
  2. POJ1704 Georgia and Bob
  3. linux内存使用计算方式
  4. 装b指南
  5. HDU 4280:Island Transport(ISAP模板题)
  6. Eclipse安装easyShell插件
  7. Android String操作
  8. WPF-拖动面板移动窗口&amp;设置窗口状态
  9. 解决:子元素设置margin-top,父元素也受影响的问题
  10. MySQL入门笔记
  11. TFS2013 安装出现TF400102错误解决
  12. 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)
  13. hdu1033
  14. Sublime Text3 最常用快捷键
  15. 201521123114 《Java程序设计》第11周学习总结
  16. bzoj:1299: [LLH邀请赛]巧克力棒
  17. _3_form_标签
  18. mongodb 高级操作
  19. Activity的运行过程
  20. 限制oracle某用户仅能从某IP登录

热门文章

  1. 【puppeteer+Node.js安装环境】之WebStorm编辑器运行失败问题:Error: Cannot find module &#39;puppeteer&#39;并且代码出不来“asnyc”标识以及有红色波浪线解决办法
  2. sqlite3常用操作命令 和mysql的区别及优缺点
  3. Cesium--气泡弹窗
  4. 设置Eclipse中properties文件打开方式myeclipse一样有source和properties两个视图方法
  5. IIS发布问题集锦
  6. intellij idea同一个窗口打开多个项目
  7. cocos2d-js添加360广告联盟插屏(通过jsb反射机制)
  8. php异步任务处理: gearman
  9. 题解 P3389 【【模板】高斯消元法】
  10. 列举你了解的Python较其他语言的优势