HDU 3974 Assign the task

来源:互联网 发布:投资软件 编辑:程序博客网 时间:2024/05/18 16:19

http://acm.hdu.edu.cn/showproblem.php?pid=3974

Assign the task

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 845    Accepted Submission(s): 420


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
 

Source
2011 Multi-University Training Contest 14 - Host by FZU
 

Recommend
和 poj 3321 apple tree挺像,那个是树状数组求和  这个是线段树区间修改 单点查询 这两道题目都用到了dfs 对节点进行标号。区间修改一般都需要延迟标记
AC代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 500000+10
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;

using namespace std;

bool cmp(int a,int b)
{
return a>b;
}
struct node
{
int l,r;
int val,lazy;
}nod[maxn*3];

int Left[maxn],Right[maxn];

vector<int>edge[maxn];

char s[10];
int x,y,z,n;
void build(int i,int l,int r)
{
nod[i].l=l;
nod[i].r=r;
nod[i].val=-1;//初始化为-1
nod[i].lazy=0;
if(l==r)return ;
int mid=(l+r)/2;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
}
void update(int i,int a,int b,int c)
{
if(a==nod[i].l&&b==nod[i].r)
{
nod[i].lazy=1;//不往下更新了
nod[i].val=c;
return ;
}
int mid=(nod[i].l+nod[i].r)/2;
if(nod[i].lazy)
{
nod[i].lazy=0;
update(i<<1,nod[i].l,mid,nod[i].val);
update(i<<1|1,mid+1,nod[i].r,nod[i].val);
}
if(b<=mid) update(i<<1,a,b,c);
else if(a>mid)update(i<<1|1,a,b,c);
else
{
update(i<<1,a,mid,c);
update(i<<1|1,mid+1,b,c);
}

}
int query(int i,int x)
{
if(nod[i].l==nod[i].r)return nod[i].val;
int mid=(nod[i].l+nod[i].r)/2;
if(nod[i].lazy)
{
nod[i].lazy=0;
update(i<<1,nod[i].l,mid,nod[i].val);
update(i<<1|1,mid+1,nod[i].r,nod[i].val);
}
if(x<=mid)return query(i<<1,x);
else return query(i<<1|1,x);
}
int cnt;
ll sum;
void dfs(int u)
{
Left[u]=cnt;
for(int i=0;i<edge[u].size();i++)
{
cnt++;
dfs(edge[u][i]);
}
Right[u]=cnt;
}
void init()
{
cnt=1;
sum=n*(1+n)/2;
cle(Left),cle(Right);
for(int i=0;i<=n;i++)
{
edge[i].clear();
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int t,m;
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
printf("Case #%d:\n",i);
scanf("%d",&n);
init();
int u,v;
for(int j=1;j<n;j++)
{
scanf("%d%d",&u,&v);
sum-=u;
edge[v].push_back(u);
}
dfs(sum);
build(1,1,cnt);
scanf("%d",&m);
for(int k=1;k<=m;k++)
{
scanf("%s",&s);
if(s[0]=='C')
{
scanf("%d",&x);
printf("%d\n",query(1,Left[x]));
}
else
{
scanf("%d%d",&y,&z);
update(1,Left[y],Right[y],z);
}
}
}
return 0;
}

  
0 0