J - Assign the task(dfs序+线段树)J

来源:互联网 发布:女仆咖啡厅 网络剧 编辑:程序博客网 时间:2024/05/18 22:55

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=66989#overview

J - Assign the task
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 3974

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
代码是世界上最好的语言~~,AC之路,我选择坚持~~

/**dfs序+线段树:将树映射成区间,最大的根映射最大的区间,从根节点开始,假设其为2Start【2】=1;映射成线段树第一个区间,根据dfs序依次映射线段树区间2~n(设有n个点)修改区间,假设修改根2,然后只需修改Start【2】,End【2】;End存的为该子树的先序遍历的最后一个映射到线段树上的区间总的说一句话就是将树映射到线段树上,改变原有的树和他子树,就是改变线段树上从区间Start到End的值**/#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <map>#include <set>#include <stack>#include <string>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long longusing namespace std;const int SIZE=5e4+10;const int maxn=500000;int head[SIZE],tot;int Start[SIZE],End[SIZE],sum[SIZE<<2],add[SIZE<<2];bool vis[SIZE];struct edge{    int v,next;}a[SIZE];void addedge(int u,int v){    a[tot].v=u;    a[tot].next=head[v];    head[v]=tot++;}void dfs(int rt){    Start[rt]=++tot;    for(int i=head[rt];i!=-1;i=a[i].next){        dfs(a[i].v);    }    End[rt]=tot;}void build(int l,int r,int rt){    sum[rt]=-1;    add[rt]=0;    if(l==r)return;    int m=(l+r)>>1;    build(lson);    build(rson);}void pushdown(int rt){    if(!add[rt])return;    add[rt<<1|1]=add[rt<<1]=add[rt];    sum[rt<<1|1]=sum[rt<<1]=add[rt];    add[rt]=0;}void update(int L,int R,int c,int l,int r,int rt){    if(L<=l&&r<=R){        sum[rt]=c;        add[rt]=c;        return;    }    pushdown(rt);    int m=(l+r)>>1;    if(L<=m)update(L,R,c,lson);    if(R>m)update(L,R,c,rson);}int query(int s,int l,int r,int rt){    if(l==r)return sum[rt];    pushdown(rt);    int m=(l+r)>>1;    if(s<=m)return query(s,lson);    else return query(s,rson);}int main(){    int m,n,T,u,v,x,y,rt;    char s[10];    scanf("%d",&T);    for(int cas=1;cas<=T;cas++){        scanf("%d",&n);        tot=0;        memset(head,-1,sizeof(head));        memset(vis,false,sizeof(vis));        for(int i=1;i<n;i++){            scanf("%d%d",&u,&v);            addedge(u,v);            vis[u]=true;        }        tot=0;        for(int i=1;i<=n;i++)            if(!vis[i]){                dfs(i);                break;            }        build(1,n,1);        scanf("%d",&m);        printf("Case #%d:\n",cas);        while(m--){            scanf("%s",s);            if(s[0]=='C'){                scanf("%d",&rt);                printf("%d\n",query(Start[rt],1,n,1));            }            else {                scanf("%d%d",&x,&y);                update(Start[x],End[x],y,1,n,1);            }        }    }    return 0;}


0 0
原创粉丝点击