题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务。输入T表示分配新的任务,

输入C表示查询某员工的任务。本题的难度在于建树,一开始百思不得其解,后来看了lx大大的博客后才明白,用递归建立了各个员工之间的关系,Start[x]

表示x员工为Boss的起点,End[x]表示x员工为Boss的终点。之后对这样的整体线段进行赋值即可。

#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
#define N 50005
struct tree
{
int l, r, task, iscover;
int mid()
{
return (l+r)/;
}
}a[N<<];
int vis[N], index, Start[N], End[N];
vector<int>G[N];
void dfs(int k)///模拟为boss树,记录各员工在树上的编号
{
Start[k] = ++index;
for(int i=, len=G[k].size(); i<len; i++)
dfs(G[k][i]); End[k] = index;
}
void build(int rt, int l, int r)
{
a[rt].l = l;
a[rt].r = r;
a[rt].task = -;
a[rt].iscover = ;
if(l==r)return ;
build(lson, l, a[rt].mid());
build(rson, a[rt].mid()+, r);
}
void Down(int rt)
{
if(a[rt].l!=a[rt].r && a[rt].iscover)
{
a[lson].iscover = a[rson].iscover = ;
a[rt].iscover = ;
a[lson].task = a[rson].task = a[rt].task;
}
}
void Update(int rt, int l, int r, int task)
{
if(a[rt].l==l && a[rt].r==r)
{
a[rt].task = task;
a[rt].iscover = ;
return ;
} Down(rt); if(a[rt].mid()>=r)Update(lson, l, r, task);
else if(a[rt].mid()<l)Update(rson, l, r, task);
else
{
Update(lson, l, a[rt].mid(), task);
Update(rson, a[rt].mid()+, r, task);
}
}
int Query(int rt, int k)
{
if(a[rt].l==a[rt].r)
{
return a[rt].task;
} Down(rt); if(a[rt].mid()>=k)return Query(lson, k);
else return Query(rson, k);
}
int main()
{
int T, icase = ;
scanf("%d", &T);
while(T--)
{
int n, b, c;
memset(vis, , sizeof(vis));
scanf("%d", &n);
for(int i=; i<=n; i++)
G[i].clear();
for(int i=; i<n; i++)
{
scanf("%d %d", &b, &c);
G[c].push_back(b);
vis[b] = ;
}
index = ;
for(int i=; i<=n; i++)
{
if(!vis[i])
{
dfs(i);///找到最高BOSS
break;
}
}
build(, , n);
int m, x, y;
scanf("%d", &m);
char order[];
printf("Case #%d:\n", icase++);
while(m--)
{
scanf("%s", order);
if(order[]=='T')
{
scanf("%d %d", &x, &y);
Update(, Start[x], End[x], y);
}
else
{
scanf("%d", &x);
printf("%d\n", Query(, Start[x]));
}
}
}
return ;
}

HDU 3974 Assign the task(dfs建树+线段树)的更多相关文章

  1. HDU 3974 Assign the task (DFS序 + 线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...

  2. HDU 3974 Assign the task(DFS序+线段树单点查询,区间修改)

    描述There is a company that has N employees(numbered from 1 to N),every employee in the company has a ...

  3. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. [Assign the task][dfs序+线段树]

    http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...

  5. HDU 3974 Assign the task (DFS+线段树)

    题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...

  6. hdu3974 Assign the task dfs序+线段树

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  7. HDU3974 Assign the task —— dfs时间戳 + 线段树

    题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...

  8. hdu 3974 Assign the task(dfs序上线段树)

    Problem Description There is a company that has N employees(numbered from 1 to N),every employee in ...

  9. HDU - 3974 Assign the task (DFS建树+区间覆盖+单点查询)

    题意:一共有n名员工, n-1条关系, 每次给一个人分配任务的时候,(如果他有)给他的所有下属也分配这个任务, 下属的下属也算自己的下属, 每次查询的时候都输出这个人最新的任务(如果他有), 没有就输 ...

随机推荐

  1. Spring4.1.6 标签注解大全以及解释

    常用的spring注解有如下几种: @Controller@Service@Autowired@RequestMapping@RequestParam@ModelAttribute@Cacheable ...

  2. Linux命令全集

    一.Ubuntu10.4 启动纯文件界面 打开 /etc/default/grub 文件, 注释掉  GRUB_CMDLINE_LINUX_DEFAULT="quiet splash&quo ...

  3. Geneve: Generic Network Virtualization Encapsulation

    Earlier this year, VMware, Microsoft, Red Hat and Intel published an IETF draft on Generic Network V ...

  4. 报错:严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [/index.jsp (line: 13, column: 20) No tag "textfiled" defined in tag library imported with prefix

    严重: Servlet.service() for servlet [jsp] in context with path [/20161116-Struts2-6] threw exception [ ...

  5. Android 另类方法监听软键盘的弹出收起事件

    http://www.cnblogs.com/csonezp/p/5065624.html 最近做的项目碰到个问题,a界面是fragment+recyclerview,b界面带个edittext,并且 ...

  6. ORM框架

    半自动:iBATIS是一个半自动化的ORM框架,需要通过配置方式指定映射SQL语句 全自:由框架本身生成(如Hibernate自动生成对应SQL来持久化对象),即Hibernate属于全自动ORM框架 ...

  7. Linux学习笔记——查看Linux系统信息的方法

    由于Linux的发行版本比较多,并且有些差异性,所以,分析问题时我们常常需要知道自己的Linux系统的版本信息,以下是我搜集到的一些方法 1:显示电脑以及操作系统的相关信息 qian@ubuntu:~ ...

  8. android开发之单选按钮

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  9. python中的装饰器

    一.什么是装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能 ...

  10. java 中小数点的处理

    第一种 BigDecimal bg = new BigDecimal(f); double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleVa ...