QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#334611 | #5421. Factories Once More | Skyjoy | TL | 1ms | 5896kb | C++14 | 2.7kb | 2024-02-22 09:19:19 | 2024-02-22 09:19:20 |
Judging History
answer
#include<bits/stdc++.h>
#define I using
#define love namespace
#define Elaina std
#define ll long long
I love Elaina;
const int N=100010;
ll read(){
ll x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<3)+(x<<1)+ch-'0';
ch=getchar();
}
return x*f;
}
ll n,m,x,y,z,head[N],cnt,ans;
struct edge{
int to,nxt;
ll val;
}e[N<<1];
void addedge(int u,int v,ll w){e[++cnt].to=v,e[cnt].nxt=head[u],e[cnt].val=w,head[u]=cnt;}
namespace AyaseEli{
int root[N],idx;
ll fa[N<<2],val[N<<2],siz[N<<2],ch[N<<2][2],tag1[N<<2],tag2[N<<2];
vector<ll>stk;
void clear(int k){fa[k]=val[k]=siz[k]=ch[k][0]=ch[k][1]=tag1[k]=tag2[k]=0;}
void pushup(int k){siz[k]=siz[ch[k][0]]+siz[ch[k][1]]+1;}
void pushdown(int k){
if(ch[k][0])val[ch[k][0]]+=tag1[k]+(siz[ch[ch[k][0]][0]]+1)*tag2[k],tag1[ch[k][0]]+=tag1[k],tag2[ch[k][0]]+=tag2[k];
if(ch[k][1])val[ch[k][1]]+=tag1[k]+(siz[ch[k][0]]+siz[ch[ch[k][1]][0]]+2)*tag2[k],tag1[ch[k][1]]+=tag1[k]+(siz[ch[k][0]]+1)*tag2[k],tag2[ch[k][1]]+=tag2[k];
tag1[k]=tag2[k]=0;
}
void rotate(int x){
int y=fa[x],z=fa[y];
int k=x==ch[y][1];
ch[y][k]=ch[x][k^1];
if(ch[x][k^1])fa[ch[x][k^1]]=y;
ch[x][k^1]=y,fa[y]=x,fa[x]=z;
if(z)ch[z][y==ch[z][1]]=x;
pushup(y),pushup(x);
}
void splay(int &rt,int x,int goal=0){
for(int y=fa[x];y=fa[x],y!=goal;rotate(x)){
int z=fa[y];
if(z!=goal)rotate((x==ch[y][1])^(y==ch[z][1])?x:y);
}
if(!goal)rt=x;
}
void insert(int &rt,int v){
if(!rt){
rt=++idx,val[rt]=v,siz[rt]=1;
pushup(rt);
return;
}
int x=rt,y=0;
while(1){
pushdown(x);
y=x,x=ch[y][v<val[y]];
if(!x){
x=++idx,val[x]=v,siz[x]=1,fa[x]=y,ch[y][v<val[y]]=x;
pushup(x),pushup(y),splay(rt,x);
break;
}
}
}
void merge(int &rt,int x){
pushdown(x);
if(ch[x][0])merge(rt,ch[x][0]);
insert(rt,val[x]);
if(ch[x][1])merge(rt,ch[x][1]);
clear(x);
}
}
I love AyaseEli;
void dfs1(int u,int fa){
insert(root[u],0);
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(v==fa)continue;
dfs1(v,u);
val[root[v]]+=1ll*(m+1)*e[i].val-2ll*(siz[ch[root[v]][0]]+1)*e[i].val,tag1[root[v]]+=1ll*(m+1)*e[i].val,tag2[root[v]]-=2ll*e[i].val;
if(siz[root[u]]<siz[root[v]])swap(root[u],root[v]);
merge(root[u],root[v]);
}
}
void dfs2(int u){
if(!u)return;
pushdown(u);
dfs2(ch[u][0]);
stk.push_back(val[u]);
dfs2(ch[u][1]);
}
int main(){
n=read(),m=read();
for(int i=1;i<n;i++){
x=read(),y=read(),z=read();
addedge(x,y,z),addedge(y,x,z);
}
dfs1(1,0),dfs2(root[1]);
for(int i=0;i<min(m,(ll)stk.size());i++)ans+=stk[i];
printf("%lld",ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5816kb
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: 5896kb
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: 5884kb
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...