求助
查看原帖
求助
752318
Liuyc07楼主2023/9/23 10:25

样例已过,在200多行后wa调,求大佬帮助 qwq

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); i ++)
#define rop(i,a,b) for(int i = (a); i < (b); i ++)
#define per(i,a,b) for(int i = (a); i >= (b); i --)
#define por(i,a,b) for(int i = (a); i > (b); i --)
#define ls (x << 1)
#define rs ((x << 1) | 1)
#define mid ((tr[x].l + tr[x].r) >> 1)

using namespace std;

const int N = 1e6 + 10;

int n,idx,h[N];
int nv[N],v[N];
int fa[N],sz[N],son[N],dep[N];
int top[N],id[N],dfn;

struct node{
	int nx,fr,to,w;
}e[N << 1];

struct sgt{
	int l,r;
	int maxn,add,cov;
}tr[N << 2];

void add(int a,int b,int c){
	e[++ idx].nx = h[a]; e[idx].to = b; e[idx].w = c; e[idx].fr = a; h[a] = idx;
}

void dfs(int x,int f){
	fa[x] = f; dep[x] = dep[f] + 1; sz[x] = 1;
	for(int i = h[x]; i ; i = e[i].nx){
		int y = e[i].to;
		if(y == f) continue;
		v[y] = e[i].w;
		dfs(y,x);
		sz[x] += sz[y];
		if(sz[y] > sz[son[x]]) son[x] = y;
	}
}

void dfs2(int x,int t){
	top[x] = t;
	id[x] = ++ dfn;
	nv[dfn] = v[x];
	if(son[x]) dfs2(son[x],t);
	for(int i = h[x]; i ; i = e[i].nx){
		int y = e[i].to;
		if(!id[y]) dfs2(y,y);
	}
}

void pushup(int x){
	tr[x].maxn = max(tr[ls].maxn,tr[rs].maxn);
}

void pushdown(int x){
	if(~tr[x].cov){
		tr[ls].cov = tr[rs].cov = tr[x].cov;
		tr[ls].maxn = tr[rs].maxn = tr[x].cov;
		tr[ls].add = tr[rs].add = 0;
		tr[x].cov = -1;
	}
	if(tr[x].add) {
		tr[ls].add += tr[x].add; tr[rs].add += tr[x].add;
		tr[ls].maxn += tr[x].add; tr[rs].maxn += tr[x].add;
		tr[x].add = 0;
	}
}

void build(int x,int l,int r){
	tr[x].l = l, tr[x].r = r, tr[x].cov = -1;
	if(l == r){
		tr[x].maxn = nv[l];
		return;
	}
	build(ls,l,mid); build(rs,mid + 1,r);
	pushup(x);
}

void update(int x,int l,int r,int k){
	if(tr[x].l >= l and tr[x].r <= r){
		tr[x].maxn += k;
		tr[x].add += k;
		return;
	}
	pushdown(x);
	if(mid >= l) update(ls,l,r,k);
	if(mid < r)  update(rs,l,r,k);
	pushup(x);
}

void modify(int x,int l,int r,int k){
	if(tr[x].l >= l and tr[x].r <= r){
		tr[x].maxn = k;
		tr[x].cov = k;
		tr[x].add = 0;
		return;
	}
	pushdown(x);
	if(mid >= l) modify(ls,l,r,k);
	if(mid < r)  modify(rs,l,r,k);
	pushup(x);
}

int query(int x,int l,int r){
	if(tr[x].l >= l and tr[x].r <= r) return tr[x].maxn;
	int ans = -1e9;
	pushdown(x);
	if(mid >= l) ans = max(ans, query(ls,l,r));
	if(mid < r)  ans = max(ans, query(rs,l,r));
	return ans;
}

void update_path(int x,int y,int k){
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		update(1,id[top[x]],id[x],k);
		x = fa[top[x]];
	}
	if(dep[x] < dep[y]) swap(x,y);
	update(1,id[y] + 1,id[x],k);
}

void modify_path(int x,int y,int k){
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		modify(1,id[top[x]],id[x],k);
		x = fa[top[x]];
	}
	if(dep[x] < dep[y]) swap(x,y);
	modify(1,id[y] + 1,id[x],k);
}

int query_path(int x,int y){
	int ans = -1e9;
	while(top[x] != top[y]){
		if(dep[top[x]] < dep[top[y]]) swap(x,y);
		ans = max(ans, query(1,id[top[x]],id[x])) ;
		x = fa[top[x]];
	}
	if(dep[x] < dep[y]) swap(x,y);
	ans = max(ans, query(1,id[y] + 1,id[x]));
	return ans;
}

void check(){
	puts("sgt:");
	rep(x,1,7){
		cout<<tr[x].l<<" "<<tr[x].r<<" "<<tr[x].maxn<<endl; 
	}
}

signed main(){
	scanf("%lld",&n);
	rop(i,1,n){
		int u,v,w;
		scanf("%lld%lld%lld",&u,&v,&w);
		add(u,v,w); add(v,u,w);
	}
	dfs(1,0);
	dfs2(1,1);
	build(1,1,n);
//	check();
	while(1){
		string opt;
		int x,y,k;
		cin>>opt;
		if(opt == "Stop") break;
		if(opt == "Cover"){
			scanf("%lld%lld%lld",&x,&y,&k);			
			modify_path(x,y,k);
//			check();
		}
		if(opt == "Change"){
			scanf("%lld%lld",&x,&k);
			int u = e[x].fr, v = e[x].to;
			if (v == fa[u]) swap(u, v);
			modify_path(u, v, k);
//			check();
		}
		if(opt == "Add"){
			scanf("%lld%lld%lld",&x,&y,&k);
			update_path(x,y,k);
//			check();
		}
		if(opt == "Max"){
			scanf("%lld%lld",&x,&y);
			int ans = query_path(x,y);
			printf("%lld\n",ans);
		}
	}
	return 0;
}
2023/9/23 10:25
加载中...