QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#491739 | #6538. Lonely King | totest | WA | 3ms | 20060kb | C++14 | 3.5kb | 2024-07-25 21:40:23 | 2024-07-25 21:40:24 |
Judging History
answer
#include<bits/stdc++.h>
#define int ll
#define ll long long
#define fr(i,j,k) for(register int i=j;i<=k;++i)
#define rf(i,j,k) for(register int i=j;i>=k;--i)
#define foredge(i,j) for(register int i=head[j];i;i=e[i].nxt)
#define pb push_back
#define Times printf("Time:%.3lf\n",clock()/CLOCKS_PER_SEC)
#define pii pair<int,int>
#define mk make_pair
using namespace std;
inline int read(){
int x=0;
bool f=0;
char c=getchar();
while(!isdigit(c)) f|=(c=='-'),c=getchar();
while(isdigit(c)) x=(x<<3)+(x<<1)+(c^48),c=getchar();
return f?-x:x;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void writeln(int x){write(x); puts("");}
inline void writepl(int x){write(x); putchar(' ');}
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
inline int randfind(int l,int r){return rnd()%(r-l+1)+l;}
//inline void init(){
// int t=read();
// while(t--) work();
//}
const int Maxn=2e5+10,inf=1e18,N=1e6;
struct line{int k,b;}ln[Maxn];
struct node{int id,ls,rs,tag;};
inline int getval(int id,int x){return ln[id].k*x+ln[id].b;}
inline bool chk(int id1,int id2,int x){return getval(id1,x)<getval(id2,x);}
int vis[Maxn];
namespace Tree{
node e[Maxn*60];
inline void change(int x,int k){
if(!x) return;
ln[x].b+=k;e[x].tag+=k;
}
inline void pushdown(int x){
if(!e[x].tag) return;
change(e[x].ls,e[x].tag);change(e[x].rs,e[x].tag);
e[x].tag=0;
}
int nod;
inline int update(int x,int l,int r,int id){
if(!x) x=++nod;
pushdown(x);
if(!e[x].id){e[x].id=id;vis[id]=1;return x;}
int mid;mid=(l+r)>>1;
if(chk(id,e[x].id,mid)) swap(id,e[x].id),swap(vis[id],vis[e[x].id]);
if(l==r) return x;
if(chk(id,e[x].id,l)) e[x].ls=update(e[x].ls,l,mid,id);
if(chk(id,e[x].id,r)) e[x].rs=update(e[x].rs,mid+1,r,id);
return x;
}
inline void del(int x){
if(!x) return;
pushdown(x);
del(e[x].ls);del(e[x].rs);
}
inline int query(int k,int l,int r,int x){
if(!x) return inf;
pushdown(x);
int ans=getval(e[x].id,k);
if(l==r) return ans;
int mid;mid=(l+r)>>1;
if(k<=mid) ans=min(ans,query(k,l,mid,e[x].ls));
else ans=min(ans,query(k,mid+1,r,e[x].rs));
return ans;
}
}
vector<int> vc[Maxn];
int rt[Maxn];
int idx,dfn[Maxn],siz[Maxn],son[Maxn];
inline void dfs(int x){
dfn[x]=inf;
if(vc[x].empty()) dfn[x]=++idx,siz[x]=1;
for(auto y:vc[x]){
dfs(y);
siz[x]+=siz[y];dfn[x]=min(dfn[x],dfn[y]);
if(siz[son[x]]<siz[y] || !son[x]) son[x]=y;
}
}
int Ans[Maxn];
int c[Maxn];
inline void dp(int x){
if(!son[x]){
ln[dfn[x]]=(line){c[x],0};
rt[x]=Tree::update(rt[x],1,N,dfn[x]);
return;
}
int s=0;
for(auto y:vc[x]){
dp(y);
Ans[y]=Tree::query(c[x],1,N,rt[y]);
s+=Ans[y];
}
// cout<<"*"<<x<<' '<<s<<' '<<Ans[son[x]]<<endl;
for(auto y:vc[x]) Tree::change(rt[y],s-Ans[y]);
for(auto y:vc[x]) if(y!=son[x]) Tree::del(y);
// Tree::change(rt[son[x]],s-Ans[son[x]]);
rt[x]=rt[son[x]];
for(auto y:vc[x]){
if(y==son[x]) continue;
fr(j,dfn[y],dfn[y]+siz[y]-1) if(vis[j]) vis[j]=0,rt[x]=Tree::update(rt[x],1,N,j);
}
}
int n;
inline void init(){
n=read();
fr(i,2,n) vc[read()].pb(i);
fr(i,1,n) c[i]=read();
}
inline void work(){
dfs(1);dp(1);
// cout<<siz[1]<<endl;
writeln(Tree::query(c[1],1,N,rt[1]));
}
signed main(){
// freopen("input.in","r",stdin);
// freopen("output.out","w",stdout);
init();work();
// printf("\nTIME:%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 18044kb
input:
4 1 1 2 2 1 3 2
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 20060kb
input:
50 1 2 1 1 2 1 6 3 7 5 11 11 8 10 7 8 9 7 17 2 18 4 23 8 17 21 3 19 2 4 21 18 1 26 21 36 26 24 7 7 29 27 19 29 36 11 29 42 21 15 31 15 40 15 33 2 33 15 6 50 48 33 6 43 36 19 37 28 32 47 50 8 26 50 44 50 31 32 44 22 15 46 11 33 38 22 27 43 29 8 1 21 31 28 26 39 29 39 42
output:
90
result:
wrong answer 1st numbers differ - expected: '22728', found: '90'