QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#329982#5421. Factories Once MoreDoqeTL 0ms21776kbC++143.4kb2024-02-17 10:16:432024-02-17 10:16:43

Judging History

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

  • [2024-02-17 10:16:43]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:21776kb
  • [2024-02-17 10:16:43]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int n,k;
vector<pair<int,int>>to[N];
int TT,rt[N],stk[N*4],top;
int fa[N*4],son[N*4][2],sz[N*4],w[N*4];
long long val[N*4],tD[N*4],tC[N*4];
inline int NEW(long long x)
{
    int p;if(top)p=stk[top--];else p=++TT;
    fa[p]=son[p][0]=son[p][1]=tD[p]=tC[p]=0,
    w[p]=sz[p]=1,val[p]=x;return p;
}
inline void pu(int p){sz[p]=sz[son[p][0]]+sz[son[p][1]]+w[p];}
inline void TG(int p,long long D,long long C){if(!p)return;val[p]+=C+1ll*D*sz[son[p][0]];tD[p]+=D,tC[p]+=C;}
inline void PD(int p){if(tD[p]&&tC[p])TG(son[p][0],tD[p],tC[p]),TG(son[p][1],tD[p],(sz[son[p][0]]+w[p])*tD[p]+tC[p]),tD[p]=tC[p]=0;}
inline bool gc(int p){return son[fa[p]][1]==p;}
void check(int p,int o=0)
{
    if(!o)cerr<<"CHECK: "<<p<<endl;
    if(!p)return;
    assert(!son[p][0]||fa[son[p][0]]==p);
    assert(!son[p][1]||fa[son[p][1]]==p);
    assert(!son[0][0]&&!son[0][1]);
    check(son[p][0],1),check(son[p][1],1);
}
inline void ro(int p)
{
    int f=fa[p],gf=fa[f],o=gc(p);
    if(gf)son[gf][gc(f)]=p;fa[p]=gf;
    fa[son[f][o]=son[p][!o]]=f;
    fa[son[p][!o]=f]=p;
    pu(f),pu(p);
}
inline void splay(int&RT,int p,int goal=0)
{
    #ifdef DOQ
    cerr<<"SPALY: "<<RT<<" "<<p<<"\n";
    check(RT);
    #endif
    while(fa[p]!=goal)
    {
        int f=fa[p],gf=fa[f];
        if(gf!=goal)ro(gc(p)!=gc(f)?f:p);
        ro(p);
    }
    if(!goal)RT=p;
    #ifdef DOQ
    cerr<<"ED\n";
    #endif
}
inline void ins(int&RT,long long va)
{
    #ifdef DOQ
    cerr<<"INS: "<<RT<<" "<<va<<endl;
    check(RT);
    #endif
    if(!RT){RT=NEW(va);return;}
    int p=RT;
    while(1)
    {
        PD(p);int o=va<val[p];
        // cerr<<p<<" "<<va<<" "<<val[p]<<" "<<o<<" "<<son[p][o]<<endl;
        if(son[p][o])p=son[p][o];
        else
        {
            int x=NEW(va);
            fa[son[p][o]=x]=p;
            return splay(RT,p);
        }
    }
}
inline void del(int p){stk[++top]=p;}
void merge(int&A,int B)
{
    #ifdef DOQ
    cerr<<"merge: "<<A<<" "<<B<<endl;
    #endif
    if(!B)return;PD(B);
    merge(A,son[B][0]);
    ins(A,val[B]);
    merge(A,son[B][1]);
    del(B);
}
int s[N],so[N],W[N];
void dfs(int u,int f)
{
    s[u]=1;
    for(auto k:to[u])
    {
        int v=k.first;if(v==f)continue;
        dfs(v,u),s[u]+=s[v];
        if(s[so[u]]<s[v])so[u]=v,W[u]=k.second;
    }
}
void dfs1(int u,int f)
{
    #ifdef DOQ
    cerr<<"DFS1: "<<u<<" "<<f<<endl;
    #endif
    rt[u]=NEW(0);
    if(!so[u])return;
    dfs1(so[u],u);long long w=W[u];
    TG(rt[so[u]],-2ll*w,1ll*w*(k-1));
    merge(rt[u],rt[so[u]]);
    for(auto o:to[u])
    {
        int v=o.first;if(v==f||v==so[u])continue;
        dfs1(v,u);w=o.second;
        #ifdef DOQ
        cerr<<"_MERGE: "<<u<<" "<<v<<endl;
        #endif
        TG(rt[v],-2ll*w,1ll*w*(k-1));
        #ifdef DOQ
        check(rt[u]);check(rt[v]);
        #endif
        merge(rt[u],rt[v]);
        #ifdef DOQ
        cerr<<"ED\n";
        #endif
    }
}
long long ans[N];
int tt;
void pia(int p)
{
    if(!p)return;pia(son[p][0]);
    ans[++tt]=val[p];pia(son[p][1]);
}
int main()
{
    cin.tie(0)->sync_with_stdio(0);
    cin>>n>>k;
    for(int i=1,u,v,w;i<n;++i)
    {
        cin>>u>>v>>w;
        to[u].emplace_back(v,w);
        to[v].emplace_back(u,w);
    }
    dfs(1,0);dfs1(1,0);pia(rt[1]);
    for(int i=1;i<=k;++i)ans[i]+=ans[i-1];
    cout<<ans[k]<<"\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 21776kb

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: 0ms
memory: 17820kb

input:

2 2
1 2 1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Time Limit Exceeded

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:


result: