[BOJ] 5916 농장 관리

농장 관리

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

#define MAX_N 100001
#define ll long long
using namespace std;
int n, m, no = 0;

ll in[MAX_N], out[MAX_N];
ll level[MAX_N];
ll sz[MAX_N], p[MAX_N], heavy[MAX_N];
bool vis[MAX_N];
ll seg[MAX_N * 4], lazy[MAX_N * 4];
vector<int> adj[MAX_N], adj2[MAX_N];

void dfs(int u) {
vis[u] = true;
for (auto &v: adj[u]) {
if (!vis[v]) {
adj2[u].push_back(v);
dfs(v);
}
}
}

ll dfs1(int u, int lvl) {
sz[u] = 1;
level[u] = lvl;
for (auto &v : adj2[u]) {
sz[u] += dfs1(v, lvl + 1);
p[v] = u;
if (sz[v] > sz[adj2[u][0]])
swap(v, adj2[u][0]);
}
return sz[u];
}

void dfs2(int u) {
in[u] = ++no;
for (auto &v : adj2[u]) {
heavy[v] = v == adj2[u][0] ? heavy[u] : v;
dfs2(v);
}
out[u] = no;
}

void hld() {
dfs(1);
dfs1(1, 1);
dfs2(1);
}

void lazyUpdate(int u, int s, int e) {
if (!lazy[u]) return;
seg[u] += (e - s + 1) * lazy[u];
if (s != e) {
lazy[u * 2] += lazy[u];
lazy[u * 2 + 1] += lazy[u];
}
lazy[u] = 0;
}

void segUpdate(int node, int s, int e, int l, int r, ll v){
lazyUpdate(node, s, e);
if(r < s || e < l) return;
if(l <= s && e <= r){
seg[node] += (e-s+1) * v;
if(s ^ e){
lazy[node*2] += v;
lazy[node*2+1] += v;
}
return;
}
int m = s + e >> 1;
segUpdate(node*2, s, m, l, r, v);
segUpdate(node*2+1, m+1, e, l, r, v);
seg[node] = seg[node << 1] + seg[node << 1 | 1];
}

ll segQuery(int u, int s, int e, int l, int r) {
lazyUpdate(u, s, e);
if (r < s or e < l) return 0;
if (l <= s and e <= r) return seg[u];
int m = s + (e - s) / 2;
return segQuery(u * 2, s, m, l, r) + segQuery(u * 2 + 1, m + 1, e, l, r);
}

void update(int u, int v, ll w) {
while (heavy[u] != heavy[v]) {
if (level[heavy[u]] < level[heavy[v]]) swap(u, v);
int hvy = heavy[u];
segUpdate(1, 1, n, in[hvy], in[u], w);
u = p[hvy];
}
if (in[u] > in[v]) swap(u, v);
segUpdate(1, 1, n, in[u] + 1, in[v], w);
}

ll query(int u, int v) {
ll res = 0;
while (heavy[u] != heavy[v]) {
if (level[heavy[u]] < level[heavy[v]]) swap(u, v);
int hvy = heavy[u];
res += segQuery(1, 1, n, in[hvy], in[u]);
u = p[hvy];
}
if (in[u] > in[v]) swap(u, v);
res += segQuery(1, 1, n, in[u] + 1, in[v]);
return res;
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int u, v;
char op;
cin>>n>>m;
for (int i = 1; i < n; i++) {
cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}

hld();
while (m--) {
cin>>op>>u>>v;
if (op == 'P') {
update(u, v, 1);
} else {
printf("%lld\n", query(u, v));
}
}
return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/20/PS/BOJ/5916/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.