标签:线段树 type space 链接 exce current print red panel
题目链接:https://vjudge.net/problem/HDU-3974
InputThe 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)OutputFor 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 <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 using namespace std; 13 typedef long long LL; 14 const double EPS = 1e-8; 15 const int INF = 2e9; 16 const LL LNF = 2e18; 17 const int MAXN = 5e4+10; 18 19 vector<int>g[MAXN]; 20 int have_fa[MAXN]; 21 int index, id[MAXN], le[MAXN], ri[MAXN]; 22 int task[MAXN<<2]; 23 24 void dfs(int u) 25 { 26 id[u] = le[u] = ++index; 27 for(int i = 0; i<g[u].size(); i++) 28 dfs(g[u][i]); 29 ri[u] = index; 30 } 31 32 void push_down(int u) 33 { 34 if(task[u]!=-1) 35 { 36 task[u*2] = task[u*2+1] = task[u]; 37 task[u] = -1; 38 } 39 } 40 41 void set_val(int u, int l, int r, int x, int y, int val) 42 { 43 if(x<=l && r<=y) 44 { 45 task[u] = val; 46 return; 47 } 48 49 push_down(u); 50 int mid = (l+r)>>1; 51 if(x<=mid) set_val(u*2, l, mid, x, y, val); 52 if(y>=mid+1) set_val(u*2+1, mid+1, r, x, y, val); 53 } 54 55 int query(int u, int l, int r, int x) 56 { 57 if(l==r) 58 return task[u]; 59 60 push_down(u); 61 int mid = (l+r)>>1; 62 if(x<=mid) return query(u*2, l, mid, x); 63 else return query(u*2+1, mid+1, r, x); 64 } 65 66 int main() 67 { 68 int n, m, T; 69 scanf("%d", &T); 70 for(int kase = 1; kase<=T; kase++) 71 { 72 scanf("%d", &n); 73 for(int i = 1; i<=n; i++) 74 g[i].clear(), have_fa[i] = 0; 75 for(int i = 1; i<n; i++) 76 { 77 int u, v; 78 scanf("%d%d", &u, &v); 79 g[v].push_back(u); 80 have_fa[u] = 1; 81 } 82 83 index = 0; 84 for(int i = 1; i<=n; i++) 85 if(!have_fa[i]) 86 dfs(i); 87 88 scanf("%d", &m); 89 memset(task, -1, sizeof(task)); 90 printf("Case #%d:\n", kase); 91 for(int i = 1; i<=m; i++) 92 { 93 char op[2]; int x, y; 94 scanf("%s", op); 95 if(op[0]==‘T‘) 96 { 97 scanf("%d%d", &x, &y); 98 set_val(1, 1, n, le[x], ri[x], y); 99 } 100 else 101 { 102 scanf("%d", &x); 103 printf("%d\n", query(1, 1, n, le[x])); 104 } 105 } 106 } 107 }
HDU3974 Assign the task —— dfs时间戳 + 线段树
标签:线段树 type space 链接 exce current print red panel
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7726086.html