QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#446843#8518. Roars III2745518585WA 797ms341900kbC++204.5kb2024-06-17 16:52:062024-06-17 16:52:07

Judging History

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

  • [2024-06-17 16:52:07]
  • 评测
  • 测评结果:WA
  • 用时:797ms
  • 内存:341900kb
  • [2024-06-17 16:52:06]
  • 提交

answer

#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int N=1000001;
int n,tot,rt1[N],rt2[N];
ll f[N],g[N];
char b[N];
vector<int> a[N];
struct tree
{
    int b,bm;
}T[N];
struct str
{
    int x,w;
    str():x(0),w(-1e9) {}
    str(int x,int w):x(x),w(w) {}
}h[N];
str max(str a,str b)
{
    return a.w>b.w?a:b;
}
namespace sgt
{
    int tot;
    struct tree
    {
        int l,r,k;
        str s;
    }T[N<<4];
    void pushup(int x)
    {
        T[x].s=max(T[T[x].l].s,T[T[x].r].s);
    }
    void pushdown(int &x)
    {
        if(T[x].k==0) return;
        T[++tot]=T[x],x=tot;
        if(T[x].l)
        {
            T[++tot]=T[T[x].l],T[x].l=tot;
            T[T[x].l].s.w+=T[x].k;
            T[T[x].l].k+=T[x].k;
        }
        if(T[x].r)
        {
            T[++tot]=T[T[x].r],T[x].r=tot;
            T[T[x].r].s.w+=T[x].k;
            T[T[x].r].k+=T[x].k;
        }
        T[x].k=0;
    }
    void add1(int &x,int ls,int rs,int l,int r,int k)
    {
        T[++tot]=T[x],x=tot;
        if(ls>=l&&rs<=r)
        {
            T[x].s.w+=k;
            T[x].k+=k;
            return;
        }
        pushdown(x);
        int z=ls+rs>>1;
        if(l<=z) add1(T[x].l,ls,z,l,r,k);
        if(r>z) add1(T[x].r,z+1,rs,l,r,k);
        pushup(x);
    }
    void add2(int &x,int ls,int rs,int q,str k)
    {
        T[++tot]=T[x],x=tot;
        if(ls==rs)
        {
            T[x].s=k;
            return;
        }
        pushdown(x);
        int z=ls+rs>>1;
        if(q<=z) add2(T[x].l,ls,z,q,k);
        else add2(T[x].r,z+1,rs,q,k);
        pushup(x);
    }
    str sum(int x,int ls,int rs,int l,int r)
    {
        if(x==0||l>r) return str();
        if(ls>=l&&rs<=r) return T[x].s;
        pushdown(x);
        int z=ls+rs>>1;
        str s;
        if(l<=z) s=max(s,sum(T[x].l,ls,z,l,r));
        if(r>z) s=max(s,sum(T[x].r,z+1,rs,l,r));
        return s;
    }
    int merge(int x1,int x2,int ls,int rs)
    {
        if(x1==0||x2==0) return x1|x2;
        pushdown(x1),pushdown(x2);
        if(ls==rs)
        {
            ++tot;
            T[tot].s=max(T[x1].s,T[x2].s);
            return tot;
        }
        int z=ls+rs>>1,p=++tot;
        T[p].l=merge(T[x1].l,T[x2].l,ls,z);
        T[p].r=merge(T[x1].r,T[x2].r,z+1,rs);
        pushup(p);
        return p;
    }
}
void dfs1(int x,int fa)
{
    T[x].b=++tot;
    for(auto i:a[x])
    {
        if(i==fa) continue;
        dfs1(i,x);
        sgt::add2(rt1[i],1,n,T[i].b,str(i,0));
        if(b[i]=='0')
        {
            h[i]=sgt::T[rt1[i]].s;
            sgt::add2(rt1[i],1,n,T[h[i].x].b,str());
            f[i]+=h[i].w;
        }
        sgt::add1(rt1[i],1,n,1,n,1);
        rt1[x]=sgt::merge(rt1[x],rt1[i],1,n);
        f[x]+=f[i];
    }
    T[x].bm=tot;
}
void dfs2(int x,int fa)
{
    // printf("%d: ",x);for(int j=1;j<=n;++j) printf("%d ",sgt::sum(rt2[x],1,n,T[j].b,T[j].b).w);printf("\n");
    for(auto i:a[x])
    {
        if(i==fa) continue;
        rt2[i]=rt2[x];
        g[i]=g[x];
        sgt::add2(rt2[i],1,n,T[x].b,str(x,0));
        if(b[x]=='0')
        {
            str p=max(sgt::sum(rt2[i],1,n,1,T[i].b-1),sgt::sum(rt2[i],1,n,T[i].bm+1,n));
            sgt::add2(rt2[i],1,n,T[p.x].b,str());
            g[i]+=p.w;
        }
        // printf("%d: ",i);for(int j=1;j<=n;++j) printf("%d ",sgt::sum(rt2[i],1,n,T[j].b,T[j].b).w);printf("\n");
        sgt::add1(rt2[i],1,n,1,n,1);
        // printf("%d: ",i);for(int j=1;j<=n;++j) printf("%d ",sgt::sum(rt2[i],1,n,T[j].b,T[j].b).w);printf("\n");
        sgt::add1(rt2[i],1,n,T[i].b,T[i].bm,-2);
        if(b[i]=='0')
        {
            sgt::add2(rt2[i],1,n,T[h[i].x].b,h[i]);
            g[i]-=h[i].w;
        }
        sgt::add2(rt2[i],1,n,T[i].b,str());
        dfs2(i,x);
    }
}
int main()
{
    scanf("%d%s",&n,b+1);
    for(int i=1;i<=n-1;++i)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfs1(1,0);
    rt2[1]=rt1[1];
    g[1]=f[1];
    dfs2(1,0);
    for(int i=1;i<=n;++i)
    {
        // for(int j=1;j<=n;++j) printf("%d ",sgt::sum(rt2[i],1,n,T[j].b,T[j].b).w);printf("\n");
        sgt::add2(rt2[i],1,n,T[i].b,str(i,0));
        if(b[i]=='0')
        {
            str p=sgt::T[rt2[i]].s;
            sgt::add2(rt2[i],1,n,T[p.x].b,str());
            g[i]+=p.w;
        }
        printf("%lld ",g[i]);
        // printf("%lld\n",g[i]);
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 23ms
memory: 324916kb

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: 23ms
memory: 324208kb

input:

1
0

output:

0 

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 15ms
memory: 324196kb

input:

1
1

output:

0 

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 3ms
memory: 324224kb

input:

2
10
1 2

output:

0 1 

result:

ok 2 number(s): "0 1"

Test #5:

score: 0
Accepted
time: 16ms
memory: 324248kb

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: 19ms
memory: 324456kb

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: 8ms
memory: 324120kb

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: 16ms
memory: 325092kb

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: 19ms
memory: 324228kb

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: 11ms
memory: 325472kb

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: 28ms
memory: 325372kb

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: 119ms
memory: 327448kb

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
Wrong Answer
time: 797ms
memory: 341900kb

input:

200000
10010111111100101000010111101000010111110101100011001101010001010011111001000101111110000010001000011111011011100111001110101011010011111001010100110100100100110110100101010111011010010110111000000100100010100010111001011111011010111010111001000101010111010011010000111011001110000100010110001...

output:

76960 77039 76948 76926 77248 77640 76928 76917 76923 77099 77102 77378 77408 76928 77220 77405 76926 77417 76942 77358 77230 76929 77244 76921 77029 76965 76929 77238 77028 76901 77090 77226 77111 76912 77222 77384 77003 77084 76997 76912 76915 76906 77290 77367 76914 77386 76924 76930 76913 76927 ...

result:

wrong answer 2nd numbers differ - expected: '76970', found: '77039'