제출 #798254

#제출 시각아이디문제언어결과실행 시간메모리
798254vjudge1Construction of Highway (JOI18_construction)C++17
100 / 100
390 ms99336 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] , g;


void get_sz(int n , int pa = 0){
    sz[n] = 1 , h[n] = h[pa] + 1 , par[n] = pa;
    for(auto to : v[n]){
        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]});
    for(auto to : v[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;
}

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 = 0 , y , mn  = 1e18 , mx = -1;
    cin>>n;
    set<ll>s1;
    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]);
    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 : حسن

컴파일 시 표준 에러 (stderr) 메시지

construction.cpp: In function 'void solve()':
construction.cpp:104:8: warning: unused variable 'q' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |        ^
construction.cpp:104:16: warning: unused variable 'j' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                ^
construction.cpp:104:20: warning: unused variable 'm' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                    ^
construction.cpp:104:26: warning: unused variable 'z' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                          ^
construction.cpp:104:30: warning: unused variable 's' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                              ^
construction.cpp:104:38: warning: unused variable 'f' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                      ^
construction.cpp:104:41: warning: unused variable 'l' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                         ^
construction.cpp:104:45: warning: unused variable 'r' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                             ^
construction.cpp:104:49: warning: unused variable 'k' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                                 ^
construction.cpp:104:61: warning: unused variable 'y' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                                             ^
construction.cpp:104:65: warning: unused variable 'mn' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                                                 ^~
construction.cpp:104:78: warning: unused variable 'mx' [-Wunused-variable]
  104 |     ll q , i , j , m ,n, z , s  = 0, f, l , r , k , x = 0 , y , mn  = 1e18 , mx = -1;
      |                                                                              ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...