QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#449093#8222. 投票游戏Kalenist20 122ms69888kbC++204.7kb2024-06-20 17:10:362024-06-20 17:10:38

Judging History

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

  • [2024-06-20 17:10:38]
  • 评测
  • 测评结果:20
  • 用时:122ms
  • 内存:69888kb
  • [2024-06-20 17:10:36]
  • 提交

answer

#include<bits/stdc++.h>
#define N 200010
#define ll long long
#define pc putchar
#define inf (1ll<<60)
#define lc k<<1
#define rc k<<1|1
#define pii pair<int,int>
#define fi first
#define se second
#define For(i,a,b) for(register int i=a;i<=b;i++)
using namespace std;
int n,m,a[N],b[N],fa[N],top[N];
int sz[N],son[N],seg[N];
int ch[N][2],rt[N],p[N];
ll f[N],sum[N],val[N],mn[N];
vector<int> go[N];
mt19937 rnd(time(0));
struct ele
{
    ll n0,v0,v1;
    inline ll get(ll nw)
        {return nw<n0?v0:v1;}
    inline ele operator +(const ele b)
    {
        ele res;res.v1=get(b.v1);
        res.n0=b.n0,res.v0=get(b.v0);
        return res;
    }
}v[N],tr[N<<2];
inline int read()
{
    register int x=0,c=getchar();
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x;
}

inline void pushup(int x)
{
    val[x]=val[ch[x][0]]+b[x]+val[ch[x][1]];
    mn[x]=min(mn[ch[x][0]],val[ch[x][0]]+f[x]);
    mn[x]=min(mn[x],val[ch[x][0]]+b[x]+mn[ch[x][1]]);
    return;
}

inline int merge(int x,int y)
{
    if(!x || !y) return x+y;
    if(p[x] < p[y])
    {
        ch[x][1]=merge(ch[x][1],y);
        pushup(x);return x;
    }ch[y][0]=merge(x,ch[y][0]);
    pushup(y);return y;
}

inline pii divide(int x,ll k)
{
    if(!x) return pii(0,0);
    if(f[x] > k)
    {
        pii a=divide(ch[x][1],k);
        ch[x][1]=a.fi,pushup(x);
        return pii(x,a.se);
    }pii a=divide(ch[x][0],k);
    ch[x][0]=a.se,pushup(x);
    return pii(a.fi,x);
}

inline ll binary(int x,ll k)
{
    if(!x) return k;
    if(k > mn[ch[x][0]])
        return binary(ch[x][0],k);
    if(k > val[ch[x][0]]+f[x])
        return k-val[ch[x][0]];
    return binary(ch[x][1],k-val[ch[x][0]]-b[x]);
}

inline void insert(int &rt,int x)
{
    pii a=divide(rt,f[x]);
    rt=merge(a.fi,merge(x,a.se));
    return;
}

inline void erase(int &rt,int x)
{
    pii a=divide(rt,f[x]);
    pii b=divide(a.se,f[x]-1);
    rt=merge(a.fi,b.se);
    return;
}

inline void calc(int t,int x)
{
    if(!x) {v[t].n0=inf,v[t].v0=a[t];return;}
    ll nw=binary(rt[t],sum[t]+a[t]);
    v[t].n0=v[t].v0=nw;
    v[t].v1=binary(rt[t],sum[t]+a[t]-b[x]);
    return;
}

inline void modify(int k,int l,int r,int pos,ele nw)
{
    if(l == r) {tr[k]=nw;return;}
    int mid=l+r>>1;
    if(pos <= mid) modify(lc,l,mid,pos,nw);
    else modify(rc,mid+1,r,pos,nw);
    return void(tr[k]=tr[lc]+tr[rc]);
}

inline ele query(int k,int l,int r,int pos)
{
    if(l >= pos) return tr[k];
    int mid=l+r>>1;
    if(pos > mid) return query(rc,mid+1,r,pos);
    return query(lc,l,mid,pos)+query(rc,mid+1,r,pos);
}

inline void dfs1(int x)
{
    sz[x]=1;
    for(int to:go[x])
    {
        dfs1(to),sum[x]+=b[to],sz[x]+=sz[to];
        if(sz[to] > sz[son[x]]) son[x]=to;
    }
    for(int to:go[x]) if(to^son[x])
    {
        val[to]=b[to],p[to]=rnd();
        mn[to]=f[to],insert(rt[x],to);
    }calc(x,son[x]),f[x]=v[x].get(f[son[x]]);
    return;
}

inline void dfs2(int x)
{
    modify(1,1,n,seg[x],v[x]);
    if(son[x]) 
    {
        top[son[x]]=top[x];
        seg[son[x]]=++seg[0];
        dfs2(son[x]);
    }
    for(int to:go[x]) if(to^son[x])
    {
        top[to]=to;
        seg[to]=++seg[0];
        dfs2(to);
    }
    return;
}

inline void update(int x)
{
    for(;true;x=fa[top[x]])
    {
        calc(x,son[x]);
        modify(1,1,n,seg[x],v[x]);
        int t=top[x];
        if(!fa[t]) break;
        erase(rt[fa[t]],t);
        f[t]=query(1,1,n,seg[t]).get(0);
        insert(rt[fa[t]],t);
    }
    return;
}

int main()
{
    n=read(),m=read();
    mn[0]=f[n+1]=inf,f[0]=-1;
    For(i,2,n) go[fa[i]=read()].push_back(i);
    For(i,1,n) a[i]=read();
    For(i,1,n) b[i]=read();
    top[1]=seg[0]=seg[1]=1;
    dfs1(1),dfs2(1);
    /*
    For(i,1,n) printf("%lld ",f[i]);
    printf("\n");*/
    //return 0;
    For(i,1,m)
    {
        int opt=read(),x=read(),y=read();
        if(opt == 1)
        {
            a[x]=y,update(x);
            int nw=read(),t=fa[x];
            if(t) 
            {
                sum[t]+=nw-b[x],b[x]=nw;
                if(x^son[t])
                {
                    erase(rt[t],x);
                    f[x]=query(1,1,n,seg[x]).get(0);
                    val[x]=b[x],mn[x]=f[x];
                    insert(rt[t],x);
                }update(t);
            }
        }
        else 
        {
            f[x]=query(1,1,n,seg[x]).get(0);
            f[y]=query(1,1,n,seg[y]).get(0);
            //printf("%lld %lld\n",f[x],f[y]);
            bool ans=f[y]>f[x]||f[y]==f[x]&&y>x;
            pc(ans+'0'),pc('\n');
        }
    }
    return 0;
}

詳細信息

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 5
Accepted
time: 3ms
memory: 22096kb

input:

20 500
1 1 3 1 3 5 6 6 7 3 5 3 9 5 4 7 7 18 2
42129194 82765784 1447057 29726963 82321558 32094641 22474113 49277574 27527633 20746795 47630926 92888389 26787144 80703040 43876864 97991729 12117966 75633318 33577855 93462601
69163546 49621952 45236756 46447931 34085269 55246550 54414402 99593046 103...

output:

0
0
1
1
0
0
0
0
1
1
0
0
0
0
0
1
1
1
1
1
0
0
1
1
0
1
1
1
0
1
1
0
0
0
0
1
1
1
1
1
1
0
1
0
0
1
0
1
1
0
0
0
1
1
1
1
1
1
0
0
1
1
0
0
0
1
0
0
0
0
0
0
0
0
0
1
1
0
0
1
0
0
0
0
0
0
1
1
0
1
1
1
1
1
0
1
1
0
1
0
0
1
0
0
1
1
1
0
0
0
0
1
1
0
1
0
1
0
1
1
0
1
0
0
1
0
0
1
1
1
1
0
1
0
0
1
1
1
1
0
0
1
0
0
0
1
0
1
1
1
...

result:

ok 238 numbers

Test #2:

score: -5
Time Limit Exceeded

input:

20 500
1 1 3 1 3 5 3 4 2 4 3 12 6 7 12 6 4 11 10
2 0 2 1 1 1 2 0 2 2 1 0 0 2 1 0 1 0 1 1
0 0 2 0 0 0 1 2 2 2 0 2 2 2 1 0 1 0 1 1
2 5 2
1 6 1 0
1 7 1 1
2 5 11
2 18 16
2 13 7
2 4 3
1 6 0 0
1 5 0 2
2 16 12
2 5 18
2 8 16
1 4 0 0
2 5 2
1 19 2 2
1 10 0 0
1 15 2 2
2 12 14
1 12 1 1
1 13 1 2
1 3 2 2
1 6 2 2
...

output:


result:


Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Memory Limit Exceeded

Test #13:

score: 0
Memory Limit Exceeded

input:

200 200000
1 2 3 3 5 3 1 6 2 9 11 5 5 2 4 9 17 8 1 4 10 20 18 20 23 13 16 28 15 4 6 27 26 11 30 25 10 2 13 23 25 35 4 8 40 43 36 26 7 27 45 35 14 31 54 45 9 8 9 54 16 32 62 9 29 2 43 39 34 39 27 2 52 56 6 9 48 26 66 28 35 57 79 13 71 61 38 43 80 26 61 26 79 1 24 64 79 15 41 42 56 55 6 24 92 43 89 76...

output:


result:


Subtask #4:

score: 0
Memory Limit Exceeded

Test #18:

score: 0
Memory Limit Exceeded

input:

200 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:


result:


Subtask #5:

score: 20
Accepted

Test #26:

score: 20
Accepted
time: 21ms
memory: 16004kb

input:

200 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1...

output:

1
0
1
0
1
1
1
1
0
1
1
1
0
0
0
0
1
0
1
1
0
1
0
0
1
1
1
0
1
0
0
0
1
0
1
1
0
0
0
1
1
1
0
0
1
1
1
0
1
0
0
1
0
1
1
0
1
1
0
0
1
0
0
1
0
0
0
1
0
1
0
0
1
1
1
1
1
1
1
0
0
0
1
0
0
1
0
0
1
1
1
1
1
0
1
1
0
1
0
0
0
0
0
0
0
0
0
1
0
1
1
1
1
1
0
0
0
0
1
1
1
0
1
0
1
1
1
0
0
1
1
0
1
0
0
1
0
0
0
0
1
0
1
1
1
0
1
1
1
0
...

result:

ok 100455 numbers

Test #27:

score: 0
Accepted
time: 18ms
memory: 18028kb

input:

200 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 1...

output:

0
1
0
0
1
0
0
0
0
0
1
1
0
0
1
1
0
0
1
1
0
1
1
0
1
0
0
0
0
1
1
1
0
0
1
0
1
1
1
1
0
0
0
1
1
0
1
1
0
0
0
1
0
1
0
0
1
0
1
1
0
0
0
0
1
1
0
1
0
1
0
1
1
1
0
0
0
1
0
1
0
1
1
1
1
0
1
0
0
0
1
0
0
0
1
1
0
0
1
0
1
0
1
1
0
0
0
1
0
0
0
1
0
0
1
1
1
0
1
0
1
1
1
1
0
0
1
1
1
1
0
1
1
1
1
1
1
0
0
0
0
1
0
0
1
1
1
1
0
0
...

result:

ok 99902 numbers

Test #28:

score: 0
Accepted
time: 38ms
memory: 20604kb

input:

2000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 ...

output:

0
0
1
0
0
1
0
0
0
0
0
0
1
0
0
0
0
1
0
1
1
0
1
0
1
0
1
0
1
0
1
1
1
1
0
0
0
0
0
0
1
0
0
0
1
0
1
0
1
1
0
0
1
0
1
1
0
0
1
0
0
1
0
0
1
1
1
1
0
1
0
0
0
1
0
0
0
1
1
0
0
1
1
0
0
0
1
1
0
0
0
0
1
1
0
0
1
1
0
0
0
0
0
0
1
1
1
0
1
0
0
0
0
1
0
0
1
1
1
1
0
0
0
0
1
1
1
0
1
0
0
1
0
0
0
0
1
0
1
1
0
0
1
0
0
1
0
1
1
1
...

result:

ok 99882 numbers

Test #29:

score: 0
Accepted
time: 54ms
memory: 23604kb

input:

20000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99...

output:

0
0
0
0
1
0
0
0
1
0
0
1
0
1
1
1
0
1
0
0
0
1
0
0
1
0
1
0
0
1
1
1
1
0
0
1
1
1
0
1
1
0
1
1
0
0
0
0
1
0
1
0
1
1
0
0
0
1
0
0
1
1
1
1
1
0
0
1
1
1
1
1
1
1
0
1
0
0
1
1
0
0
0
1
0
1
1
1
1
0
1
0
1
0
1
0
0
1
0
0
1
1
0
0
0
1
1
0
0
0
1
0
1
0
1
1
0
1
0
1
0
0
1
1
1
1
1
0
1
1
0
1
0
0
1
1
1
0
1
0
1
0
1
0
0
1
0
1
0
1
...

result:

ok 100151 numbers

Test #30:

score: 0
Accepted
time: 115ms
memory: 67844kb

input:

200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:

0
0
1
0
1
1
1
0
0
1
0
1
0
0
1
0
1
1
1
1
0
0
1
0
1
1
0
1
1
0
0
1
1
0
0
1
0
1
1
1
1
0
0
1
0
0
0
1
1
0
0
1
1
1
0
0
0
0
0
0
0
1
1
1
1
0
0
1
0
0
1
1
0
1
0
1
0
0
1
1
1
0
1
0
0
0
0
0
0
1
1
0
1
0
1
1
0
1
0
1
1
0
1
0
1
0
1
1
1
1
1
1
1
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
1
0
1
0
1
1
1
0
0
1
0
1
0
0
1
1
0
0
1
0
0
0
...

result:

ok 100291 numbers

Test #31:

score: 0
Accepted
time: 122ms
memory: 69748kb

input:

200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:

0
1
1
0
0
0
0
1
1
0
0
1
0
1
1
1
1
1
1
0
1
0
0
0
1
0
0
1
1
1
0
0
1
0
0
0
1
0
0
1
1
0
1
1
1
0
1
0
1
1
1
1
0
1
0
1
1
0
0
0
0
1
0
0
0
1
0
0
1
1
0
1
0
1
1
1
0
1
1
1
1
0
1
0
1
1
1
1
0
0
1
0
0
1
0
1
1
0
1
1
1
0
0
1
1
1
0
0
0
1
0
0
1
1
0
1
0
1
1
0
1
0
0
1
0
0
1
1
0
1
0
1
1
0
1
0
1
0
1
0
0
0
0
1
0
0
1
1
1
1
...

result:

ok 100358 numbers

Test #32:

score: 0
Accepted
time: 92ms
memory: 69888kb

input:

200000 200000
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 9...

output:

1
0
0
1
1
0
1
0
1
1
1
0
0
1
1
1
1
0
1
0
1
0
0
0
0
0
0
1
1
0
0
1
0
0
1
0
0
0
1
0
1
0
0
0
1
1
0
1
1
1
0
1
1
1
0
1
0
1
0
0
1
1
0
1
0
0
0
0
1
1
0
0
0
0
0
1
1
0
1
0
0
0
1
1
0
1
0
0
1
1
0
1
0
0
1
1
1
1
0
1
0
1
1
0
1
1
0
0
1
1
0
0
1
0
1
0
0
0
1
0
1
1
1
0
1
0
1
0
1
1
0
0
0
1
1
0
1
1
1
1
0
1
1
0
1
0
0
1
0
0
...

result:

ok 66538 numbers

Subtask #6:

score: 0
Skipped

Dependency #1:

0%