Submission #798249

# Submission time Handle Problem Language Result Execution time Memory
798249 2023-07-30T14:20:32 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
401 ms 101328 KB

#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

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 time Memory Grader output
1 Correct 34 ms 69964 KB Output is correct
2 Correct 33 ms 69980 KB Output is correct
3 Correct 38 ms 70092 KB Output is correct
4 Correct 33 ms 70044 KB Output is correct
5 Correct 35 ms 70168 KB Output is correct
6 Correct 33 ms 70100 KB Output is correct
7 Correct 34 ms 70180 KB Output is correct
8 Correct 34 ms 70132 KB Output is correct
9 Correct 34 ms 70124 KB Output is correct
10 Correct 36 ms 70180 KB Output is correct
11 Correct 34 ms 70180 KB Output is correct
12 Correct 35 ms 70224 KB Output is correct
13 Correct 33 ms 70136 KB Output is correct
14 Correct 44 ms 70248 KB Output is correct
15 Correct 34 ms 70176 KB Output is correct
16 Correct 34 ms 70100 KB Output is correct
17 Correct 35 ms 70076 KB Output is correct
18 Correct 35 ms 70080 KB Output is correct
19 Correct 34 ms 70100 KB Output is correct
20 Correct 35 ms 70160 KB Output is correct
21 Correct 35 ms 70084 KB Output is correct
22 Correct 34 ms 70100 KB Output is correct
23 Correct 35 ms 70116 KB Output is correct
24 Correct 39 ms 70104 KB Output is correct
25 Correct 35 ms 70100 KB Output is correct
26 Correct 35 ms 70116 KB Output is correct
27 Correct 35 ms 70092 KB Output is correct
28 Correct 34 ms 70092 KB Output is correct
29 Correct 35 ms 70140 KB Output is correct
30 Correct 34 ms 70152 KB Output is correct
31 Correct 35 ms 70124 KB Output is correct
32 Correct 35 ms 70040 KB Output is correct
33 Correct 34 ms 70164 KB Output is correct
34 Correct 35 ms 70100 KB Output is correct
35 Correct 36 ms 70124 KB Output is correct
36 Correct 36 ms 70084 KB Output is correct
37 Correct 42 ms 70092 KB Output is correct
38 Correct 34 ms 70016 KB Output is correct
39 Correct 35 ms 70188 KB Output is correct
40 Correct 35 ms 70116 KB Output is correct
41 Correct 37 ms 70244 KB Output is correct
42 Correct 35 ms 70100 KB Output is correct
43 Correct 34 ms 70140 KB Output is correct
44 Correct 42 ms 70140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 69964 KB Output is correct
2 Correct 33 ms 69980 KB Output is correct
3 Correct 38 ms 70092 KB Output is correct
4 Correct 33 ms 70044 KB Output is correct
5 Correct 35 ms 70168 KB Output is correct
6 Correct 33 ms 70100 KB Output is correct
7 Correct 34 ms 70180 KB Output is correct
8 Correct 34 ms 70132 KB Output is correct
9 Correct 34 ms 70124 KB Output is correct
10 Correct 36 ms 70180 KB Output is correct
11 Correct 34 ms 70180 KB Output is correct
12 Correct 35 ms 70224 KB Output is correct
13 Correct 33 ms 70136 KB Output is correct
14 Correct 44 ms 70248 KB Output is correct
15 Correct 34 ms 70176 KB Output is correct
16 Correct 34 ms 70100 KB Output is correct
17 Correct 35 ms 70076 KB Output is correct
18 Correct 35 ms 70080 KB Output is correct
19 Correct 34 ms 70100 KB Output is correct
20 Correct 35 ms 70160 KB Output is correct
21 Correct 35 ms 70084 KB Output is correct
22 Correct 34 ms 70100 KB Output is correct
23 Correct 35 ms 70116 KB Output is correct
24 Correct 39 ms 70104 KB Output is correct
25 Correct 35 ms 70100 KB Output is correct
26 Correct 35 ms 70116 KB Output is correct
27 Correct 35 ms 70092 KB Output is correct
28 Correct 34 ms 70092 KB Output is correct
29 Correct 35 ms 70140 KB Output is correct
30 Correct 34 ms 70152 KB Output is correct
31 Correct 35 ms 70124 KB Output is correct
32 Correct 35 ms 70040 KB Output is correct
33 Correct 34 ms 70164 KB Output is correct
34 Correct 35 ms 70100 KB Output is correct
35 Correct 36 ms 70124 KB Output is correct
36 Correct 36 ms 70084 KB Output is correct
37 Correct 42 ms 70092 KB Output is correct
38 Correct 34 ms 70016 KB Output is correct
39 Correct 35 ms 70188 KB Output is correct
40 Correct 35 ms 70116 KB Output is correct
41 Correct 37 ms 70244 KB Output is correct
42 Correct 35 ms 70100 KB Output is correct
43 Correct 34 ms 70140 KB Output is correct
44 Correct 42 ms 70140 KB Output is correct
45 Correct 38 ms 70268 KB Output is correct
46 Correct 39 ms 70904 KB Output is correct
47 Correct 39 ms 70896 KB Output is correct
48 Correct 40 ms 70868 KB Output is correct
49 Correct 37 ms 71252 KB Output is correct
50 Correct 38 ms 71288 KB Output is correct
51 Correct 39 ms 71212 KB Output is correct
52 Correct 38 ms 71112 KB Output is correct
53 Correct 39 ms 71032 KB Output is correct
54 Correct 39 ms 71116 KB Output is correct
55 Correct 38 ms 71132 KB Output is correct
56 Correct 38 ms 71132 KB Output is correct
57 Correct 41 ms 70868 KB Output is correct
58 Correct 49 ms 71144 KB Output is correct
59 Correct 42 ms 70840 KB Output is correct
60 Correct 42 ms 70936 KB Output is correct
61 Correct 38 ms 71124 KB Output is correct
62 Correct 38 ms 71124 KB Output is correct
63 Correct 38 ms 71128 KB Output is correct
64 Correct 42 ms 70476 KB Output is correct
65 Correct 39 ms 70456 KB Output is correct
66 Correct 39 ms 70484 KB Output is correct
67 Correct 40 ms 70612 KB Output is correct
68 Correct 36 ms 70848 KB Output is correct
69 Correct 38 ms 71116 KB Output is correct
70 Correct 36 ms 70740 KB Output is correct
71 Correct 37 ms 70616 KB Output is correct
72 Correct 41 ms 70860 KB Output is correct
73 Correct 42 ms 70548 KB Output is correct
74 Correct 39 ms 70648 KB Output is correct
75 Correct 38 ms 71040 KB Output is correct
76 Correct 38 ms 70996 KB Output is correct
77 Correct 38 ms 70944 KB Output is correct
78 Correct 40 ms 70540 KB Output is correct
79 Correct 38 ms 70528 KB Output is correct
80 Correct 38 ms 70504 KB Output is correct
81 Correct 38 ms 71036 KB Output is correct
82 Correct 47 ms 71008 KB Output is correct
83 Correct 41 ms 70996 KB Output is correct
84 Correct 39 ms 70576 KB Output is correct
85 Correct 40 ms 70612 KB Output is correct
86 Correct 38 ms 70528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 69964 KB Output is correct
2 Correct 33 ms 69980 KB Output is correct
3 Correct 38 ms 70092 KB Output is correct
4 Correct 33 ms 70044 KB Output is correct
5 Correct 35 ms 70168 KB Output is correct
6 Correct 33 ms 70100 KB Output is correct
7 Correct 34 ms 70180 KB Output is correct
8 Correct 34 ms 70132 KB Output is correct
9 Correct 34 ms 70124 KB Output is correct
10 Correct 36 ms 70180 KB Output is correct
11 Correct 34 ms 70180 KB Output is correct
12 Correct 35 ms 70224 KB Output is correct
13 Correct 33 ms 70136 KB Output is correct
14 Correct 44 ms 70248 KB Output is correct
15 Correct 34 ms 70176 KB Output is correct
16 Correct 34 ms 70100 KB Output is correct
17 Correct 35 ms 70076 KB Output is correct
18 Correct 35 ms 70080 KB Output is correct
19 Correct 34 ms 70100 KB Output is correct
20 Correct 35 ms 70160 KB Output is correct
21 Correct 35 ms 70084 KB Output is correct
22 Correct 34 ms 70100 KB Output is correct
23 Correct 35 ms 70116 KB Output is correct
24 Correct 39 ms 70104 KB Output is correct
25 Correct 35 ms 70100 KB Output is correct
26 Correct 35 ms 70116 KB Output is correct
27 Correct 35 ms 70092 KB Output is correct
28 Correct 34 ms 70092 KB Output is correct
29 Correct 35 ms 70140 KB Output is correct
30 Correct 34 ms 70152 KB Output is correct
31 Correct 35 ms 70124 KB Output is correct
32 Correct 35 ms 70040 KB Output is correct
33 Correct 34 ms 70164 KB Output is correct
34 Correct 35 ms 70100 KB Output is correct
35 Correct 36 ms 70124 KB Output is correct
36 Correct 36 ms 70084 KB Output is correct
37 Correct 42 ms 70092 KB Output is correct
38 Correct 34 ms 70016 KB Output is correct
39 Correct 35 ms 70188 KB Output is correct
40 Correct 35 ms 70116 KB Output is correct
41 Correct 37 ms 70244 KB Output is correct
42 Correct 35 ms 70100 KB Output is correct
43 Correct 34 ms 70140 KB Output is correct
44 Correct 42 ms 70140 KB Output is correct
45 Correct 38 ms 70268 KB Output is correct
46 Correct 39 ms 70904 KB Output is correct
47 Correct 39 ms 70896 KB Output is correct
48 Correct 40 ms 70868 KB Output is correct
49 Correct 37 ms 71252 KB Output is correct
50 Correct 38 ms 71288 KB Output is correct
51 Correct 39 ms 71212 KB Output is correct
52 Correct 38 ms 71112 KB Output is correct
53 Correct 39 ms 71032 KB Output is correct
54 Correct 39 ms 71116 KB Output is correct
55 Correct 38 ms 71132 KB Output is correct
56 Correct 38 ms 71132 KB Output is correct
57 Correct 41 ms 70868 KB Output is correct
58 Correct 49 ms 71144 KB Output is correct
59 Correct 42 ms 70840 KB Output is correct
60 Correct 42 ms 70936 KB Output is correct
61 Correct 38 ms 71124 KB Output is correct
62 Correct 38 ms 71124 KB Output is correct
63 Correct 38 ms 71128 KB Output is correct
64 Correct 42 ms 70476 KB Output is correct
65 Correct 39 ms 70456 KB Output is correct
66 Correct 39 ms 70484 KB Output is correct
67 Correct 40 ms 70612 KB Output is correct
68 Correct 36 ms 70848 KB Output is correct
69 Correct 38 ms 71116 KB Output is correct
70 Correct 36 ms 70740 KB Output is correct
71 Correct 37 ms 70616 KB Output is correct
72 Correct 41 ms 70860 KB Output is correct
73 Correct 42 ms 70548 KB Output is correct
74 Correct 39 ms 70648 KB Output is correct
75 Correct 38 ms 71040 KB Output is correct
76 Correct 38 ms 70996 KB Output is correct
77 Correct 38 ms 70944 KB Output is correct
78 Correct 40 ms 70540 KB Output is correct
79 Correct 38 ms 70528 KB Output is correct
80 Correct 38 ms 70504 KB Output is correct
81 Correct 38 ms 71036 KB Output is correct
82 Correct 47 ms 71008 KB Output is correct
83 Correct 41 ms 70996 KB Output is correct
84 Correct 39 ms 70576 KB Output is correct
85 Correct 40 ms 70612 KB Output is correct
86 Correct 38 ms 70528 KB Output is correct
87 Correct 48 ms 72328 KB Output is correct
88 Correct 110 ms 76872 KB Output is correct
89 Correct 297 ms 92840 KB Output is correct
90 Correct 266 ms 92848 KB Output is correct
91 Correct 310 ms 92868 KB Output is correct
92 Correct 183 ms 101328 KB Output is correct
93 Correct 170 ms 101216 KB Output is correct
94 Correct 178 ms 101288 KB Output is correct
95 Correct 194 ms 97040 KB Output is correct
96 Correct 210 ms 97416 KB Output is correct
97 Correct 203 ms 97424 KB Output is correct
98 Correct 183 ms 97312 KB Output is correct
99 Correct 195 ms 97084 KB Output is correct
100 Correct 342 ms 92456 KB Output is correct
101 Correct 397 ms 92776 KB Output is correct
102 Correct 395 ms 92748 KB Output is correct
103 Correct 401 ms 92704 KB Output is correct
104 Correct 194 ms 97136 KB Output is correct
105 Correct 186 ms 97092 KB Output is correct
106 Correct 187 ms 97088 KB Output is correct
107 Correct 236 ms 81904 KB Output is correct
108 Correct 210 ms 81996 KB Output is correct
109 Correct 239 ms 85128 KB Output is correct
110 Correct 139 ms 90564 KB Output is correct
111 Correct 175 ms 97040 KB Output is correct
112 Correct 124 ms 86604 KB Output is correct
113 Correct 121 ms 86412 KB Output is correct
114 Correct 340 ms 92460 KB Output is correct
115 Correct 314 ms 81996 KB Output is correct
116 Correct 138 ms 86372 KB Output is correct
117 Correct 178 ms 94268 KB Output is correct
118 Correct 179 ms 93600 KB Output is correct
119 Correct 183 ms 93348 KB Output is correct
120 Correct 131 ms 84304 KB Output is correct
121 Correct 127 ms 83736 KB Output is correct
122 Correct 126 ms 83308 KB Output is correct
123 Correct 186 ms 94360 KB Output is correct
124 Correct 185 ms 93764 KB Output is correct
125 Correct 196 ms 93416 KB Output is correct
126 Correct 139 ms 84352 KB Output is correct
127 Correct 133 ms 83732 KB Output is correct
128 Correct 143 ms 83456 KB Output is correct