您的位置:首页 > 其它

hdu 3974 Assign the task(区间建树)(区间更新+单点查询)

2017-05-01 20:52 316 查看

Assign the task

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 inqu
4000
iry, 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

思路:区间建树,左端点记录当前节点的位置,右端点记录当前节点的最后一个子节点的位置,这样每一个节点的左右端点表示的就是[自身的位置,最后一个子节点的位置],也就相当于这个区间存储了它和它的每一个子节点

然后线段树走起(具体详见代码)

代码:

#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;

#define maxn 50010
struct node
{
int le,ri,val;
int mid()
{
return (le+ri)>>1;
}
} tree[maxn<<2];
int left[maxn],right[maxn],pre[maxn],change[maxn];
vector<int>v[maxn];
int len,n;

void init()
{
memset(change,0,sizeof(change));
memset(left,0,sizeof(left));
memset(right,0,sizeof(right));
for(int i=1; i<=n; ++i)
pre[i]=i,v[i].clear();
}

void dfs(int rt)//区间建树
{
left[rt]=len;
for(int i=0; i<v[rt].size(); ++i)
{
++len;
dfs(v[rt][i]);
}
right[rt]=len;
}

void Build(int rt,int le,int ri)
{
tree[rt].le=le;
tree[rt].ri=ri;
tree[rt].val=-1;
if(le==ri)
return ;
int mid=tree[rt].mid();
Build(rt<<1,le,mid);
Build(rt<<1|1,mid+1,ri);
}

void Pushdown(int rt)
{
change[rt<<1]=change[rt<<1|1]=change[rt];
tree[rt<<1].val=tree[rt<<1|1].val=change[rt];
change[rt]=0;
}

void Update(int rt,int left,int right,int val)
{
if(left==tree[rt].le&&tree[rt].ri==right)
{
change[rt]=val;
tree[rt].val=val;
return ;
}
if(change[rt])
Pushdown(rt);
int mid=tree[rt].mid();
if(right<=mid)
Update(rt<<1,left,right,val);
else if(left>mid)
Update(rt<<1|1,left,right,val);
else
{
Update(rt<<1,left,mid,val);
Update(rt<<1|1,mid+1,right,val);
}

}

int Query(int rt,int u)
{
if(tree[rt].le==u&&tree[rt].ri==u)
return tree[rt].val;
if(change[rt])
Pushdown(rt);
int mid=tree[rt].mid();
if(u<=mid)
return Query(rt<<1,u);
else if(u>mid)
return Query(rt<<1|1,u);
}

int main()
{
int t,Q,k=0;
scanf("%d",&t);
while(++k<=t)
{
scanf("%d",&n);
init();
int x,y,root;
for(int i=1; i<n; ++i)
{
scanf("%d%d",&x,&y);
v[y].push_back(x);
pre[x]=y;
}
for(int i=1; i<=n; ++i)
if(pre[i]==i)
{
root=i;
break;
}
len=1;
dfs(root);
Build(1,1,len);
scanf("%d",&Q);
char op[5];
printf("Case #%d:\n",k);
for(int i=1; i<=Q; ++i)
{
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d",&x);
printf("%d\n",Query(1,left[x]));
}
else
{
scanf("%d%d",&x,&y);
Update(1,left[x],right[x],y);
}
}
}
return 0;
}


总结:真是笨啊,都已经建好树了,却不知道怎样利用线段树去进行操作~Orz

我以为区间建的树可以直接当成线段树使用,却发现它不是二叉树,然后就。。。

却不曾想区间建的树它本质就是一个区间,直接通过这个区间建起线段树就好了。

现在果然体会到以前碰到题不会就看题解的弊端了。如果一道题思考不够深刻,就去看题解的话,那么做完这道题所得的收获就非常小了。得不偿失啊!

谨记:遇到不会的题,不要轻易地看题解,除非你认为把自己所有能考虑的情况都考虑了。如果看题解的话,不要看代码,看看思路就行了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: