Submission #825379

#TimeUsernameProblemLanguageResultExecution timeMemory
825379winter0101Designated Cities (JOI19_designated_cities)C++14
100 / 100
441 ms63856 KiB
#include<bits/stdc++.h> using namespace std; #define all(fl) fl.begin(),fl.end() #define pb push_back #define fi first #define se second #define for1(i,j,k) for(int i=j;i<=k;i++) #define for2(i,j,k) for(int i=j;i>=k;i--) #define for3(i,j,k,l) for(int i=j;i<=k;i+=l) #define lb lower_bound #define ub upper_bound #define sz(a) (int)a.size() #define pii pair<int,int> #define pli pair<long long,int> #define gcd __gcd #define lcm(x,y) x*y/__gcd(x,y) #define pil pair<int,long long> #define pll pair<long long,long long> #define eb emplace_back #define pli pair<long long,int> //#define int long long pli cmp(pli &p, pli &q){ if (p.fi>q.fi)return p; return q; } struct IT{ vector<pli>st; vector<long long>lazy; void resz(int n){ st.resize(4*n+9); lazy.resize(4*n+9); } void push(int id){ if (lazy[id]){ lazy[id*2]+=lazy[id]; lazy[id*2+1]+=lazy[id]; st[id*2].fi+=lazy[id]; st[id*2+1].fi+=lazy[id]; lazy[id]=0; } } void update(int id,int l,int r,int u,int v,long long val){ if (l>v||r<u||u>v)return; if (u<=l&&r<=v){ st[id].fi+=val; lazy[id]+=val; if (l==r)st[id].se=l; return; } push(id); int mid=(l+r)/2; update(id*2,l,mid,u,v,val); update(id*2+1,mid+1,r,u,v,val); st[id]=cmp(st[id*2],st[id*2+1]); } }; const int maxn=2e5+9; vector<pil>a[maxn]; long long f[maxn];//subtree long long g[maxn];//g[i] if i root pli dfs1(int u,int par){ pli test={0,u}; for (auto v:a[u]){ if (v.fi==par)continue; auto tmp=dfs1(v.fi,u); tmp.fi+=v.se; test=cmp(test,tmp); f[u]+=v.se+f[v.fi]; } return test; } void dfs2(int u,int par){ for (auto v:a[u]){ if (v.fi==par){ g[u]+=v.se; } } for (auto v:a[u]){ if (v.fi==par)continue; g[v.fi]=g[u]-v.se; dfs2(v.fi,u); } } long long dep[maxn],ans[maxn],w[maxn]; int in[maxn],out[maxn],rev[maxn],tme=0,p[maxn]; bool vis[maxn]; void dfs3(int u,int par){ in[u]=++tme; rev[tme]=u; for (auto v:a[u]){ if (v.fi==par)continue; dep[v.fi]=dep[u]+v.se; w[v.fi]=v.se; p[v.fi]=u; dfs3(v.fi,u); } out[u]=tme; } IT st; int n; void del(int u){ while (u){ if (vis[u])return; st.update(1,1,n,in[u],out[u],-w[u]); vis[u]=1; u=p[u]; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); //freopen("temp.INP","r",stdin); //freopen("temp.OUT","w",stdout); cin>>n; for1(i,1,n-1){ int u,v; long long x,y; cin>>u>>v>>x>>y; a[u].pb({v,x}); a[v].pb({u,y}); } dfs1(1,0); g[1]=f[1]; dfs2(1,0); int r1=min_element(g+1,g+1+n)-g; ans[1]=g[r1]; int root=dfs1(r1,0).se; vis[root]=1; dfs3(root,0); st.resz(n); for1(i,1,n){ st.update(1,1,n,in[i],in[i],dep[i]); } long long sum=g[root]; for1(i,2,n){ if (st.st[1].fi==0){ ans[i]=sum; continue; } sum-=st.st[1].fi; ans[i]=sum; del(rev[st.st[1].se]); } int q; cin>>q; for1(i,1,q){ int x; cin>>x; cout<<ans[x]<<'\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...