HDU3974-Assign the task(线段树+区间建树)

来源:互联网 发布:网络数据库类型有哪些 编辑:程序博客网 时间:2024/05/18 15:51

Assign the task

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


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
 

第一次写这种区间建树和线段树扯一块的题,不知道怎样将建好的树用线段树操作;

思路:建树的时候开两个组数分别记录自己和最后一个子节点的坐标,这样[   Left[i],right[i]   ] 就表示从自己到最后一个子节点这一段区间;

区间可以表示了,那么直接进行线段树操作即可;

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cstdlib>#include<vector>#include<cmath>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define mem(a,b) memset(a,b,sizeof(a))#define maxn 50010using namespace std;int t[maxn<<2],Left[maxn],Right[maxn],lazy[maxn<<2],f[maxn];int n,len;vector<int>v[maxn];void dfs(int x){    Left[x]=len;    for(int i=0;i<v[x].size();i++)    {        len++;        dfs(v[x][i]);    }    Right[x]=len;}void init(){    mem(t,-1);    mem(Left,0);    mem(Right,0);    mem(lazy,0);    for(int i=1;i<=n;i++)    f[i]=i,v[i].clear();    len=1;}void pushdown(int rt){    if(lazy[rt])    {        t[rt<<1]=t[rt<<1|1]=lazy[rt];        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];        lazy[rt]=0;    }}void updata(int x,int y,int l,int r,int rt,int num){    if(x<=l&&y>=r)    {        t[rt]=num;        lazy[rt]=num;        return;    }    pushdown(rt);    int m=(l+r)>>1;    if(x<=m)        updata(x,y,l,m,rt<<1,num);    if(y>m)        updata(x,y,m+1,r,rt<<1|1,num);}int query(int x,int l,int r,int rt){    if(x==l&x==r)    return t[rt];    pushdown(rt);    int m=(l+r)>>1;    if(x<=m)        return query(x,l,m,rt<<1);    if(x>m)            return query(x,m+1,r,rt<<1|1);}int main(){    int t,q=1,x,y,m;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        init();        for(int i=1;i<n;i++)        {            scanf("%d%d",&x,&y);            v[y].push_back(x);            f[x]=y;        }        int root;        for(int i=1;i<=n;i++)//寻找根节点            if(f[i]==i)        {            root=i;            break;        }        dfs(root);//从根节点向下建树,获得区间        scanf("%d",&m);        printf("Case #%d:\n",q++);        char op[4];        for(int i=1;i<=m;i++)        {            scanf("%s",op);            if(op[0]=='T')            {                scanf("%d%d",&x,&y);                 updata(Left[x],Right[x],1,len,1,y);            }            else            {                scanf("%d",&x);                printf("%d\n",query(Left[x],1,len,1));            }        }    }}




原创粉丝点击