HDU3974 Assign the task (修改子树对应到区间修改)

本文介绍了一种解决树状结构中子树区间更新及单点查询问题的方法。通过构建深度优先搜索树,并利用线段树实现高效区间更新与查询操作。适用于处理大规模员工任务分配与查询场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=3974

题目

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

题意

给定一颗树,要求修改子树的值和查询树上某个结点的值

分析

对雇佣关系图进行一次dfs,建立深搜树,用遍历的先后顺序来作为结点在一个区间中的映射。可以证明,每一颗子树在区间中的映射是一个连续区间。故修改子树就变成了区间修改。

AC代码

//296ms 4.8MB
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define inf 0x3f3f3f3f
#define lson p<<1
#define rson p<<1|1
using namespace std;
const int maxn=5e4+100;
vector<int> g[maxn];
struct node
{
    int l,r;//结点所维护的区间
    int mark;//延迟标记
}T[maxn<<2];//线段树要开四倍空间

void down(int p)
{
    if(T[p].mark!=-1) //有延迟标记,那么调用p子树信息前要下传延迟标记
    {
        T[lson].mark=T[rson].mark=T[p].mark; //下传延迟标记
        T[p].mark=-1; //标记已下传,当前结点就没有了标记
    }
}
void build(int p,int l,int r)//p表示结点编号,l、r表示结点p所表示的区间
{
    T[p].mark=-1;
    T[p].l=l,T[p].r=r;//确定结点p所表示的区间[l,r]
    if(l==r) //确定叶子结点所表示的信息
    {
        return ;
    }
    int mid=(l+r)>>1;
    build(lson,l,mid); //递归创建左子树
    build(rson,mid+1,r); //递归创建右子树
}
int query(int p,int k)//单点查询,k号雇员的任务
{
    if(T[p].l==T[p].r) return T[p].mark;
    int mid=(T[p].l+T[p].r)>>1;
    down(p);
    if(k<=mid) return query(lson,k);
    else return query(rson,k);
}
void update(int p,int x,int y,int v)
{
    if(T[p].l==x && T[p].r==y) //区间恰好重合
    {
        T[p].mark=v; //标记信息修改到此结点打止
        return ;
    }
    //区间在p的子树结点中,要调用子树结点信息,必须先下传延迟标记
    down(p); //下传标记将p的左右子结点修改,只修改了这两个结点,延迟标记放在了这两个结点上
    int mid=(T[p].l+T[p].r)>>1;
    if(y<=mid) //区间[x,y]一定在左子树上
        update(lson,x,y,v);
    else if(x>mid) //区间[x,y]一定在右子树上
        update(rson,x,y,v);
    else
    {
        //必须将区间分成两份小区间[x,mid]和[mid+1,y]
        update(lson,x,mid,v);
        update(rson,mid+1,y,v);
    }
}
int L[maxn];//L[i]表示结点i及其下属的新编号的最小值
int R[maxn];//R[i]表示结点i及其下属的新编号的最大值
int cnt;//新编号
int vis[maxn];//有无上司
//深搜后,可以发现每一颗子树的结点新编号都是连续的,一颗子树对应一个区间
//修改子树就是区间修改
void dfs(int u)//建一颗深搜树,用遍历的时间给结点重新编号,新编号对应线段树中的编号
{
    L[u]=++cnt;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        dfs(v);
    }
    R[u]=cnt;
}
int main()
{
    int T,kase=1;
    scanf("%d",&T);
    while(T--)
    {
        printf("Case #%d:\n",kase++);
        int n;
        scanf("%d",&n);
        cnt=0;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=1;i<n;i++)//v是u的上司
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[v].push_back(u);
            vis[u]=1;//有上司
        }
        for(int i=1;i<=n;i++)
            if(!vis[i]) dfs(i);//从老板开始建深搜树
        build(1,1,n);
        int q;
        scanf("%d",&q);
        while(q--)
        {
            char op[2];
            int x,y;
            scanf("%s",op);
            if(op[0]=='C')//单点查询
            {
                scanf("%d",&x);
                printf("%d\n",query(1,L[x]));//注意是L[x]
            }
            else
            {
                scanf("%d%d",&x,&y);//区间修改,修改子树x所表示的区间
                update(1,L[x],R[x],y);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值