QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#883619#10040. Taxilgvc#WA 186ms29564kbC++233.5kb2025-02-05 17:26:402025-02-05 17:26:41

Judging History

This is the latest submission verdict.

  • [2025-02-05 17:26:41]
  • Judged
  • Verdict: WA
  • Time: 186ms
  • Memory: 29564kb
  • [2025-02-05 17:26:40]
  • Submitted

answer

#include <bits/stdc++.h>
#define LL long long
#define int long long
#define INF_LL 0x3f3f3f3f3f3f3f3f
int N,hd[100009],to[200009],nxt[200009],val[200009],k,siz[100009],son[100009]
,top[100009],pa[100009],pa2[100009],v[100009],dep2[100009];
LL dep[100009],xx,yy;
void dfs(int n,int f) {
    siz[n]=1;
    pa2[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) {
        bool zz=0;
        LL vv=v+1ll*sl*dis(x,n);
        while(tp[n].size()>=1) {
            int sz=tp[n].size();
            if(tp[n][sz-1].b==sl&&tp[n][sz-1].a<vv) {
                zz=1;
                break;
            }
            if(tp[n][sz-1].b==sl&&tp[n][sz-1].a>=vv) {
                tp[n].pop_back();
                continue;
            }
            if(sz<2) break;
            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();
        }
        if(!zz) {
            tp[n].push_back((n_t){vv,sl,0});
        }
        n=pa[n];
    }
}
LL qr(int n) {
    int x=n;
    LL ans=xx+dep[n]*yy;
    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("%lld",&N);
    for(int i=1;i<=N;i++) {
        scanf("%lld",&f[i].a);
    }
    for(int i=1;i<=N;i++) {
        scanf("%lld",&f[i].b);
        f[i].i=i;
    }
    xx=f[1].a;yy=f[1].b;
    for(int i=1;i<N;i++) {
        int u,v,w;
        scanf("%lld %lld %lld",&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: 0ms
memory: 12124kb

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: 1ms
memory: 14172kb

input:

2
353 313
928248 475634
2 1 898027

output:

833591767049 

result:

ok 1 number(s): "833591767049"

Test #3:

score: 0
Accepted
time: 0ms
memory: 16224kb

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
Wrong Answer
time: 186ms
memory: 29564kb

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...

output:

3277802359022 2545130433939 3318461549924 3398407067031 3408186962422 2313225494349 3168327995012 2617382759540 3301586552149 3225856621706 3427809794287 3299508953261 1420667518950 2769397798952 3326967926930 2360145250829 3397909900620 3194367645638 3421664513579 3443120490387 2774846467496 341107...

result:

wrong answer 1st numbers differ - expected: '2046893410201', found: '3277802359022'