首页 > ACM题库 > HDU-杭电 > HDU 3974-Assign the task-线段树-[解题报告]HOJ
2015
04-14

HDU 3974-Assign the task-线段树-[解题报告]HOJ

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.

输入:

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)

输出:

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)

样例输入:

1 
5 
4 3 
3 2 
1 3 
5 2 
5 
C 3 
T 2 1
 C 3 
T 3 2 
C 3

样例输出:

Case #1:
-1 
1 
2

http://acm.hdu.edu.cn/showproblem.php?pid=3974
题意: 给定一棵树,50000个节点,50000个操作,C x表示查询x节点的值,T x y表示更新x节点及其子节点的值为y。。。

分析: 很明显的时间戳类型线段树。。。通过一个dfs给树中各节点重新分配一个遍历的序号。。写的过程中写着写着写乱了,所以写了好久,甚至一度以为是错的。。。悲剧,还是太水啊。。。
ps:此题的数据太弱了实际上乱暴力都可以过。。。.。。。。

代码:

#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <iostream>
using namespace std;

const int N=100010;
struct node
{
	int l, r, mid;
	int value, lazy, parent;
} tr[N*4];
int n, m, up, l[N], r[N], first[N], edge[N], next[N], f[N];
int value;

void add(int x, int y)
{
	f[x] = y;
	edge[up] = x;
	next[up] = first[y];
	first[y] = up++;
}
int findset(int i)
{
	while(f[i]!=i)
		i = f[i];
	return i;
}
void dfs(int p)
{
	int i;
	i = first[p];
	l[p] = up;
	while(i!=-1)
	{
		dfs(edge[i]);
		i = next[i];
	}
	r[p] = up++;
}
void build(int l, int r, int p)
{
	tr[p].l = l;
	tr[p].r = r;
	tr[p].value = -1;
	tr[p].lazy = -1;
	tr[p].parent = -1;
	tr[p].mid = (l+r)>>1;
	if(l==r)
		return;
	build(l, tr[p].mid, p*2);
	build(tr[p].mid+1, r, p*2+1);
}
void down(int p)
{
	if(tr[p].lazy!=-1)
	{
		tr[p*2].value = tr[p].lazy;
		tr[p*2].lazy = tr[p].lazy;
		tr[p*2].parent = tr[p].lazy;
		tr[p*2+1].value = tr[p].lazy;
		tr[p*2+1].lazy = tr[p].lazy;
		tr[p*2+1].parent = tr[p].lazy;
		tr[p].lazy = -1;
	}
}
void update(int l, int r, int p)
{
	if(l==tr[p].l && r==tr[p].r)
	{
		tr[p].value = value;
		tr[p].lazy = value;
		return ;
	}
	down(p);
	if(r<=tr[p].mid)
		update(l, r, p*2);
	else if(l>tr[p].mid)
		update(l, r, p*2+1);
	else
	{
		update(l, tr[p].mid, p*2);
		update(tr[p].mid+1, r, p*2+1);
	}
}
int query(int l, int r, int p)
{
	if(l==tr[p].l && r==tr[p].r)
		return tr[p].value;
	down(p);
	if(r<=tr[p].mid)
		return query(l, r, p*2);
	else if(l>tr[p].mid)
		return query(l, r, p*2+1);
	else
	{
		return tr[p*2].parent;
	}
}
int main()
{
	char s[20];
	int i, j, x, y, cas, cas1, root;
	
	scanf("%d", &cas);
	cas1 = 1;
	while(cas--)
	{
		scanf("%d", &n);
		for(i=1; i<=n; i++)
		{
			first[i] = -1;
			f[i] = i;
		}
		
		up = 1;
		for(i=1; i<n; i++)
		{
			scanf("%d %d", &x, &y);
			add(x, y);
		}

		root = findset(1);
		up = 1;
		dfs(root);
		up--;
		//for(i=1; i<=5; i++)
		//	printf("i=%d...%d %d...\n", i, l[i],  r[i]);

		build(1, up, 1);
		printf("Case #%d:\n", cas1++);
		scanf("%d", &m);
		while(m--)
		{
			scanf("%s", s);
			if(s[0]=='C')
			{
				scanf("%d", &x);
				printf("%d\n", query(r[x], r[x], 1));
			}
			else
			{
				scanf("%d %d", &x, &value);
				update(l[x], r[x], 1);
			}
		}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/ggggiqnypgjg/article/details/6723585


  1. Thanks for using the time to examine this, I truly feel strongly about it and enjoy finding out far more on this subject matter. If achievable, as you achieve knowledge

  2. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。

  3. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同,其树的前、中、后序遍历是相同的,但在此处不能使用中序遍历,因为,中序遍历的结果就是排序的结果。经在九度测试,运行时间90ms,比楼主的要快。