QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#329945 | #5421. Factories Once More | 275307894a | WA | 64ms | 26924kb | C++14 | 2.3kb | 2024-02-17 09:51:18 | 2024-02-17 09:51:18 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=4e5+5,M=N*30+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
}using namespace Debug;
int n,k,rt[N];vector<pii> S[N];
ll ans;
namespace fhq{
int l[N],r[N],siz[N],rd[N],cnt;ll val[N],t1[N],t2[N];
void up(int v){siz[v]=siz[l[v]]+siz[r[v]]+1;}
int newn(){rd[++cnt]=rnd();siz[cnt]=1;return cnt;}
void Pg(int v,ll w1,ll w2){
if(!v) return;
t1[v]+=w1;t2[v]+=w2;
val[v]+=t1[v]+t2[v]*siz[l[v]];
}
void P(int v){
if(t1[v]||t2[v]) Pg(l[v],t1[v],t2[v]),Pg(r[v],t1[v]+(siz[l[v]]+1)*t2[v],t2[v]),t1[v]=t2[v]=0;
}
void split(ll x,int v,int &a,int &b){
if(!v){a=b=0;return;}P(v);
if(val[v]>=x) a=v,split(x,r[v],r[a],b);
else b=v,split(x,l[v],a,l[v]);
up(v);
}
int merge(int x,int y){
if(!x||!y) return x|y;P(x);P(y);
if(rd[x]>rd[y]) swap(x,y);
int r1,r2;split(val[x],y,r1,r2);
l[x]=merge(l[x],r1);r[x]=merge(r[x],r2);up(x);
return x;
}
void print(int x){
if(!x) return;P(x);
print(l[x]);/*gdb(val[x]);*/if(k) k--,ans+=val[x];print(r[x]);
}
}
void dfs(int x,int La){
rt[x]=fhq::newn();
for(auto i:S[x]) if(i.fi^La){
dfs(i.fi,x);
fhq::Pg(rt[i.fi],1ll*i.se*(k-1),-2*i.se);
rt[x]=fhq::merge(rt[x],rt[i.fi]);
}
}
void Solve(){
int i,j;scanf("%d%d",&n,&k);
for(i=1;i<n;i++){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
S[x].emplace_back(y,z);S[y].emplace_back(x,z);
}
dfs(1,0);fhq::print(rt[1]);
printf("%lld\n",ans);
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18444kb
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: 3ms
memory: 20332kb
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: 16244kb
input:
2 2 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #4:
score: -100
Wrong Answer
time: 64ms
memory: 26924kb
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:
3475515527010
result:
wrong answer 1st numbers differ - expected: '4915539756', found: '3475515527010'