BZOJ 4538: [Hnoi2016]网络

Description

一个简单的网络系统可以被描述成一棵无根树。每个节点为一个服务器。连接服务器与服务器的数据线则看做一条树边。两个服务器进行数据的交互时,数据会经过连接这两个服务器的路径上的所有服务器(包括这两个服务器自身)。

由于这条路径是唯一的,当路径上的某个服务器出现故障,无法正常运行时,数据便无法交互。此外,每个数据交互请求都有一个重要度,越重要的请求显然需要得到越高的优先处理权。

现在,你作为一个网络系统的管理员,要监控整个系统的运行状态。系统的运行也是很简单的,在每一个时刻,只有可能出现下列三种事件中的一种:

  1. 在某两个服务器之间出现一条新的数据交互请求;

  2. 某个数据交互结束请求;

  3. 某个服务器出现故障。

系统会在任何故障发生后立即修复。也就是在出现故障的时刻之后,这个服务器依然是正常的。但在服务器产生故障时依然会对需要经过该服务器的数据交互请求造成影响。

你的任务是在每次出现故障时,维护未被影响的请求中重要度的最大值。

注意,如果一个数据交互请求已经结束,则不将其纳入未被影响的请求范围。

Input

第一行两个正整数n,m,分别描述服务器和事件个数。

服务器编号是从1开始的,因此n个服务器的编号依次是1,2,3,…,n。

接下来n-1行,每行两个正整数u,v,描述一条树边。u和v是服务器的编号。接下来m行,按发生时刻依次描述每一个事件;即第i行(i=1,2,3,…,m)描述时刻i发生的事件。

每行的第一个数type描述事件类型,共3种类型:

(1)若type=0,之后有三个正整数a,b,v,表示服务器a,b之间出现一条重要度为v的数据交互请求;

(2)若type=1,之后有一个正整数t,表示时刻t(也就是第t个发生的事件)出现的数据交互请求结束;

(3)若type=2,之后有一个正整数x,表示服务器x在这一时刻出现了故障。

对于每个type为2的事件,就是一次询问,即询问“当服务器x发生故障时,未被影响的请求中重要度的最大值是多少?”注意可能有某个服务器自身与自身进行数据交互的情况。

2 ≤ n ≤ 10^5, 1 ≤ m ≤ 2×10^5,其他的所有输入值不超过 10^9.

Output

对于每个type=2的事件,即服务器出现故障的事件,输出一行一个整数,描述未被影响的请求中重要度的最大值。

如果此时没有任何请求,或者所有请求均被影响,则输出-1。

Sample Input

13 23

1 2

1 3

2 4

2 5

3 6

3 7

4 8

4 9

6 10

6 11

7 12

7 13

2 1

0 8 13 3

0 9 12 5

2 9

2 8

2 2

0 10 12 1

2 2

1 3

2 7

2 1

0 9 5 6

2 4

2 5

1 7

0 9 12 4

0 10 5 7

2 1

2 4

2 12

1 2

2 5

2 3

Sample Output

-1

3

5

-1

1

-1

1

1

3

6

7

7

4

6

HINT

样例给出的树如下所示:

解释其中的部分询问;下面的解释中用(a,b;t,v)表示在t时刻出现的服务器a和b之间的重要度为v的请求:

对于第一个询问(在时刻1),此时没有任何请求,输出-1。

对于第四个询问(在时刻6),此时有两条交互(8,13;2,3),(9,12;3,5),所有询问均经过2号服务器,输出-1。

对于第五个询问(在时刻8),此时有三条交互(8,13;2,3),(9,12;3,5),(10,12;7,1),只有交互(10,12;7,1)没有经过2号服务器,因此输出其重要度1。

对于最后一个询问(在时刻23),此时有三条交互(9,5;12,6),(9,12;16,4),(10,5;17,7)。当3号服务器出现故障时,只有交互(9,5;12,6)没有经过3号服务器,因此输出6。

2016.5.20新加数据一组,未重测

Solution

Heavy-Light Decomposition + Segment Tree + Heap

After h-l d, the tree became a interval. Then build a segment tree on it. For each interval on the tree, we can directly maintain the min-number using heap and do NOT push down it. For a path from u to v on a tree,  have O(log n) heavy chains, and each chain we can divide it into O(log n) intervals, and we have a heap on each interval. So the time complexity of the algorithm is O(n log^3 n).

Code

 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
#include <queue> 
#include <cstdio> 
#include <algorithm> 
using namespace std; 
const int N = 100005; 
int cnte, h[N], dep[N], fa[N], siz[N], son[N], tid[N], top[N], clo, ans, n, m, x, y, typ, tot; 
bool vis[N << 1];
typedef pair<int, bool*> pib; 
pib val; 
pair<int, int> inv[N];
priority_queue<pib> H[N << 2]; 
struct Edge { int to, next; } edge[N<<1]; 
int getint() { 
    int r = 0, k = 1; char c = getchar(); 
    for (; c < '0' || c > '9'; c = getchar()) if (c == '-') k = -1; 
    for (; c >= '0' && c <= '9'; c = getchar()) r = r * 10 + c - '0'; 
    return r * k; 
} 
void ins(const int x, const int y) { 
    edge[++cnte].to = y; 
    edge[cnte].next = h[x]; 
    h[x] = cnte; 
} 
int dfs1(int now, int father, int deep) { 
    fa[now] = father; dep[now] = deep; siz[now] = 1; 
    for (int i = h[now]; i; i = edge[i].next) { 
        if (edge[i].to == father) continue; 
        siz[now] += dfs1(edge[i].to, now, deep+1); 
        if (siz[son[now]] <= siz[edge[i].to]) 
            son[now] = edge[i].to; 
    } 
    return siz[now]; 
} 
void dfs2(int now, int father, int tp) { 
    top[now] = tp; tid[now] = ++clo; 
    if (son[now]) { 
        dfs2(son[now], now, tp); 
        for (int i = h[now]; i; i = edge[i].next) { 
            if (edge[i].to == father || edge[i].to == son[now]) continue; 
            dfs2(edge[i].to, now, edge[i].to); 
        } 
    } 
} 
void sgtUpdate(int now, int l, int r, const int ll, const int rr) { 
    while (!H[now].empty() && *H[now].top().second) H[now].pop();
    if (ll <= l && r <= rr) return H[now].push(val); 
    int mid = (l + r) >> 1; 
    if (ll <= mid) sgtUpdate(now << 1, l, mid, ll, rr); 
    if (rr > mid) sgtUpdate(now << 1 | 1, mid + 1, r, ll, rr); 
} 
void sgtQuery(int now, int l, int r, const int pos) { 
    while (!H[now].empty() && *H[now].top().second) H[now].pop(); 
    if (!H[now].empty()) ans = max(ans, H[now].top().first); 
    if (l == r) return; int mid = (l + r) >> 1; 
    if (pos <= mid) sgtQuery(now << 1, l, mid, pos); 
    else sgtQuery(now << 1 | 1, mid + 1, r, pos); 
} 
int main() { 
    n = getint(); m = getint(); 
    for (int i = 1; i < n; ++i) { 
        x = getint(); y = getint(); 
        ins(x, y); ins(y, x); 
    } 
    dfs1(1, 0, 1); 
    dfs2(1, 0, 1); 
    for (int i = 1; i <= m; ++i) { 
        typ = getint(); 
        if (typ == 0) { 
            x = getint(); y = getint(); 
            val = make_pair(getint(), &vis[i]); 
            tot = 0; 
            while (top[x] != top[y]) { 
                if (dep[top[x]] < dep[top[y]]) swap(x, y); 
                inv[++tot] = make_pair(tid[top[x]], tid[x]); 
                x = fa[top[x]]; 
            } 
            if (dep[x] < dep[y]) swap(x, y); 
            inv[++tot] = make_pair(tid[y], tid[x]); 
            sort(inv+1, inv+tot+1); 
            int lst = 1; 
            for (int i = 1; i <= tot; ++i) { 
                if (lst < inv[i].first) sgtUpdate(1, 1, n, lst, inv[i].first - 1); 
                lst = inv[i].second + 1; 
            } 
            sgtUpdate(1, 1, n, lst, n); 
        } else if (typ == 1) { 
            vis[getint()] = 1; 
        } else { 
            ans = -1; 
            sgtQuery(1, 1, n, tid[getint()]); 
            printf("%d\n", ans); 
        } 
    } 
}