QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#190624#5421. Factories Once MoreSoyTonyWA 50ms28524kbC++145.9kb2023-09-29 08:13:452023-09-29 08:13:46

Judging History

你现在查看的是最新测评结果

  • [2023-09-29 08:13:46]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:28524kb
  • [2023-09-29 08:13:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=1e5+10;
const int maxm=2e6+10;
const ll llinf=0x3f3f3f3f3f3f3f3f;

inline int read(){
    int x=0,w=1;char c=getchar();
    while(c<'0'||c>'9'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-'0';c=getchar();}
    return x*w;
}

int n,k;
struct edge{
    int to,nxt,w;
}e[maxn<<1];
int head[maxn],cnt;
inline void add_edge(int u,int v,int w){
    e[++cnt].to=v,e[cnt].nxt=head[u],head[u]=cnt,e[cnt].w=w;
    e[++cnt].to=u,e[cnt].nxt=head[v],head[v]=cnt,e[cnt].w=w;
}

ll ans[maxn];
int p;

struct Splay{
    int tot,Q[maxm],head,tail;
    int fa[maxm],ch[maxm][2];
    int cnt[maxm],siz[maxm];
    ll val[maxm];
    ll tagD[maxm],tagC[maxm];
    Splay(){
        head=1,tail=0;
    }
    inline int new_node(ll k){
        int x;
        if(head<=tail){
            x=Q[head++];
            if(head>tail) head=1,tail=0;
        }
        else x=++tot;
        fa[x]=0,ch[x][0]=ch[x][1]=0,cnt[x]=1,siz[x]=1,val[x]=k,tagD[x]=0,tagC[x]=0;
        if(k==llinf||k==-llinf) cnt[x]=0,siz[x]=0;
        return x;
    }
    inline void push_up(int x){
        siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];
    }
    inline void push_tag(int x,int D,int C){
        if(cnt[x]) val[x]+=1ll*D*siz[ch[x][0]]+C;
        tagD[x]+=D,tagC[x]+=C;
    }
    inline void push_down(int x){
        if(tagD[x]){
            if(ch[x][0]) push_tag(ch[x][0],tagD[x],tagC[x]);
            if(ch[x][1]) push_tag(ch[x][1],tagD[x],1ll*(siz[ch[x][0]]+cnt[x])*tagD[x]+tagC[x]);
            tagD[x]=0,tagC[x]=0;
        }
    }
    inline int build(){
        int x=new_node(0),y=new_node(llinf),z=new_node(-llinf);
        fa[y]=x,ch[x][0]=y,fa[z]=x,ch[x][1]=z;
        push_up(x);
        return x;
    }
    inline bool chkson(int x){
        return ch[fa[x]][1]==x;
    }
    inline void rotate(int x){
        int y=fa[x],z=fa[y];
        bool chk=chkson(x);
        if(z) ch[z][chkson(y)]=x;
        fa[x]=z;
        ch[y][chk]=ch[x][chk^1],fa[ch[x][chk^1]]=y;                                                                                                                                
        ch[x][chk^1]=y,fa[y]=x;
        push_up(y),push_up(x);
    }
    inline void splay(int &rt,int x,int goal=0){
        while(fa[x]!=goal){
            int y=fa[x],z=fa[y];
            if(z!=goal) rotate(chkson(x)==chkson(y)?y:x);
            rotate(x);
        }
        if(!goal) rt=x;
    }
    inline void insert(int &rt,ll k){
        // cerr<<k<<endl;
        int x=rt;
        while(1){
            push_down(x);
            if(k>=val[x]){
                if(ch[x][0]) x=ch[x][0];
                else{
                    int y=new_node(k);
                    fa[y]=x,ch[x][0]=y;
                    splay(rt,y);
                    // output1(rt);
                    // cerr<<endl;
                    return;
                }
            }
            else{
                if(ch[x][1]) x=ch[x][1];
                else{
                    int y=new_node(k);
                    fa[y]=x,ch[x][1]=y;
                    splay(rt,y);
                    // output1(rt);
                    // cerr<<endl;
                    return;
                }
            }
        }
    }
    inline void erase(int x){
        Q[++tail]=x;
        fa[x]=0,ch[x][0]=ch[x][1]=0,val[x]=0,tagD[x]=0,tagC[x]=0;
    }
    void merge(int &rt,int x){
        // cerr<<"x:"<<x<<" val:"<<val[x]<<endl;
        push_down(x);
        if(ch[x][0]) merge(rt,ch[x][0]);
        if(cnt[x]) insert(rt,val[x]);
        if(ch[x][1]) merge(rt,ch[x][1]);
        erase(x);
    }
    void dfs(int x){
        push_down(x);
        if(ch[x][0]) dfs(ch[x][0]);
        if(cnt[x]) ans[++p]=val[x];
        if(ch[x][1]) dfs(ch[x][1]);
    }
    void output1(int x){
        push_down(x);
        if(ch[x][0]) output1(ch[x][0]);
        cerr<<val[x]<<" ";
        if(ch[x][1]) output1(ch[x][1]);
    }
}S;

int siz[maxn],son[maxn];
int rt[maxn];
void dfs1(int u,int fa){
    siz[u]=1;
    int maxson=-1;
    for(int i=head[u],v;i;i=e[i].nxt){
        v=e[i].to;
        if(v==fa) continue;
        dfs1(v,u);
        siz[u]+=siz[v];
        if(siz[v]>maxson) son[u]=v,maxson=siz[v];
    }
}
void dfs2(int u,int fa){
    rt[u]=S.build();
    // cerr<<"u:"<<u<<endl;
    // S.output1(rt[u]);
    // cerr<<endl;
    if(!son[u]) return;
    int tmp;
    for(int i=head[u],v,w;i;i=e[i].nxt){
        v=e[i].to,w=e[i].w;
        if(v==son[u]) tmp=w;
    }
    dfs2(son[u],u);
    // cerr<<"u:"<<u<<" son:"<<son[u]<<endl;
    S.push_tag(rt[son[u]],-2ll*tmp,1ll*tmp*(k-1));
    // cerr<<"D:"<<-2ll*tmp<<" C:"<<1ll*tmp*(k-1)<<endl;
    // cerr<<"son:"<<son[u]<<endl;
    S.merge(rt[son[u]],rt[u]);
    rt[u]=rt[son[u]];
    // S.output1(rt[u]);
    // cerr<<endl;
    for(int i=head[u],v,w;i;i=e[i].nxt){
        v=e[i].to,w=e[i].w;
        if(v==fa||v==son[u]) continue;
        dfs2(v,u);
        S.push_tag(rt[v],-2ll*w,1ll*w*(k-1));
        S.merge(rt[u],rt[v]);
        // cerr<<"u:"<<u<<" v:"<<v<<endl;
        // S.output1(rt[u]);
        // cerr<<endl;
    }
    // cerr<<"u:"<<u<<endl;
    // S.output1(rt[u]);
    // cerr<<endl;
}

int main(){
    // freopen("h.in","r",stdin);
    // freopen("h.out","w",stdout);
    n=read(),k=read();
    for(int i=1,u,v,w;i<n;++i){
        u=read(),v=read(),w=read();
        add_edge(u,v,w);
    }
    dfs1(1,0);
    dfs2(1,0);
    S.dfs(rt[1]);
    // for(int i=1;i<=n;++i) cerr<<ans[i]<<" ";
    // cerr<<endl;
    for(int i=1;i<=k;++i) ans[i]+=ans[i-1];
    printf("%lld\n",ans[k]);
    return 0;
}


/*
f(u,i+j) <-- f(u,i) + f(v,j) + w(u,v)*j*(k-j) 

w(u,v)[j(k-j)-(j-1)(k-j+1)]

g(x)=x(k-x) \Delta g(x)=(xk-x^2)-(xk-x^2+x-k+x-1)=-2x+(k+1)

\Delta g(1)=k-1 \Delta^2 g(x)=-2

a_1=w(u,v)(k-1),d=-2w(u,v)
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 18172kb

input:

6 3
1 2 3
2 3 2
2 4 1
1 5 2
5 6 3

output:

22

result:

ok 1 number(s): "22"

Test #2:

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

input:

4 3
1 2 2
1 3 3
1 4 4

output:

18

result:

ok 1 number(s): "18"

Test #3:

score: 0
Accepted
time: 3ms
memory: 20156kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 50ms
memory: 28524kb

input:

100000 17
37253 35652 9892
56367 53643 1120
47896 49255 4547
93065 88999 1745
5251 6742 5031
49828 50972 8974
31548 46729 1032
56341 56287 4812
21896 22838 1682
82124 90557 7307
76289 76949 7028
33834 45380 6856
15499 15064 2265
10127 5251 9920
87208 93945 9487
68990 72637 6891
91640 85004 2259
4748...

output:

62486728062

result:

wrong answer 1st numbers differ - expected: '4915539756', found: '62486728062'