답안 #335383

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
335383 2020-12-12T07:50:05 Z mehrdad_sohrabi Construction of Highway (JOI18_construction) C++14
100 / 100
537 ms 33052 KB
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=1e5+100;
vector <int> g[N];
vector <pii> y[N];
ll hd[N];
ll a[N],st[N],fn[N],ts=1;
ll sz[N];
ll par[N];
ll hi[N];
void dfssz(ll v,ll p){
    par[v]=p;
    sz[v]=1;
    hi[v]=hi[p]+1;
    for (auto u : g[v]){
        if (u==p) continue;
        dfssz(u,v);
        sz[v]+=sz[u];
    }
}
void dfshld(ll v,ll p,ll h){
    hd[v]=h;
    ll big=0;
    st[v]=ts;
    for (auto u : g[v]){
        if (u==p) continue;
        if (sz[u]>sz[big]) big=u;
    }
    if (big){
        ts++;
        dfshld(big,v,h);
    }
    for (auto u : g[v]){
        if (u==p || u==big) continue;
        ts++;
        dfshld(u,v,u);
    }
    fn[v]=ts;
}
ll fen[N];
void add(ll idx,ll val){
    for (;idx<N;idx+=idx & (-idx)){
        fen[idx]+=val;
    }
}
ll get(ll idx){
    ll s=0;
    for (;idx;idx-= idx & (-idx)){
        s+=fen[idx];
    }
    return s;
}
ll solve(vector <pii> t){
    ll s=0;
    for (auto u : t){
        s+=get(u.S-1)*u.F;
        add(u.S,u.F);
    }
    for (auto u : t){
        add(u.S,-u.F);
    }
    return s;
}
map <int,int> mp;
int32_t main(){
    sync;
    ll n;
    cin >> n;
    vector <int> com;
    for (int i=1;i<=n;i++){
        cin >> a[i];
        com.pb(a[i]);
    }
    sort(com.begin(),com.end());
    for (int i=0;i<com.size();i++){
        mp[com[i]]=i+1;
    }
    for (int i=1;i<=n;i++){
        a[i]=mp[a[i]];
    }
    vector <pii> yal;
    for (int i=0;i<n-1;i++){
        ll u,v;
        cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
        yal.pb({u,v});
    }
    dfssz(1,1);
    dfshld(1,1,1);
    par[1]=0;
    for (int i=0;i<n-1;i++){
        ll v=yal[i].S;
        ll z=v;
        vector <pii> tt,t;
        while(true){
            ll k=hd[v];
        //    cout << v << " erjf " << k << endl;
            while(y[k].size() && y[k].back().F<=hi[v]) tt.pb({y[k].back().F,y[k].back().S}),y[k].pop_back();
            if (y[k].size()) tt.pb({hi[v],y[k].back().S});
            y[k].pb({hi[v],a[z]});
            v=par[k];

            reverse(tt.begin(),tt.end());
            for (int i=0;i<(ll)tt.size()-1;i++){
                tt[i].F-=tt[i+1].F;
            }
            if (tt.size())
                tt[tt.size()-1].F-=hi[par[k]];
            for (auto u : tt) t.pb(u);
            tt.clear();
            if (k==1) break;
        }
       // reverse(t.begin(),t.end());
 //  for (auto u : t) cout << u.F << " " << u.S << "  rwegr ";
   //    cout << " mrgrtg " << endl;
       cout << solve(t) << endl;
    }
    for (int i=1;i<=n;i++){
       // cout << hd[i] << endl;
    }

}
/*
5
1 7 3 4 8
1 2
1 3
2 4
3 5
*/

Compilation message

construction.cpp: In function 'int32_t main()':
construction.cpp:86:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   86 |     for (int i=0;i<com.size();i++){
      |                  ~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 5 ms 5248 KB Output is correct
7 Correct 5 ms 5228 KB Output is correct
8 Correct 4 ms 5228 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 4 ms 5228 KB Output is correct
11 Correct 4 ms 5228 KB Output is correct
12 Correct 4 ms 5228 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 5 ms 5228 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 5 ms 5228 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 5 ms 5228 KB Output is correct
19 Correct 4 ms 5228 KB Output is correct
20 Correct 4 ms 5228 KB Output is correct
21 Correct 4 ms 5240 KB Output is correct
22 Correct 4 ms 5248 KB Output is correct
23 Correct 4 ms 5228 KB Output is correct
24 Correct 5 ms 5228 KB Output is correct
25 Correct 5 ms 5228 KB Output is correct
26 Correct 4 ms 5284 KB Output is correct
27 Correct 4 ms 5228 KB Output is correct
28 Correct 6 ms 5228 KB Output is correct
29 Correct 5 ms 5248 KB Output is correct
30 Correct 5 ms 5248 KB Output is correct
31 Correct 4 ms 5228 KB Output is correct
32 Correct 4 ms 5228 KB Output is correct
33 Correct 4 ms 5228 KB Output is correct
34 Correct 4 ms 5228 KB Output is correct
35 Correct 4 ms 5228 KB Output is correct
36 Correct 4 ms 5228 KB Output is correct
37 Correct 4 ms 5228 KB Output is correct
38 Correct 4 ms 5228 KB Output is correct
39 Correct 5 ms 5228 KB Output is correct
40 Correct 4 ms 5228 KB Output is correct
41 Correct 4 ms 5228 KB Output is correct
42 Correct 4 ms 5228 KB Output is correct
43 Correct 4 ms 5228 KB Output is correct
44 Correct 4 ms 5228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 5 ms 5248 KB Output is correct
7 Correct 5 ms 5228 KB Output is correct
8 Correct 4 ms 5228 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 4 ms 5228 KB Output is correct
11 Correct 4 ms 5228 KB Output is correct
12 Correct 4 ms 5228 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 5 ms 5228 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 5 ms 5228 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 5 ms 5228 KB Output is correct
19 Correct 4 ms 5228 KB Output is correct
20 Correct 4 ms 5228 KB Output is correct
21 Correct 4 ms 5240 KB Output is correct
22 Correct 4 ms 5248 KB Output is correct
23 Correct 4 ms 5228 KB Output is correct
24 Correct 5 ms 5228 KB Output is correct
25 Correct 5 ms 5228 KB Output is correct
26 Correct 4 ms 5284 KB Output is correct
27 Correct 4 ms 5228 KB Output is correct
28 Correct 6 ms 5228 KB Output is correct
29 Correct 5 ms 5248 KB Output is correct
30 Correct 5 ms 5248 KB Output is correct
31 Correct 4 ms 5228 KB Output is correct
32 Correct 4 ms 5228 KB Output is correct
33 Correct 4 ms 5228 KB Output is correct
34 Correct 4 ms 5228 KB Output is correct
35 Correct 4 ms 5228 KB Output is correct
36 Correct 4 ms 5228 KB Output is correct
37 Correct 4 ms 5228 KB Output is correct
38 Correct 4 ms 5228 KB Output is correct
39 Correct 5 ms 5228 KB Output is correct
40 Correct 4 ms 5228 KB Output is correct
41 Correct 4 ms 5228 KB Output is correct
42 Correct 4 ms 5228 KB Output is correct
43 Correct 4 ms 5228 KB Output is correct
44 Correct 4 ms 5228 KB Output is correct
45 Correct 5 ms 5356 KB Output is correct
46 Correct 14 ms 5996 KB Output is correct
47 Correct 12 ms 6016 KB Output is correct
48 Correct 11 ms 5996 KB Output is correct
49 Correct 8 ms 6124 KB Output is correct
50 Correct 10 ms 6124 KB Output is correct
51 Correct 8 ms 6124 KB Output is correct
52 Correct 9 ms 6252 KB Output is correct
53 Correct 9 ms 6252 KB Output is correct
54 Correct 9 ms 6252 KB Output is correct
55 Correct 9 ms 6252 KB Output is correct
56 Correct 9 ms 6124 KB Output is correct
57 Correct 12 ms 5996 KB Output is correct
58 Correct 12 ms 6124 KB Output is correct
59 Correct 12 ms 5996 KB Output is correct
60 Correct 12 ms 5996 KB Output is correct
61 Correct 9 ms 6124 KB Output is correct
62 Correct 9 ms 6124 KB Output is correct
63 Correct 9 ms 6252 KB Output is correct
64 Correct 9 ms 5612 KB Output is correct
65 Correct 11 ms 5740 KB Output is correct
66 Correct 10 ms 5740 KB Output is correct
67 Correct 10 ms 5740 KB Output is correct
68 Correct 7 ms 5868 KB Output is correct
69 Correct 10 ms 6252 KB Output is correct
70 Correct 7 ms 5996 KB Output is correct
71 Correct 8 ms 5868 KB Output is correct
72 Correct 12 ms 5996 KB Output is correct
73 Correct 12 ms 5740 KB Output is correct
74 Correct 8 ms 5868 KB Output is correct
75 Correct 12 ms 5996 KB Output is correct
76 Correct 12 ms 6016 KB Output is correct
77 Correct 10 ms 5996 KB Output is correct
78 Correct 8 ms 5740 KB Output is correct
79 Correct 8 ms 5740 KB Output is correct
80 Correct 8 ms 5740 KB Output is correct
81 Correct 10 ms 5996 KB Output is correct
82 Correct 10 ms 5996 KB Output is correct
83 Correct 9 ms 5996 KB Output is correct
84 Correct 8 ms 5740 KB Output is correct
85 Correct 8 ms 5740 KB Output is correct
86 Correct 8 ms 5740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 5228 KB Output is correct
2 Correct 4 ms 5100 KB Output is correct
3 Correct 4 ms 5100 KB Output is correct
4 Correct 4 ms 5228 KB Output is correct
5 Correct 4 ms 5228 KB Output is correct
6 Correct 5 ms 5248 KB Output is correct
7 Correct 5 ms 5228 KB Output is correct
8 Correct 4 ms 5228 KB Output is correct
9 Correct 4 ms 5228 KB Output is correct
10 Correct 4 ms 5228 KB Output is correct
11 Correct 4 ms 5228 KB Output is correct
12 Correct 4 ms 5228 KB Output is correct
13 Correct 4 ms 5228 KB Output is correct
14 Correct 5 ms 5228 KB Output is correct
15 Correct 5 ms 5248 KB Output is correct
16 Correct 5 ms 5228 KB Output is correct
17 Correct 5 ms 5228 KB Output is correct
18 Correct 5 ms 5228 KB Output is correct
19 Correct 4 ms 5228 KB Output is correct
20 Correct 4 ms 5228 KB Output is correct
21 Correct 4 ms 5240 KB Output is correct
22 Correct 4 ms 5248 KB Output is correct
23 Correct 4 ms 5228 KB Output is correct
24 Correct 5 ms 5228 KB Output is correct
25 Correct 5 ms 5228 KB Output is correct
26 Correct 4 ms 5284 KB Output is correct
27 Correct 4 ms 5228 KB Output is correct
28 Correct 6 ms 5228 KB Output is correct
29 Correct 5 ms 5248 KB Output is correct
30 Correct 5 ms 5248 KB Output is correct
31 Correct 4 ms 5228 KB Output is correct
32 Correct 4 ms 5228 KB Output is correct
33 Correct 4 ms 5228 KB Output is correct
34 Correct 4 ms 5228 KB Output is correct
35 Correct 4 ms 5228 KB Output is correct
36 Correct 4 ms 5228 KB Output is correct
37 Correct 4 ms 5228 KB Output is correct
38 Correct 4 ms 5228 KB Output is correct
39 Correct 5 ms 5228 KB Output is correct
40 Correct 4 ms 5228 KB Output is correct
41 Correct 4 ms 5228 KB Output is correct
42 Correct 4 ms 5228 KB Output is correct
43 Correct 4 ms 5228 KB Output is correct
44 Correct 4 ms 5228 KB Output is correct
45 Correct 5 ms 5356 KB Output is correct
46 Correct 14 ms 5996 KB Output is correct
47 Correct 12 ms 6016 KB Output is correct
48 Correct 11 ms 5996 KB Output is correct
49 Correct 8 ms 6124 KB Output is correct
50 Correct 10 ms 6124 KB Output is correct
51 Correct 8 ms 6124 KB Output is correct
52 Correct 9 ms 6252 KB Output is correct
53 Correct 9 ms 6252 KB Output is correct
54 Correct 9 ms 6252 KB Output is correct
55 Correct 9 ms 6252 KB Output is correct
56 Correct 9 ms 6124 KB Output is correct
57 Correct 12 ms 5996 KB Output is correct
58 Correct 12 ms 6124 KB Output is correct
59 Correct 12 ms 5996 KB Output is correct
60 Correct 12 ms 5996 KB Output is correct
61 Correct 9 ms 6124 KB Output is correct
62 Correct 9 ms 6124 KB Output is correct
63 Correct 9 ms 6252 KB Output is correct
64 Correct 9 ms 5612 KB Output is correct
65 Correct 11 ms 5740 KB Output is correct
66 Correct 10 ms 5740 KB Output is correct
67 Correct 10 ms 5740 KB Output is correct
68 Correct 7 ms 5868 KB Output is correct
69 Correct 10 ms 6252 KB Output is correct
70 Correct 7 ms 5996 KB Output is correct
71 Correct 8 ms 5868 KB Output is correct
72 Correct 12 ms 5996 KB Output is correct
73 Correct 12 ms 5740 KB Output is correct
74 Correct 8 ms 5868 KB Output is correct
75 Correct 12 ms 5996 KB Output is correct
76 Correct 12 ms 6016 KB Output is correct
77 Correct 10 ms 5996 KB Output is correct
78 Correct 8 ms 5740 KB Output is correct
79 Correct 8 ms 5740 KB Output is correct
80 Correct 8 ms 5740 KB Output is correct
81 Correct 10 ms 5996 KB Output is correct
82 Correct 10 ms 5996 KB Output is correct
83 Correct 9 ms 5996 KB Output is correct
84 Correct 8 ms 5740 KB Output is correct
85 Correct 8 ms 5740 KB Output is correct
86 Correct 8 ms 5740 KB Output is correct
87 Correct 23 ms 7144 KB Output is correct
88 Correct 73 ms 11368 KB Output is correct
89 Correct 388 ms 25828 KB Output is correct
90 Correct 371 ms 25828 KB Output is correct
91 Correct 372 ms 25932 KB Output is correct
92 Correct 235 ms 31076 KB Output is correct
93 Correct 226 ms 31076 KB Output is correct
94 Correct 220 ms 31076 KB Output is correct
95 Correct 212 ms 32868 KB Output is correct
96 Correct 222 ms 32868 KB Output is correct
97 Correct 236 ms 32868 KB Output is correct
98 Correct 224 ms 32868 KB Output is correct
99 Correct 238 ms 29668 KB Output is correct
100 Correct 439 ms 25700 KB Output is correct
101 Correct 488 ms 25700 KB Output is correct
102 Correct 462 ms 25828 KB Output is correct
103 Correct 537 ms 25700 KB Output is correct
104 Correct 241 ms 29668 KB Output is correct
105 Correct 259 ms 29924 KB Output is correct
106 Correct 250 ms 29668 KB Output is correct
107 Correct 235 ms 18788 KB Output is correct
108 Correct 284 ms 19564 KB Output is correct
109 Correct 322 ms 21476 KB Output is correct
110 Correct 131 ms 24856 KB Output is correct
111 Correct 237 ms 33052 KB Output is correct
112 Correct 140 ms 26596 KB Output is correct
113 Correct 144 ms 23396 KB Output is correct
114 Correct 438 ms 25700 KB Output is correct
115 Correct 370 ms 19556 KB Output is correct
116 Correct 168 ms 23396 KB Output is correct
117 Correct 215 ms 27364 KB Output is correct
118 Correct 247 ms 26884 KB Output is correct
119 Correct 231 ms 26340 KB Output is correct
120 Correct 146 ms 21476 KB Output is correct
121 Correct 150 ms 20708 KB Output is correct
122 Correct 163 ms 20452 KB Output is correct
123 Correct 247 ms 27364 KB Output is correct
124 Correct 255 ms 26724 KB Output is correct
125 Correct 248 ms 26468 KB Output is correct
126 Correct 186 ms 21476 KB Output is correct
127 Correct 180 ms 20836 KB Output is correct
128 Correct 180 ms 20452 KB Output is correct