Submission #200984

#TimeUsernameProblemLanguageResultExecution timeMemory
200984NordwayPutovanje (COCI20_putovanje)C++14
25 / 110
155 ms21368 KiB
#include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #define x first #define y second #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() #define sz(v) (int)v.size() #define up_b upper_bound #define low_b lower_bound #define nl '\n' using namespace std; using namespace __gnu_pbds; typedef unsigned long long ll; typedef long double ld; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set; template<class T,int SZ> struct BIT{ T t[SZ]; void upd(int x,T y){ for(int i=x;i<SZ;i=(i|(i+1))){ t[i]+=y; } } T pref(int x){ T res=0; for(int i=x;i>=0;i=(i&(i+1))-1){ res+=t[i]; } return res; } T get(int l,int r){ return pref(r)-pref(l-1); } }; template<class T,int SZ> struct ST{ T t[4*SZ]; void build(T a[],int v,int tl,int tr){ if(tl==tr){ t[v]=a[tl]; return ; } int tm=(tl+tr)/2; build(a,v*2,tl,tm); build(a,v*2+1,tm+1,tr); t[v]=t[v*2]+t[v*2+1]; } void upd(int v,int tl,int tr,int pos,T val){ if(tl==tr){ t[v]+=val; return ; } int tm=(tl+tr)/2; if(pos<=tm)upd(v*2,tl,tm,pos,val); else upd(v*2+1,tm+1,tr,pos,val); t[v]=t[v*2]+t[v*2+1]; } T get(int v,int tl,int tr,int l,int r){ if(tl>r||l>tr)return 0; if(l<=tl&&tr<=r)return t[v]; int tm=(tl+tr)/2; return get(v*2,tl,tm,l,r)+get(v*2+1,tm+1,tr,l,r); } }; const int N=2e5+11; const int M=1e3+11; const int W=1e3+11; const int inf=INT_MAX; const ll INF=1e18; const ll mod=1e9+7; const ld EPS=1e-9; const int dx[4]={0,0,1,-1}; const int dy[4]={1,-1,0,0}; vector<pair<int,int> >g[N]; int c[N][2]; int up[N][18]; int st[N],en[N],timer,h[N]; int add[N],ans[N]; bool used[N]; void dfs(int v,int p){ up[v][0]=p; h[v]=h[p]+1; for(int i=1;i<=17;i++){ up[v][i]=up[up[v][i-1]][i-1]; } st[v]=++timer; for(int i=0;i<sz(g[v]);i++){ int to=g[v][i].x; if(to==p)continue; dfs(to,v); } en[v]=timer; } bool upper(int a,int b){ return st[a]<=st[b]&&en[a]>=en[b]; } int lca(int a,int b){ if(upper(a,b))return a; if(upper(b,a))return b; for(int i=17;i>=0;i--){ if(!upper(up[a][i],b)){ a=up[a][i]; } } return up[a][0]; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0),cout.tie(0); int n; cin>>n; for(int i=1;i<n;i++){ int u,v; cin>>u>>v>>c[i][0]>>c[i][1]; g[u].pb(mp(v,i)); g[v].pb(mp(u,i)); } dfs(1,1); for(int i=2;i<=n;i++){ int l=lca(i,i-1); if(l!=i)add[i]++,add[l]--; if(l!=(i-1))add[i-1]++,add[l]--; } queue<int>q; for(int i=1;i<=n;i++){ if(st[i]==en[i]){ q.push(i); used[i]=1; } } while(!q.empty()){ int v=q.front(); q.pop(); for(int i=0;i<sz(g[v]);i++){ if(g[v][i].x==up[v][0]){ int to=g[v][i].x; add[to]+=add[v]; ans[g[v][i].y]+=add[v]; if(!used[to]){ used[to]=1; q.push(to); } break; } } } ll res=0; for(int i=1;i<n;i++){ res+=min(1ll*c[i][0]*ans[i],1ll*c[i][1]); } cout<<res; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...