QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#322145 | #8222. 投票游戏 | zhouhuanyi | 0 | 73ms | 44740kb | C++20 | 6.1kb | 2024-02-06 12:31:16 | 2024-02-06 12:31:17 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<random>
#define N 500000
using namespace std;
mt19937 RAND(random_device{}());
const long long inf=(long long)(1e18);
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
int n,q,dfn[N+1],leng,fa[N+1],rev[N+1],bt[N+1],a[N+1],b[N+1],sz[N+1],p[N+1],hs[N+1],top[N+1],rt[N+1],delta[N+1],delta2[N+1];
long long F[N+1],dp[N+1],DP[N+1],DP2[N+1];
vector<int>E[N+1];
bool cmp(int x,int y)
{
return dp[x]>dp[y];
}
void dfs(int x)
{
sz[x]=1;
for (int i=0;i<E[x].size();++i)
{
fa[E[x][i]]=x,dfs(E[x][i]),sz[x]+=sz[E[x][i]];
if (sz[E[x][i]]>sz[hs[x]]) hs[x]=E[x][i];
}
return;
}
void dfs2(int x)
{
dfn[x]=++leng,rev[dfn[x]]=x;
if (hs[x]) top[hs[x]]=top[x],dfs2(hs[x]),bt[x]=bt[hs[x]];
else bt[x]=x;
for (int i=0;i<E[x].size();++i)
if (E[x][i]!=hs[x])
top[E[x][i]]=E[x][i],dfs2(E[x][i]);
return;
}
void dfs3(int x)
{
dp[x]=F[x];
for (int i=0;i<E[x].size();++i) dfs3(E[x][i]);
for (int i=0;i<E[x].size();++i) p[i]=dp[E[x][i]];
sort(p,p+E[x].size(),cmp);
for (int i=0;i<E[x].size();++i)
if (dp[x]<=dp[p[E[x][i]]])
dp[x]-=b[p[E[x][i]]];
return;
}
struct reads
{
int num;
long long data;
bool operator < (const reads &t)const
{
return data!=t.data?data>t.data:num>t.num;
}
bool operator > (const reads &t)const
{
return data!=t.data?data<t.data:num<t.num;
}
bool operator <= (const reads &t)const
{
return data!=t.data?data>t.data:num>=t.num;
}
};
struct rds
{
long long a,b;
};
rds operator + (rds x,rds y)
{
return (rds){x.a+y.a,min(x.a+y.b,x.b)};
}
struct fhq_treap
{
struct node
{
int ls,rs,rd;
reads data;
rds nw,res;
};
node tree[N+1];
int length;
int new_node(reads x)
{
++length,tree[length]=(node){0,0,(int)(RAND()%inf),x,(rds){b[x.num],x.data},(rds){b[x.num],x.data}};
return length;
}
void push_up(int k)
{
tree[k].res=tree[tree[k].ls].res+tree[k].nw+tree[tree[k].rs].res;
return;
}
int merge(int x,int y)
{
if (!x||!y) return x^y;
if (tree[x].rd<tree[y].rd)
{
tree[x].rs=merge(tree[x].rs,y),push_up(x);
return x;
}
else
{
tree[y].ls=merge(x,tree[y].ls),push_up(y);
return y;
}
}
void split(int k,reads d,int &x,int &y)
{
if (!k)
{
x=y=0;
return;
}
if (tree[k].data<=d) split(tree[k].rs,d,tree[k].rs,y),x=k;
else split(tree[k].ls,d,x,tree[k].ls),y=k;
push_up(k);
return;
}
void split2(int k,reads d,int &x,int &y)
{
if (!k)
{
x=y=0;
return;
}
if (tree[k].data<d) split2(tree[k].rs,d,tree[k].rs,y),x=k;
else split2(tree[k].ls,d,x,tree[k].ls),y=k;
push_up(k);
return;
}
int insert(int k,reads d)
{
int A,B;
split(k,d,A,B);
return merge(merge(A,new_node(d)),B);
}
int del(int k,reads d)
{
int tmp,A,B,C;
split(k,d,tmp,C),split2(tmp,d,A,B);
return merge(A,C);
}
long long calc(int k,long long x,rds d)
{
if (!k) return 0;
if (x<=(d+tree[tree[k].ls].res+tree[k].nw).b) return tree[tree[k].ls].res.a+tree[k].nw.a+calc(tree[k].rs,x,d+tree[tree[k].ls].res+tree[k].nw);
else if (x<=(d+tree[tree[k].ls].res).b) return tree[tree[k].ls].res.a;
else return calc(tree[k].ls,x,d);
}
};
fhq_treap T;
struct dst
{
int p[2];
};
dst cs;
dst operator * (dst a,dst b)
{
for (int i=0;i<=1;++i) cs.p[i]=b.p[a.p[i]];
return cs;
}
struct seg
{
struct node
{
int l,r;
dst data;
};
node tree[(N<<2)+1];
void push_up(int k)
{
tree[k].data=tree[k<<1].data*tree[k<<1|1].data;
return;
}
void build(int k,int l,int r)
{
tree[k].l=l,tree[k].r=r;
if (l==r)
{
tree[k].data.p[0]=delta[rev[l]],tree[k].data.p[1]=delta2[rev[l]];
return;
}
int mid=(l+r)>>1;
build(k<<1,l,mid),build(k<<1|1,mid+1,r),push_up(k);
return;
}
void add(int k,int x,dst d)
{
if (tree[k].l==tree[k].r)
{
tree[k].data=d;
return;
}
int mid=(tree[k].l+tree[k].r)>>1;
if (x<=mid) add(k<<1,x,d);
else add(k<<1|1,x,d);
push_up(k);
return;
}
dst query(int k,int l,int r)
{
if (tree[k].l==l&&tree[k].r==r) return tree[k].data;
int mid=(tree[k].l+tree[k].r)>>1;
if (r<=mid) return query(k<<1,l,r);
else if (l>=mid+1) return query(k<<1|1,l,r);
else return query(k<<1,l,mid)*query(k<<1|1,mid+1,r);
}
};
seg T2;
long long calc(int x)
{
if (bt[x]==x) return a[x];
int d=T2.query(1,dfn[x]+1,dfn[bt[x]]).p[0];
if (!d) return DP[x];
else return DP2[x];
}
void recover(int x,int y)
{
rt[x]=T.del(rt[x],(reads){y,dp[y]}),dp[y]=calc(y),rt[x]=T.insert(rt[x],(reads){y,dp[y]});
return;
}
void rebuild(int x)
{
DP[x]=F[x]-T.calc(rt[x],F[x],(rds){0,inf}),DP2[x]=F[x]-b[hs[x]]-T.calc(rt[x],F[x]-b[hs[x]],(rds){0,inf});
if (hs[x]) delta[hs[x]]=DP[hs[x]]>=DP[x],delta[hs[x]]=DP2[hs[x]]>=DP[x],T2.add(1,dfn[hs[x]],(dst){delta[hs[x]],delta2[hs[x]]});
if (fa[x]) delta[x]=DP[x]>=DP[fa[x]],delta[x]=DP2[x]>=DP[fa[x]],T2.add(1,dfn[x],(dst){delta[x],delta2[x]});
return;
}
void change(int p,int x,int y)
{
if (fa[p]) F[fa[p]]+=x-a[p];
F[p]+=y-b[p],a[p]=x,b[p]=y,rebuild(p);
if (fa[p])
{
while (fa[p]) recover(fa[p],p),rebuild(fa[p]),p=top[fa[p]];
}
return;
}
int main()
{
int op,p,x,y,c,d;
n=read(),q=read();
for (int i=2;i<=n;++i) x=read(),E[x].push_back(i);
for (int i=1;i<=n;++i) a[i]=read();
for (int i=1;i<=n;++i) b[i]=read();
for (int i=1;i<=n;++i)
{
F[i]=a[i];
for (int j=0;j<E[i].size();++j) F[i]+=b[E[i][j]];
}
dfs(1),top[1]=1,dfs2(1),dfs3(1);
for (int i=1;i<=n;++i)
for (int j=0;j<E[i].size();++j)
if (E[i][j]!=hs[i])
rt[i]=T.insert(rt[i],(reads){E[i][j],dp[E[i][j]]});
for (int i=1;i<=n;++i) DP[i]=F[i]-T.calc(rt[i],F[i],(rds){0,inf}),DP2[i]=F[i]-b[hs[i]]-T.calc(rt[i],F[i]-b[hs[i]],(rds){0,inf});
for (int i=1;i<=n;++i)
if (hs[i])
delta[hs[i]]=DP[hs[i]]>=DP[i],delta2[hs[i]]=DP2[hs[i]]>=DP[i];
T2.build(1,1,n);
while (q--)
{
op=read();
if (op==1) p=read(),x=read(),y=read(),change(p,x,y);
else c=read(),d=read(),printf("%d\n",(reads){c,calc(c)}>(reads){d,calc(d)});
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
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:
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #13:
score: 0
Wrong Answer
time: 68ms
memory: 44740kb
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:
0 0 0 0 1 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 1 0 1 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 0 1 1 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 0 1 1 1 0 1 1 1 1 1 0 0 0 1 0 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 1 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 1 0 0 1 0 1 0 0 0 0 0 0 0 ...
result:
wrong answer 26th numbers differ - expected: '1', found: '0'
Subtask #4:
score: 0
Runtime Error
Test #18:
score: 25
Accepted
time: 73ms
memory: 38608kb
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:
1 0 0 0 0 0 1 1 1 0 0 0 0 0 1 0 1 0 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1 1 1 1 0 1 0 0 1 1 1 0 0 0 1 0 0 0 1 1 1 1 0 1 1 0 0 1 1 0 1 0 1 1 1 0 0 0 1 0 1 1 0 0 0 1 0 0 0 0 1 1 0 1 0 1 1 0 1 1 1 0 0 1 0 1 1 1 1 1 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 1 0 ...
result:
ok 99788 numbers
Test #19:
score: -25
Runtime Error
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: 0
Wrong Answer
Test #26:
score: 0
Wrong Answer
time: 45ms
memory: 41012kb
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 0 0 1 0 0 0 1 0 1 1 1 0 1 1 0 1 0 1 0 1 1 1 1 0 1 0 0 0 1 1 1 0 1 1 0 0 0 1 1 0 1 1 0 1 1 0 0 0 0 0 0 1 0 1 1 0 1 1 0 1 1 1 1 0 0 0 1 0 1 1 0 0 1 0 1 0 1 1 1 1 0 1 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 1 0 1 0 0 1 1 0 1 1 0 1 1 0 0 1 0 0 0 ...
result:
wrong answer 17th numbers differ - expected: '1', found: '0'
Subtask #6:
score: 0
Skipped
Dependency #1:
0%