hdu3974 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
这题建树有些困难
首先要把所有的员工都重新编号
用used 来标记做过下属的员工 找到最大的boss
之后记录下每一个员工管辖的区间
这样就可以转换为线段树的问题了
单点查询区间更新
实现的时候和普通线段树都一样 函数的调用点的时候有一点差别而已
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std;
const int maxn = 50005;
int t, m, n, tot, cnt;
struct edge{
int to, next;
}edges[maxn];
int start[maxn], ed[maxn], head[maxn];
struct node{
int val, lazy;
int l, r;
}tree[maxn << 2];
void init()
{
tot = 0;
cnt = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v)
{
edges[tot].to = v;
edges[tot].next = head[u];
head[u] = tot++;
}
void dfs(int u)//给boss和employee重新编号 变成区间
{
++cnt;
start[u] = cnt;
for(int i = head[u]; i != -1; i = edges[i].next){
dfs(edges[i].to);
}
ed[u] = cnt;
}
void build(int l, int r, int rt)
{
tree[rt].l = l;
tree[rt].r = r;
tree[rt].val = -1;
tree[rt].lazy = 0;
if(l == r) return;
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
}
void updatesame(int rt, int v)
{
if(rt){
tree[rt].val = v;
tree[rt].lazy = 1;
}
}
void pushdown(int rt)
{
if(tree[rt].lazy){
updatesame(rt << 1, tree[rt].val);
updatesame(rt << 1 | 1, tree[rt].val);
tree[rt].lazy = 0;
}
}
void update(int v, int l, int r, int rt)
{
if(tree[rt].l == l && tree[rt].r == r){
updatesame(rt, v);
return;
}
pushdown(rt);
int m = (tree[rt].l + tree[rt].r) >>1;
if(r <= m) update(v, l, r, rt << 1);
else if(l > m) update(v, l, r, rt << 1 | 1);
else{
update(v, l, m, rt << 1);
update(v, m + 1, r, rt << 1 | 1);
}
}
int query(int u, int rt)
{
if(tree[rt].l == u && tree[rt].r == u){
return tree[rt].val;
}
pushdown(rt);
int m = (tree[rt].l + tree[rt].r) / 2;
if(u <= m) return query(u, rt << 1);
else return query(u, rt << 1 | 1);
}
bool used[maxn];
int main()
{
cin>>t;
for(int cas = 1; cas <= t; cas++){
printf("Case #%d:\n", cas);
memset(used, 0, sizeof(used));
init();
cin>>n;
for(int i = 1; i < n; i++){
int u, v;
scanf("%d%d", &u, &v);
used[u] = true;
addedge(v, u);
}
for(int i = 1; i <= n; i++){
if(!used[i]){
dfs(i);
break;
}
}
build(1, cnt, 1);
char op[10];
scanf("%d", &m);
while(m--){
scanf("%s", op);
int a, b;
if(op[0] == 'C'){
scanf("%d", &a);
printf("%d\n", query(start[a], 1));
}
else{
scanf("%d%d", &a, &b);
update(b, start[a], ed[a], 1);
}
}
}
return 0;
}
hdu3974 Assign the task【线段树】的更多相关文章
- hdu3974 Assign the task线段树 dfs序
题意: 无序的给编号为1-n的员工安排上下级, 操作一:给一个员工任务C,则该员工以及他的下级任务都更换为任务C 操作二:询问一个员工,返回他的任务 题解: 给一个员工任务,则他所在组都要改变,联 ...
- HDU3974 Assign the task —— dfs时间戳 + 线段树
题目链接:https://vjudge.net/problem/HDU-3974 There is a company that has N employees(numbered from 1 to ...
- 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 ...
- 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 im ...
- HDU-3974 Assign the task(多叉树DFS时间戳建线段树)
http://acm.hdu.edu.cn/showproblem.php?pid=3974 Time Limit: 15000/5000 MS (Java/Others) Memory Lim ...
- HDU3974 Assign the task(多叉树转换为线段+线段树区间染色)
题目大意:有n个人,给你他们的关系(老板和员工),没有直属上司的人就是整个公司的领导者,这意味着n个人形成一棵树(多叉树).当一个人被分配工作时他会让他的下属也做同样的工作(并且立即停止手头正在做的工 ...
- HDU3974 Assign the task
Assign the task Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树
E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...
- Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序
题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...
随机推荐
- [Model] LeNet-5 by Keras
典型的卷积神经网络. 数据的预处理 Keras傻瓜式读取数据:自动下载,自动解压,自动加载. # X_train: array([[[[ 0., 0., 0., ..., 0., 0., 0.], [ ...
- Batch Normalization 学习笔记
原文:http://blog.csdn.net/happynear/article/details/44238541 今年过年之前,MSRA和Google相继在ImagenNet图像识别数据集上报告他 ...
- 配置ORACLE 11g绿色版客户端和PLSQL远程连接环境
配置ORACLE 11g绿色版客户端和PLSQL环境 本方法是通过使用ORACLE官方提供的精简版客户端,即绿色免安装的客户端. Instant client的版本很多:主要是Basic和Ba ...
- 安装office2010出现了错误,提示要安装MSXML6.10.1129.0解决方法
将下面的内容复制到记事本中,然后将记事本存成.reg文件 Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\TypeLib\{F5078F1 ...
- ios开发之--MJRefresh的简单使用
MJRefresh是MJ大神写的框架,很强大,好多外国开发者都在用! 具体方法如下: -(void)requestData { NSString *userIdStr = [NSString stri ...
- C语言中的static关键字
C语言代码是以文件为单位来组织的,在一个源程序的所有源文件中,一个外部变量(注意不是局部变量)或者函数只能在一个源程序中定义一次,如果有重复定义的话编译器就会报错.伴随着不同源文件变量和函数之间的相互 ...
- 【RF库Collections测试】Log Dictionary 【同log list】
Name:Log DictionarySource:Collections <test library>Arguments:[ dictionary | level=INFO ]Logs ...
- Unity Animation需要Inspector右键打开Debug模式,然后勾选Legacy,最后再Inspector右键打开Normal
- Myecplise反编译工具安装
我使用的是Myecplise8.5 下载jad.exe文件jad158g.win.zip:http://varaneckas.com/jad/ 下载jadeclipse插件 net.sf.jadcli ...
- C#实现新建文件并写入内容
using System; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(stri ...