题目链接
就是两个问题,改变树上的一个点的值,以及查询一段链上的最大值问题。
我们直接拆点,并且构建LCT树,然后直接查询即可,模版题。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef long long ll;
const int maxN = 2e4 + 7;
int N, fa[maxN], c[maxN][2], st[maxN], r[maxN];
ll v[maxN], s[maxN];
inline bool isroot(int x) { return c[fa[x]][0] != x && c[fa[x]][1] != x; }
inline void pushup(int x) { s[x] = max(v[x], max(s[c[x][0]], s[c[x][1]])); }
inline void pushr(int x) { swap(c[x][0], c[x][1]); r[x] ^= 1; }
inline void pushdown(int x)
{
if(r[x])
{
if(c[x][0]) pushr(c[x][0]);
if(c[x][1]) pushr(c[x][1]);
r[x] = 0;
}
}
inline void Rotate(int x)
{
int y = fa[x], z = fa[y], k = c[y][1] == x;
if(!isroot(y)) c[z][c[z][1] == y] = x;
fa[x] = z;
c[y][k] = c[x][k^1];
fa[c[x][k^1]] = y;
c[x][k^1] = y;
fa[y] = x;
pushup(y); pushup(x);
}
inline void splay(int x)
{
int y = x, z = 0;
st[++z] = y;
while(!isroot(y)) st[++z] = y = fa[y];
while(z) pushdown(st[z--]);
while(!isroot(x))
{
y = fa[x]; z = fa[y];
if(!isroot(y)) (c[z][0] == y) ^ (c[y][0] == x) ? Rotate(x) : Rotate(y);
Rotate(x);
}
}
inline void access(int x)
{
int y = 0;
while(x)
{
splay(x); c[x][1] = y;
pushup(x);
y = x; x = fa[x];
}
}
inline void makeroot(int x)
{
access(x); splay(x);
pushr(x);
}
int findroot(int x)
{
access(x); splay(x);
while(c[x][0]) { pushdown(x); x = c[x][0]; }
splay(x);
return x;
}
inline void split(int x, int y)
{
makeroot(x);
access(y); splay(y);
}
inline void link(int x, int y)
{
makeroot(x);
if(findroot(y) != x) fa[x] = y;
}
inline void init()
{
memset(fa, 0, sizeof(fa));
memset(r, 0, sizeof(r));
memset(c, 0, sizeof(c));
memset(v, 0, sizeof(v));
memset(s, 0, sizeof(s));
}
char op[10];
int main()
{
int T; scanf("%d", &T);
while(T--)
{
scanf("%d", &N);
init();
for(int i=1, x, y, w; i<N; i++)
{
scanf("%d%d%d", &x, &y, &w);
v[N + i] = w;
link(x, N + i);
link(N + i, y);
}
int x, y;
while(scanf("%s", op) && op[0] != 'D')
{
scanf("%d%d", &x, &y);
if(op[0] == 'C')
{
splay(x + N);
v[x + N] = y;
}
else
{
split(x, y);
printf("%lld\n", s[y]);
}
}
}
return 0;
}