Assign the task(DFS时间戳+线段树)

描述

传送门:HDU3974

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 ywhich 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
2
3
4
5
6
7
8
9
10
11
12
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

1
2
3
4
Case #1:
-1
1
2

思路

  • 题意:给定点的上下级关系,规定如果给$i$分配任务$a$,那么他的所有下属。都停下手上的工作,开始做$a$。现在要编程实现分配任务和查询操作。
  • 用DFS给每个结点打上时间戳,使每个结点对应一个区间,这样儿子结点所对应的一定在父亲结点的区间内。
  • 然后更新和查询操作就可以对应线段树的区间更新和单点查询了。

代码

1
2
3
4
#define lson rt << 1
#define rson rt << 1 | 1
#define Lson l, m, lson
#define Rson m + 1, r, rson
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <bits/stdc++.h>
using namespace std;
#define clr(a,x) memset(a, x, sizeof(a))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

const int maxn = 50005;
vector <int> G[maxn];
int deg[maxn];
int L[maxn], R[maxn];
int val[maxn << 2];
int dfs_clock;

void init(int n)
{
dfs_clock = 0;
clr(deg, 0);
for (int i = 1; i <= n; i++)
G[i].clear();
}

void add_edge(int u, int v)
{
G[v].pb(u);
deg[u]++;
}

void dfs(int u)
{
L[u] = ++dfs_clock;
for (int i = 0; i < G[u].size(); i++)
dfs(G[u][i]);
R[u] = dfs_clock;
}

void build(int l, int r, int rt)
{
val[rt] = -1;
if (l == r) return;
int m = (l + r) >> 1;
build(Lson);
build(Rson);
}

void PushDown(int rt)
{
if (val[rt] == -1) return;
val[lson] = val[rson] = val[rt];
val[rt] = -1;
}

void update(int L, int R, int setv, int l, int r, int rt)
{
if (L <= l && r <= R)
{
val[rt] = setv;
return;
}
PushDown(rt);
int m = (l + r) >> 1;
if (L <= m) update(L, R, setv, Lson);
if (m < R) update(L, R, setv, Rson);
}

int query(int p, int l, int r, int rt)
{
if (l == r) return val[rt];
PushDown(rt);
int m = (l + r) >> 1;
if (p <= m) return query(p, Lson);
if (m < p) return query(p, Rson);
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int t, kase = 0;
scanf("%d", &t);
while (t--)
{
int n, m, x, y;
char c[5];
scanf("%d", &n);
init(n);
for (int i = 1; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v);
}
for (int i = 1; i <= n; i++)
if (deg[i] == 0)
{
dfs(i);
break;
}
build(1, n, 1);
scanf("%d", &m);
printf("Case #%d:\n", ++kase);
while (m--)
{
scanf("%s", c);
if (c[0] == 'T')
{
scanf("%d%d", &x, &y);
update(L[x], R[x], y, 1, n, 1);
}
else
{
scanf("%d", &x);
printf("%d\n", query(L[x], 1, n, 1));
}
}
}
return 0;
}
捐助作者
0%