QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#210649 | #6538. Lonely King | ucup-team1004 | RE | 3ms | 15032kb | C++14 | 2.5kb | 2023-10-11 18:10:43 | 2023-10-11 18:10:43 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
if(x.empty())return out<<"[]";
out<<'['<<x[0];
for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
cerr<<x<<' ',debug(y...);
}
const int N=2e5+10,K=N*22,V=1e6;
const ll INF=1e18;
int n,a[N];
vector<int>to[N];
ll calc(ll k,ll b,int x){
return k*x+b;
}
struct tree{
int ls,rs;
ll k,b,laz;
tree(){
ls=rs=k=laz=0,b=INF;
}
}t[N];
int k,root[N];
void pushadd(int rt,ll x){
if(rt)t[rt].b+=x,t[rt].laz+=x;
}
void pushdown(int rt){
if(t[rt].laz){
pushadd(t[rt].ls,t[rt].laz);
pushadd(t[rt].rs,t[rt].laz);
t[rt].laz=0;
}
}
void insert(int &rt,ll k,ll b,int l=1,int r=V){
if(!rt)rt=++::k;
int mid=(l+r)>>1;
if(calc(k,b,mid)<calc(t[rt].k,t[rt].b,mid))swap(k,t[rt].k),swap(b,t[rt].b);
pushdown(rt);
if(calc(k,b,l)<calc(t[rt].k,t[rt].b,l))insert(t[rt].ls,k,b,l,mid);
if(calc(k,b,r)<calc(t[rt].k,t[rt].b,r))insert(t[rt].rs,k,b,mid+1,r);
}
ll query(int rt,int x,int l=1,int r=V){
if(!rt)return INF;
ll val=calc(t[rt].k,t[rt].b,x);
if(l==r)return val;
int mid=(l+r)>>1;
pushdown(rt);
if(x<=mid)return min(val,query(t[rt].ls,x,l,mid));
else return min(val,query(t[rt].rs,x,mid+1,r));
}
void merge(int &x,int y,int l=1,int r=V){
if(!x||!y){
x|=y;return;
}
if(l==r){
if(calc(t[x].k,t[x].b,l)>calc(t[y].k,t[y].b,l))swap(x,y);
return;
}
int mid=(l+r)>>1;
insert(x,t[y].k,t[y].b,l,r);
pushdown(x),pushdown(y);
merge(t[x].ls,t[y].ls,l,mid);
merge(t[x].rs,t[y].rs,mid+1,r);
}
void print(int rt){
if(!rt)return;
pushdown(rt);
fprintf(stderr," (%lld,%lld)",t[rt].k,t[rt].b);
print(t[rt].ls);
print(t[rt].rs);
}
void dfs(int u){
if(to[u].empty()){
insert(root[u],a[u],0);
// debug(u,a[u],t[root[u]].k,t[root[u]].b);
// cerr<<u,print(root[u]),cerr<<endl;
return;
}
for(int v:to[u]){
dfs(v);
if(!root[u])root[u]=root[v];
else{
ll x=query(root[u],a[u]),y=query(root[v],a[u]);
pushadd(root[u],y),pushadd(root[v],x);
merge(root[u],root[v]);
}
}
// cerr<<u,print(root[u]),cerr<<endl;
}
int main(){
scanf("%d",&n);
for(int i=2,x;i<=n;i++){
scanf("%d",&x),to[x].push_back(i);
}
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
dfs(1);
cout<<query(root[1],a[1])<<endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14584kb
input:
4 1 1 2 2 1 3 2
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: 0
Accepted
time: 3ms
memory: 14676kb
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: 3ms
memory: 14320kb
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: 3ms
memory: 15032kb
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: 0ms
memory: 14440kb
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...