QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#408862 | #6538. Lonely King | Doqe | WA | 1ms | 7720kb | C++23 | 1.6kb | 2024-05-11 09:57:15 | 2024-05-11 09:57:16 |
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
{
int tg;Line val;
}tr[M];
void TG(int p,int 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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5740kb
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: 7720kb
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: -100
Wrong Answer
time: 1ms
memory: 5612kb
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:
-1316500620
result:
wrong answer 1st numbers differ - expected: '150134230018', found: '-1316500620'