现在就是后悔,非常后悔
本来想随便拿个树剖热身,不料开了个毒瘤题。
题意:动态维护一棵树上的链最大值,最小值,和,修改的操作是一条链全部取反以及一条边单点修改
边转点是肯定要边转点的,把所有的边化成两端深度更深的那个点的权值,然后直接上树剖化成线段树
线段树就是很常规的最大小值还有和,单点修改就直接改,区间修改就打lazy标记,对于一个区间的取反操作,和直接取反,最大小值互换之后取反。
问题在于对树链u,v操作的时候,lca(u,v)这个点是不可以算进去的,因为边转点的特性这个点不属于这条树链,所以考虑对u到lca和v到lca这两条树链分别操作,这样lca这个点就是需要查询(修改)的序列的端点,在操作的时候手动 + 1把这个点忽略掉就可以。
整体感觉不难,唯一的毒瘤点在于写起来很麻烦
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
struct Edge{
int to,next,dis,id;
}edge[maxn * ];
int head[maxn],tot;
void init(){
for(int i = ; i <= N + ; i ++) head[i] = -;
tot = ;
}
void add(int u,int v,int w,int id){
edge[tot].to = v;
edge[tot].id = id;
edge[tot].next = head[u];
edge[tot].dis = w;
head[u] = tot++;
}
int nw[maxn];
int Index[maxn];
int dep[maxn],top[maxn],fa[maxn],val[maxn];
int pos[maxn],size[maxn],son[maxn];
const int SP = ;
int pa[maxn][SP];
void dfs1(int t,int la){
size[t] = ; son[t] = t;
pa[t][] = la;
for(int i = ; i < SP; i ++) pa[t][i] = pa[pa[t][i - ]][i - ];
int heavy = ;
for(int i = head[t]; ~i ; i = edge[i].next){
int v = edge[i].to;
if(v == la) continue;
dep[v] = dep[t] + ;
fa[v] = t; val[v] = edge[i].dis;
Index[edge[i].id] = v;
dfs1(v,t);
if(size[v] > heavy){
heavy = size[v];
son[t] = v;
}
size[t] += size[v];
}
}
int lca(int u,int v){
if(dep[u] < dep[v]) swap(u,v);
int t = dep[u] - dep[v];
for(int i = ; i < SP; i ++){
if(t & ( << i)) u = pa[u][i];
}
for(int i = SP - ; i >= ; i --){
int uu = pa[u][i],vv = pa[v][i];
if(uu != vv){
u = uu;
v = vv;
}
}
return u == v?u : pa[u][];
}
int cnt;
void dfs2(int t,int la){
top[t] = la;
pos[t] = ++cnt;
nw[cnt] = val[t];
if(son[t] == t) return;
dfs2(son[t],la);
for(int i = head[t]; ~i ; i = edge[i].next){
int v = edge[i].to;
if((fa[t] == v) || (v == son[t])) continue;
dfs2(v,v);
}
}
struct Tree{
int l,r;
int Min,Sum,Max;
int lazy;
}tree[maxn << ];
void Pushup(int t){
tree[t].Min = min(tree[t << ].Min,tree[t << | ].Min);
tree[t].Max = max(tree[t << ].Max,tree[t << | ].Max);
tree[t].Sum = tree[t << ].Sum + tree[t << | ].Sum;
}
void Build(int t,int l,int r){
tree[t].l = l; tree[t].r = r;
tree[t].lazy = ;
if(l == r){
tree[t].Min = tree[t].Sum = tree[t].Max = nw[l];
return;
}
int m = (l + r) >> ;
Build(t << ,l,m); Build(t << | ,m + ,r);
Pushup(t);
}
void change(int t){
tree[t].Sum = -tree[t].Sum;
swap(tree[t].Max,tree[t].Min);
tree[t].Max = -tree[t].Max;
tree[t].Min = -tree[t].Min;
tree[t].lazy ^= ;
}
void Pushdown(int t){
if(tree[t].lazy){
change(t << );
change(t << | );
tree[t].lazy = ;
}
}
void update1(int t,int p,int x){
if(tree[t].l == tree[t].r){
tree[t].Sum = tree[t].Max = tree[t].Min = x;
return;
}
Pushdown(t);
int m = (tree[t].l + tree[t].r) >> ;
if(p <= m) update1(t << ,p,x);
else update1(t << | ,p,x);
Pushup(t);
}
void update2(int t,int l,int r){
if(l <= tree[t].l && tree[t].r <= r){
change(t);
return;
}
Pushdown(t);
int m = (tree[t].l + tree[t].r) >> ;
if(r <= m) update2(t << ,l,r);
else if(l > m) update2(t << | ,l,r);
else{
update2(t << ,l,m);
update2(t << | ,m + ,r);
}
Pushup(t);
}
int query(int t,int l,int r,int p){
if(l <= tree[t].l && tree[t].r <= r){
if(p == ) return tree[t].Sum;
else if(p == ) return tree[t].Max;
return tree[t].Min;
}
Pushdown(t);
int m = (tree[t].l + tree[t].r) >> ;
if(r <= m) return query(t << ,l,r,p);
else if(l > m) return query(t << | ,l,r,p);
else{
if(p == ) return query(t << ,l,m,p) + query(t << | ,m + ,r,p);
else if(p == ) return max(query(t << ,l,m,p),query(t << | ,m + ,r,p));
else return min(query(t << ,l,m,p),query(t << | ,m + ,r,p));
}
}
void update(int u,int v){
while(top[u] != top[v]){
update2(,pos[top[u]],pos[u]);
u = fa[top[u]];
}
if(u != v) update2(,pos[v] + ,pos[u]);
}
int query(int u,int v,int p){
int ans = ;
if(p == ) ans = -INF;
else if(p == ) ans = INF;
while(top[u] != top[v]){
int t = query(,pos[top[u]],pos[u],p);
if(p == ) ans += t;
else if(p == ) ans = max(ans,t);
else ans = min(ans,t);
u = fa[top[u]];
}
if(u != v){
int t = query(,pos[v] + ,pos[u],p);
if(p == ) ans += t;
else if(p == ) ans = max(ans,t);
else ans = min(ans,t);
}
return ans;
}
int main(){
Sca(N); init();
for(int i = ; i <= N - ; i ++){
int u,v,w; Sca3(u,v,w);
u++;v++;
add(u,v,w,i); add(v,u,w,i);
}
int root = ; dfs1(root,);
cnt = ;
dfs2(root,root);
Build(,,N);
Sca(M);
while(M--){
char op[]; int u,v;
scanf("%s%d%d",op,&u,&v);
u++;v++; int l;
if(op[] == 'C') v--,u--;
else l = lca(u,v);
if(op[] == 'C'){
update1(,pos[Index[u]],v);
}else if(op[] == 'N'){
update(u,l); update(v,l);
}else if(op[] == 'S'){
Pri(query(u,l,) + query(v,l,));
}else if(op[] == 'A'){
Pri(max(query(u,l,),query(v,l,)));
}else{
Pri(min(query(u,l,),query(v,l,)));
}
}
return ;
}