QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#883493 | #10040. Taxi | lgvc# | TL | 1ms | 10068kb | C++23 | 3.1kb | 2025-02-05 16:38:18 | 2025-02-05 16:38:20 |
Judging History
answer
#include <bits/stdc++.h>
#define LL long long
#define INF_LL 0x3f3f3f3f3f3f3f3f
int N,hd[100009],to[200009],nxt[200009],val[200009],k,siz[100009],son[100009],
dfn[100009],top[100009],pa[100009],pa2[100009],kk,v[100009],dep2[100009];
LL dep[100009];
void dfs(int n,int f) {
siz[n]=1;
pa2[n]=f;
pa[n]=f;
dep2[n]=dep2[f]+1;
for(int i=hd[n];i;i=nxt[i]) {
if(to[i]==f) continue;
dep[to[i]]=dep[n]+val[i];
dfs(to[i],n);
siz[n]+=siz[to[i]];
if(siz[son[n]]<siz[to[i]]) son[n]=to[i];
}
}
void dfs2(int n,int tp) {
top[n]=tp;
if(!son[n]) return;
dfs2(son[n],tp);
for(int i=hd[n];i;i=nxt[i]) {
if(to[i]==pa2[n]||to[i]==son[n]) continue;
dfs2(to[i],to[i]);
}
}
void l(int u,int v,int w) {
to[++k]=v;nxt[k]=hd[u];val[k]=w;hd[u]=k;
}
inline LL dis(int x,int y) {
LL as=dep[x]+dep[y];
while(top[x]!=top[y]) {
if(dep2[top[x]]<dep2[top[y]]) std::swap(x,y);
x=pa2[top[x]];
}
if(dep2[x]<dep2[y]) std::swap(x,y);
return as-2*dep[y];
}
struct n_t{
LL a;int b,i;
} f[100009];
std::vector<n_t> tp[100009];
bool cmp(n_t x,n_t y) {
return x.b>y.b;
}
void up(int n,LL v,int sl) {
int x=n;
while(n) {
LL vv=v+1ll*sl*dis(x,n);
while(tp[n].size()>=2) {
int sz=tp[n].size();
if((tp[n][sz-2].a-tp[n][sz-1].a)*(tp[n][sz-1].b-sl)>
(tp[n][sz-1].a-vv)*(tp[n][sz-2].b-tp[n][sz-1].b)) {
break;
}
tp[n].pop_back();
}
tp[n].push_back((n_t){vv,sl,0});
n=pa[n];
}
}
LL qr(int n) {
int x=n;
LL ans=f[1].a+dep[n]*f[1].b;
while(n) {
if(tp[n].size()) {
LL va=dis(x,n);
int l=0,r=tp[n].size()-1,md;
while(l<r) {
md=(l+r)/2;
if((tp[n][md].a-tp[n][md+1].a)>-va*(tp[n][md].b-tp[n][md+1].b)) {
l=md+1;
} else {
r=md;
}
}
ans=std::min(ans,tp[n][l].a+tp[n][l].b*va);
}
n=pa[n];
}
return ans;
}
void cl(int n,int f) {
siz[n]=1;son[n]=0;
for(int i=hd[n];i;i=nxt[i]) {
if(to[i]==f||v[to[i]]) continue;
cl(to[i],n);
siz[n]+=siz[to[i]];
if(siz[son[n]]<siz[to[i]]) son[n]=to[i];
}
}
void sv(int n,int ff) {
cl(n,0);
int x=n;
while(son[x]&&siz[son[x]]>=(siz[n]+1)/2) {
x=son[x];
}
n=x;
// pa[n]=ff;
v[n]=1;
for(int i=hd[n];i;i=nxt[i]) {
if(!v[to[i]]) sv(to[i],n);
}
}
signed main(void) {
scanf("%d",&N);
for(int i=1;i<=N;i++) {
scanf("%lld",&f[i].a);
}
for(int i=1;i<=N;i++) {
scanf("%d",&f[i].b);
f[i].i=i;
}
for(int i=1;i<N;i++) {
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
l(u,v,w),l(v,u,w);
}
dfs(1,0);
dfs2(1,1);
sv(1,0);
std::sort(f+1,f+N+1,cmp);
for(int i=1;i<=N;i++) {
up(f[i].i,qr(f[i].i)+f[i].a,f[i].b);
}
for(int i=2;i<=N;i++) printf("%lld ",qr(i));
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 8032kb
input:
3 0 1 2 8 4 4 1 2 1 1 3 7
output:
8 41
result:
ok 2 number(s): "8 41"
Test #2:
score: 0
Accepted
time: 0ms
memory: 8036kb
input:
2 353 313 928248 475634 2 1 898027
output:
833591767049
result:
ok 1 number(s): "833591767049"
Test #3:
score: 0
Accepted
time: 1ms
memory: 10068kb
input:
8 24 33 35 37 7 18 44 22 10 10 8 5 2 10 1 8 4 8 5 2 8 4 3 1 3 8 1 2 8 6 5 7 2 3 5 8 3
output:
84 54 94 74 94 101 44
result:
ok 7 numbers
Test #4:
score: -100
Time Limit Exceeded
input:
70000 4143803 235079 3104014 1324928 817104 4389177 1003681 70761 3892615 3597920 2342949 1233648 113802 1035288 657102 3005375 3502514 3873335 2951413 1689927 3076977 2125807 4040868 3145202 2971761 617952 3798745 3121839 2249402 3007573 766828 4128210 4559210 2176972 1598850 3221715 355381 3869924...