hdu 3974 Assign the task(dfs序上线段树)
Problem Description
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
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
Sample Input
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序上建立线段树 可以把树上问题转化为区间问题 我们可以发现一个点的dfs序之间的数就是他的儿子节点 所以问题转化为 区间跟新+单点查询的基本问题
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 5e4+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef long long ll;
const ll mod = 1e9+7;
struct edge{
int next,v;
};
edge e[N<<1];
int head[N],cnt,tot,L[N],flag[N],R[N];
void init(){
cnt=0;
tot=0;
memset(head,0,sizeof(head));
memset(flag,0,sizeof(flag));
}
void add(int u,int v){
e[++cnt]=edge{head[u],v};
head[u]=cnt;
}
void dfs(int u){
L[u]=++tot;
for(int i=head[u];i;i=e[i].next){
int v=e[i].v;
dfs(v);
}
R[u]=++tot;
}
struct tree{
int l,r,v,lazy;
}t[N<<2];
void build(int p,int l,int r){
t[p].l=l; t[p].r=r; t[p].v=-1; t[p].lazy=0;
if(l==r) return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}
void pushdown(int p){
if(t[p].lazy){
t[p<<1].v=t[p].lazy;
t[p<<1|1].v=t[p].lazy;
t[p<<1].lazy=t[p].lazy;
t[p<<1|1].lazy=t[p].lazy;
t[p].lazy=0;
}
}
void update(int p,int l,int r,int v){
if(l<=t[p].l&&t[p].r<=r){
t[p].lazy=v;
t[p].v=v;
return ;
}
pushdown(p);
int mid=(t[p].l+t[p].r)>>1;
if(l<=mid) update(p<<1,l,r,v);
if(r>mid) update(p<<1|1,l,r,v);
}
int query(int p,int x){
if(t[p].l==t[p].r&&t[p].l==x){
return t[p].v;
}
pushdown(p);
int mid=(t[p].l+t[p].r)>>1;
int res;
if(x<=mid) res=query(p<<1,x);
else res=query(p<<1|1,x);
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t; cin>>t;
int w=0;
while(t--){
cout<<"Case #"<<++w<<":"<<endl;
init();
int n,m; cin>>n;
for(int i=1;i<n;i++){
int u,v; cin>>u>>v;
add(v,u);
flag[u]=1;
}
int s;
for(int i=1;i<=n;i++)
if(!flag[i]){
s=i; break;
}
dfs(s);
build(1,1,n<<1);
cin>>m;
for(int i=1;i<=m;i++){
char op; cin>>op;
if(op=='C'){
int x; cin>>x;
cout<<query(1,L[x])<<endl;
}else{
int x,y; cin>>x>>y;
update(1,L[x],R[x],y);
}
}
}
}
hdu 3974 Assign the task(dfs序上线段树)的更多相关文章
- HDU 3974 Assign the task (DFS序 + 线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3974 给你T组数据,n个节点,n-1对关系,右边的是左边的父节点,所有的值初始化为-1,然后给你q个操 ...
- 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 ...
- HDU 3974 Assign the task (DFS+线段树)
题意:给定一棵树的公司职员管理图,有两种操作, 第一种是 T x y,把 x 及员工都变成 y, 第二种是 C x 询问 x 当前的数. 析:先把该树用dfs遍历,形成一个序列,然后再用线段树进行维护 ...
- HDU 3974 Assign the task(dfs建树+线段树)
题目大意:公司里有一些员工及对应的上级,给出一些员工的关系,分配给某员工任务后,其和其所有下属都会进行这项任务.输入T表示分配新的任务, 输入C表示查询某员工的任务.本题的难度在于建树,一开始百思不得 ...
- bzoj3306: 树(dfs序+倍增+线段树)
比较傻逼的一道题... 显然求子树最小值就是求出dfs序用线段树维护嘛 换根的时候树的形态不会改变,所以我们可以根据相对于根的位置分类讨论. 如果询问的x是根就直接输出整棵树的最小值. 如果询问的x是 ...
- HDU 3974 Assign the task(DFS序)题解
题意:给出一棵树,改变树的一个节点的值,那么该节点及所有子节点都变为这个值.给出m个询问. 思路:DFS序,将树改为线性结构,用线段树维护.start[ ]记录每个节点的编号,End[ ]为该节点的最 ...
- [Assign the task][dfs序+线段树]
http://acm.hdu.edu.cn/showproblem.php?pid=3974 Assign the task Time Limit: 15000/5000 MS (Java/Other ...
- bzoj2819 DFS序 + LCA + 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=2819 题意:树上单点修改及区间异或和查询. 思维难度不高,但是题比较硬核. 整体思路是维护每一个结 ...
- HDU - 3974 Assign the task (DFS建树+区间覆盖+单点查询)
题意:一共有n名员工, n-1条关系, 每次给一个人分配任务的时候,(如果他有)给他的所有下属也分配这个任务, 下属的下属也算自己的下属, 每次查询的时候都输出这个人最新的任务(如果他有), 没有就输 ...
随机推荐
- MySQL学习Day01
1.MySQL的层级关系 2.xampp的安装使用 如果之前安装过mysql那么就需要将原来的mysql完全卸载干净 1.卸载之前安装的MySQL 安装xampp需要先卸载之前的mysql,以及更改m ...
- 改进你的c#代码的5个技巧(四)
像每一篇文章一样,我会重复几行.我在我的Core i3 CPU.4GB主内存和Windows 7平台上测试了以下代码.如果你在不同的硬件配置或使用不同的平台,那么你的输出可能会随着我的输出屏幕而变化, ...
- 【Linux】记一次xfs分区数据恢复
项目有一块磁盘无法挂载,而且还没有做RAID.... # mount /dev/sda /xxx 报错 mount: special device /dev/sda/ does not exist ...
- 【Linux】CentOS4 系统最后的网络yum源
------------------------------------------------------------------------------------------------- | ...
- 【Oracle】CBO优化详解
SQL优化是数据优化的重要方面,本文将分析Oracle自身的CBO优化,即基于成本的优化方法.Oracle为了自动的优化sql语句需要各种统计数据作为优化基础.外面会通过sql的追踪来分析sql的执行 ...
- 分布式系统:xxl-job改造spring-cloud
目录 改造原因 主要改造思路 调度中心 调度中心 执行器侧 总结 修改后的源码仓库地址:GitHub. : 改造原因 原有的xxl-job使用自己实现的http协议进行注册以及调度等,与目前框架中本身 ...
- EnvironmentPostProcessor怎么做单元测试?阿里P7解答
简介 从Spring Boot 1.3开始,我们可以在应用程序上下文刷新之前使用EnvironmentPostProcessor来自定义应用程序的Environment.Environment表示当前 ...
- Linux Ubuntu系统版本通过Crontab设置定时任务的执行
Linux Ubuntu系统版本通过Crontab设置定时任务的执行 本文由本人收集网络信息总结而来 特别鸣谢:https://linux.zone/2258 1 crontab 简单介绍以及语法使用 ...
- EntityFramework Core如何映射动态模型?
前言 本文我们来探讨下映射动态模型的几种方式,相信一部分童鞋项目有这样的需求,比如每天/每小时等生成一张表,此种动态模型映射非常常见,经我摸索,这里给出每一步详细思路,希望能帮助到没有任何头绪的童鞋, ...
- 前端面试之CSS权重问题!
前端面试之CSS权重问题! 下面的权重按照从小到大来排列! 1.通用选择器(*) 2.元素(类型)选择器 权重1 3.类选择器 权重10 4.属性选择器 5.伪类 6.ID 选择器 权重100 7.内 ...