QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#330122 | #5421. Factories Once More | Evier | WA | 101ms | 31372kb | C++14 | 2.0kb | 2024-02-17 12:46:45 | 2024-02-17 12:46:46 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define x first
#define y second
mt19937 rnd(time(0));
inline int read(){
int x=0;bool f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=0;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
if(f)return x;return ~(x-1);
}
int n,k,tot;
vector<pii> g[100005];
int rt[100005];
struct{
int l,r,val,sz;
ll key,a,d;
}t[3000005];
int newn(ll v){
t[++tot]={0,0,rnd(),1,v,0,0};
return tot;
}
void pushup(int p){t[p].sz=t[t[p].l].sz+t[t[p].r].sz+1;}
void mdf(int p,ll a,ll d){
if(!p)return;
t[p].key+=a+d*t[t[p].r].sz;
t[p].a+=a,t[p].d+=d;
}
void pushdown(int p){
if(!t[p].a&&!t[p].d)return;
mdf(t[p].l,t[p].a+(1+t[t[p].r].sz)*t[p].d,t[p].d),mdf(t[p].r,t[p].a,t[p].d);
t[p].a=t[p].d=0;
}
void split(int p,int key,int &x,int &y){
if(!p){
x=y=0;return;
}
pushdown(p);
if(t[p].key<=key)x=p,split(t[p].r,key,t[p].r,y);
else y=p,split(t[p].l,key,x,t[p].l);
pushup(p);
}
int merge(int x,int y){
if(!x||!y)return x|y;
pushdown(x),pushdown(y);
if(t[x].val>t[y].val){t[x].r=merge(t[x].r,y),pushup(x);return x;}
else{t[y].l=merge(x,t[y].l),pushup(y);return y;}
}
void ins(int p,ll v){
int nw=newn(v),x,y;
split(rt[p],v,x,y);
x=merge(x,merge(nw,y));
rt[p]=x;
}
void dfs(int x,int y){
if(!x)return;
ins(y,t[x].key);
pushdown(x);
dfs(t[x].l,y),dfs(t[x].r,y);
}
void calc(int x,int fa){
rt[x]=newn(0);
for(auto i:g[x])if(i.x!=fa){
calc(i.x,x);
mdf(rt[i.x],1ll*(k-1)*i.y,-2ll*i.y);
int a=rt[x],b=rt[i.x];
if(t[a].sz<t[b].sz)swap(a,b);
rt[x]=a,dfs(b,x);
}
}
int rk;
ll ans=0;
void find(int x){
if(!x)return;
pushdown(x);
find(t[x].r),++rk;
if(rk<=k)ans+=t[x].key;
find(t[x].l);
}
int main(){
n=read(),k=read();
for(int i=1;i<n;i++){
int x=read(),y=read(),z=read();
g[x].push_back({y,z}),g[y].push_back({x,z});
}
calc(1,0);
find(rt[1]);
cout<<ans;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 6460kb
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: 1ms
memory: 6984kb
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: 1ms
memory: 7640kb
input:
2 2 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 101ms
memory: 31372kb
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:
1368931416
result:
wrong answer 1st numbers differ - expected: '4915539756', found: '1368931416'