提交记录 9627


用户 题目 状态 得分 用时 内存 语言 代码长度
lzoilxy noip18f. 【NOIP2018】保卫王国 Accepted 100 139.342 ms 25520 KB C++ 3.23 KB
提交时间 评测时间
2019-06-20 22:22:33 2020-08-01 01:41:41
#include<algorithm>
#include<cstdio>
#define mxn 131072
#define LL long long
using namespace std;
const LL inf=1e12;
int n,m,sl,fh,val[mxn];
int siz[mxn],son[mxn];
LL g[mxn][2];
int t,h[mxn];
struct Tre
{
	int to,nxt;
}e[mxn<<1];
struct Martix
{
	LL s[2][2];
	inline Martix() {s[0][0]=s[0][1]=s[1][0]=s[1][1]=inf;}
	inline LL* operator [](const int &x) {return s[x];}
};
inline Martix operator *(const Martix &a,const Martix &b)
{
	Martix c;
	c.s[0][0]=min(a.s[0][0]+b.s[0][0],a.s[0][1]+b.s[1][0]);
	c.s[0][1]=min(a.s[0][0]+b.s[0][1],a.s[0][1]+b.s[1][1]);
	c.s[1][0]=min(a.s[1][0]+b.s[0][0],a.s[1][1]+b.s[1][0]);
	c.s[1][1]=min(a.s[1][0]+b.s[0][1],a.s[1][1]+b.s[1][1]);
	return c;
}
struct Bst
{
	int rt,top,stk[mxn],vis[mxn],lsiz[mxn];
	Martix w[mxn],f[mxn];
	struct Tree
	{
		int l,r,fa;
		Tree() {fa=l=r=0;}
	}tr[mxn];
	inline Bst() {w[0][0][0]=w[0][1][1]=f[0][0][0]=f[0][1][1]=0;}
	inline void init() {for(int i=1;i<=n;++i) w[i][1][0]=g[i][0],w[i][0][0]=w[i][0][1]=g[i][1];}
	inline void upd(int u) {f[u]=f[tr[u].l]*w[u]*f[tr[u].r];}
	inline bool isrt(int u) {return (tr[tr[u].fa].l!=u)&&(tr[tr[u].fa].r!=u);}
	int build(int l,int r)
	{
		if(l>r) return 0;
		int tot=0;
		for(int i=l;i<=r;++i) tot+=lsiz[stk[i]];
		for(int i=l,x=lsiz[stk[i]];i<=r;++i,x+=lsiz[stk[i]])
			if(x*2>=tot)
			{
				x=stk[i];tr[x].l=build(l,i-1);tr[x].r=build(i+1,r);
				tr[tr[x].l].fa=x;tr[tr[x].r].fa=x;upd(x);return x;
			}
	}
	int build(int x)
	{
		for(int i=x;i;i=son[i]) vis[i]=1;
		for(int v,u=x;u;u=son[u])
			for(int i=h[u];i;i=e[i].nxt)
				if(!vis[v=e[i].to])
					tr[build(v)].fa=u;
		top=0;for(int i=x;i;i=son[i]) stk[++top]=i,lsiz[i]=siz[i]-siz[son[i]];
		return build(1,top);
	}
	inline void change(int x,LL v1,LL v2)
	{
		w[x][1][0]+=v1;
		w[x][0][0]=(w[x][0][1]+=v2);
		for(int v;x;x=v)
			if((v=tr[x].fa)&&isrt(x))
			{
				w[v][0][1]=(w[v][0][0]-=min(f[x][0][0],f[x][1][0]));
				w[v][1][0]-=f[x][0][0];upd(x);
				w[v][0][1]=(w[v][0][0]+=min(f[x][0][0],f[x][1][0]));
				w[v][1][0]+=f[x][0][0];
			}
			else upd(x);
	}
}T;
inline char gc()
{
	static char buf[1<<20],*S,*T;
	if(S==T){T=(S=buf)+fread(buf,1,1<<20,stdin);if(S==T)return EOF;}
	return *S++;
}
inline int rd()
{
	sl=0;fh=1;
	char ch=gc();
	while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=gc();}
	while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=gc();
	return sl*fh;
}
inline void add(int u,int v)
{
	e[++t]=(Tre){v,h[u]};h[u]=t;
	e[++t]=(Tre){u,h[v]};h[v]=t;
}
void dfs1(int u,int f)
{
	int v;siz[u]=1;g[u][1]=val[u];
	for(int i=h[u];i;i=e[i].nxt)
		if((v=e[i].to)!=f)
		{
			dfs1(v,u);siz[u]+=siz[v];
			if(siz[v]>siz[son[u]]) son[u]=v;
			g[u][1]+=min(g[v][0],g[v][1]);
			g[u][0]+=g[v][1];
		}
}
void dfs2(int u,int f)
{
	if(!son[u]) return ;
	g[u][1]-=min(g[son[u]][0],g[son[u]][1]);
	g[u][0]-=g[son[u]][1];
	int v;
	for(int i=h[u];i;i=e[i].nxt)
		if((v=e[i].to)!=f)
			dfs2(v,u);
}
int main()
{
	n=rd();m=rd();rd();int a,b,x,y;LL ans;
	for(int i=1;i<=n;++i) val[i]=rd();
	for(int i=1;i<n;++i) x=rd(),y=rd(),add(x,y);
	dfs1(1,0);dfs2(1,0);T.init();T.rt=T.build(1);
	while(m--)
	{
		a=rd();x=rd();b=rd();y=rd();
		T.change(a,inf*x,inf*(!x));
		T.change(b,inf*y,inf*(!y));
		ans=min(T.f[T.rt][0][0],T.f[T.rt][1][0]);
		if(ans<inf) printf("%lld\n",ans);
		else puts("-1");
		T.change(a,-inf*x,-inf*(!x));
		T.change(b,-inf*y,-inf*(!y));
	}
	return 0;
}

CompilationN/AN/ACompile OKScore: N/A

Testcase #11.264 ms9 MB + 564 KBAcceptedScore: 4

Testcase #21.269 ms9 MB + 564 KBAcceptedScore: 4

Testcase #31.27 ms9 MB + 564 KBAcceptedScore: 4

Testcase #41.264 ms9 MB + 564 KBAcceptedScore: 4

Testcase #51.333 ms9 MB + 572 KBAcceptedScore: 4

Testcase #61.321 ms9 MB + 572 KBAcceptedScore: 4

Testcase #71.369 ms9 MB + 564 KBAcceptedScore: 4

Testcase #82.985 ms9 MB + 900 KBAcceptedScore: 4

Testcase #92.981 ms9 MB + 900 KBAcceptedScore: 4

Testcase #103.021 ms9 MB + 808 KBAcceptedScore: 4

Testcase #112.896 ms9 MB + 808 KBAcceptedScore: 4

Testcase #12112.805 ms24 MB + 944 KBAcceptedScore: 4

Testcase #13112.676 ms24 MB + 944 KBAcceptedScore: 4

Testcase #14120.245 ms24 MB + 748 KBAcceptedScore: 4

Testcase #15120.634 ms24 MB + 752 KBAcceptedScore: 4

Testcase #16120.605 ms24 MB + 752 KBAcceptedScore: 4

Testcase #17139.342 ms24 MB + 944 KBAcceptedScore: 4

Testcase #1862.365 ms16 MB + 936 KBAcceptedScore: 4

Testcase #1962.509 ms16 MB + 936 KBAcceptedScore: 4

Testcase #2086.959 ms20 MB + 500 KBAcceptedScore: 4

Testcase #2187.143 ms20 MB + 500 KBAcceptedScore: 4

Testcase #22111.116 ms20 MB + 304 KBAcceptedScore: 4

Testcase #23127.816 ms20 MB + 496 KBAcceptedScore: 4

Testcase #24128.007 ms20 MB + 500 KBAcceptedScore: 4

Testcase #25127.754 ms20 MB + 512 KBAcceptedScore: 4


Judge Duck Online | 评测鸭在线
Server Time: 2024-04-26 05:28:09 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用