Submission #798254

# Submission time Handle Problem Language Result Execution time Memory
798254 2023-07-30T14:23:16 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
390 ms 99336 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] , 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 : حسن

Compilation message

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 time Memory Grader output
1 Correct 35 ms 70076 KB Output is correct
2 Correct 33 ms 70032 KB Output is correct
3 Correct 35 ms 70000 KB Output is correct
4 Correct 36 ms 70100 KB Output is correct
5 Correct 35 ms 70144 KB Output is correct
6 Correct 49 ms 70124 KB Output is correct
7 Correct 38 ms 70172 KB Output is correct
8 Correct 38 ms 70120 KB Output is correct
9 Correct 41 ms 70180 KB Output is correct
10 Correct 37 ms 70260 KB Output is correct
11 Correct 42 ms 70132 KB Output is correct
12 Correct 39 ms 70192 KB Output is correct
13 Correct 36 ms 70084 KB Output is correct
14 Correct 36 ms 70144 KB Output is correct
15 Correct 42 ms 70100 KB Output is correct
16 Correct 46 ms 70184 KB Output is correct
17 Correct 36 ms 70056 KB Output is correct
18 Correct 36 ms 70128 KB Output is correct
19 Correct 36 ms 70164 KB Output is correct
20 Correct 48 ms 70192 KB Output is correct
21 Correct 39 ms 70132 KB Output is correct
22 Correct 35 ms 70036 KB Output is correct
23 Correct 39 ms 70112 KB Output is correct
24 Correct 46 ms 70116 KB Output is correct
25 Correct 37 ms 70120 KB Output is correct
26 Correct 40 ms 70168 KB Output is correct
27 Correct 34 ms 70140 KB Output is correct
28 Correct 35 ms 70080 KB Output is correct
29 Correct 43 ms 70140 KB Output is correct
30 Correct 42 ms 70084 KB Output is correct
31 Correct 47 ms 70120 KB Output is correct
32 Correct 37 ms 70028 KB Output is correct
33 Correct 35 ms 70092 KB Output is correct
34 Correct 47 ms 70176 KB Output is correct
35 Correct 38 ms 70060 KB Output is correct
36 Correct 43 ms 70132 KB Output is correct
37 Correct 38 ms 70112 KB Output is correct
38 Correct 37 ms 70124 KB Output is correct
39 Correct 34 ms 70132 KB Output is correct
40 Correct 44 ms 70152 KB Output is correct
41 Correct 37 ms 70048 KB Output is correct
42 Correct 45 ms 70132 KB Output is correct
43 Correct 35 ms 70044 KB Output is correct
44 Correct 35 ms 70028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70076 KB Output is correct
2 Correct 33 ms 70032 KB Output is correct
3 Correct 35 ms 70000 KB Output is correct
4 Correct 36 ms 70100 KB Output is correct
5 Correct 35 ms 70144 KB Output is correct
6 Correct 49 ms 70124 KB Output is correct
7 Correct 38 ms 70172 KB Output is correct
8 Correct 38 ms 70120 KB Output is correct
9 Correct 41 ms 70180 KB Output is correct
10 Correct 37 ms 70260 KB Output is correct
11 Correct 42 ms 70132 KB Output is correct
12 Correct 39 ms 70192 KB Output is correct
13 Correct 36 ms 70084 KB Output is correct
14 Correct 36 ms 70144 KB Output is correct
15 Correct 42 ms 70100 KB Output is correct
16 Correct 46 ms 70184 KB Output is correct
17 Correct 36 ms 70056 KB Output is correct
18 Correct 36 ms 70128 KB Output is correct
19 Correct 36 ms 70164 KB Output is correct
20 Correct 48 ms 70192 KB Output is correct
21 Correct 39 ms 70132 KB Output is correct
22 Correct 35 ms 70036 KB Output is correct
23 Correct 39 ms 70112 KB Output is correct
24 Correct 46 ms 70116 KB Output is correct
25 Correct 37 ms 70120 KB Output is correct
26 Correct 40 ms 70168 KB Output is correct
27 Correct 34 ms 70140 KB Output is correct
28 Correct 35 ms 70080 KB Output is correct
29 Correct 43 ms 70140 KB Output is correct
30 Correct 42 ms 70084 KB Output is correct
31 Correct 47 ms 70120 KB Output is correct
32 Correct 37 ms 70028 KB Output is correct
33 Correct 35 ms 70092 KB Output is correct
34 Correct 47 ms 70176 KB Output is correct
35 Correct 38 ms 70060 KB Output is correct
36 Correct 43 ms 70132 KB Output is correct
37 Correct 38 ms 70112 KB Output is correct
38 Correct 37 ms 70124 KB Output is correct
39 Correct 34 ms 70132 KB Output is correct
40 Correct 44 ms 70152 KB Output is correct
41 Correct 37 ms 70048 KB Output is correct
42 Correct 45 ms 70132 KB Output is correct
43 Correct 35 ms 70044 KB Output is correct
44 Correct 35 ms 70028 KB Output is correct
45 Correct 42 ms 70264 KB Output is correct
46 Correct 54 ms 70880 KB Output is correct
47 Correct 50 ms 70868 KB Output is correct
48 Correct 51 ms 70792 KB Output is correct
49 Correct 37 ms 71264 KB Output is correct
50 Correct 41 ms 71200 KB Output is correct
51 Correct 39 ms 71296 KB Output is correct
52 Correct 44 ms 70984 KB Output is correct
53 Correct 39 ms 71072 KB Output is correct
54 Correct 39 ms 71076 KB Output is correct
55 Correct 42 ms 71008 KB Output is correct
56 Correct 40 ms 71036 KB Output is correct
57 Correct 58 ms 70832 KB Output is correct
58 Correct 54 ms 70832 KB Output is correct
59 Correct 42 ms 70836 KB Output is correct
60 Correct 53 ms 70892 KB Output is correct
61 Correct 47 ms 71080 KB Output is correct
62 Correct 40 ms 70968 KB Output is correct
63 Correct 49 ms 71036 KB Output is correct
64 Correct 41 ms 70420 KB Output is correct
65 Correct 40 ms 70472 KB Output is correct
66 Correct 51 ms 70496 KB Output is correct
67 Correct 53 ms 70560 KB Output is correct
68 Correct 44 ms 70760 KB Output is correct
69 Correct 41 ms 70952 KB Output is correct
70 Correct 44 ms 70592 KB Output is correct
71 Correct 38 ms 70596 KB Output is correct
72 Correct 46 ms 70804 KB Output is correct
73 Correct 43 ms 70480 KB Output is correct
74 Correct 39 ms 70552 KB Output is correct
75 Correct 42 ms 70924 KB Output is correct
76 Correct 54 ms 70956 KB Output is correct
77 Correct 44 ms 70860 KB Output is correct
78 Correct 42 ms 70468 KB Output is correct
79 Correct 41 ms 70504 KB Output is correct
80 Correct 41 ms 70424 KB Output is correct
81 Correct 58 ms 70856 KB Output is correct
82 Correct 48 ms 70948 KB Output is correct
83 Correct 49 ms 70960 KB Output is correct
84 Correct 38 ms 70560 KB Output is correct
85 Correct 37 ms 70424 KB Output is correct
86 Correct 49 ms 70508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 70076 KB Output is correct
2 Correct 33 ms 70032 KB Output is correct
3 Correct 35 ms 70000 KB Output is correct
4 Correct 36 ms 70100 KB Output is correct
5 Correct 35 ms 70144 KB Output is correct
6 Correct 49 ms 70124 KB Output is correct
7 Correct 38 ms 70172 KB Output is correct
8 Correct 38 ms 70120 KB Output is correct
9 Correct 41 ms 70180 KB Output is correct
10 Correct 37 ms 70260 KB Output is correct
11 Correct 42 ms 70132 KB Output is correct
12 Correct 39 ms 70192 KB Output is correct
13 Correct 36 ms 70084 KB Output is correct
14 Correct 36 ms 70144 KB Output is correct
15 Correct 42 ms 70100 KB Output is correct
16 Correct 46 ms 70184 KB Output is correct
17 Correct 36 ms 70056 KB Output is correct
18 Correct 36 ms 70128 KB Output is correct
19 Correct 36 ms 70164 KB Output is correct
20 Correct 48 ms 70192 KB Output is correct
21 Correct 39 ms 70132 KB Output is correct
22 Correct 35 ms 70036 KB Output is correct
23 Correct 39 ms 70112 KB Output is correct
24 Correct 46 ms 70116 KB Output is correct
25 Correct 37 ms 70120 KB Output is correct
26 Correct 40 ms 70168 KB Output is correct
27 Correct 34 ms 70140 KB Output is correct
28 Correct 35 ms 70080 KB Output is correct
29 Correct 43 ms 70140 KB Output is correct
30 Correct 42 ms 70084 KB Output is correct
31 Correct 47 ms 70120 KB Output is correct
32 Correct 37 ms 70028 KB Output is correct
33 Correct 35 ms 70092 KB Output is correct
34 Correct 47 ms 70176 KB Output is correct
35 Correct 38 ms 70060 KB Output is correct
36 Correct 43 ms 70132 KB Output is correct
37 Correct 38 ms 70112 KB Output is correct
38 Correct 37 ms 70124 KB Output is correct
39 Correct 34 ms 70132 KB Output is correct
40 Correct 44 ms 70152 KB Output is correct
41 Correct 37 ms 70048 KB Output is correct
42 Correct 45 ms 70132 KB Output is correct
43 Correct 35 ms 70044 KB Output is correct
44 Correct 35 ms 70028 KB Output is correct
45 Correct 42 ms 70264 KB Output is correct
46 Correct 54 ms 70880 KB Output is correct
47 Correct 50 ms 70868 KB Output is correct
48 Correct 51 ms 70792 KB Output is correct
49 Correct 37 ms 71264 KB Output is correct
50 Correct 41 ms 71200 KB Output is correct
51 Correct 39 ms 71296 KB Output is correct
52 Correct 44 ms 70984 KB Output is correct
53 Correct 39 ms 71072 KB Output is correct
54 Correct 39 ms 71076 KB Output is correct
55 Correct 42 ms 71008 KB Output is correct
56 Correct 40 ms 71036 KB Output is correct
57 Correct 58 ms 70832 KB Output is correct
58 Correct 54 ms 70832 KB Output is correct
59 Correct 42 ms 70836 KB Output is correct
60 Correct 53 ms 70892 KB Output is correct
61 Correct 47 ms 71080 KB Output is correct
62 Correct 40 ms 70968 KB Output is correct
63 Correct 49 ms 71036 KB Output is correct
64 Correct 41 ms 70420 KB Output is correct
65 Correct 40 ms 70472 KB Output is correct
66 Correct 51 ms 70496 KB Output is correct
67 Correct 53 ms 70560 KB Output is correct
68 Correct 44 ms 70760 KB Output is correct
69 Correct 41 ms 70952 KB Output is correct
70 Correct 44 ms 70592 KB Output is correct
71 Correct 38 ms 70596 KB Output is correct
72 Correct 46 ms 70804 KB Output is correct
73 Correct 43 ms 70480 KB Output is correct
74 Correct 39 ms 70552 KB Output is correct
75 Correct 42 ms 70924 KB Output is correct
76 Correct 54 ms 70956 KB Output is correct
77 Correct 44 ms 70860 KB Output is correct
78 Correct 42 ms 70468 KB Output is correct
79 Correct 41 ms 70504 KB Output is correct
80 Correct 41 ms 70424 KB Output is correct
81 Correct 58 ms 70856 KB Output is correct
82 Correct 48 ms 70948 KB Output is correct
83 Correct 49 ms 70960 KB Output is correct
84 Correct 38 ms 70560 KB Output is correct
85 Correct 37 ms 70424 KB Output is correct
86 Correct 49 ms 70508 KB Output is correct
87 Correct 63 ms 72060 KB Output is correct
88 Correct 89 ms 75776 KB Output is correct
89 Correct 277 ms 89164 KB Output is correct
90 Correct 281 ms 89228 KB Output is correct
91 Correct 265 ms 89148 KB Output is correct
92 Correct 172 ms 99284 KB Output is correct
93 Correct 194 ms 99232 KB Output is correct
94 Correct 174 ms 99336 KB Output is correct
95 Correct 175 ms 93644 KB Output is correct
96 Correct 175 ms 93748 KB Output is correct
97 Correct 172 ms 93648 KB Output is correct
98 Correct 167 ms 93708 KB Output is correct
99 Correct 170 ms 93388 KB Output is correct
100 Correct 355 ms 89560 KB Output is correct
101 Correct 381 ms 89552 KB Output is correct
102 Correct 390 ms 89588 KB Output is correct
103 Correct 385 ms 89508 KB Output is correct
104 Correct 172 ms 93456 KB Output is correct
105 Correct 176 ms 93352 KB Output is correct
106 Correct 179 ms 93368 KB Output is correct
107 Correct 194 ms 78940 KB Output is correct
108 Correct 205 ms 78968 KB Output is correct
109 Correct 222 ms 81888 KB Output is correct
110 Correct 112 ms 89036 KB Output is correct
111 Correct 162 ms 93748 KB Output is correct
112 Correct 112 ms 83528 KB Output is correct
113 Correct 113 ms 83284 KB Output is correct
114 Correct 325 ms 89612 KB Output is correct
115 Correct 301 ms 79508 KB Output is correct
116 Correct 114 ms 83232 KB Output is correct
117 Correct 173 ms 90936 KB Output is correct
118 Correct 182 ms 90192 KB Output is correct
119 Correct 171 ms 89932 KB Output is correct
120 Correct 115 ms 81260 KB Output is correct
121 Correct 116 ms 80584 KB Output is correct
122 Correct 118 ms 80288 KB Output is correct
123 Correct 176 ms 90988 KB Output is correct
124 Correct 182 ms 90296 KB Output is correct
125 Correct 181 ms 90060 KB Output is correct
126 Correct 127 ms 81372 KB Output is correct
127 Correct 122 ms 80708 KB Output is correct
128 Correct 122 ms 80388 KB Output is correct