hdu3974-Assign the task(dfs序建线段树)


title: hdu3974-Assign the task(dfs序建线段树)
date: 2019-05-28 15:08:01
categories: ACM
tags: [ACM,算法]

今天做线段树的题碰到一个需要dfs序转化成建线段树,挺有意义的一道题
我的个人博客:https://www.kimiye.xyz

题目描述

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.

题目大意

给你一个多叉树,有2种操作,
C x:查询x结点正在做的任务
T x y:给x结点布置y任务
每一次查询要给出一个答案

解题思路

一开始因为是多叉树,没什么区间性,没往线段树上想,就暴力写了一发,给结点染色后,所有子结点也染上色,然后用lazy标记稍稍优化一下,避免重复染色,但还是T了 QAQ

查了一下发现有dfs序建树这种东西,主要用到时间戳(tot),时间戳以前了解过,很快就上手了

主要思想如下:
对一颗树进行dfs,由于dfs的性质,父亲结点出来的时刻会在儿子结点之后,用l[] , r[]分别表示一个结点进去、出来的时刻,这样从时间戳上看,就形成了一段区间,并且父亲结点是盖住儿子结点的,就可以用线段树求解了~~~
每个结点会产生2个时间戳,所以区间长度是2*n(这边可以优化一下,每个结点产生1个时间戳)
对于更新操作,对l[x]~r[x]执行区间更新
对于查询操作,对l[x]或者r[x]单点查询都可以

#include <bits/stdc++.h>
typedef long long LL;
typedef unsigned long long ULL;
using namespace std;
const int maxn = 5e4+7;
const int inf = 0x3f3f3f3f;
int T,kase;
int n,m;
vector<int> edge[maxn];
int degree[maxn],tree[8*maxn];
int l[maxn],r[maxn],tot;
// hdu 3974
void dfs(int u)
{
    l[u]=++tot;
    for(int i=0;i<edge[u].size();i++)
        dfs(edge[u][i]);
    r[u]=++tot;
}
void build(int l,int r,int rt)
{
    tree[rt]=-1;
    if(l==r)
        return;
    int mid=(l+r)/2;
    build(l,mid,2*rt);
    build(mid+1,r,2*rt+1);
}
void pushdown(int rt)
{
    if(tree[rt]!=-1)
    {
        tree[2*rt]=tree[rt];
        tree[2*rt+1]=tree[rt];
        tree[rt]=-1;
    }
}
void update(int L,int R,int y,int l,int r,int rt)
{
    if(L<=l && R>=r)
    {
        tree[rt]=y;
        return;
    }
    pushdown(rt);
    int mid=(l+r)/2;
    if(L<=mid)
        update(L,R,y,l,mid,2*rt);
    if(R>mid)
        update(L,R,y,mid+1,r,2*rt+1);
}
int query(int L,int l,int r,int rt)
{
    if(l==r && l==L)
        return tree[rt];
    pushdown(rt);
    int mid=(l+r)/2;
    if(L<=mid)
        return query(L,l,mid,2*rt);
    else return query(L,mid+1,r,2*rt+1);
}
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        tot=0;
        memset(degree,0,sizeof(degree));
        for(int i=0;i<maxn;i++) edge[i].clear();

        scanf("%d",&n);
        for(int i=1;i<n;i++)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            edge[v].push_back(u);
            degree[u]++;
        }
        int root;
        for(int i=1;i<=n;i++)
            if(!degree[i]) {root=i;break;}
        //printf("rt=%d\n",root);
        dfs(root);
        build(1,2*n,1);
        scanf("%d",&m);
        printf("Case #%d:\n",++kase);
        while(m--)
        {
            char op[3];
            int x,y;
            scanf("%s",op);
            if(op[0]=='C')
            {
                scanf("%d",&x);
                printf("%d\n",query(l[x],1,2*n,1));
            }
            else
            {
                scanf("%d %d",&x,&y);
                update(l[x],r[x],y,1,2*n,1);
            }
        }
    }
    return 0;
}

全部评论

相关推荐

人力小鱼姐:实习经历没有什么含金量,咖啡店员迎宾这种就别写了,其他两段包装一下 想找人力相关的话,总结一下个人优势,结合校园经历里有相关性的部分,加一段自我评价
点赞 评论 收藏
分享
来个厂收我吧:首先,市场侧求职我不是很懂。 但是,如果hr把这份简历给我,我会觉得求职人不适合做产品经理。 问题点: 1,简历的字体格式不统一,排版不尽如人意 2,重点不突出,建议参考star法则写个人经历 3,印尼官方货币名称为印度尼西亚卢比(IDR),且GMV690000印尼盾换算为305人民币,总成交额不高。 4,右上角的意向职位在发给其他公司时记得删除。 5,你所有的经历都是新媒体运营,但是你要投市场营销岗位,jd和简历不匹配,建议用AI+提示词,参照多个jd改一下经历内容。 修改建议: 1,统一字体(中文:思源黑体或微软雅黑,英文数字:time new romans),在word中通过表格进行排版(b站学) 2,校招个人经历权重:实习经历=创业经历(大创另算)>项目经历>实训经历>校园经历 3,请将项目经历时间顺序改为倒序,最新的放最上方。 4,求职方向不同,简历文字描述侧重点也需要不同。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务