QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#334607 | #5421. Factories Once More | Skyjoy | WA | 43ms | 22384kb | C++14 | 2.6kb | 2024-02-22 09:04:21 | 2024-02-22 09:04:22 |
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];
ll fa[N<<2],val[N<<2],siz[N<<2],ch[N<<2][2],tag1[N<<2],tag2[N<<2];
vector<int>stk;
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 dfs(int u){
if(!u)return;
pushdown(u);
dfs(ch[u][0]);
stk.push_back(u);
dfs(ch[u][1]);
}
void insert(int &rt,int k){
int v=val[k];
if(!rt){
rt=k,ch[rt][0]=ch[rt][1]=0,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=k,ch[x][0]=ch[x][1]=0,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){
stk.clear();
dfs(x);
for(int tmp:stk)insert(rt,tmp);
rt=stk.back();
}
}
I love AyaseEli;
void dfs1(int u,int fa){
insert(root[u],u);
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]);
}
}
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);
stk.clear();
dfs(root[1]);
for(int i=0;i<min(m,(ll)stk.size());i++)ans+=val[stk[i]];
printf("%lld",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 14124kb
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: 2ms
memory: 14120kb
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: 2ms
memory: 14168kb
input:
2 2 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 43ms
memory: 22384kb
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:
344426270
result:
wrong answer 1st numbers differ - expected: '4915539756', found: '344426270'