HDU - 3974 Assign the task (dfs序+区间更新 单点查询)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974点击打开链接

Assign the task

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


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
 


关于dfs序 简单讲就是从根结点开始进行深搜 每碰到一个点就将记录点数+1 每个点记录开始的记录点数和结束的记录点数 

这样子就将每个点看成区间 而就可以应用线段树

区间覆盖问题可以使用并查集 并且在时间空间上都优于线段树 不过因为这次写的线段树专题暂时不写 

并查集就是用一个next数组 记录下一个集合的开始位置 这样就避免了区间多次操作而导致的超时

#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
#include <limits>
#include <string>
#include <string.h>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
using namespace std;
#define maxn 100000+5
int flag;
struct xjy
{
    int left;
    int right;
    int num;
    int target;
    int lazy;
    int sum;
    int time;
};
xjy tree[maxn<<2];
vector<int > point[maxn];
int a[maxn],l[maxn],r[maxn];
int ans=0;
int cnt=1;
int remember[maxn];
int n,m;
int t;
void show(int i,int left,int right)
{
   if(left==right)
    {
        printf("%d %d %d %d %d %d\n",tree[i].sum,tree[i].left,tree[i].right,tree[i].lazy,tree[i].target,tree[i].time);
        return;
    }
    int mid=(left+right)>>1;
    show(i<<1,left,mid);
    show(i<<1|1,mid+1,right);
    printf("%d %d %d %d %d %d\n",tree[i].sum,tree[i].left,tree[i].right,tree[i].lazy,tree[i].target,tree[i].time);
}
int ccnt=0;
void dfs(int i)
{
    ccnt++;
    l[i]=ccnt;
    for(int ii=0;ii<point[i].size();ii++)
        {
            dfs(point[i][ii]);
        }
    r[i]=ccnt;
}
void build(int i,int left,int right)
{
    if(left==right)
    {
        tree[i].sum=-1;
        tree[i].left=left;
        tree[i].right=right;
        return;
    }
    int mid=(left+right)>>1;
    build(i<<1,left,mid);
    build(i<<1|1,mid+1,right);
    tree[i].sum=-1;
    tree[i].left=left;
    tree[i].right=right;
}
void updateregion(int i,int left,int right,int ffind,int val)
{
    if(tree[i].lazy)
    {
        tree[i].lazy=0;
        if(tree[i<<1].time<tree[i].time)
            {
                tree[i<<1].lazy=1;
                tree[i<<1].target=tree[i].target;
                tree[i<<1].time=tree[i].time;
            }
        if(tree[i<<1|1].time<tree[i].time)
            {
                tree[i<<1|1].lazy=1;
                tree[i<<1|1].target=tree[i].target;
                tree[i<<1|1].time=tree[i].time;
            }
        tree[i].sum=tree[i].target;
        tree[i].target=tree[i].time=0;
    }
    if(tree[i].left==left&&tree[i].right==right)
    {
        tree[i].lazy=1;
        tree[i].target=val;
        tree[i].time=cnt;
        return;
    }
    
    int mid=(tree[i].left+tree[i].right)>>1;
    if(right<=mid)
        updateregion(i<<1,left,right,ffind,val);
    else if(left>mid)
        updateregion(i<<1|1,left,right,ffind,val);
    else
    {
        updateregion(i<<1,left,mid,ffind,val);
        updateregion(i<<1|1,mid+1,right,ffind,val);
    }
}
int queryregion(int i,int ffind,int left,int right)
{
    if(tree[i].lazy)
    {
        tree[i].lazy=0;
        if(tree[i<<1].time<tree[i].time)
            {
                tree[i<<1].lazy=1;
                tree[i<<1].target=tree[i].target;
                tree[i<<1].time=tree[i].time;
            }
        if(tree[i<<1|1].time<tree[i].time)
            {
                tree[i<<1|1].lazy=1;
                tree[i<<1|1].target=tree[i].target;
                tree[i<<1|1].time=tree[i].time;
            }
        tree[i].sum=tree[i].target;
        tree[i].target=tree[i].time=0;
    }
    if(tree[i].left==left&&tree[i].right==right)
    {
        //show(1,1,n);
        return tree[i].sum;
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    if(right<=mid)
        return queryregion(i<<1,ffind,left,right);
    else if(left>mid)
        return queryregion(i<<1|1,ffind,left,right);
    else
    {
        return queryregion(i<<1,ffind,left,mid);
        return queryregion(i<<1|1,ffind,mid+1,right);
    }
}
int main()
{

    scanf("%d",&t);
    for(int step=1;step<=t;step++)
    {
        printf("Case #%d:\n",step);
        for(int i=0;i<maxn;i++)
        {
            tree[i].lazy=0;
            tree[i].sum=-1;
            tree[i].target=0;
            tree[i].time=0;
            a[i]=0;
            point[i].clear();
        }
        cnt=1;
        ccnt=0;
        set<int > sset;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            sset.insert(i);
        for(int i=1;i<n;i++)
            {
                int x,y;
                scanf("%d%d",&x,&y);
                point[y].push_back(x);
                sset.erase(x);
            }
        int root=*sset.begin();
        build(1,1,n);
        dfs(root);
        //for(int i=1;i<=n;i++)
            //cout << l[i] << " " << r[i] << endl;
        //show(1);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            string s;
            cin >> s;
            if(s[0]=='C')
            {
                int mid;
                scanf("%d",&mid);
                //printf("%d %d",l[mid],r[mid]);
                printf("%d\n",queryregion(1,mid,l[mid],r[mid]));

            }
            else
            {
                int mid1,mid2;
                scanf("%d%d",&mid1,&mid2);
                cnt++;
                updateregion(1,l[mid1],r[mid1],mid1,mid2);
                //show(1,1,n);
                //show(1,1,n);

            }
        }
    }
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值