QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#188156 | #5421. Factories Once More | Eternity112 | WA | 2ms | 9856kb | C++14 | 3.5kb | 2023-09-25 15:58:52 | 2023-09-25 15:58:52 |
Judging History
answer
#include<bits/stdc++.h>
#define re register
typedef long long ll;
typedef unsigned long long ull;
template<class T>
inline void read(T &x)
{
x=0;
char ch=getchar(),t=0;
while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
if(t) x=-x;
}
template<class T,class ...Arc>
inline void read(T &x,Arc &...arc){ read(x),read(arc...); }
template<class T>
inline void write(T x)
{
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10+48);
}
inline void write(char c){ putchar(c); }
inline void write(bool x){ putchar(x?'1':'0'); }
inline void write(char *s){ while(*s!='\0') putchar(*s++); }
inline void write(const char *s){ while(*s!='\0') putchar(*s++); }
template<class T,class ...Arc>
inline void write(T x,Arc ...arc){ write(x),write(arc...); }
template<class T>
inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; }
template<class T>
inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; }
using Pir=std::pair<int,int>;
#define fir first
#define sec second
#define Mkr std::make_pair
const int MAXN=2e5+10;
int N,K;
std::vector<Pir>Edges[MAXN];
std::mt19937 rnd(time(NULL));
struct FHQ
{
#define ls(p) Tr[p].lc
#define rs(p) Tr[p].rc
struct Treap
{
int lc,rc,siz,rd;
ll val,k,b;
}Tr[MAXN<<4];
int Idx;
std::stack<int>Pool;
inline int newNode(ll v)
{
int p=Pool.empty()?++Idx:Pool.top();
if(!Pool.empty()) Pool.pop();
Tr[p].k=Tr[p].b=Tr[p].lc=Tr[p].rc=0;
Tr[p].val=v,Tr[p].siz=1,Tr[p].rd=rnd();
return p;
}
inline void pushUp(int p)
{ Tr[p].siz=Tr[ls(p)].siz+Tr[rs(p)].siz+1; }
inline void upDate(int p,ll k,ll b)
{
Tr[p].val+=k*(Tr[ls(p)].siz+1)+b;
Tr[p].k+=k,Tr[p].b+=b;
}
inline void pushDown(int p)
{
if(Tr[p].k||Tr[p].b)
{
if(Tr[p].lc) upDate(ls(p),Tr[p].k,Tr[p].b);
if(Tr[p].rc) upDate(rs(p),Tr[p].k,Tr[p].k*(Tr[ls(p)].siz+1)+Tr[p].b);
Tr[p].k=Tr[p].b=0;
}
}
void split(int p,ll val,int &u,int &v)
{
if(!p) u=v=0;
else
{
pushDown(p);
if(Tr[p].val<val) v=p,split(ls(p),val,u,ls(p));
else u=p,split(rs(p),val,rs(p),v);
return pushUp(p);
}
}
int merge(int u,int v)
{
if(!u||!v) return u^v;
pushDown(u),pushDown(v);
if(Tr[u].rd<Tr[v].rd)
{
rs(u)=merge(rs(u),v);
return pushUp(u),u;
}
else
{
ls(v)=merge(u,ls(v));
return pushUp(v),v;
}
}
inline void insert(int &p,ll v)
{
int x=0,y=0;
split(p,v,x,y);
p=merge(merge(x,newNode(v)),y);
}
void flatten(int p,std::vector<ll>&vec)
{
Pool.push(p);pushDown(p);
if(Tr[p].lc) flatten(ls(p),vec);
vec.push_back(Tr[p].val);
if(Tr[p].rc) flatten(rs(p),vec);
}
#undef ls
#undef rs
}Tr;
int Rt[MAXN],Siz[MAXN];
void dfsTree(int x,int last)
{
Siz[x]=1;
int s=0;
for(auto e:Edges[x])
{
int v=e.fir,vl=e.sec;
if(v==last) continue;
dfsTree(v,x);
Tr.upDate(Rt[v],-2ll*vl,1ll*vl*(K+1));
Siz[x]+=Siz[v];
if(Siz[v]>Siz[s]) s=v;
}
if(s) Rt[x]=Rt[s];
Tr.insert(Rt[x],0);
for(auto e:Edges[x])
{
int v=e.fir;
if(v==last||v==s) continue;
std::vector<ll>vec;
Tr.flatten(Rt[v],vec);
for(ll val:vec) Tr.insert(Rt[x],val);
}
}
int main()
{
// freopen("memory.in","r",stdin);
// freopen("memory.out","w",stdout);
read(N,K);
for(int i=2,u,v,w;i<=N;++i)
{
read(u,v,w);
Edges[u].push_back(Mkr(v,w));
Edges[v].push_back(Mkr(u,w));
}
dfsTree(1,0);
ll ans=0;
std::vector<ll>vec;
Tr.flatten(Rt[1],vec);
for(int i=0;i<K;++i) ans+=vec[i];
write(ans*2);
return 0;
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 9856kb
input:
6 3 1 2 3 2 3 2 2 4 1 1 5 2 5 6 3
output:
44
result:
wrong answer 1st numbers differ - expected: '22', found: '44'