HDU 3974 Assign the task

dfs序可以把结点v及其子树放在连续的区间中
分配任务操作T x y相当于把一个连续的区间置为y,可以用线段树来维护
查询操作为线段树的单点查询

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
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 50000 + 10;

int N, M;
vector<int> G[MAX_N];
bool leader[MAX_N];

vector<int> vs;
int id[MAX_N];
int size[MAX_N];

void init() {
for (int v = 1; v <= N; v++) {
G[v].clear();
leader[v] = true;
}
vs.clear();
}

void dfs(int v) {
id[v] = vs.size();
vs.push_back(v);
size[v] = 1;
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
dfs(u);
size[v] += size[u];
}
}

#define lc (2 * k + 1)
#define rc (2 * k + 2)
#define m ((l + r) / 2)

const int ST_SIZE = 1 << 17;
int task[ST_SIZE];

void build() {
memset(task, -1, sizeof(task));
}

void push_down(int k) {
if (task[k] != -1) {
task[lc] = task[rc] = task[k];
task[k] = -1;
}
}

void update(int k, int l, int r, int a, int b, int x) {
if (b <= l || r <= a) {
return;
} else if (a <= l && r <= b) {
task[k] = x;
} else {
push_down(k);
update(lc, l, m, a, b, x);
update(rc, m, r, a, b, x);
}
}

int query(int k, int l, int r, int a) {
if (l == r - 1) {
return task[k];
} else {
push_down(k);
if (a < m) return query(lc, l, m, a);
else return query(rc, m, r, a);
}
}

int main() {
int T;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
printf("Case #%d:\n", t);
scanf("%d", &N);
init();
for (int i = 1; i < N; i++) {
int u, v;
scanf("%d %d", &u, &v);
G[v].push_back(u);
leader[u] = false;
}

int root = -1;
for (int v = 1; v <= N; v++) {
if (leader[v]) root = v;
}

dfs(root);
build();

scanf("%d", &M);
for (int i = 0; i < M; i++) {
char cmd;
int x, y;
scanf(" %c", &cmd);
if (cmd == 'C') {
scanf("%d", &x);
printf("%d\n", query(0, 0, N, id[x]));
} else {
scanf("%d %d", &x, &y);
update(0, 0, N, id[x], id[x] + size[x], y);
}

}
}
return 0;
}