Assign the task HDU

来源:互联网 发布:网吧软件管理系统 编辑:程序博客网 时间:2024/05/22 17:32
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
很好的一道线段树变形题目。
思路:只要动了他们子树的头结点就会使整个子树都是相同值,可以想到是一个类似lazy更新的线段树。那么如何转化成线段树呢,其实我们可以把这个子树给打扁,让他变成区间,虽然按照他给的节点这个区间是不连续的,但是我们可以让他连续,我们只要用dfs跑一遍所有的节点,然后给每一个节点都标记一个值,同时计算出每一个节点对应的子树的结束节点是什么,这样的话就转化成了一个区间了。然后直接区间更新单点查询就ok了。
#include<bits/stdc++.h>using namespace std;const int M=5e4+10;int tree[4*M],lazy[4*M],s[M],e[M],cnt,pre[M],cont;bool vis[M];struct aa{    int u,v,next;} edge[M];void add(int u,int v){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].next=pre[u];    pre[u]=cnt++;}void buildsegment(int t){    s[t]=++cont;    for(int i=pre[t]; i!=-1; i=edge[i].next)    {        buildsegment(edge[i].v);    }    e[t]=cont;}void creat(int now,int l,int r){    lazy[now]=tree[now]=-1;    if(l!=r)    {        int mid=(r+l)/2;        creat(now*2+1,l,mid);        creat(now*2+2,mid+1,r);    }}void falldown(int now){    if(lazy[now]>=0)    {        tree[now*2+1]=tree[now*2+2]=lazy[now*2+1]=lazy[now*2+2]=lazy[now];        lazy[now]=-1;    }}void update(int now,int l,int r,int al,int ar,int k){    if(al>r||ar<l)        return;    if(al<=l&&ar>=r)    {        tree[now]=lazy[now]=k;        return ;    }    int mid=(r+l)/2;    falldown(now);    update(now*2+1,l,mid,al,ar,k);    update(now*2+2,mid+1,r,al,ar,k);}int query(int now,int l,int r,int s){    int mid=(l+r)/2;    if(l==r)    {        return tree[now];    }    falldown(now);    if(s<=mid)    {        query(now*2+1,l,mid,s);    }    else    {        query(now*2+2,mid+1,r,s);    }}int main(){    int t;    scanf("%d",&t);    int kase=1;    while(t--)    {        printf("Case #%d:\n",kase++);        int n;        scanf("%d",&n);        cnt=0,cont=-1;        for(int i=0; i<=n; i++)        {            pre[i]=-1,vis[i]=0;        }        for(int i=0; i<n-1; i++)        {            int u,v;            scanf("%d%d",&u,&v);            add(v,u);            vis[u]=1;        }        creat(0,0,n-1);        for(int i=1; i<=n; i++)        {            if(!vis[i])            {                buildsegment(i);                break;            }        }        int m;        scanf("%d",&m);        while(m--)        {            char temp[4];            scanf("%s",temp);            if(temp[0]=='C')            {                int a;                scanf("%d",&a);                printf("%d\n",query(0,0,n-1,s[a]));            }            else            {                int a,b;                scanf("%d%d",&a,&b);               // printf("*** T  %d %d***\n",s[a],e[a]);                update(0,0,n-1,s[a],e[a],b);            }        }    }}



原创粉丝点击