QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#447336 | #8518. Roars III | marher | WA | 913ms | 58576kb | C++14 | 3.6kb | 2024-06-18 10:05:10 | 2024-06-18 10:05:13 |
Judging History
answer
#pragma GCC optimize("Ofast","inline")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e6+50,inf=1e18;
struct edge
{
int u,v,nxt;
}e[N];
int n,head[N],cnt,dep[N],f[N],fa[N],dfn[N],cc,siz[N],p[N],pre[N],val;
char s[N];
void dfs(int x,int F)
{
fa[x]=F;dep[x]=dep[F]+1;
dfn[x]=++cc;p[cc]=x;
siz[x]=1;
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==F)continue;
dfs(v,x);siz[x]+=siz[v];
}
}
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((l+r)>>1)
#define LS ls,l,mid
#define RS rs,mid+1,r
#define mk(x,y) make_pair(x,y)
int la[N];
pair<int,int>mx[N];
void up(int x)
{
mx[x]=max(mx[ls],mx[rs]);
}
void build(int x,int l,int r)
{
if(l==r)
{
int u=p[l];
mx[x]=mk(s[u]?dep[u]:-inf,u);
return;
}
build(LS);build(RS);up(x);
}
void ad(int x,int d)
{
la[x]+=d;mx[x].first+=d;
}
void down(int x)
{
if(la[x]==0)return;
ad(ls,la[x]);ad(rs,la[x]);
la[x]=0;
}
pair<int,int>find(int x,int l,int r,int L,int R)
{
if(L>R)return mk(-inf,0);
if(L<=l&&R>=r)return mx[x];
down(x);
pair<int,int>ans;ans.first=-inf;
if(L<=mid)ans=max(ans,find(LS,L,R));
if(R>mid)ans=max(ans,find(RS,L,R));
return ans;
}
void sol(int x,int l,int r,int d,int w)
{
if(l==r)
{
mx[x]=mk(w?(dep[p[l]]+la[x]):-inf,p[l]);
return;
}
down(x);
if(d<=mid)sol(LS,d,w);
else sol(RS,d,w);
up(x);
}
void add(int x,int l,int r,int L,int R,int d)
{
if(L>R)return;
if(L<=l&&R>=r)return ad(x,d);
down(x);
if(L<=mid)add(LS,L,R,d);
if(R>mid)add(RS,L,R,d);
up(x);
}
int findw(int x,int l,int r,int d)
{
if(l==r)return la[x]+dep[p[l]];
down(x);
if(d<=mid)return findw(LS,d);
else return findw(RS,d);
}
void solve(int u,int v)
{
pre[u]=v;
sol(1,1,n,dfn[v],0);
sol(1,1,n,dfn[u],1);
val+=findw(1,1,n,dfn[v])-findw(1,1,n,dfn[u]);
}
void back(int x)
{
int u=pre[x];pre[x]=0;
sol(1,1,n,dfn[x],0);
sol(1,1,n,dfn[u],1);
val-=findw(1,1,n,dfn[u])-findw(1,1,n,dfn[x]);
}
void sol(int x)
{
for(int i=head[x];i;i=e[i].nxt)
{
int v=e[i].v;
if(v==fa[x])continue;
sol(v);
}
if(s[x])return;
int l=dfn[x],r=l+siz[x]-1;
auto pos=find(1,1,n,l,r);
if(pos.first!=-inf)solve(x,pos.second);
}
void solve(int x)
{
f[x]=val;
for(int i=head[x];i;i=e[i].nxt)if(e[i].v!=fa[x])
{
int u=pre[x],v=e[i].v,l=dfn[v],r=l+siz[v]-1,f=0,c,d;
if(dfn[u]>=l&&dfn[u]<=r)f=1,back(x);
int t=pre[v];if(t)back(v);
add(1,1,n,1,l-1,1);
add(1,1,n,l,r,-1);
add(1,1,n,r+1,n,1);
auto[a,b]=mx[1];
if(!s[v]&&a>0)solve(v,b);
if(f)
{
auto [cc,dd]=max(find(1,1,n,1,l-1),find(1,1,n,r+1,n));
c=cc;d=dd;
if(c>0)solve(x,d);
}
solve(v);
if(f&&c>0)back(x);
if(!s[v]&&a>0)back(v);
add(1,1,n,1,l-1,-1);
add(1,1,n,l,r,1);
add(1,1,n,r+1,n,-1);
if(t)solve(v,t);
if(f)solve(x,u);
}
}
main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
cin>>n>>(s+1);
for(int i=1;i<=n;i++)s[i]-='0';
for(int i=1,u,v;i<n;i++)
{
cin>>u>>v;
e[++cnt]=(edge){u,v,head[u]};head[u]=cnt;
e[++cnt]=(edge){v,u,head[v]};head[v]=cnt;
}
int rt=rand()%n+1;
dfs(rt,0);build(1,1,n);sol(rt);solve(rt);
// cerr<<rt<<'\n';
for(int i=1;i<=n;i++)cout<<f[i]<<' ';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3744kb
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: 3592kb
input:
1 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
1 1
output:
0
result:
ok 1 number(s): "0"
Test #4:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
2 10 1 2
output:
0 1
result:
ok 2 number(s): "0 1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 3732kb
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: 0ms
memory: 3648kb
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: 3684kb
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: 3648kb
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: 1ms
memory: 3628kb
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: 1ms
memory: 3756kb
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: 7ms
memory: 4376kb
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: 58ms
memory: 8884kb
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: 0
Accepted
time: 519ms
memory: 37996kb
input:
200000 10010111111100101000010111101000010111110101100011001101010001010011111001000101111110000010001000011111011011100111001110101011010011111001010100110100100100110110100101010111011010010110111000000100100010100010111001011111011010111010111001000101010111010011010000111011001110000100010110001...
output:
76960 76970 77040 76999 77019 77042 77011 76949 77007 76939 76942 77018 77042 77033 76979 77033 77016 77097 77044 77026 76989 77001 77003 76921 77029 76965 77014 76997 76929 77043 77060 76994 76991 76969 76981 76999 77003 76962 76973 76973 77028 76951 77110 77007 76973 77026 76967 77063 76985 76927 ...
result:
ok 200000 numbers
Test #14:
score: 0
Accepted
time: 449ms
memory: 47596kb
input:
200000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok 200000 numbers
Test #15:
score: 0
Accepted
time: 913ms
memory: 58576kb
input:
200000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
161492 137521 86353 91682 179554 32952 199999 187698 194579 146953 46676 121254 116117 100440 87715 189200 152989 51997 85956 198765 184369 83564 17495 17817 160444 175764 63191 96898 77717 148773 180625 179211 40055 96933 184676 88548 118706 197000 174795 135984 22745 90516 89377 146233 177451 1998...
result:
ok 200000 numbers
Test #16:
score: 0
Accepted
time: 886ms
memory: 51312kb
input:
200000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...
output:
199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998 199998...
result:
ok 200000 numbers
Test #17:
score: 0
Accepted
time: 752ms
memory: 53232kb
input:
199997 10001001111101000110010000100011101000000101001010011000001001000000000011011000010100110010000000011000010000101000000001000110011011010000100111101000101110100001000110001100101011000111000001010010010100110000000111000101101100001000100000100001101100100000110110000011000000000010000010000...
output:
3374373051 4189970859 3184123733 3450268583 2277945377 3696454439 3061951659 3949712041 4220671753 3323963325 2190820273 2815791721 3038430245 2442474205 2679717347 2445099211 4234389437 2761141455 2243098995 3312035333 2354016045 3302499627 3219965331 2883371715 3480709045 2516738855 3524451139 272...
result:
ok 199997 numbers
Test #18:
score: 0
Accepted
time: 530ms
memory: 54364kb
input:
200000 11110001010101111010101000111010011101011110111101111111111111000111110110110001100111011111110111111000111101011101000110111111101101011101111001011011111101110110011110110010110111100111101101110111101111101101110110110111101100010111111011011111111011111101001000111010111111110001011101111...
output:
3725613546 2828499752 2737429131 2045154129 2029946119 3165261139 2911119762 2515530064 2165720641 2144045544 2040045369 2228309145 2901494750 2024811935 3169167665 3707239399 2070360489 2087404270 2988622806 3134475317 2781740246 2029884243 2121709189 2075106780 2130071393 2029683930 2058685522 211...
result:
ok 200000 numbers
Test #19:
score: -100
Wrong Answer
time: 712ms
memory: 44464kb
input:
200000 10110000001010000100000001101000110111100100011000110110011000000110101001000100100100100011100100000000011010000000000010011000000001001010000110001000011101001100010011001000000101011000001000000011100000111111101101000001111001111101011110100101000000010100100010101001000101101010100100001...
output:
1308385941 1497995208 1266404072 1289062935 858619709 1566912729 1017422986 1091038784 1038086490 1223278595 1006222526 1394298647 854966615 885696155 852709442 987838292 955819388 1507851132 1386310224 1364736704 854420649 1482911174 848349724 1408402251 978706570 970316455 849763624 883633108 1681...
result:
wrong answer 146478th numbers differ - expected: '848062258', found: '848062257'