记录编号 498849 评测结果 AAAAAAAAAAA
题目名称 [河南省队2016]无根树 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 0.832 s
提交时间 2018-06-24 15:39:13 内存使用 19.38 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=200005;
struct binary_heap{
	priority_queue<int>a,b;
	binary_heap(){}
	inline void push(int x){a.push(x);}
	inline void erase(int x){b.push(x);}
	int top(){
		while(!b.empty()&&a.top()==b.top()){
			a.pop();
			b.pop();
		}
		return a.top();
	}
}heap;
struct node{
	int mxsm,lmx,rmx,sum;
	node *lc,*rc;
	node(){}
	node(int w):mxsm(max(w,0)),lmx(max(w,0)),rmx(max(w,0)),sum(w){}
	inline void refresh(){
		mxsm=max(lc->rmx+rc->lmx,max(lc->mxsm,rc->mxsm));
		lmx=max(lc->lmx,lc->sum+rc->lmx);
		rmx=max(rc->rmx,rc->sum+lc->rmx);
		sum=lc->sum+rc->sum;
	}
}nodes[maxn<<1],*root[maxn],*ptr=nodes;
void dfs1(int);
void dfs2(int);
void buildchain(int);
void modify(int,int);
void build(int,int,node*&);
void modify(int,int,node*);
vector<int>G[maxn];
int p[maxn],d[maxn],size[maxn],son[maxn],top[maxn],len[maxn],a[maxn];
int n,m,s,t,w[maxn],f[maxn];
int main(){
	freopen("nortree.in","r",stdin);
	freopen("nortree.out","w",stdout);
	//int __size__=128<<20;
	//char *__p__=(char*)malloc(__size__)+__size__;
	//__asm__("movl %0, %%esp\n"::"r"(__p__));
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)scanf("%d",&w[i]);
	for(int i=1,x,y;i<n;i++){
		scanf("%d%d",&x,&y);
		G[x].push_back(y);
		G[y].push_back(x);
	}
	dfs1(1);
	dfs2(1);
	while(m--){
		int tp;
		scanf("%d",&tp);
		if(tp==1){
			int x,v;
			scanf("%d%d",&x,&v);
			modify(x,v);
		}
		else printf("%d\n",heap.top());
	}
	return 0;
}
void dfs1(int x){
	size[x]=1;
	d[x]=d[p[x]]+1;
	for(int i=0;i<(int)G[x].size();i++)
		if(G[x][i]!=p[x]){
			p[G[x][i]]=x;
			dfs1(G[x][i]);
			size[x]+=size[G[x][i]];
			if(size[G[x][i]]>size[son[x]])son[x]=G[x][i];
		}
}
void dfs2(int x){
	top[x]=(x==son[p[x]]?top[p[x]]:x);
	f[x]=w[x];
	if(son[x])dfs2(son[x]);
	for(int i=0;i<(int)G[x].size();i++)
		if(G[x][i]!=p[x]&&G[x][i]!=son[x]){
			dfs2(G[x][i]);
			f[x]+=root[G[x][i]]->lmx;
		}
	if(top[x]==x)buildchain(x);
}
void buildchain(int x){
	for(int y=x;y;y=son[y])a[++len[x]]=y;
	build(1,len[x],root[x]);
	heap.push(root[x]->mxsm);
}
void modify(int x,int v){
	f[x]+=v-w[x];
	w[x]=v;
	while(x){
		if(p[top[x]])f[p[top[x]]]-=root[top[x]]->lmx;
		heap.erase(root[top[x]]->mxsm);
		t=f[x];
		s=d[x]-d[top[x]]+1;
		modify(1,len[top[x]],root[top[x]]);
		if(p[top[x]])f[p[top[x]]]+=root[top[x]]->lmx;
		heap.push(root[top[x]]->mxsm);
		x=p[top[x]];
	}
}
void build(int l,int r,node *&rt){
	rt=ptr++;
	if(l==r){
		*rt=node(f[a[l]]);
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,rt->lc);
	build(mid+1,r,rt->rc);
	rt->refresh();
}
void modify(int l,int r,node *rt){
	if(l==r){
		*rt=node(t);
		return;
	}
	int mid=(l+r)>>1;
	if(s<=mid)modify(l,mid,rt->lc);
	else modify(mid+1,r,rt->rc);
	rt->refresh();
}