Submission #1158834

#TimeUsernameProblemLanguageResultExecution timeMemory
1158834MighilonPetrol stations (CEOI24_stations)C++20
55 / 100
1697 ms732436 KiB
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG #include "../Library/debug.h" #else #define dbg(x...) #endif typedef long long ll; typedef long double ld; typedef pair<int, int> pi; typedef pair<ll, ll> pl; typedef vector<int> vi; typedef vector<bool> vb; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) for (int i = 0; i < (a); ++i) #define FORd(i, a, b) for (int i = (b) - 1; i >= (a); --i) #define F0Rd(i, a) for (int i = (a) - 1; i >= 0; --i) #define trav(a, x) for (auto& a : x) #define f first #define s second #define pb push_back #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() const char nl = '\n'; const int INF = 1e9; // const ll MOD = 1e9 + 7; const ll MOD=998244353; int n; ll k; // vector<vi> adj; // vector<array<int,2>> e; // vi ans,sz,cnt,depth; // vb used; // vector<vl> dist,pred; const int mxN=70005; vi adj[mxN]; array<int,2> e[mxN]; ll ans[mxN],dist[20][mxN]; int sz[mxN],cnt[mxN],depth[mxN],pred[20][mxN]; bool used[mxN]; map<int,vpi> mp; struct node { int sum = 0; node *left_child = nullptr, *right_child = nullptr; void extend(ll left, ll right) { if (!left_child && left + 1 < right) { left_child = new node(); right_child = new node(); } } void add(ll left, ll right,ll kk,ll x) { sum += x; if (left + 1 < right) { if (kk < (left + right) / 2) { if (!left_child) left_child = new node(); left_child->add(left, (left + right) / 2,kk,x); } else { if (!right_child) right_child = new node(); right_child->add((left + right) / 2, right,kk,x); } } } int query(ll left,ll right, ll lq, ll rq) { if (lq <= left && right <= rq) return sum; if (max(left, lq) >= min(right, rq)) return 0; ll answer = 0; if (left_child) answer += left_child->query(left, (left + right) / 2,lq,rq); if (right_child) answer += right_child->query((left + right) / 2, right,lq,rq); return answer; } }; // struct node{ // ll val; // node*c[2]={nullptr,nullptr}; // node():val(0){ } // ~node(){ // if(c[0]!=nullptr){ // c[0]->~node(); // node *tmp=c[0]; // c[0]=nullptr; // free(tmp); // } // if(c[1]!=nullptr){ // c[1]->~node(); // node *tmp=c[1]; // c[1]=nullptr; // free(tmp); // } // } // void push(){ // if(c[0]==nullptr) // c[0]=new node(); // if(c[1]==nullptr) // c[1]=new node(); // } // void merge(){ // val=c[0]->val+c[1]->val; // } // void add(ll l, ll r, ll q, ll valq){ // if(q<l||q>r)return; // if(l==r&&l==q){ // val+=valq; // return; // } // push(); // ll m=l+(r-l)/2; // c[0]->add(l,m,q,valq); // c[1]->add(m+1,r,q,valq); // merge(); // } // ll query(ll l,ll r,ll ql,ll qr){ // if(r<ql||l>qr)return 0; // if(ql<=l&&r<=qr) // return val; // push(); // ll m=l+(r-l)/2; // return c[0]->query(l,m,ql,qr)+c[1]->query(m+1,r,ql,qr); // } // }; int size_tree(int v,int p=-1){ sz[v]=1; trav(id,adj[v]){ int u=e[id][0]^v; if(used[u]||u==p)continue; sz[v]+=size_tree(u,v); } return sz[v]; } int find_centroid(int v,int size,int p=-1){ trav(id,adj[v]){ int u=e[id][0]^v; if(u==p||used[u])continue; if(sz[u]>size/2) return find_centroid(u,size,v); } return v; } void up_stage(int v,int p,int w,int d,int st){ depth[v]=d; pred[0][v]=p; dist[0][v]=w; sz[v]=1; cnt[v]=1; FOR(i,1,20){ pred[i][v]=pred[i-1][pred[i-1][v]]; dist[i][v]=dist[i-1][v]+dist[i-1][pred[i-1][v]]; } trav(id,adj[v]){ int u=e[id][0]^v; if(used[u]||u==p)continue; up_stage(u,v,e[id][1],d+1,d==0?u:st); sz[v]+=sz[u]; } int u=v; ll dist_up=0; F0Rd(i,20){ if(dist_up+dist[i][u]<=k){ dist_up+=dist[i][u]; u=pred[i][u]; } } if(depth[u])cnt[u]+=cnt[v]; else mp[st].pb({k-dist_up,cnt[v]}); } void down_stage(int v,int p,int d,node* tree,int tree_size,int st_sz=0){ ans[v]+=1LL*(cnt[v]-1)*(tree_size-st_sz); trav(id,adj[v]){ int u=e[id][0]^v; if(used[u]||u==p)continue; if(!d)trav(x,mp[u]){ tree->add(0,1e15,x.f,-x.s); } ll tmp=tree->query(0,1e15,d,d+e[id][1]); ans[v]+=tmp*sz[u]; tree->add(0,1e15,d+k,tmp); if(depth[v]==0)st_sz=sz[u]; down_stage(u,v,d+e[id][1],tree,tree_size,st_sz); tree->add(0,1e15,d+k,-tmp); if(!d)trav(x,mp[u])tree->add(0,1e15,x.f,x.s); } } void centroid_decomposition(int c){ int v=find_centroid(c,size_tree(c)); used[v]=true; up_stage(v,v,0,0,-1); node *tree=new node(); trav(i,mp) trav(x,i.s) tree->add(0,1e15,x.f,x.s); down_stage(v,v,0,tree,sz[v]); // free(tree); mp.clear(); trav(id,adj[v]){ int u=e[id][0]^v; if(used[u])continue; centroid_decomposition(u); } } void solve(){ cin>>n>>k; F0R(i,n-1){ int a,b; cin>>a>>b>>e[i][1]; e[i][0]=a^b; adj[a].pb(i); adj[b].pb(i); } centroid_decomposition(0); F0R(i,n)cout<<ans[i]<<nl; } int32_t main(){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int TC = 1; // cin >> TC; while(TC--){ solve(); } return 0; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...