题目大意
给出一棵 n 个点的无根树,待边权,要求维护一下操作:
- 修改某条边的边权
- 修改点 u 到点 v 路径上所有边的边权
- 点 u 到点 v 路径上所有边的边权加上某个值
- 查询点 u 到点 v 路径上所有边的边权最大值
Solution
边权下放后 是 树链剖分 裸题,代码略长;
用线段树维护区间加、改、查操作,关于线段树多操作优先级的处理可以看这里。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 100007;
int n, a[maxn];
//edge-Table
int edgenum, head[maxn], nxt[maxn << 1], vet[maxn << 1], val[maxn << 1], id[maxn << 1], id_val[maxn];
inline void addedge(int u, int v, int cost, int ID){
++edgenum;
vet[edgenum] = v;
val[edgenum] = cost;
id[edgenum] = ID;
nxt[edgenum] = head[u];
head[u] = edgenum;
}
//Segment Tree
int Max[maxn << 2], changetag[maxn << 2], addtag[maxn << 2];
inline void PushUp(int rt){
Max[rt] = max(Max[rt<<1], Max[rt<<1|1]);
}
inline void PushDown(int rt, int ln, int rn){
if (changetag[rt] != -1){
Max[rt<<1] = changetag[rt]; Max[rt<<1|1] = changetag[rt];
changetag[rt << 1] = changetag[rt]; addtag[rt << 1] = 0;
changetag[rt << 1 | 1] = changetag[rt]; addtag[rt << 1 | 1] = 0;
changetag[rt] = -1;
}else if (addtag[rt]){
Max[rt<<1] += addtag[rt]; Max[rt<<1|1] += addtag[rt];
if (changetag[rt<<1] != -1) changetag[rt<<1] += addtag[rt];
else addtag[rt<<1] += addtag[rt];
if (changetag[rt<<1|1] != -1) changetag[rt<<1|1] += addtag[rt];
else addtag[rt<<1|1] += addtag[rt];
addtag[rt] = 0;
}
}
void Change(int rt, int l, int r, int L, int R, int C){
if (L <= l && r <= R){
Max[rt] = C;
changetag[rt] = C;
addtag[rt] = 0;
return;
}
int m = (l + r) >> 1;
PushDown(rt, m - l + 1, r - m);
if (L <= m) Change(rt<<1, l, m, L, R, C);
if (R > m) Change(rt<<1|1, m+1, r, L, R, C);
PushUp(rt);
}
void Add(int rt, int l, int r, int L, int R, int C){
if (L <= l && r <= R){
Max[rt] = Max[rt] + C;
if (changetag[rt] == -1) addtag[rt] += C;
else changetag[rt] += C;
return;
}
int m = (l + r) >> 1;
PushDown(rt, m - l + 1, r - m);
if (L <= m) Add(rt<<1, l, m, L, R, C);
if (R > m) Add(rt<<1|1, m+1, r, L, R, C);
PushUp(rt);
}
int Query(int rt, int l, int r, int L, int R){
if (L <= l && r <= R) return Max[rt];
int m = (l + r) >> 1, res = -1;
PushDown(rt, m - l + 1, r - m);
if (L <= m) res = max(res, Query(rt<<1, l, m, L, R));
if (R > m) res = max(res, Query(rt<<1|1, m+1, r, L, R));
return res;
}
//树剖
int size[maxn], tid[maxn], top[maxn], son[maxn], dep[maxn], stamp, dfspath[maxn], fa[maxn];
void dfs(int u, int D){
size[u] = 1; dep[u] = D; son[u] = 0;
for (int e = head[u]; e; e = nxt[e]){
int v = vet[e], cost = val[e], ID = id[e];
if (v == fa[u]) continue;
fa[v] = u; a[v] = cost; id_val[ID] = v;
dfs(v, D + 1);
size[u] += size[v];
if (size[v] > size[son[u]]) son[u] = v;
}
}
void Dfs(int u, int ance){
top[u] = ance; tid[u] = ++stamp; dfspath[stamp] = u;
if (son[u])Dfs(son[u], ance);
for (int e = head[u]; e; e = nxt[e]){
int v = vet[e];
if (v != fa[u] && v != son[u]){
Dfs(v, v);
}
}
}
int query(int u, int v){
int res = 0;
while (top[u] != top[v]){
if (dep[top[u]] < dep[top[v]]) swap(u, v);
res = max(res, Query(1, 1, n, tid[top[u]], tid[u]));
u = fa[top[u]];
}
if (dep[u] > dep[v]) swap(u, v);
if (tid[u] < tid[v]) res = max(res, Query(1, 1, n, tid[u] + 1, tid[v]));
return res;
}
void change(int u, int v, int val){
while (top[u] != top[v]){
if (dep[top[u]] < dep[top[v]]) swap(u, v);
Change(1, 1, n, tid[top[u]], tid[u], val);
u = fa[top[u]];
}
if (dep[u] > dep[v]) swap(u, v);
if (tid[u] < tid[v]) Change(1, 1, n, tid[u] + 1, tid[v], val);
}
void add(int u, int v, int val){
while (top[u] != top[v]){
if (dep[top[u]] < dep[top[v]]) swap(u, v);
Add(1, 1, n, tid[top[u]], tid[u], val);
u = fa[top[u]];
}
if (dep[u] > dep[v]) swap(u, v);
if (tid[u] < tid[v]) Add(1, 1, n, tid[u] + 1, tid[v], val);
}
inline int read(){
int f = 1, val = 0; char ch = getchar();
while ((ch < '0' || ch > '9') && (ch != '-')) ch = getchar();
if (ch == '-') f = -1, ch = getchar();
while (ch >= '0' && ch <= '9') val = (val << 3) + (val << 1) + ch - '0', ch = getchar();
return val * f;
}
void Build(int rt, int l, int r){
changetag[rt] = -1; addtag[rt] = 0;
if (l == r){
Max[rt] = a[dfspath[l]];
return;
}
int m = (l + r) >> 1;
Build(rt<<1, l, m);
Build(rt<<1|1, m+1, r);
PushUp(rt);
}
int main(){
n = read();
for (int i = 1; i < n; ++i){
int u = read(), v = read(), cost = read();
addedge(u, v, cost, i);
addedge(v, u, cost, i);
}
dfs(1, 0);
Dfs(1, 1);
Build(1, 1, n);
char order[20];
scanf("%s", order);
while (order[0] != 'S'){
if (order[0] == 'M'){
int u = read(), v = read();
printf("%d
", query(u, v));
}else if (order[0] == 'C' && order[1] == 'h'){
int k = read(), w = read();
Change(1, 1, n, tid[id_val[k]], tid[id_val[k]], w);
}else if (order[0] == 'C' && order[1] == 'o'){
int u = read(), v = read(), w = read();
change(u, v, w);
}else{
int u = read(), v = read(), w = read();
add(u, v, w);
}
scanf("%s", order);
}
return 0;
}