码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3974 Assign the task(dfs编号+线段树成段更新)

时间:2014-09-04 01:42:47      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   ar   for   2014   

题意:给定点的上下级关系,规定如果给i分配任务a,那么他的所有下属。都停下手上的工作,开始做a。

          操作 T x y 分配x任务y,C x询问x的当前任务;

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深度优先遍历重新编号,使一个结点的儿子连续。然后成段更新。

bubuko.com,布布扣

2:1-5

5:2-2

3:3-5

1:4-4

4:5-5


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define N 50001
using namespace std;

int lazy[N<<2];
int task[N<<2];
int next[N],pre[N],to[N];
int uset[N];
int num;
int l[N],r[N];

int find_set(int x)
{
    while(uset[x]!=x)
        x=uset[x];
    return x;
}

void dfs(int root)
{
    int i=pre[root];
    l[root]=(++num);
    while(i!=-1)
    {
        dfs(to[i]);
        i=next[i];
    }
    r[root]=num;
}

void pushdown(int rt)
{
    if(lazy[rt]!=-1)
    {
        task[rt<<1]=lazy[rt];
        lazy[rt<<1]=lazy[rt];
        task[rt<<1|1]=lazy[rt];
        lazy[rt<<1|1]=lazy[rt];
        lazy[rt]=-1;
    }
}


void build(int l,int r,int rt)
{
    task[rt]=-1;
    lazy[rt]=-1;
    if(l==r)
        return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
}

void update(int L,int R,int l,int r,int rt,int val)
{
    if(L<=l&&r<=R)
    {
        lazy[rt]=val;
        task[rt]=val;
        return;
    }
    pushdown(rt);
    int m=(r+l)>>1;
    if(L<=m) update(L,R,lson,val);
    if(R>m) update(L,R,rson,val);
}

void query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        printf("%d\n",task[rt]);
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) query(L,R,lson);
    if(R>m) query(L,R,rson);
}


int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int n;
        int u,v;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            uset[i]=i;
            pre[i]=-1;
        }

        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            uset[u]=v;
            to[i]=u;
            next[i]=pre[v];
            pre[v]=i;

        }

        int root=find_set(1);
        num=0;

        dfs(root);
        //for(int i=1;i<=num;i++)
        //    printf("%d -- %d  %d\n",i,l[i],r[i]);
        //system("pause");

        build(1,num,1);
        int m;
        int a,b;
        char op[10];
        scanf("%d",&m);
        printf("Case #%d:\n",cas);
        for(int i=0;i<m;i++)
        {
            scanf("%s",&op);
            if(op[0]=='C')
            {
                scanf("%d",&a);
                query(l[a],l[a],1,num,1);
            }
            else
            {
                scanf("%d %d",&a,&b);
                update(l[a],r[a],1,num,1,b);
            }
        }
    }
    return 0;
}


HDU 3974 Assign the task(dfs编号+线段树成段更新)

标签:style   blog   http   color   os   io   ar   for   2014   

原文地址:http://blog.csdn.net/code_or_code/article/details/39038573

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!