离大谱,样例 AC,提交全 WA!
查看原帖
离大谱,样例 AC,提交全 WA!
681036
OldDriverTree楼主2023/4/29 00:01

rt,样例能过

#include<bits/stdc++.h>
#define For for (int i=head[u];~i;i=nxt[i])
#define pushdown(rt) T[rt<<1].change(),T[rt<<1|1].change()
#define v to[i]
#define w val[i]
#define mid (l+r>>1)
using namespace std;
const int inf=1e9,N=2e5+1,M=4e5;
int tot,head[N],nxt[M],to[M],val[M];
int n,m,a[N],fa[N],depth[N],size[N];
int cnt,id[N],son[N],top[N];

void add(int x,int y,int z)
{
	to[tot]=y,val[tot]=z;
	nxt[tot]=head[x];
	head[x]=tot++;
}
struct node
{
	int sum,a,b; bool tag;
	void init(int x) { sum=a=b=x; } 
	node() { sum=0,a=-inf,b=inf,tag=0; }
	void change() { sum=-sum,a=-a,b=-b; swap(a,b),tag^=1; }
	
	friend node operator +(node x,node y)
	{
		node z; z.sum=x.sum+y.sum;
		z.a=max(x.a,y.a),z.b=min(x.b,y.b);
		return z;
	}
}T[N<<2];
	
void update(int rt,int l,int r,int pos,int x)
{
	if (l==r) return T[rt].init(x);
	if (T[rt].tag) pushdown(rt),T[rt].tag=0;
	if (pos<=mid) update(rt<<1,l,mid,pos,x);
	else update(rt<<1|1,mid+1,r,pos,x);
	T[rt]=T[rt<<1]+T[rt<<1|1];
}
void modify(int rt,int l,int r,int L,int R)
{
	if (L<=l&&r<=R) return T[rt].change();
	if (T[rt].tag) pushdown(rt),T[rt].tag=0;
	if (L<=mid) modify(rt<<1,l,mid,L,R);
	if (mid<R) modify(rt<<1|1,mid+1,r,L,R);
	T[rt]=T[rt<<1]+T[rt<<1|1];
}
node query(int rt,int l,int r,int L,int R)
{
	if (L<=l&&r<=R) return T[rt];
	if (T[rt].tag) pushdown(rt),T[rt].tag=0;
	if (R<=mid) return query(rt<<1,l,mid,L,R);
	if (mid<L) return query(rt<<1|1,mid+1,r,L,R);
	return query(rt<<1,l,mid,L,R)+query(rt<<1|1,mid+1,r,L,R);
	T[rt]=T[rt<<1]+T[rt<<1|1];
}
void dfs1(int u,int f)
{
	fa[u]=f,size[u]=1;
	depth[u]=depth[f]+1;
	For if (v^f) {
		dfs1(v,u); size[u]+=size[v],a[v]=w;
		if (size[v]>size[son[u] ]) son[u]=v;
	}
}
void dfs2(int u,int topf)
{
	top[u]=topf,id[u]=++cnt;
	update(1,1,n,cnt,a[u]);
	if (son[u]) dfs2(son[u],topf);
	For if (!top[v]) dfs2(v,v);
}
void Change(int x,int y)
{
	while (top[x]^top[y]) {
		if (depth[top[x] ]<depth[top[y] ]) swap(x,y);
		modify(1,1,n,id[top[x] ],id[x]),x=fa[top[x] ];
	}if (depth[x]>depth[y]) swap(x,y); modify(1,1,n,id[x],id[y]);
}
node Query(int x,int y)
{
	node ans; while (top[x]^top[y]) {
		if (depth[top[x] ]<depth[top[y] ]) swap(x,y);
		ans=ans+query(1,1,n,id[top[x] ],id[x]),x=fa[top[x] ];
	}if (depth[x]>depth[y]) swap(x,y); return ans+query(1,1,n,id[x],id[y]);
}
int main()
{
	ios::sync_with_stdio(false);
	cin>>n; memset(head,-1,sizeof head);
	for (int i=1,x,y,z;i<n;i++) {
		cin>>x>>y>>z; x++,y++;
		add(x,y,z),add(y,x,z);
	}dfs1(1,0),dfs2(1,1); cin>>m;
	string op; int x,y; while (m--) {
		cin>>op>>x>>y; if (op=="C") {
			int tx=to[x*2-2],ty=to[x*2-1];
			if (depth[tx]<depth[ty]) swap(tx,ty);
			update(1,1,n,id[tx],y);
		}else x++,y++;
		
		if (op=="N") Change(x,y);
		if (op=="SUM") cout<<Query(x,y).sum<<endl;
		if (op=="MAX") cout<<Query(x,y).a<<endl;
		if (op=="MIN") cout<<Query(x,y).b<<endl;
	}
	return 0;
}
2023/4/29 00:01
加载中...