QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#570997#8518. Roars IIIxyz123WA 6ms26368kbC++238.2kb2024-09-17 19:44:022024-09-17 19:44:02

Judging History

你现在查看的是最新测评结果

  • [2024-09-17 19:44:02]
  • 评测
  • 测评结果:WA
  • 用时:6ms
  • 内存:26368kb
  • [2024-09-17 19:44:02]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
long long a,d[1000001],f[1000001],de[1000001],dfn[1000001],ls[1000001],cnt,ann[1000001],cnn,sz[1000001],Sm[1000001];
int rt[1000001],q,w,si[1000001],son[1000001],fa[1000001];
inline int read(){int x=0,f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}while(c>='0'&&c<='9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}return x*f;}
vector<int> qu[1000001],qu1[1000001];
struct seg{long long sm,smm;int l,r;}l[10000001];
void dfs(int qq,int ww)
{
	sz[qq]=d[qq];
	de[qq]=de[ww]+1;si[qq]=1;
	int mxx=0;
	for(int i=0;i<qu[qq].size();i++)
	{
		if(qu[qq][i]==ww) continue;
		dfs(qu[qq][i],qq);sz[qq]+=sz[qu[qq][i]];
		si[qq]+=si[qu[qq][i]];
		if(si[qu[qq][i]]>mxx) mxx=si[qu[qq][i]],son[qq]=qu[qq][i];
	}
}
void dfs2(int qq,int ww)
{
	qu1[ww].push_back(qq);
	if(!son[qq]) return;
	dfs2(son[qq],ww);
	for(int i=0;i<qu[qq].size();i++)
	{
		if(de[qu[qq][i]]>de[qq]&&qu[qq][i]!=son[qq]) dfs2(qu[qq][i],qu[qq][i]);
	}
}
void pushup(int x)
{
	l[x].sm=l[l[x].l].sm+l[l[x].r].sm;
	l[x].smm=l[l[x].l].smm+l[l[x].r].smm;
}
void merge_(int &x,int x1,int x2,int ll,int rr)
{
	if(!x1||!x2)
	{
		x=x1|x2;return;
	}
	if(ll==rr)
	{
		x=++cnt;
		l[x]=l[x1];l[x].sm+=l[x2].sm;
		l[x].smm+=l[x2].smm;
		return;
	}
	int mid=((ll+rr)>>1);
	x=++cnt;
	merge_(l[x].l,l[x1].l,l[x2].l,ll,mid);
	merge_(l[x].r,l[x1].r,l[x2].r,mid+1,rr);
	pushup(x);
}
struct p{int w,e,r;}St[1000001];
long long sm[2000001],Cn,Li=1,sm1[2000001],cnnt,Id,nww;
int L,R;
struct pp{int op,q,sm,sm1;}t[10000001];
void change(int &x,int x1,int ll,int rr,int qq,long long ww)
{
	x=++cnt;
	l[x]=l[x1];
	if(ll==rr)
	{
		l[x].sm+=ww*ll;
		l[x].smm+=ww;
		return;
	}int mid=((ll+rr)>>1);
	if(mid>=qq) change(l[x].l,l[x1].l,ll,mid,qq,ww);
	else change(l[x].r,l[x1].r,mid+1,rr,qq,ww);
	pushup(x);
}
void change1(int &x,int ll,int rr,int qq)
{
	++cnt;l[cnt]=l[x];x=cnt;
	if(ll==rr)
	{
		if(ll<=qq) l[x]=l[0];
		return;
	}
	int mid=((ll+rr)>>1);
	if(mid+1<=qq) l[x].l=0,change1(l[x].r,mid+1,rr,qq);
	else change1(l[x].l,ll,mid,qq);pushup(x);
}
void change2(int &x,int ll,int rr,int qq)
{
	++cnt;l[cnt]=l[x];x=cnt;
	if(ll==rr)
	{
		if(ll>=qq) l[x]=l[0];
		return;
	}
	int mid=((ll+rr)>>1);
	if(mid>=qq) l[x].r=0,change2(l[x].l,ll,mid,qq);
	else change2(l[x].r,mid+1,rr,qq);pushup(x);
}
long long get1(int qq,int ww)
{
	qq=max(qq,L);
	ww=min(ww,R);
	if(qq>ww) return 0;
	return sm[ww]-sm[qq-1];
}
long long get2(int qq,int ww)
{
	qq=max(qq,L);
	ww=min(ww,R);
	if(qq>ww) return 0;
	return sm1[ww]-sm1[qq-1];
}
void get(int x,int ll,int rr,long long qq,int ww)
{
	if(ll==rr)
	{
//		cout<<L<<" "<<R<<" "<<ww-ll<<" "<<get1(L,ww-ll)<<" "<<l[x].smm<<"\n"; 
		nww=get1(L,ww-ll)+l[x].smm-qq;
		Id=ll;
		return;
	}
	int mid=((ll+rr)>>1);
	if(get1(L,ww-(mid+1))+l[l[x].r].smm<=qq) get(l[x].l,ll,mid,qq-l[l[x].r].smm,ww);
	else get(l[x].r,mid+1,rr,qq,ww);
}
void dfs1(int qq,int ww)
{
	for(int i=0;i<qu[qq].size();i++)
	{
		if(qu[qq][i]==ww) continue;
		dfs1(qu[qq][i],qq);
		merge_(rt[qq],rt[qq],rt[qu[qq][i]],1,a);
	}
	if(l[rt[qq]].smm+d[qq]<=f[qq])
	{
		nww=l[rt[qq]].smm+d[qq];
		rt[qq]=0;
		change(rt[qq],rt[qq],1,a,de[qq],nww);
	}
	else
	{
		long long nw=0;
		get(rt[qq],1,a,f[qq]-d[qq],a);
		change2(rt[qq],1,a,Id);
		change(rt[qq],rt[qq],1,a,Id,nww);
		change(rt[qq],rt[qq],1,a,de[qq],f[qq]);
	}
	cnnt=0;
//	cout<<rt[qq]<<" "<<l[rt[qq]].sm<<" "<<nw<<" "<<l[rt[qq]].cnt<<" "<<de[qq]<<"\n";
}
void ins(int qq,long long ww)
{
	t[++cnnt]=pp{1,qq,sm[qq],sm1[qq]};
	sm[qq]=sm[qq-1]+ww;
	sm1[qq]=sm1[qq-1]+ww*qq;
}
void Get(long long qq)
{
	t[++cnnt]=pp{3,L,0,0};
	t[++cnnt]=pp{4,Li,0,0};
	t[++cnnt]=pp{5,R,0,0};
	long long tt=f[qq]-d[qq];
	while(Li<=Cn)
	{
		long long gg=get1(L,St[Li].w)+l[St[Li].r].smm;
		if(gg<=tt)
		{
			L=max(L,St[Li].w+1);
			tt-=gg;
			++Li;
		}
		else break;
	}
	if(!tt)
	{
		ins(R,f[qq]);
		return;
	}
	if(Li>Cn)
	{
		if(get1(L,R-1)<=tt)
		{
			tt=get1(L,R);
			L=R;ins(R,tt);
		}
		else
		{
			long long ll=L,rr=R,nx=R+1;
			while(ll<=rr)
			{
				int mid=((ll+rr)>>1);
				if(get1(L,mid)<=tt) ll=mid+1;
				else rr=mid-1,nx=mid;
			}
			tt=get1(L,nx)-tt;L=nx;
			t[++cnnt]=pp{1,nx-1,sm[nx-1],sm1[nx-1]};
			sm[nx-1]=sm[nx]-tt;
			sm1[nx-1]=sm1[nx]-tt*nx;
			ins(R,f[qq]);
		}
	}
	else
	{
		t[++cnnt]=pp{2,Li,St[Li].r,0};
		Id=a;
		get(St[Li].r,1,a,tt,St[Li].w+St[Li].e);
//		cout<<nww<<" "<<tt<<"\n";
		change2(St[Li].r,1,a,Id);
		change(St[Li].r,St[Li].r,1,a,Id,nww);
		int ff=St[Li].w+St[Li].e-Id;
		L=ff+1;L=min(L,R);
		ins(R,f[qq]);
	}
}
long long tmp[1000001];
void Dfs(int x,int ll,int rr,int qq,int ww,int ee,int e1)
{
	if(ll==rr)
	{
//		cout<<ee-ll<<" "<<l[x].smm<<" "<<e1<<"\n";
		tmp[R-(ee-ll)]+=e1*l[x].smm;
		return;
	}int mid=((ll+rr)>>1);
	if(mid>=ww) Dfs(l[x].l,ll,mid,qq,ww,ee,e1);
	else if(mid<qq) Dfs(l[x].r,mid+1,rr,qq,ww,ee,e1);
	else Dfs(l[x].l,ll,mid,qq,ww,ee,e1),Dfs(l[x].r,mid+1,rr,qq,ww,ee,e1);
}
void back(int qq)
{
	while(cnnt>qq)
	{
		if(t[cnnt].op==1) sm[t[cnnt].q]=t[cnnt].sm,sm1[t[cnnt].q]=t[cnnt].sm1;
		else if(t[cnnt].op==2) St[t[cnnt].q].r=t[cnnt].sm;
		else if(t[cnnt].op==3) L=t[cnnt].q;
		else if(t[cnnt].op==4) Li=t[cnnt].q;
		else R=t[cnnt].q;
		--cnnt;
	}
}
void work(int qq)
{
	int lsst=cnnt;
	t[++cnnt]=pp{3,L,0,0};
	t[++cnnt]=pp{4,Li,0,0};
	t[++cnnt]=pp{5,R,0,0};
	for(int i=0;i<qu1[qq].size();i++)
	{
		int tt=qu1[qq][i];
		int gg=son[tt];
		++R;
		ins(R,d[tt]);
		for(int j=0;j<qu[tt].size();j++)
		{
			if(de[qu[tt][j]]>de[tt]&&qu[tt][j]!=son[tt])
			{
				for(int k=0;k<=si[qu[tt][j]];k++) tmp[k]=get1(R-k,R-k);
				Dfs(rt[qu[tt][j]],1,a,de[tt],de[tt]+si[qu[tt][j]],R+de[tt],1);
				long long mxx=0;
				for(int k=si[qu[tt][j]];k>=0;k--)
				{
					if(tmp[k]){mxx=k;break;}
				}
				if(R-mxx<L)
				{
					L=R-mxx;
					for(int k=L;k<=R;k++) ins(k,tmp[R-k]);
				}
				else
				{
					for(int k=R-mxx;k<=R;k++) ins(k,tmp[R-k]);
				}
			}
		}
//		cout<<L<<" "<<R<<" "<<sm[L]-sm[L-1]<<" "<<sm[L+1]-sm[L]<<" "<<sm[L+2]-sm[L+1]<<"\n";
		int lss=cnnt;
		for(int j=0;j<qu[tt].size();j++)
		{
			if(de[qu[tt][j]]>de[tt]&&qu[tt][j]!=son[tt])
			{
				t[++cnnt]=pp{3,L,0,0};
				t[++cnnt]=pp{4,Li,0,0};
				t[++cnnt]=pp{5,R,0,0};
				for(int k=0;k<=si[qu[tt][j]];k++) tmp[k]=get1(R-k,R-k);
				Dfs(rt[qu[tt][j]],1,a,de[tt],de[tt]+si[qu[tt][j]],R+de[tt],-1);
				St[++Cn]=p{R-si[qu[tt][j]]-1,de[tt]+si[qu[tt][j]]+1,rt[gg]};
//				cout<<tmp[1]<<" "<<tmp[2]<<"\n";
				Dfs(St[Cn].r,1,a,de[tt],de[tt]+si[qu[tt][j]],R+de[tt],1);
//				cout<<tmp[1]<<" "<<tmp[2]<<" "<<l[St[Cn].r].smm<<"\n";
				t[++cnnt]=pp{2,Cn,St[Cn].r,0};
				change1(St[Cn].r,1,a,de[tt]+si[qu[tt][j]]);
				long long mxx=0;
				for(int k=si[qu[tt][j]];k>=0;k--)
				{
					if(tmp[k]){mxx=k;break;}
				}
				if(R-mxx<L)
				{
					L=R-mxx;
					for(int k=L;k<=R;k++) ins(k,tmp[R-k]);
				}
				else
				{
					for(int k=R-mxx;k<=R;k++) ins(k,tmp[R-k]);
				}
				Get(tt);
				work(qu[tt][j]);
				--Cn;
				back(lss);
			}
		}
//		cout<<L<<" "<<R<<" "<<sm[L]-sm[L-1]<<" "<<sm[L+1]-sm[L]<<" "<<sm[L+2]-sm[L+1]<<"\n";
		St[++Cn]=p{R-1,de[tt]+1,rt[gg]};
		t[++cnnt]=pp{2,Cn,St[Cn].r,0};
		Get(tt);
//		cout<<tt<<" "<<L<<" "<<R<<" "<<sm[L]-sm[L-1]<<" "<<sm[L+1]-sm[L]<<" "<<sm[L+2]-sm[L+1]<<" "<<l[St[Cn].r].smm<<"\n";
		for(int i=Li;i<=Cn;i++) ann[tt]+=l[St[i].r].sm+(R-St[i].w-St[i].e)*l[St[i].r].smm;
		ann[tt]+=R*(sm[R]-sm[L-1])-(sm1[R]-sm1[L-1]);
		--Cn;
		back(lss);
		Get(tt);
	}
	back(lsst);
}
char s[1000001];
long long an1[1000001];
void dfs3(int qq,int ww)
{
	for(int i=0;i<qu[qq].size();i++)
	{
		if(qu[qq][i]!=ww)
		{
			an1[qu[qq][i]]=an1[qq]-sz[qu[qq][i]]+(sz[1]-sz[qu[qq][i]]);
			dfs3(qu[qq][i],qq);
		}
	}
}
int main()
{
	scanf("%lld",&a);
	scanf("%s",s+1);
	for(int i=1;i<=a;i++) f[i]=1,d[i]=s[i]-'0';
//	for(int i=1;i<=a;i++) f[i]=read();
//	for(int i=1;i<=a;i++) d[i]=read();
	for(int i=1;i<a;i++)
	{
		q=read(),w=read();
		qu[q].push_back(w),qu[w].push_back(q);
	}
	dfs(1,0);
	dfs1(1,0);
	dfs2(1,1);
	L=1000001;R=L-1;
	work(1);
	for(int i=2;i<=a;i++) an1[1]+=sz[i];
	dfs3(1,0);
	for(int i=1;i<=a;i++) printf("%lld ",an1[i]-ann[i]);
	return 0;
}
//5
//1 1 1 1 1
//1 0 1 0 1
//1 2
//2 3
//2 4
//4 5

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 6ms
memory: 26368kb

input:

5
10101
1 2
2 3
2 4
4 5

output:

2 2 2 3 3 

result:

ok 5 number(s): "2 2 2 3 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 20336kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 0ms
memory: 20364kb

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 0ms
memory: 24292kb

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

score: 0
Accepted
time: 2ms
memory: 24500kb

input:

3
100
2 3
2 1

output:

0 1 2 

result:

ok 3 number(s): "0 1 2"

Test #6:

score: 0
Accepted
time: 5ms
memory: 24320kb

input:

4
1100
4 1
4 3
4 2

output:

1 1 3 1 

result:

ok 4 number(s): "1 1 3 1"

Test #7:

score: 0
Accepted
time: 0ms
memory: 26368kb

input:

5
10100
4 5
1 3
2 1
3 5

output:

0 2 0 4 2 

result:

ok 5 number(s): "0 2 0 4 2"

Test #8:

score: 0
Accepted
time: 0ms
memory: 24460kb

input:

8
11001000
4 2
7 2
5 7
6 2
3 1
6 3
7 8

output:

5 3 5 5 4 4 4 6 

result:

ok 8 numbers

Test #9:

score: -100
Wrong Answer
time: 3ms
memory: 24552kb

input:

64
1110101001010110011100110000010100011011010001000111010110100101
57 60
58 63
7 43
64 9
19 8
62 17
4 40
41 18
34 56
46 14
41 36
57 26
46 58
16 32
59 1
8 17
17 13
34 29
55 10
43 5
34 8
28 36
6 10
37 21
11 48
2 8
56 55
3 8
56 61
53 52
49 51
20 30
52 39
57 22
9 49
18 16
9 27
50 52
38 40
59 43
2 18
31...

output:

34 29 29 33 34 32 34 29 31 26 49 31 41 33 27 36 34 29 29 27 30 36 26 27 34 36 38 46 29 27 38 36 50 27 33 36 30 33 30 27 36 40 34 34 31 33 38 40 36 30 36 30 37 36 27 27 29 33 34 36 32 34 40 27 

result:

wrong answer 6th numbers differ - expected: '37', found: '32'