QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#408865 | #6538. Lonely King | Doqe | RE | 2ms | 7716kb | C++23 | 1.6kb | 2024-05-11 09:58:53 | 2024-05-11 09:58:54 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10,M=1e7+10,W=1e6;
vector<int>to[N];
int fa[N],v[N],tt,n;
long long ans=0;
int rt[N],ls[M],rs[M];
struct Line
{
long long k,b;
long long at(int x){return k*x+b;}
};
struct node
{
long long tg;Line val;
}tr[M];
void TG(int p,long long v){if(p)tr[p].tg+=v,tr[p].val.b+=v;}
void PD(int p){if(tr[p].tg)TG(ls[p],tr[p].tg),TG(rs[p],tr[p].tg),tr[p].tg=0;}
void mdf(int&p,int l,int r,Line x)
{
if(!p){tr[p=++tt]={0,x};return;}
auto&Z=tr[p].val;
if(l==r){if(Z.at(l)>x.at(l))tr[p].val=x;return;}
int mid=l+r>>1;PD(p);
if(Z.at(mid)>x.at(mid))swap(Z,x);
if(Z.at(l)>x.at(l))mdf(ls[p],l,mid,x);
if(Z.at(r)>x.at(r))mdf(rs[p],mid+1,r,x);
}
long long qry(int p,int l,int r,int x)
{
if(!p)return 1e18;
if(l==r)return tr[p].val.at(x);
int mid=l+r>>1;PD(p);
if(x<=mid)return min(tr[p].val.at(x),qry(ls[p],l,mid,x));
return min(tr[p].val.at(x),qry(rs[p],mid+1,r,x));
}
int merge(int x,int y,int l,int r)
{
if(!x||!y)return x|y;
mdf(x,l,r,tr[y].val);
if(l==r)return x;
int mid=l+r>>1;PD(x),PD(y);
ls[x]=merge(ls[x],ls[y],l,mid);
rs[x]=merge(rs[x],rs[y],mid+1,r);
return x;
}
void dfs(int u,int f)
{
if(!to[u].size())return mdf(rt[u],1,W,{v[u],0});
int w=0;
for(int x:to[u])
{
dfs(x,u);
if(!w)rt[u]=rt[x],w=1;
else
{
long long v1=qry(rt[u],1,W,v[u]),v2=qry(rt[x],1,W,v[u]);
TG(rt[u],v2),TG(rt[x],v1);rt[u]=merge(rt[u],rt[x],1,W);
}
}
}
int main()
{
cin>>n;if(n==1)return puts("0"),0;
for(int i=2;i<=n;++i)cin>>fa[i],to[fa[i]].push_back(i);
for(int i=1;i<=n;++i)cin>>v[i];
dfs(1,0);
cout<<qry(rt[1],1,W,v[1])<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5688kb
input:
4 1 1 2 2 1 3 2
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: 0
Accepted
time: 1ms
memory: 7596kb
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:
22728
result:
ok 1 number(s): "22728"
Test #3:
score: 0
Accepted
time: 2ms
memory: 7716kb
input:
500 1 1 2 4 3 1 7 2 8 10 8 12 1 7 11 9 14 18 1 17 9 1 16 17 6 14 17 1 26 25 26 29 6 8 7 15 32 9 27 11 34 31 35 6 25 4 35 40 12 2 39 34 21 8 48 8 49 1 39 32 30 46 10 1 45 29 2 17 31 22 30 16 59 10 63 15 71 53 28 50 46 29 59 53 5 3 5 83 48 50 39 18 24 76 6 65 28 72 81 38 54 8 35 88 89 89 18 99 9 99 76...
output:
150134230018
result:
ok 1 number(s): "150134230018"
Test #4:
score: 0
Accepted
time: 0ms
memory: 5712kb
input:
1000 1 1 2 2 4 2 7 3 8 1 9 4 4 4 4 15 3 12 12 6 21 10 20 19 14 23 3 24 26 26 25 31 2 25 28 27 22 38 29 37 16 26 40 5 7 4 25 38 18 41 47 45 14 53 45 18 18 5 10 26 24 59 31 27 58 59 20 47 58 38 29 34 67 68 42 51 44 4 79 7 45 8 73 82 36 51 38 44 77 80 70 21 15 76 40 82 60 61 17 94 46 1 46 25 83 56 57 1...
output:
291482220072
result:
ok 1 number(s): "291482220072"
Test #5:
score: 0
Accepted
time: 1ms
memory: 5692kb
input:
20 1 1 2 3 2 6 1 5 4 7 5 7 9 11 6 7 9 12 4 78702 90933 62714 66603 17557 48708 96257 69094 34701 81307 3224 81752 2616 58113 86806 5963 73405 5059 66996 97390
output:
34002240599
result:
ok 1 number(s): "34002240599"
Test #6:
score: -100
Runtime Error
input:
200000 1 2 2 3 1 5 5 8 3 3 1 1 5 13 3 10 15 18 13 5 3 12 18 15 8 8 21 13 4 5 15 8 27 24 20 17 8 26 4 21 36 32 36 42 25 41 25 8 22 37 51 13 26 6 41 26 28 41 13 51 37 35 1 35 28 25 66 14 49 40 24 46 38 20 8 6 15 35 40 2 26 57 65 53 51 17 23 41 14 17 80 54 74 7 32 43 13 49 29 16 17 13 45 51 51 18 20 75...