Assign the task

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

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
/*
hdu 3974 线段树 将树弄到区间上 题意: 给定一棵树,50000个节点,50000个操作
C x表示查询x节点的值,
T x y表示更新x节点及其子节点的值为y 没想到的是普通并查集都能过,数据是由多水 - - 由于T操作每次更新当前节点以及它的子树,所有dfs一次,给每个节点进行编号。 每次更新
就成了对当前节点所覆盖区间的更新。 hhh-2016-04-22 14:09:04
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
using namespace std;
const ll mod = 1e9 + 7;
const int maxn = 50010; struct node
{
int l,r;
int val;
int lazy;
int mid() {return (l+r) >> 1;}
}tree[maxn<<2]; int tot,cnt;
int rt[maxn];
int st[maxn],ed[maxn];
vector <int > vec[maxn];
void dfs(int cur)
{
st[cur] = ++cnt;
for(int i = 0;i < vec[cur].size();i++)
{
dfs(vec[cur][i]);
}
ed[cur] = cnt;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].val = -1,tree[i].lazy = 0;
if(l == r)
return ;
int mid = tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
} void push_down(int i)
{
if(tree[i].lazy)
{
tree[lson].val = tree[i].val,tree[lson].lazy = 1;
tree[rson].val = tree[i].val,tree[rson].lazy = 1;
tree[i].lazy = 0;
}
} void update(int i,int l,int r,int val)
{
if(tree[i].l >= l && tree[i].r <= r)
{
tree[i].val = val;
tree[i].lazy = 1;
return ;
}
push_down(i);
int mid = tree[i].mid();
if(l <= mid)
update(lson,l,r,val);
if(r > mid)
update(rson,l,r,val);
// if(r <= mid)
// update(lson,l,r,val);
// else if(l > mid)
// update(rson,l,r,val);
// else
// {
// update(lson,l,mid,val);
// update(rson,mid+1,r,val);
// }
return ;
} int query(int i,int k)
{
if(tree[i].l == k && tree[i].r == k)
return tree[i].val;
int mid = tree[i].mid();
push_down(i);
if(k <= mid)
return query(lson,k);
else
return query(rson,k);
} int main()
{
int T,n,m,x,y;
int cas = 1;
scanf("%d",&T);
while(T--)
{
cnt = 0;
scanf("%d",&n);
printf("Case #%d:\n",cas++);
for(int i = 1;i <= n;i++)
{
vec[i].clear();
}
memset(rt,0,sizeof(rt));
int u,v;
for(int i = 1;i < n;i++)
{
scanf("%d%d",&u,&v);
vec[v].push_back(u);
rt[u] = 1;
}
int rot;
for(int i = 1;i <= n;i++)
{
if(!rt[i])
{
rot = i;
break;
}
}
dfs(rot);
build(1,1,cnt);
char op[10];
scanf("%d",&m);
for(int i = 1;i <= m;i++)
{
scanf("%s",op);
if(op[0] == 'C')
{
scanf("%d",&x);
printf("%d\n",query(1,st[x]));
}
else if(op[0] == 'T')
{
scanf("%d%d",&x,&y);
update(1,st[x],ed[x],y);
}
}
}
return 0;
}

  

hdu 3974 线段树 将树弄到区间上的更多相关文章

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

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

  2. HDU - 3974 Assign the task (线段树区间修改+构建模型)

    https://cn.vjudge.net/problem/HDU-3974 题意 有一棵树,给一个结点分配任务时,其子树的所有结点都能接受到此任务.有两个操作,C x表示查询x结点此时任务编号,T ...

  3. Assign the task HDU - 3974 (dfs序 + 线段树)

    有一家公司有N个员工(从1到N),公司里每个员工都有一个直接的老板(除了整个公司的领导).如果你是某人的直接老板,那个人就是你的下属,他的所有下属也都是你的下属.如果你是没有人的老板,那么你就没有下属 ...

  4. hdu 4747 线段树

    Mex Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  5. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  6. HDU 5877 dfs+ 线段树(或+树状树组)

    1.HDU 5877  Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...

  7. 归并树 划分树 可持久化线段树(主席树) 入门题 hdu 2665

    如果题目给出1e5的数据范围,,以前只会用n*log(n)的方法去想 今天学了一下两三种n*n*log(n)的数据结构 他们就是大名鼎鼎的 归并树 划分树 主席树,,,, 首先来说两个问题,,区间第k ...

  8. HDU 1754 I Hate It(线段树之单点更新,区间最值)

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. hdu 5877 线段树(2016 ACM/ICPC Asia Regional Dalian Online)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. 从PRISM开始学WPF(三)Prism-Region?

    从PRISM开始学WPF(一)WPF? 从PRISM开始学WPF(二)Prism? 从PRISM开始学WPF(三)Prism-Region? 从PRISM开始学WPF(四)Prism-Module? ...

  2. 搭建vue项目环境

    前言 在开发本项目之前,我对vue,react,angular等框架了解,仅限于知道它们是什么框架,他们的核心是什么,但是并没有实际使用过(angular 1.0版本用过,因为太难用,所以对这类框架都 ...

  3. 原生ajax的请求函数

    ajax:一种请求数据的方式,不需要刷新整个页面:ajax的技术核心是 XMLHttpRequest 对象:ajax 请求过程:创建 XMLHttpRequest 对象.连接服务器.发送请求.接收响应 ...

  4. Linux安装svn服务图文详解 ;出现No repository found in 'svn***问题

    Linux安装svn服务 ** 示例都是用的root权限,可选择用 sudo** 1:检查 安装条件为:Linux(centos)上未安装过svn服务,若安装过或安装失败请自行删除,这里不多介绍.检查 ...

  5. 微信开发之SVN提交代码与FTP同步到apache的根目录

    SVN是协同开发的,版本控制器,就是几个人同时开发,可以提交代码到SVN服务器,这样就可以协同开发,一般是早上上班首先更新下代码,然后自己修改代码 工作一天之后,修改代码之后,下班之前,更新代码,然后 ...

  6. Class-Based-View(CBV)

    我们都知道,Python是一个面向对象的编程语言,如果只用函数来开发,有很多面向对象的优点就错失了(继承.封装.多态).所以Django在后来加入了Class-Based-View.可以让我们用类写V ...

  7. jquery实现对div的拖拽功能

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 新概念英语(1-17)How do you do ?

    Is there a problem wtih the Customers officer? What are Michael Baker and Jeremy Short's jobs? A:Com ...

  9. Python 中格式化字符串 % 和 format 两种方法之间的区别

    Python2.6引入了 format 格式化字符串的方法,现在格式化字符串有两种方法,就是 % 和 format ,具体这两种方法有什么区别呢?请看以下解析. # 定义一个坐标值 c = (250, ...

  10. JSON定义

    如果我们要在不同的编程语言之间传递对象,就必须把对象序列化为标准格式,比如xml,但更好的方法是序列化为JSON,因为JSON表示出来就是一个字符串,可以被所有语言读取,也可以方便地存储到磁盘或者通过 ...