Assign the task (线段树)

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

思路1: 由于人员关系本身就像一颗并查集类似的树,因此可以给任务分配时间,对于每次查询只要从查询点往上找找到所有父亲里面最新的任务就行了。

#include <cstdio>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int k;
int pre[50004];
int job[50004];
int tme[50004];
int Find(int x)
{
    int cur = tme[x];
    int ans = job[x];
    while(pre[x]!= x)
    {
        if(cur == k)
            return ans;
        x = pre[x];
        if(tme[x] > cur)
        {
            cur = tme[x];
            ans = job[x];
        }
    }
    if(tme[x] > cur)
    {
        cur = tme[x];
        ans = job[x];
    }
    return ans;
}
int main()
{
    int t,n,m;
    int index = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        k = 0;
        for(int i = 1; i <= n; i++)
        {
             pre[i] = i;
             job[i] = -1;
             tme[i] = -1;
        }
        for(int i = 1; i < n; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            pre[x] = y;
        }
        scanf("%d",&m);
        printf("Case #%d:\n",index++);
        for(int i = 0; i < m; i++)
        {
            char s[10];
            int a,b;
            scanf("%s",s);
            if(s[0] == 'C')
            {
                scanf("%d",&a);
                printf("%d\n",Find(a));
            }
            else
            {
                scanf("%d%d",&a,&b);
                job[a] = b;
                tme[a] = ++k;
            }
        }

    }
    return 0;
}



思路 2 : 因为上级包含下级,所以更新a的工作相当于更新a所有下属的工作,相当于区间更新点查询,就想到线段树了,难点在与如何重新编号,这里采用一遍深搜编号,同时记录每个点子节点个数。

#include <cstdio>
#include <queue>
#include <map>
#include <vector>
#include <cmath>
#include <climits>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int pre[50004];
int num[50004];
map <int,int> mp;
vector <int> vc[50004];
int k;
void setNum(int cur,int last)
{
    for(int j = 0; j < vc[cur].size(); j++)
    {
        int tmp = vc[cur][j];
        mp[tmp] = k++;
        setNum(tmp,cur);
    }
    if(cur!=last)
    num[last] += num[cur] + 1;
}
struct node{
   int l,r,task,lazy;
};
node tree[200005];
void build(int i,int l,int r)
{
    tree[i].l = l;
    tree[i].r = r;
    tree[i].task = -1;
    tree[i].lazy = 0;
    if(l == r) return;
    int mid = (l+r) / 2;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
}

void update(int i,int l,int r,int task)
{
    if(tree[i].l == l && tree[i].r == r)
    {
        tree[i].task = task;
        tree[i].lazy = 1;
        return;
    }
    if(task == tree[i].task) return;
    if(tree[i].lazy)
    {
        tree[i].lazy = 0;
        tree[i<<1].lazy = 1;
        tree[i<<1].task = tree[i].task;
        tree[i<<1|1].lazy = 1;
        tree[i<<1|1].task = tree[i].task;
    }
    tree[i].task = -2; // 表示区间内人任务不同
    int mid = (tree[i].l + tree[i].r) / 2;
    if(r <= mid) update(i<<1,l,r,task);
    else if(l >mid) update(i<<1|1,l,r,task);
    else
    {
        update(i<<1,l,mid,task);
        update(i<<1|1,mid+1,r,task);
    }
    if(tree[i<<1].task == tree[i<<1|1].task && tree[i<<1].task!= -2)
        tree[i].task = tree[i<<1].task;
}

int query(int i,int t)
{
    if(tree[i].task != -2) return tree[i].task;
    if(tree[i].lazy)
    {
        tree[i].lazy = 0;
        tree[i<<1].lazy = 1;
        tree[i<<1].task = tree[i].task;
        tree[i<<1|1].lazy = 1;
        tree[i<<1|1].task = tree[i].task;
    }
    int mid = (tree[i].l+tree[i].r) / 2;
    if(t <= mid)  query(i<<1,t);
    else  query(i<<1|1,t);
}

int main()
{
    int t,n,m;
    int index = 1;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(pre,-1,sizeof(pre));
        memset(num,0,sizeof(num));
        mp.clear();
        k = 1;
        for(int i = 1; i <= n; i++)
            vc[i].clear();
        for(int i = 1; i < n; i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            pre[x] = y;
            vc[y].push_back(x);
        }
        int root = 0;
        for(int i = 1; i <= n; i++)
        {
            if(pre[i] == -1) root = i;
        }
        mp[root] = k++;
        build(1,1,n);
        setNum(root,root);
        scanf("%d",&m);
        printf("Case #%d:\n",index++);
        for(int i = 0; i < m; i++)
        {
            char s[10];
            int a,b;
            scanf("%s",s);
            if(s[0] == 'C')
            {
                scanf("%d",&a);
                printf("%d\n",query(1,mp[a]));
            }
            else
            {
                scanf("%d%d",&a,&b);
                update(1,mp[a],mp[a]+num[a],b);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值