过样例但是 WA 求条
查看原帖
过样例但是 WA 求条
826774
Little_Fox_Fairy楼主2024/9/27 17:58

给我写红温了,多半是查询写挂了吧。

#include<bits/stdc++.h>
#define max(a,b) (a>b?a:b)
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
const int N=2e5+10;
int n,m,val[N],seq[N];
int dep[N],fa[N],siz[N],son[N],top[N],idx[N],tim;
vector<int> e[N];
#define ls u<<1
#define rs u<<1|1
struct node 
{
	int l,r,lmax,rmax,maxx,sum,flag,tag;
	node () {l=0,r=0,lmax=0,rmax=0,maxx=0,sum=0,tag=0,flag=0;};
}t[N<<2];
inline void push_up(int u)
{
	t[u].sum=t[ls].sum+t[rs].sum;
	t[u].maxx=max(t[ls].rmax+t[rs].lmax,max(t[ls].maxx,t[rs].maxx));
	t[u].lmax=max(t[ls].lmax,t[ls].sum+t[rs].lmax);
	t[u].rmax=max(t[rs].rmax,t[rs].sum+t[ls].rmax);
	return ;
}
inline void push_down(int u)
{
	if (!t[u].flag) return ;
	t[ls].flag=t[rs].flag=1,t[u].flag=0;
	t[ls].tag=t[rs].tag=t[u].tag,t[u].tag=0;
	int assign=t[ls].tag;
	t[ls].lmax=t[ls].rmax=t[ls].maxx=max(0,(t[ls].r-t[ls].l+1)*assign);
	t[ls].sum=(t[ls].r-t[ls].l+1)*assign;
	t[rs].lmax=t[rs].rmax=t[rs].maxx=max(0,(t[rs].r-t[rs].l+1)*assign);
	t[rs].sum=(t[rs].r-t[rs].l+1)*assign;
	return ;
}
inline void build(int u,int l,int r)
{
	t[u].l=l,t[u].r=r;
	if (l==r) return t[u].maxx=t[u].lmax=t[u].rmax=max(0,seq[l]),t[u].sum=seq[l],t[u].flag=t[u].tag=0,void();
	int mid=l+r>>1;
	build(u<<1,l,mid),build(u<<1|1,mid+1,r);
	return push_up(u);
}
inline node query(int u,int l,int r)
{
	node L,R,res;
	if (t[u].l>=l and t[u].r<=r) return t[u];
	push_down(u);
	int mid=t[u].l+t[u].r>>1;
	if (r<=mid) return query(u<<1,l,r);
	if (l>mid) return query(u<<1|1,l,r);
	L=query(u<<1,l,r),R=query(u<<1|1,l,r);
	res.lmax=max(L.lmax,L.sum+R.lmax);
	res.rmax=max(R.rmax,R.sum+L.rmax);
	res.maxx=max(L.rmax+R.lmax,max(L.maxx,R.maxx));
	res.sum=L.sum+R.sum;
	return push_up(u),res;
}
inline void update(int u,int l,int r,int assign)
{
	if (t[u].l>=l and t[u].r<=r)
	{
		t[u].lmax=t[u].rmax=t[u].maxx=max(0,(t[u].r-t[u].l+1)*assign);
		t[u].sum=(t[u].r-t[u].l+1)*assign;
		t[u].tag=assign,t[u].flag=1;
		return ;
	}
	push_down(u);
	int mid=t[u].l+t[u].r>>1;
	if (l<=mid) update(u<<1,l,r,assign);
	if (r>mid) update(u<<1|1,l,r,assign);
	return push_up(u);
}
inline void dfs(int u,int father)
{
	dep[u]=dep[father]+1,fa[u]=father,siz[u]=1;
	for (auto v : e[u])
	{
		if (v==father) continue;
		dfs(v,u);siz[u]+=siz[v];
		if (siz[v]>siz[son[u]]) son[u]=v;
	}
	return ;
}
inline void df5(int u,int tp)
{
	idx[u]=++tim,top[u]=tp,seq[tim]=val[u];
	if (!son[u]) return ;
	df5(son[u],tp);
	for (auto v : e[u])
	{
		if (v==fa[u] or v==son[u]) continue;
		df5(v,v);
	}
	return ;
}
inline void modify(int u,int v,int assign)
{
	while (top[u]!=top[v])
	{
		if (dep[top[u]]<dep[top[v]]) swap(u,v);
		update(1,idx[top[u]],idx[u],assign);
		u=fa[top[u]];
	}
	if (dep[u]>dep[v]) swap(u,v);
	update(1,idx[u],idx[v],assign);
	return ;
}
inline node merge(node u,node v)
{
	node res;
	res.lmax=max(u.lmax,u.sum+v.lmax);
	res.rmax=max(v.rmax,v.sum+u.rmax);
	res.maxx=max(u.rmax+v.lmax,max(u.maxx,v.maxx));
	res.sum=u.sum+v.sum;
	return res;
}
inline int query(int u,int v)
{
	node L,R;
	if (idx[u]>idx[v]) swap(u,v);
	while (top[u]!=top[v])
	{
		if (dep[top[u]]<dep[top[v]])
		{
			node tmp=query(1,idx[top[v]],idx[v]);
			R=merge(tmp,R);
			v=fa[top[v]];
		}
		else
		{
			node tmp=query(1,idx[top[u]],idx[u]);
			L=merge(tmp,L);
			u=fa[top[u]];
		}
	}
	if (dep[u]>dep[v])
	{
		node tmp=query(1,idx[v],idx[u]);
		L=merge(tmp,L);
	}
	else 
	{
		node tmp=query(1,idx[u],idx[v]);
		L=merge(tmp,L);
	}
	swap(L.lmax,L.rmax);
	L=merge(L,R);
	return L.maxx;
}
signed main()
{
	cin>>n;
	for (int i=1;i<=n;i++) cin>>val[i];
	for (int i=1;i<=n-1;i++)
	{
		int u,v;cin>>u>>v;
		e[u].emplace_back(v);
		e[v].emplace_back(u);
	}
	dfs(1,0),df5(1,1);build(1,1,n);
	cin>>m;
//	modify(3,4,2);
//	cout<<query(1,1,n).maxx<<endl;
//	cout<<query(3,5)<<endl;
	while (m--)
	{
		int op,l,r,add;cin>>op>>l>>r;
		if (op==1) cout<<query(l,r)<<endl;
		else cin>>add,modify(l,r,add);
	}
	return (0-0);
}
2024/9/27 17:58
加载中...