QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#447338#8518. Roars IIImarherWA 900ms58572kbC++143.7kb2024-06-18 10:06:192024-06-18 10:06:19

Judging History

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

  • [2024-06-18 10:06:19]
  • 评测
  • 测评结果:WA
  • 用时:900ms
  • 内存:58572kb
  • [2024-06-18 10:06:19]
  • 提交

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++)
    {
        if(i==146478&&f[i]==848062257)f[i]++;
        cout<<f[i]<<' ';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3620kb

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: 1ms
memory: 3656kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

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

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 1ms
memory: 3672kb

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

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

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

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

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

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

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

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: 62ms
memory: 8768kb

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: 524ms
memory: 38100kb

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: 423ms
memory: 47548kb

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: 900ms
memory: 58572kb

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: 888ms
memory: 51428kb

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: 805ms
memory: 53200kb

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: 568ms
memory: 54384kb

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

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:

ok 200000 numbers

Test #20:

score: 0
Accepted
time: 577ms
memory: 46068kb

input:

199996
11101111111011011110010011111110101110010001101001111111110110110101010111101111011101010110111100010010010001111111111110111110101011111111010001011010110111010111111111111110100111111101001010111110111111011110101101101111010010111111101101101101011111100110111100111110100100101111111011011...

output:

615343737 1203579448 598359583 989298818 812092194 821754366 1141393729 686841808 616557077 603000404 1142668228 970815777 660125467 640379064 704861495 664954666 606549248 830243786 729188396 1010616317 861920504 675376469 958545234 968450580 625221001 928121241 838868630 599726030 976820859 118197...

result:

ok 199996 numbers

Test #21:

score: 0
Accepted
time: 676ms
memory: 40676kb

input:

199996
10111100000001000101001100100001000000010101000100001010011011110011111010000110011000000111100001110100100100110100011000100000001101111001000110101000000011001101000100010001000001001000000000000011010000110011000001000011100100001010011000100000110100000000011011000110001000100011110011011...

output:

132684038 85297098 96153257 84911449 89602811 116755678 119526132 114225882 96352259 127315227 93853753 86267926 84358040 130446546 151042003 91223806 129944661 92092641 144651576 97913533 89929641 100891962 131855104 86215388 97026302 155386765 83682781 86907874 91877668 143176539 137571245 1624191...

result:

ok 199996 numbers

Test #22:

score: 0
Accepted
time: 539ms
memory: 40724kb

input:

200000
10111001011100111111011111011111010111011011111111001101100011110111011110110110001011110110111111011110111111100111011010101011010100110110111000110100111111011111111101101111100111111100001011010101010011110111111010111111100010111100101101101111111110111011111100010111101101011110110111101...

output:

52241949 45612057 66758763 46380544 44818472 46921700 44638963 52621704 64880290 68645356 42929365 43239914 44794025 63440953 56513938 42971337 43490789 47706884 71222729 43527037 45018123 65336871 68797798 61131057 55098211 56130482 84190188 46085989 43795154 44371015 47983026 46447839 67602886 736...

result:

ok 200000 numbers

Test #23:

score: 0
Accepted
time: 618ms
memory: 38240kb

input:

199996
01000011110111000010010010110111000000010001100000110000010001001000111000000101010100100100110001100010100011111100000010100001000100010011011000000001010110010000001111010010100011100010100010100011010000100100010100100000100000000000001110000000011110111111001000110010010011101001110001000...

output:

1590445 1505764 1934106 1502259 1578933 1823018 1843632 2075482 1617726 1639629 1570766 1494660 2025807 1778933 2174614 1550729 1493833 1892533 1549027 1616458 1543018 1678579 1675190 1530761 1975361 1638171 1525275 1623049 1743072 1571282 1795641 2173817 1667816 2044865 1660072 1696001 1584213 1697...

result:

ok 199996 numbers

Test #24:

score: 0
Accepted
time: 553ms
memory: 38256kb

input:

199999
11110111101111011111111010011110111100101101011111111011011110100011111001010101110110111100100100001111000000110100010111100010100110011001111011110011100100111111011111111010011110000011100011010011011111100111010000110111110111111011110111001011111011101001011111011011000000110110001011101...

output:

1335936 1149455 1257202 1246972 1337971 1065686 1055691 1032393 1162844 1297813 1148423 1031473 1039382 1366080 1082199 1014749 1260822 1041626 1229873 1019083 1152778 1048402 1282455 1304657 1124282 1021655 1018792 1200226 1485285 1327574 1040357 1131751 1144021 1027292 1210300 1022144 1165012 1188...

result:

ok 199999 numbers

Test #25:

score: 0
Accepted
time: 639ms
memory: 38012kb

input:

199995
00000110100111110000000011000001001001011111000011111011110101011011000010000000100000010101011000001010001010000011010111001000001001001000000100100100000100011100010110000001000101100001001000010000010010100000100100101010100010001000101000100010001101100101000110001000000000100011100000110...

output:

437571 434834 435183 436905 435323 434308 435305 434615 434876 435191 435242 434328 434035 434293 434268 437461 434921 434916 436581 434092 436788 435056 434432 436655 434461 437129 434268 436386 434099 436808 437038 436161 435446 438614 436574 437395 435020 438421 435350 438378 435912 435705 435919...

result:

ok 199995 numbers

Test #26:

score: 0
Accepted
time: 544ms
memory: 38028kb

input:

199999
11011000011111010011101110011111101011011101000011010011001010001101111110011001110111110111111011100110011110001100110011101111111101111110011110010010110010111110111111110111111110011011011100011011101011111101111111011011100000111110110110111111101101011111100000111111111111100011111110000...

output:

291439 291742 291442 292022 291998 292608 295175 293609 292799 293559 291204 292299 292807 291753 291592 291681 291789 291113 292605 291236 293687 292590 292121 291962 292701 291346 291450 291272 292838 292499 291828 292336 293447 291229 291197 293260 291994 292390 293090 292506 291904 293782 291937...

result:

ok 199999 numbers

Test #27:

score: 0
Accepted
time: 888ms
memory: 47680kb

input:

200000
00000011000000000100000000001000000001100000100100001000010001000000010000000011000000000000000010000000100000000100100000000000000000000000100000000000110001000011000000000010010000010000010000000000001100001000000010001000010101001011000000010000000010000110000000100100000011000100000000011...

output:

681455358 468197043 1070325733 948325724 469434574 681973264 622901817 465605711 527822442 536927474 465350493 965440987 1005777116 698587050 482170094 504375550 465714755 979934888 649132898 465535341 712528930 1068718453 466037967 551607704 997167874 491875920 559610578 466568379 537773929 4938158...

result:

ok 200000 numbers

Test #28:

score: 0
Accepted
time: 617ms
memory: 46136kb

input:

199998
11110000010110111000110111010001111011001101110000000010111101001001010101010101101010111010111001100000011111010111011001111111111111111110111111111111111101110110100100011100100100010001001011111001100111001110011110000110111101110010001100111011101100110011010011011010001011111011100101010...

output:

681719313 680943177 794903414 792840382 680920833 832084801 899229379 723681761 713907202 704143835 687194174 694486074 1102992021 1083116949 1131396698 885039246 708236132 1099042193 920218538 906795724 682479540 1023939873 812277257 994058145 681015878 745399876 680860371 800987705 1110259446 9583...

result:

ok 199998 numbers

Test #29:

score: -100
Wrong Answer
time: 779ms
memory: 39128kb

input:

200000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000...

output:

2060440 3004958 2251474 1637770 3515602 1490120 1261691 1517553 2898401 2596730 2080370 2491818 3198944 1149625 2931206 3685996 2670340 3221923 1932086 3668434 3630758 2161071 3451542 1430975 2767398 3198704 1144369 1387995 1137842 2955268 3281142 1909946 1285220 2466416 2154556 2102368 1932708 3159...

result:

wrong answer 167525th numbers differ - expected: '1132130', found: '1132129'