QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#444632 | #8518. Roars III | ucup-team3510# | WA | 2ms | 10032kb | C++20 | 4.4kb | 2024-06-15 20:30:01 | 2024-06-15 20:30:03 |
Judging History
answer
#include <bits/stdc++.h>
#define N 200011
#define ll long long
#define pii pair<int,int>
#define s1 first
#define s2 second
using namespace std;
int n;
pii mx[N*4];int tag[N*4];ll sum[N*4];
void pushup(int x){sum[x]=sum[x<<1]+sum[x<<1|1];mx[x]=max(mx[x<<1],mx[x<<1|1]);}
void apply(int x,int p,int L,int R)
{
tag[x]+=p;mx[x].s1+=p;
}
void pushdown(int x,int L,int R)
{
if(tag[x])apply(x<<1,tag[x],L,L+R>>1),apply(x<<1|1,tag[x],(L+R>>1)+1,R),tag[x]=0;
}
void build(int L,int R,int x)
{
if(L==R){mx[x]=pii(-1e9,L);return;}
build(L,L+R>>1,x<<1);build((L+R>>1)+1,R,x<<1|1);pushup(x);
}
pii qmx(int l,int r,int L,int R,int x)
{
if(l>r)return pii(-1e9,-1e9);
if(l<=L&&R<=r)return mx[x];pii ans(-1e9,-1e9);pushdown(x,L,R);
if(l<=L+R>>1)ans=max(ans,qmx(l,r,L,L+R>>1,x<<1));if(r>L+R>>1)ans=max(ans,qmx(l,r,(L+R>>1)+1,R,x<<1|1));return ans;
}
void add(int l,int r,int p,int L,int R,int x)
{
if(l>r)return;
if(l<=L&&R<=r){apply(x,p,L,R);return;}pushdown(x,L,R);
if(l<=L+R>>1)add(l,r,p,L,L+R>>1,x<<1);if(r>L+R>>1)add(l,r,p,(L+R>>1)+1,R,x<<1|1);pushup(x);
}
pii qk(int k,int L,int R,int x)
{
if(L==R)return make_pair(mx[x].s1,sum[x]);pushdown(x,L,R);
if(k<=L+R>>1)return qk(k,L,L+R>>1,x<<1);else return qk(k,(L+R>>1)+1,R,x<<1|1);
}
void modi(int k,pii p,int L,int R,int x)
{
// printf("modi(%d,{%d,%d},[%d,%d],%d)\n",k,p.s1,p.s2,L,R,x);
if(L==R){mx[x].s1=p.s1;sum[x]=p.s2;return;}pushdown(x,L,R);
if(k<=L+R>>1)modi(k,p,L,L+R>>1,x<<1);else modi(k,p,(L+R>>1)+1,R,x<<1|1);pushup(x);
}
int dfn[N],siz[N],rk[N],dep[N],clk;vector<int> G[N];
ll ans[N];
void dfs0(int u,int F)
{
dfn[u]=++clk;siz[u]=1;rk[clk]=u;dep[u]=dep[F]+1;
for(int v:G[u])if(v^F)dfs0(v,u),siz[u]+=siz[v];
}
pii from[N];int fu[N];
bool is[N];
void dfsf(int u,int F)
{//printf("------------dfsf(%d,%d)\n",u,F);
for(int v:G[u])if(v^F)dfsf(v,u);
// printf("=========start proc u:%d\n",u);
// for(int i=1;i<=n;++i)printf("{{%d,%d},%d} ",qmx(dfn[i],dfn[i],1,n,1).s1,qmx(dfn[i],dfn[i],1,n,1).s2,qk(dfn[i],1,n,1).s2);putchar(10);
add(dfn[u]+1,dfn[u]+siz[u]-1,1,1,n,1);
// for(int i=1;i<=n;++i)printf("{{%d,%d},%d} ",qmx(dfn[i],dfn[i],1,n,1).s1,qmx(dfn[i],dfn[i],1,n,1).s2,qk(dfn[i],1,n,1).s2);putchar(10);
if(!is[u])
{
// printf("not special\n");
pii tt=qmx(dfn[u]+1,dfn[u]+siz[u]-1,1,n,1);
// printf("tt:{%d,%d}\n",tt.s1,tt.s2);
if(tt.s2>0)
{
from[u]=qk(tt.s2,1,n,1);fu[u]=rk[tt.s2];
modi(tt.s2,pii(-1e9,0),1,n,1);
modi(dfn[u],pii(0,from[u].s2+from[u].s1),1,n,1);
}
}
else
{
modi(dfn[u],pii(0,0),1,n,1);
}
}
void dfsg(int u,int F)
{//printf("---------------------dfsg(%d,%d)\n",u,F);
pii tnd;
if(fu[u]>0)
{
tnd=qk(dfn[u],1,n,1);
modi(dfn[u],pii(-1e9,0),1,n,1);
modi(dfn[fu[u]],from[u],1,n,1);
}
// printf("state of %d:",u);
// for(int i=1;i<=n;++i)printf("{{%d,%d},%d} ",qmx(dfn[i],dfn[i],1,n,1).s1,qmx(dfn[i],dfn[i],1,n,1).s2,qk(dfn[i],1,n,1).s2);putchar(10);
if(!is[u])ans[u]=sum[1]+mx[1].s1;
else ans[u]=sum[1];
for(int v:G[u])if(v^F)
{
// printf("=====doing edge %d->%d\n",u,v);
pii tt,dat;
if(!is[u])
{
tt=max(qmx(1,dfn[v]-1,1,n,1),qmx(dfn[v]+siz[v],n,1,n,1));
// printf("chosen node %d(%d)\n",rk[tt.s2],tt.s1);
if(tt.s2>0)
{
dat=qk(tt.s2,1,n,1);
modi(tt.s2,pii(-1e9,0),1,n,1);
modi(dfn[u],pii(0,dat.s2+dat.s1),1,n,1);
}
}
apply(1,1,1,n);
add(dfn[v],dfn[v]+siz[v]-1,-2,1,n,1);
dfsg(v,u);
// printf("=====undoing edge %d->%d\n",u,v);
apply(1,-1,1,n);
add(dfn[v],dfn[v]+siz[v]-1,2,1,n,1);
if(!is[u])
{
if(tt.s2>0)
{
modi(tt.s2,dat,1,n,1);
modi(dfn[u],pii(-1e9,0),1,n,1);
}
}
}
if(fu[u]>0)
{
modi(dfn[u],tnd,1,n,1);
modi(dfn[fu[u]],pii(-1e9,0),1,n,1);
}
}
char s[N];
int main()
{
scanf("%d%s",&n,s+1);for(int i=1;i<=n;++i)is[i]=s[i]-'0';
for(int i=1;i<n;++i){int u,v;scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);}
dfs0(1,0);
// printf("dfn:");for(int i=1;i<=n;++i)printf("%d ",dfn[i]);putchar(10);
// printf("siz:");for(int i=1;i<=n;++i)printf("%d ",siz[i]);putchar(10);
// printf("dep:");for(int i=1;i<=n;++i)printf("%d ",dep[i]);putchar(10);
build(1,n,1);
dfsf(1,0);
// printf("after dfsf state:\n");
// for(int i=1;i<=n;++i)printf("{{%d,%d},%d} ",qmx(dfn[i],dfn[i],1,n,1).s1,qmx(dfn[i],dfn[i],1,n,1).s2,qk(dfn[i],1,n,1).s2);putchar(10);
dfsg(1,0);
for(int i=1;i<=n;++i)printf("%lld ",ans[i]);putchar(10);
fclose(stdin);fclose(stdout);return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9940kb
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: -100
Wrong Answer
time: 2ms
memory: 10032kb
input:
1 0
output:
-1000000000
result:
wrong answer 1st numbers differ - expected: '0', found: '-1000000000'