Submission #798249

#TimeUsernameProblemLanguageResultExecution timeMemory
798249vjudge1Construction of Highway (JOI18_construction)C++17
100 / 100
401 ms101328 KiB
#include <bits/stdc++.h> using namespace std; #define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); #define rall(s) s.rbegin(),s.rend() #define all(s) s.begin(),s.end() #define pb push_back #define fi first #define se second #define ll long long #define ld long double #define YES cout<<"YES\n" #define Yes cout<<"Yes\n" #define yes cout<<"yes\n" #define NO cout<<"NO\n" #define No cout<<"No\n" #define no cout<<"no\n" const int N = 1e5 + 9 , mod = 1e9 + 7; ll d[N] = {} , a[N] = {}, dp[N] = {}, b[N] , c[N] , sz[N] = {} , h[N] = {} , us[N] , par[N] , cnt = 0 , sum[2 * N] = {} , p[N]; vector<int>v[N]; deque<pair<int,int>> vc[N]; void get_sz(int n , int pa = 0){ sz[n] = 1 , h[n] = h[pa] + 1 , par[n] = pa; for(auto to : v[n]) if(to != pa){ get_sz(to , n); if(sz[to] > sz[dp[n]]) dp[n] = to; } sz[pa] += sz[n]; } void dfshld(int n ,int pa = 1){ p[n] = pa , vc[pa].pb({1 , a[n]}); //cout<<pa<<" "<<n<<"\n"; for(auto to : v[n]) if(to != par[n]) if(to == dp[n]) dfshld(to , p[n]); else dfshld(to , to); } void add(int x , int y){ while(x <= 2e5){ sum[x] += y; x += x & -x; } } ll get(int x){ ll s = 0; while(x > 0){ s += sum[x] ; x -= x & -x; } return s; } deque<pair<int,int>>g; void get_ans(ll x , ll y , ll k){ deque<pair<int,int>>v , v1; ll s = 0; while(s < y){ auto to = vc[x].front(); s += to.fi; if(s > y){ v1.pb({to.fi - (s - y) , to.se}); vc[x].pop_front(); vc[x].push_front({s - y , to.se}); continue; } v1.pb({to.fi , to.se}); vc[x].pop_front(); } reverse(all(v1)); for(auto to : v1) g.pb(to); vc[x].push_front({y , k}); } ll hld(int x , int y){ ll ans = 0; g.clear(); while(x){ get_ans(p[x] , h[x] - h[p[x]] + 1 , a[y]); x = par[p[x]]; } for(auto to : g){ ans += get(to.se - 1) * to.fi; add(to.se , to.fi); } for(auto to : g) add(to.se , -to.fi); return ans; } void solve(){ ll q , i , j , m ,n, z , s = 0, f, l , r , k , x , y , mn = 1e18 , mx = -1; cin>>n; set<ll>s1; x = 0; for(i = 1; i <= n; i++) cin>>a[i] , s1.insert(a[i]); map<int,int>mp; for(auto it : s1) mp[it] = ++x; for(i = 1; i <= n; i++) a[i] = d[i] = mp[a[i]]; for(i = 1; i < n; i++){ cin>>b[i]>>c[i]; v[b[i]].pb(c[i]); v[c[i]].pb(b[i]); } get_sz(1); dfshld(1); for(i = 1; i < n; i++) cout<<hld(b[i] , c[i])<<"\n"; } int main(){ TL; /*#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif */ int t = 1; //cin>>t; while(t--) { solve(); } } // Author : حسن

Compilation message (stderr)

construction.cpp: In function 'void dfshld(int, int)':
construction.cpp:46:11: warning: suggest explicit braces to avoid ambiguous 'else' [-Wdangling-else]
   46 |         if(to != par[n])
      |           ^
construction.cpp: In function 'void solve()':
construction.cpp:109:8: warning: unused variable 'q' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |        ^
construction.cpp:109:16: warning: unused variable 'j' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                ^
construction.cpp:109:20: warning: unused variable 'm' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                    ^
construction.cpp:109:26: warning: unused variable 'z' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                          ^
construction.cpp:109:30: warning: unused variable 's' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                              ^
construction.cpp:109:38: warning: unused variable 'f' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                                      ^
construction.cpp:109:41: warning: unused variable 'l' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                                         ^
construction.cpp:109:45: warning: unused variable 'r' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                                             ^
construction.cpp:109:49: warning: unused variable 'k' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                                                 ^
construction.cpp:109:57: warning: unused variable 'y' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                                                         ^
construction.cpp:109:61: warning: unused variable 'mn' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                                                             ^~
construction.cpp:109:74: warning: unused variable 'mx' [-Wunused-variable]
  109 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x , y , mn  = 1e18 , mx = -1;
      |                                                                          ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...