hdu 3974 线段树重新排序区间更新
2015-10-30 14:04
274 查看
Assign the task
[b] Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)[/b]
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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)
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1 5 4 3 3 2 1 3 5 2 5 C 3 T 2 1 C 3 T 3 2 C 3
[align=left]Sample Output[/align]
Case #1: -1 1 2
题目大意:v u表示v是u的下属
T x y 当x被分配到y任务时,他和下属的任务立刻变更为y
C x 询问x当前所分配的任务
解题思路:用dfs给各个点重新编号,用线段树进行区间更新与查询;
ps: 数组不能用end命名么?为何交上去会ce呢,斌神就这么写的啊,哎,我还是太弱了
代码:
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; const int maxn=50010; struct Edge { int to,next; }edge[maxn]; int head[maxn],tot; int cnt; int start[maxn],End[maxn]; void unit() { cnt=0; tot=0; memset(head,-1,sizeof(head)); } void addedge(int u , int v) { edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++; } void dfs(int u) { ++cnt; start[u]=cnt; for(int i=head[u];i!=-1;i=edge[i].next) { dfs(edge[i].to); } End[u]=cnt; } struct Node { int val; int lazy; int l,r; }tree[maxn<<2]; void pushup(int root,int v) { tree[root].val=v; tree[root].lazy=1; } void pushdown(int root) { if(tree[root].lazy) { pushup(root<<1,tree[root].val); pushup(root<<1|1,tree[root].val); tree[root].lazy=0; } } void build(int root,int l,int r) { tree[root].l=l; tree[root].r=r; tree[root].val=-1; tree[root].lazy=0; if(l==r) return; int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); } void update(int root,int l,int r,int v) { if(tree[root].l==l&&tree[root].r==r) { pushup(root,v); return; } pushdown(root); int mid=(tree[root].l+tree[root].r)>>1; if(r<=mid) update(root<<1,l,r,v); else if(mid<l) update(root<<1|1,l,r,v); else { update(root<<1,l,mid,v); update(root<<1|1,mid+1,r,v); } } int query(int root,int u) { if(tree[root].l==u&&tree[root].r==u) return tree[root].val; pushdown(root); int mid=(tree[root].l+tree[root].r)>>1; if(u<=mid) return query(root<<1,u); else if(u>mid) return query(root<<1|1,u); } bool vis[maxn]; int main() { int n,T; scanf("%d",&T); int cas=0; while(T--) { cas++; printf("Case #%d:\n",cas); int u,v; memset(vis,0,sizeof(vis)); unit(); scanf("%d",&n); for(int i=1;i<n;i++) { scanf("%d%d",&u,&v); vis[u]=1; addedge(v,u); } // cout<<"1"<<endl; for(int i=1;i<=n;i++) { if(!vis[i]) { dfs(i); break; } } build(1,1,cnt); char c[10]; int m; scanf("%d",&m); while(m--) { scanf("%s",c); if(c[0]=='C') { scanf("%d",&u); printf("%d\n",query(1,start[u])); } else { scanf("%d%d",&u,&v); update(1,start[u],End[u],v); } } } return 0; }
相关文章推荐
- 转载 SQL Server 2008 R2 事务与隔离级别实例讲解
- js-ajax相关基础知识整理
- 迪米特法则 -- 大话设计模式
- 在使用EF时调用DBFUNCTION扩展方法时,发生ENTITYCOMMANDEXECUTIONEXCEPTION 异常的解决办法
- 使用复杂的view加上listview实现页面整体滑动效果
- org.json.JSONArray#remove
- object_isClass
- 杭电ACM1159
- PLSQL中文乱码问题(转载)
- 遍历界面中所有的视图
- struts2 的多文件上传
- 模板方法模式 -- 大话设计模式
- 常用的MIME类型
- Binder进程间通信机制的Client进程和Server进程的通信过程
- 关于pg的EXTRACT(8:00-10:00)
- 1039. 到底买不买(20)
- 51nod 1101 换零钱 (0-1背包,DP)
- 网络通信基础
- unity寻路——一劳永逸地解决寻路问题
- datatables页面的刷新操作