hdu 3974 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

题目中给出一个树形关系,起初打算直接利用这个树来解,但是超时了。据说可以用并查集,但我觉得还是线段树比较稳。要把所有人都排成一条线才能用线段树,这就需要重新编号,这样下来每个人都代表一个区间(他本人及其下属),规定公司最高老总编号是1。从根节点深搜这棵树,给每个人盖一个时间戳,作为他们的新位置编号,确定出每个节点包含他子树的区间,之后就是线段树的工作了,区间更新点查询。

#include <cstdio>
#include <vector>
using namespace std;

const int M = 5e4 + 5;
struct Node
{
	int l, r;
}emp[M];
struct SegT
{
	int l, r, val, tag;
}seg[4*M];
vector<int> adj[M];
int id;
bool boss[M];
void pushDown(int rt)
{
	if(seg[rt].l==seg[rt].r)
		seg[rt].tag = -1;
	else if(seg[rt].tag!=-1)
	{
		seg[rt<<1].tag = seg[rt].tag;
		seg[rt<<1].val = seg[rt].tag;
		seg[rt<<1|1].tag = seg[rt].tag;
		seg[rt<<1|1].val = seg[rt].tag;
		seg[rt].tag = -1;
	}
	return;
}
void build(int rt, int L, int R)
{
	seg[rt].l = L;
	seg[rt].r = R;
	seg[rt].tag = -1;
	if(seg[rt].l == seg[rt].r)
	{
		seg[rt].val = -1;
		return;
	}
	int m = (seg[rt].l + seg[rt].r) >> 1;
	build(rt<<1, L, m);
	build(rt<<1|1, m+1, R);
	seg[rt].val = -1;
}
void update(int rt, int L, int R, int ch)
{
	if(seg[rt].l >= L&& seg[rt].r <= R)
	{
		seg[rt].val = ch;
		seg[rt].tag = ch;
		return;
	}
	int m = (seg[rt].l + seg[rt].r) >> 1;
	pushDown(rt);
	if(L <= m)
		update(rt<<1, L, R, ch);
	if(R > m)
		update(rt<<1|1, L, R, ch);
}
int query(int rt, int ind)
{
	if(seg[rt].l==seg[rt].r)
		return seg[rt].val;
	int ans;
	int m = (seg[rt].l + seg[rt].r) >> 1;
	pushDown(rt);
	if(ind <= m)
		ans = query(rt<<1, ind);
	else
		ans = query(rt<<1|1, ind);
	return ans;
}
void dfs(int st)//编号从这里开始
{
	emp[st].l = id++;//第一次在没有遍历子树的时候盖戳表示区间左端点
	vector<int>::iterator it;
	for(it=adj[st].begin();it!=adj[st].end();it++)
		dfs(*it);
	emp[st].r = id-1;//遍历完子树再盖一次表示区间右端点
}
int main()
{
	int t, n, q, x, y, root;
	char c[5];
	scanf("%d", &t);
	for(int k=1;k<=t;k++)
	{
		scanf("%d", &n);
		for(int i=1;i<=n;i++)
		{
			adj[i].clear();
			boss[i] = true;
		}
		id = 1;
		build(1, 1, n);
		for(int i=1;i<n;i++)
		{
			scanf("%d%d", &x, &y);
			adj[y].push_back(x);
			boss[x] = false;
		}
		for(int i=1;i<=n;i++)
			if(boss[i])
			{
				root = i;
				break;
			}
		dfs(root);
		scanf("%d", &q);
		printf("Case #%d:\n", k);
		while(q--)
		{
		 	scanf("%s", c);
			if(c[0]=='T')
			{
				scanf("%d%d", &x, &y);
				update(1, emp[x].l, emp[x].r, y);
			}
			else
			{
				scanf("%d", &x);
				printf("%d\n", query(1, emp[x].l));
			}
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值