QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#571109#8518. Roars IIIxyz123RE 44ms98764kbC++238.4kb2024-09-17 20:33:032024-09-17 20:33:05

Judging History

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

  • [2024-09-17 20:33:05]
  • 评测
  • 测评结果:RE
  • 用时:44ms
  • 内存:98764kb
  • [2024-09-17 20:33:03]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
long long a,d[1000001],f[1000001],de[1000001],cnt,ann[1000001],cnn,sz[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;long long 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];
}
void get(int x,int ll,int rr,long long qq,int ww)
{
//	cout<<ll<<" "<<rr<<" "<<l[x].smm<<" "<<qq<<"\n";
	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*2);
	}
	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*2,de[qq],nww);
	}
	else
	{
		long long nw=0;
		get(rt[qq],1,a*2,f[qq]-d[qq],a);
		change2(rt[qq],1,a*2,Id);
		change(rt[qq],rt[qq],1,a*2,Id,nww);
		change(rt[qq],rt[qq],1,a*2,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*2,tt,St[Li].w+St[Li].e);
//		cout<<nww<<" "<<tt<<"\n";
		change2(St[Li].r,1,a*2,Id);
//		cout<<nww<<" "<<Id<<" "<<a<<" "<<L<<" "<<St[Li].w+St[Li].e-a<<"\n";
		change(St[Li].r,St[Li].r,1,a*2,Id,nww);
		int ff=St[Li].w+St[Li].e-Id;
		ff=max(ff,L-1);
		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*2,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*2,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*2,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*2,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=R-si[qu[tt][j]];k<=R;k++) ins(k,tmp[R-k]);
//				cout<<R*(sm[R]-sm[L-1])-(sm1[R]-sm1[L-1])<<" "<<L<<" "<<R<<" "<<sm[L]-sm[L-1]<<" "<<sm[L+1]-sm[L]<<" "<<sm[L+2]-sm[L+1]<<"\n";
				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]<<" "<<sm[L+3]-sm[L+2]<<" "<<l[St[Cn].r].smm<<" "<<Li<<" "<<Cn<<" "<<l[St[Cn].r].sm<<"\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()
{
//	freopen("1.in","r",stdin);
//	freopen("2.out","w",stdout);
	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: 0ms
memory: 24356kb

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: 20328kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

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

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

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

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

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

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: 26484kb

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: 2ms
memory: 26432kb

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: 26540kb

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: 0
Accepted
time: 0ms
memory: 24440kb

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 37 34 29 31 30 49 31 41 33 27 36 34 29 29 27 30 36 30 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 30 27 29 33 34 36 32 34 40 27 

result:

ok 64 numbers

Test #10:

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

input:

512
11100000100000011111111011010110001001101100011110001001111001011011000001010000100011011101001001101100010000011100000110101010100001001110000011010000111001000110010011000100000010010000100100000000100001001011100010100100101101010000111100110010001010010011101010001000111001001010111111111101...

output:

229 209 213 228 257 224 211 243 208 238 231 211 246 231 235 206 211 208 220 225 226 206 220 251 215 220 238 234 230 208 215 235 230 220 220 235 233 217 243 250 206 220 221 253 230 206 240 209 208 220 215 251 228 221 228 219 208 208 219 256 221 206 222 208 228 230 208 230 235 241 228 223 231 221 229 ...

result:

ok 512 numbers

Test #11:

score: 0
Accepted
time: 6ms
memory: 33184kb

input:

4096
0011111001111111001000100000001010100111001000010100110000110111011111111001001101101010011010001010111100111110001111000000000111001110100110100011011101110011101111001010011011110101111110000101111111100101101001001000000110100111100010110110011001111111011111100111001101111110110111000010101...

output:

1579 1639 1528 1542 1551 1523 1522 1553 1553 1532 1533 1551 1562 1534 1564 1568 1583 1559 1553 1537 1533 1568 1541 1566 1563 1557 1546 1537 1551 1586 1570 1548 1576 1587 1523 1541 1539 1522 1538 1565 1554 1589 1548 1614 1570 1570 1549 1527 1578 1520 1569 1581 1540 1540 1551 1579 1594 1550 1551 1550 ...

result:

ok 4096 numbers

Test #12:

score: 0
Accepted
time: 44ms
memory: 98764kb

input:

32768
010000100111010011101111111011001000100100101001100000110010100111111101110011000110001011001001100110111010010111000001110101000011101010010101111101001100100011101101010000001011001010100011011011001001111101011000001101101110101010001001011011101001010101010000011111100011110100011000001110...

output:

12964 12950 12999 13070 12980 12974 12927 12949 12952 12953 12999 12962 12972 12972 12951 12989 12948 12971 12930 12954 13012 12972 12946 12985 12946 12926 12996 13037 12915 12915 13011 13017 12939 13055 13044 12986 12986 12984 12968 12958 12960 12973 12981 12976 12992 13011 12991 12930 13001 13018 ...

result:

ok 32768 numbers

Test #13:

score: -100
Runtime Error

input:

200000
10010111111100101000010111101000010111110101100011001101010001010011111001000101111110000010001000011111011011100111001110101011010011111001010100110100100100110110100101010111011010010110111000000100100010100010111001011111011010111010111001000101010111010011010000111011001110000100010110001...

output:


result: