每日一水第35弹(赛码"BestCoder"杯中国大学生程序设计冠军赛)

 

http://acm.hdu.edu.cn/showproblem.php?pid=5221

树链剖分,注意到子树跟路径的dfs序都是相邻的就可以暴力set搞了。

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
130
131
132
133
134
135
136
137
138
139
140
#pragma comment(linker, "/STACK:102400000,102400000")
#include <vector>
#include <algorithm>
#include <set>
#include <cstdio>
using namespace std;
const int N = 100010;
int head[N], nxt[N*2], pnt[N*2], E;
void add_edge(int a, int b)
{
    pnt[E] = b;
    nxt[E] = head[a];
    head[a] = E++;
}
int pre[N], succ[N], dep[N], size[N];
int L[N], R[N], dfn, node[N];
void dfs(int u, int fa=-1)
{
    pre[u] = fa, size[u] = 1, succ[u] = -1;
    dep[u] = ~fa ? dep[fa] + 1 : 0;
    int v, mx = 0;
    for(int i = head[u]; ~i; i = nxt[i]) {
        if(fa == (v = pnt[i])) {
            continue;
        }
        dfs(v, u);
        if(size[v] > mx) {
            succ[u] = v, mx = size[v];
        }
        size[u] += size[v];
    }
    R[u] = dfn;
}
int first[N], id[N], cnt;
void make_chain(int u, bool in_chain=false)
{
    first[u] = in_chain ? first[pre[u]] : u;
    L[u] = id[u] = ++cnt;
    node[cnt] = u;
    if(~succ[u]) {
        make_chain(succ[u], true);
    }
    for(int i = head[u]; ~i; i = nxt[i]) {
        if(succ[u] == pnt[i] ¦¦ pre[u] == pnt[i]) {
            continue;
        }
        make_chain(pnt[i]);
    }
    R[u] = cnt;
    //printf("L[%d]=%d R[%d]=%d\n", u, L[u], u, R[u]);
}
 
set<int> ord;
int v[N];
int ret;
void del(int l, int r)
{
    set<int>::iterator it1 ,it2, it;
    it1 = ord.lower_bound(l);
    it2 = ord.upper_bound(r);
    vector<set<int>::iterator > vec;
    for(it = it1; it != it2; it++) {
        int u = node[*it];
        ret += v[u];
        vec.push_back(it);
    }
    for(auto it : vec) {
        ord.erase(it);
    }
}
void Walk(int u, int v)
{
    while(first[u] != first[v]) {
        if(dep[first[u]] < dep[first[v]]) {
            swap(u, v);
        }
        del(id[first[u]], id[u]);
        u = pre[first[u]];
    }
    if(dep[u] > dep[v]) swap(u, v);
    del(id[u], id[v]);
}
void steal(int a)
{
    set<int> ::iterator it = ord.lower_bound(L[a]);
    if(*it != L[a]) {
        ret -= v[a];
        ord.insert(L[a]);
    }
}
void control(int u)
{
    del(L[u], R[u]);
}
int main()
{
    int t, n, a, b;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        E = 0;
        fill(head, head + n, -1);
        ret = 0;
        for(int i = 0; i < n; i++) {
            scanf("%d", &v[i]);
        }
        for(int i = 0; i < n - 1; i++) {
            scanf("%d%d", &a, &b);
            a--;b--;
            add_edge(a, b);
            add_edge(b, a);
        }
        dfn = 0; cnt = 0;
        dfs(0), make_chain(0);
        ord.clear();
        for(int i = 0; i < n; i++) {
            ord.insert(L[i]);
        }
        int q, op;
        scanf("%d", &q);
        while(q--) {
            scanf("%d", &op);
            if(op == 1) {
                scanf("%d%d", &a, &b);
                a--;b--;
                Walk(a, b);
            } else if(op == 2) {
                scanf("%d", &a);
                a--;
                steal(a);
            } else {
                scanf("%d", &a);
                a--;
                control(a);
            }
            printf("%d\n", ret);
        }
    }
    return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注