思路:
同时存下最大值和最小值,每次该区间数据元素乘-1就把最大值变最小值 就ok了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define Fuck cout << "wtf?????\n"
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10010;
int t, n, tot, num;
int top[maxn], son[maxn], father[maxn], depth[maxn], head[maxn];
int p[maxn], fp[maxn], node_size[maxn], Treevalue[maxn];
struct tree
{
int maxs, mins;
int lazy;
}Tree[maxn << 2];
struct node
{
int a, b;
}input[maxn];
struct Edge
{
int v, w;
int next;
}edge[maxn << 1];
void add(int u, int v, int w)
{
edge[tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot++;
}
void Init()
{
tot = num = 0;
memset(head, -1, sizeof(head));
memset(son, -1, sizeof(son));
}
///树链剖分部分
void dfs(int u, int fa, int deep)
{
father[u] = fa;
depth[u] = deep;
node_size[u] = 1;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if(v != fa)
{
Treevalue[v] = edge[i].w;
dfs(v, u, deep + 1);
node_size[u] += node_size[v];
if(son[u] == -1 || node_size[v] > node_size[son[u]])
son[u] = v;
}
}
}
void getlist(int u, int tp)
{
top[u] = tp;
p[u] = num++;
fp[p[u]] = u;
if(son[u] != -1)
getlist(son[u], tp);
else
return ;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].v;
if(v != father[u] && v != son[u])
{
getlist(v, v);
}
}
}
///线段树部分
void push_up(int rt)
{
Tree[rt].maxs = max(Tree[rt << 1].maxs, Tree[rt << 1 | 1].maxs);
Tree[rt].mins = min(Tree[rt << 1].mins, Tree[rt << 1 | 1].mins);
}
void Build(int l, int r, int rt)
{
Tree[rt].lazy = 1;
if(l == r)
{
Tree[rt].maxs = Tree[rt].mins = Treevalue[fp[l]];
return ;
}
int m = (l + r) / 2;
Build(l, m, rt << 1);
Build(m + 1, r, rt << 1 | 1);
push_up(rt);
}
void push_down(int l, int r, int rt)
{
if(Tree[rt].lazy == 1)
return ;
Tree[rt << 1].maxs *= -1;
Tree[rt << 1].mins *= -1;
swap(Tree[rt << 1].maxs, Tree[rt << 1].mins);
Tree[rt << 1].lazy *= Tree[rt].lazy;
Tree[rt << 1 | 1].maxs *= -1;
Tree[rt << 1 | 1].mins *= -1;
swap(Tree[rt << 1 | 1].maxs, Tree[rt << 1 | 1].mins);
Tree[rt << 1 | 1].lazy *= Tree[rt].lazy;
Tree[rt].lazy = 1;
}
void update(int L, int R, int val, int l, int r, int rt, bool flag)
{
if(l >= L && r <= R)
{
if(flag)
{
Tree[rt].maxs = Tree[rt].mins = val;
Tree[rt].lazy = 1;
}
else
{
Tree[rt].lazy = val * Tree[rt].lazy;
Tree[rt].maxs *= -1;
Tree[rt].mins *= -1;
swap(Tree[rt].maxs, Tree[rt].mins);
}
return ;
}
int m = (l + r) >> 1;
push_down(l, r, rt);
if(m >= L)
update(L, R, val, l, m, rt << 1, flag);
if(m < R)
update(L, R, val, m + 1, r, rt << 1 | 1, flag);
push_up(rt);
}
int Query(int L, int R, int l, int r, int rt)
{
if(l >= L && r <= R)
return Tree[rt].maxs;
int m = (l + r) >> 1;
push_down(l, r, rt);
int ans = -INF;
if(m >= L)
ans = max(ans, Query(L, R, l, m, rt << 1));
if(m < R)
ans = max(ans, Query(L, R, m + 1, r, rt << 1 | 1));
return ans;
}
int getmax(int a, int b)
{
int ans = -INF;
int f1 = top[a], f2 = top[b];
while(f1 != f2)
{
if(depth[f1] < depth[f2])
{
swap(f1, f2);
swap(a, b);
}
ans = max(ans, Query(p[f1], p[a], 1, n - 1, 1));
a = father[f1];
f1 = top[a];
}
if(a == b)
return ans;
if(depth[a] > depth[b])
swap(a, b);
ans = max(ans, Query(p[son[a]], p[b], 1, n - 1, 1));
return ans;
}
void Rever(int a, int b)
{
int f1 = top[a], f2 = top[b];
while(f1 != f2)
{
if(depth[f1] < depth[f2])
{
swap(f1, f2);
swap(a, b);
}
update(p[f1], p[a], -1, 1, n - 1, 1, false);
a = father[f1];
f1 = top[a];
}
if(a == b)
return ;
if(depth[a] > depth[b])
swap(a, b);
update(p[son[a]], p[b], -1, 1, n - 1, 1, false);
}
int main()
{
//freopen("in.txt", "r", stdin);
cin >> t;
while(t--)
{
cin >> n;
Init();
int u, v, w;
for(int i = 1; i <= n - 1; ++ i)
{
scanf("%d%d%d", &u, &v, &w);
input[i].a = u;
input[i].b = v;
add(u, v, w);
add(v, u, w);
}
dfs(1, 0, 0);
getlist(1, 1);
Build(1, n - 1, 1);
char str[10];
while(~scanf("%s", str))
{
int a, b;
if(str[0] == 'D')
break;
if(str[0] == 'Q')
{
scanf("%d%d", &a, &b);
cout << getmax(a, b) << endl;
}
else if(str[0] == 'N')
{
scanf("%d%d", &a, &b);
Rever(a, b);
}
else if(str[0] == 'C')
{
scanf("%d%d", &a, &b);
int aa = input[a].a;
int bb = input[a].b;
if(depth[aa] < depth[bb])
swap(aa, bb);
update(p[aa], p[aa], b, 1, n - 1, 1, true);
}
}
}
return 0;
}