POJ-3237 Tree
Tree
树链剖分
树的路径边权最大值询问,其中还能使一整条路径上的值取反(正负数)
线段树维护最大值和最小值就能做到取反的时候交换,再加多一个懒标记维护一下
码量很大,但是感觉挺重复的,线段树太久没写各种崩,中途还跑去聚餐,结果回来更是崩
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 10;
const ll inf = (1ll << 62) - 1;
#define pii pair<int, int>
int dep[maxn], siz[maxn], hson[maxn], fa[maxn];
int dfn[maxn], rnk[maxn], top[maxn];
int id[maxn], p[maxn];
ll w[maxn];
char op[20];
vector<pii>gra[maxn];
struct node
{
int l, r, lazy;
ll minn, maxx;
}tr[maxn << 2];
void build(int now, int l, int r)
{
tr[now].l = l;
tr[now].r = r;
tr[now].lazy = 0;
if(l == r)
{
tr[now].minn = tr[now].maxx = w[id[rnk[l]]];
return;
}
int mid = l + r >> 1;
build(now << 1, l, mid);
build(now << 1 | 1, mid + 1, r);
tr[now].maxx = max(tr[now << 1].maxx, tr[now << 1 | 1].maxx);
tr[now].minn = min(tr[now << 1].minn, tr[now << 1 | 1].minn);
}
inline void self(int now)
{
swap(tr[now].maxx, tr[now].minn);
tr[now].maxx = -tr[now].maxx;
tr[now].minn = -tr[now].minn;
}
inline void push_down(int now)
{
if(tr[now].lazy)
{
int lson = now << 1;
int rson = now << 1 | 1;
self(lson);
self(rson);
tr[lson].lazy ^= 1;
tr[rson].lazy ^= 1;
tr[now].lazy = 0;
}
}
inline void push_up(int now)
{
tr[now].minn = min(tr[now << 1].minn, tr[now << 1 | 1].minn);
tr[now].maxx = max(tr[now << 1].maxx, tr[now << 1 | 1].maxx);
}
void change(int now, int x, ll val)
{
if(tr[now].l == tr[now].r)
{
tr[now].minn = val;
tr[now].maxx = val;
return;
}
push_down(now);
int mid = tr[now].l + tr[now].r >> 1;
if(x <= mid)
change(now << 1, x, val);
else
change(now << 1 | 1, x, val);
push_up(now);
}
void update_n(int now, int L, int R)
{
if(L <= tr[now].l && tr[now].r <= R)
{
self(now);
tr[now].lazy ^= 1;
return;
}
push_down(now);
int mid = tr[now].l + tr[now].r >> 1;
if(L <= mid) update_n(now << 1, L, R);
if(R > mid) update_n(now << 1 | 1, L, R);
push_up(now);
}
ll query(int now, int L, int R)
{
if(L <= tr[now].l && tr[now].r <= R)
return tr[now].maxx;
push_down(now);
int mid = tr[now].l + tr[now].r >> 1;
ll ans = -inf;
if(L <= mid)
ans = max(ans, query(now << 1, L, R));
if(R > mid)
ans = max(ans, query(now << 1 | 1, L, R));
return ans;
}
void dfs1(int now, int pre, int d)
{
hson[now] = 0;
siz[now] = 1;
dep[now] = d;
fa[now] = pre;
for(int i=0; i<gra[now].size(); i++)
{
int nex = gra[now][i].first;
int x = gra[now][i].second;
if(nex == pre) continue;
id[nex] = x;
p[x] = nex;
dfs1(nex, now, d + 1);
siz[now] += siz[nex];
if(siz[hson[now]] < siz[nex])
hson[now] = nex;
}
}
int tp = 0;
void dfs2(int now, int t)
{
top[now] = t;
tp++;
dfn[now] = tp;
rnk[tp] = now;
if(hson[now])
{
dfs2(hson[now], t);
for(int i=0; i<gra[now].size(); i++)
{
int nex = gra[now][i].first;
if(nex == fa[now] || nex == hson[now]) continue;
dfs2(nex, nex);
}
}
}
void init(int n, int rt)
{
tp = 0;
dfs1(rt, rt, 1);
dfs2(rt, rt);
for(int i=0; i<=n; i++) gra[i].clear();
build(1, 1, n);
}
void LCA_n(int a, int b)
{
while(top[a] != top[b])
{
if(dep[top[a]] < dep[top[b]]) swap(a, b);
update_n(1, dfn[top[a]], dfn[a]);
a = fa[top[a]];
}
if(dfn[a] > dfn[b]) swap(a, b);
if(a != b) update_n(1, dfn[a] + 1, dfn[b]);
}
ll solve(int a, int b)
{
ll ans = -inf;
while(top[a] != top[b])
{
if(dep[top[a]] < dep[top[b]]) swap(a, b);
ans = max(ans, query(1, dfn[top[a]], dfn[a]));
a = fa[top[a]];
}
if(dfn[a] > dfn[b]) swap(a, b);
if(a != b) ans = max(ans, query(1, dfn[a] + 1, dfn[b]));
if(ans == -inf) ans = 0;
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n;
scanf("%d", &n);
for(int i=1; i<n; i++)
{
int x, y;
scanf("%d%d%lld", &x, &y, &w[i]);
gra[x].push_back(make_pair(y, i));
gra[y].push_back(make_pair(x, i));
}
init(n, 1);
while(1)
{
scanf("%s", op);
if(op[0] == 'D')
break;
else if(op[0] == 'C')
{
int i;
ll v;
scanf("%d%lld", &i, &v);
change(1, dfn[p[i]], v);
}
else if(op[0] == 'N')
{
int a, b;
scanf("%d%d", &a, &b);
LCA_n(a, b);
}
else
{
int a, b;
scanf("%d%d", &a, &b);
printf("%lld\n", solve(a, b));
}
}
}
return 0;
}