QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#736280 | #5421. Factories Once More | eastcloud | TL | 8ms | 60436kb | C++17 | 3.0kb | 2024-11-12 09:18:20 | 2024-11-12 09:18:20 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define pi pair<ll,ll>
#define vi vector<ll>
#define cpy(x,y,s) memcpy(x,y,sizeof(x[0])*(s))
#define mset(x,v,s) memset(x,v,sizeof(x[0])*(s))
#define all(x) begin(x),end(x)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ary array
#define eb emplace_back
#define N 500005
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9')f=(ch=='-'?-1:f),ch=getchar();
while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
void write(ll x){
if(x<0)x=-x,putchar('-');
if(x/10)write(x/10);
putchar(x%10+'0');
}
mt19937 rnd(20080623);
ll rt[N];
struct tg{
ll k,b;
tg(ll k=0,ll b=0):k(k),b(b){}
};
struct fhqtreap{
ll ls[N<<2],rs[N<<2],pri[N<<2],v[N<<2],siz[N<<2];
tg tag[N<<2];
ll tot;
stack<ll> st;
ll newnode(ll w){
ll pos=(st.size()?st.top():++tot);
if(st.size())st.pop();
tag[pos].k=tag[pos].b=0;ls[pos]=rs[pos]=0;
v[pos]=w;pri[pos]=rnd();siz[pos]=1;return pos;
}
void ps(ll x,tg T){
ll num=siz[ls[x]]+1;
v[x]+=T.k*num+T.b;tag[x].k+=T.k;tag[x].b+=T.b;
}
void pd(ll x){
tg T=tag[x];
if(ls[x])ps(ls[x],T);
if(rs[x])ps(rs[x],tg(T.k,T.b+(siz[ls[x]]+1)*T.k));
tag[x].k=tag[x].b=0;
}
void upd(ll x){siz[x]=siz[ls[x]]+siz[rs[x]]+1;}
ll merge(ll L,ll R){
if(!L || !R)return L|R;
pd(L);pd(R);
if(pri[L]>pri[R]){rs[L]=merge(rs[L],R);upd(L);return L;}
else {ls[R]=merge(L,ls[R]);upd(R);return R;}
}
void split(ll x,ll val,ll &L,ll &R){
if(!x)return L=0,R=0,void();pd(x);
if(val>v[x]){R=x;split(ls[x],val,L,ls[x]);upd(x);}
else {L=x;split(rs[x],val,rs[x],R);upd(x);}
}
void insert(ll &p,ll x){
ll L,R;split(p,v[x],L,R);
L=merge(L,x);
p=merge(L,R);
}
void flat(ll x,vector<ll> &res){
pd(x);st.push(x);
if(ls[x])flat(ls[x],res);
res.pb(v[x]);
if(rs[x])flat(rs[x],res);
}
}T;
ll siz[N],n,k;
vector<pi> e[N];
void dfs(ll x,ll fa){
siz[x]=1;ll son=0;
for(auto _:e[x]){
ll v=_.fi,w=_.se;
if(v==fa)continue;dfs(v,x);
if(siz[v]>siz[son])son=v;
T.ps(rt[v],tg(-2*w,w*(k+1)));
}
if(son)rt[x]=rt[son];
T.insert(rt[x],T.newnode(0));
for(auto _:e[x]){
ll v=_.fi,w=_.se;
if(v==fa || v==son)continue;
vector<ll> s;
T.flat(rt[v],s);
for(auto w:s)T.insert(rt[x],T.newnode(w));
}
}
int main(){
#ifdef EAST_CLOUD
freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
#endif
n=read(),k=read();
for(ll i=1;i<n;i++){
ll u=read(),v=read(),w=read();
e[u].eb(v,w);e[v].eb(u,w);
}
dfs(1,0);
vector<ll> res;
T.flat(rt[1],res);
ll ans=0;
for(ll i=0;i<k;i++)ans+=res[i];
write(ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 60436kb
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: 8ms
memory: 59036kb
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: 8ms
memory: 48128kb
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...