博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3974 Assign the task(简单线段树)
阅读量:5889 次
发布时间:2019-06-19

本文共 4940 字,大约阅读时间需要 16 分钟。

Assign the task

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 636    Accepted Submission(s): 322

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
 

 

Source
 

 

 

用线段树修改区间值,查询单点值。

好久没写线段树了,这都写挫。。。

1 /* ***********************************************  2 Author        :kuangbin  3 Created Time  :2013-11-17 19:50:24  4 File Name     :E:\2013ACM\比赛练习\2013-11-17\C.cpp  5 ************************************************ */  6   7 #include 
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 #include
15 #include
16 #include
17 #include
18 #include
19 using namespace std; 20 21 const int MAXN = 50010; 22 struct Edge 23 { 24 int to,next; 25 }edge[MAXN]; 26 int head[MAXN],tot; 27 int cnt; 28 int start[MAXN],end[MAXN]; 29 void init() 30 { 31 cnt = 0; 32 tot = 0; 33 memset(head,-1,sizeof(head)); 34 } 35 void addedge(int u,int v) 36 { 37 edge[tot].to = v; 38 edge[tot].next = head[u]; 39 head[u] = tot++; 40 } 41 void dfs(int u) 42 { 43 ++cnt; 44 start[u] = cnt; 45 for(int i = head[u];i != -1;i = edge[i].next) 46 { 47 dfs(edge[i].to); 48 } 49 end[u] = cnt; 50 } 51 struct Node 52 { 53 int l,r; 54 int val; 55 int lazy; 56 }segTree[MAXN*4]; 57 void Update_Same(int r,int v) 58 { 59 if(r) 60 { 61 segTree[r].val = v; 62 segTree[r].lazy = 1; 63 } 64 } 65 void push_down(int r) 66 { 67 if(segTree[r].lazy) 68 { 69 Update_Same(r<<1,segTree[r].val); 70 Update_Same((r<<1)|1,segTree[r].val); 71 segTree[r].lazy = 0; 72 } 73 } 74 void Build(int i,int l,int r) 75 { 76 segTree[i].l = l; 77 segTree[i].r = r; 78 segTree[i].val = -1; 79 segTree[i].lazy = 0; 80 if(l == r)return; 81 int mid = (l+r)/2; 82 Build(i<<1,l,mid); 83 Build((i<<1)|1,mid+1,r); 84 } 85 void update(int i,int l,int r,int v) 86 { 87 if(segTree[i].l == l && segTree[i].r == r) 88 { 89 Update_Same(i,v); 90 return; 91 } 92 push_down(i); 93 int mid = (segTree[i].l + segTree[i].r)/2; 94 if(r <= mid)update(i<<1,l,r,v); 95 else if(l > mid)update((i<<1)|1,l,r,v); 96 else 97 { 98 update(i<<1,l,mid,v); 99 update((i<<1)|1,mid+1,r,v);100 }101 }102 int query(int i,int u)103 {104 if(segTree[i].l == u && segTree[i].r == u)105 return segTree[i].val;106 push_down(i);107 int mid = (segTree[i].l + segTree[i].r)/2;108 if(u <= mid)return query(i<<1,u);109 else return query((i<<1)|1,u);110 }111 bool used[MAXN];112 int main()113 {114 //freopen("in.txt","r",stdin);115 //freopen("out.txt","w",stdout);116 int n;117 int T;118 scanf("%d",&T);119 int iCase = 0;120 while(T--)121 {122 iCase++;123 printf("Case #%d:\n",iCase);124 int u,v;125 memset(used,false,sizeof(used));126 init();127 scanf("%d",&n);128 for(int i = 1;i < n;i++)129 {130 scanf("%d%d",&u,&v);131 used[u] = true;132 addedge(v,u);133 }134 for(int i = 1;i <= n;i++)135 if(!used[i])136 {137 dfs(i);138 break;139 }140 Build(1,1,cnt);141 char op[10];142 int m;143 scanf("%d",&m);144 while(m--)145 {146 scanf("%s",op);147 if(op[0] == 'C')148 {149 scanf("%d",&u);150 printf("%d\n",query(1,start[u]));151 }152 else153 {154 scanf("%d%d",&u,&v);155 update(1,start[u],end[u],v);156 }157 }158 }159 return 0;160 }

 

 

 

 

转载地址:http://affsx.baihongyu.com/

你可能感兴趣的文章
《无人机DIY》——4 制作四轴直升机I:选择 机身 
查看>>
《单页Web应用:JavaScript从前端到后端》——1.3 精心编写的单页应用的用户效益...
查看>>
 百度推广:下拉框与相关搜索优化分析
查看>>
《Android游戏开发详解》一1.3 声明和初始化变量
查看>>
Go程序设计语言2.1 名称
查看>>
Git 系列(四):在 Git 中进行版本回退
查看>>
storm集群的监控
查看>>
《Android Studio应用开发实战详解》——第1章,第1.3节Android系统架构
查看>>
记录一些React的一些细节,会不断更新
查看>>
自己常用的mixin(持续更新)
查看>>
[译] 移动技术在改善财务健康方面的作用
查看>>
播放音频的工具类
查看>>
iOS面试题03-原理篇
查看>>
JavaScript 基础之原型和原型链
查看>>
jvm逃逸分析
查看>>
数组、字符串常用方法
查看>>
前端面试指南
查看>>
【整理】18种推荐的CSS命名和书写规范
查看>>
new命令的原理
查看>>
人工智能:是拿什么向奇点迫近的?
查看>>