답안 #701664

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
701664 2023-02-21T18:41:04 Z urosk Construction of Highway (JOI18_construction) C++14
100 / 100
1136 ms 36456 KB
#define here cerr<<"===========================================\n"
#define dbg(x) cerr<<#x<<": "<<x<<endl;
#include "bits/stdc++.h"
//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define llinf 100000000000000000LL // 10^17
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define ceri(a,l,r) {cerr<<#a<<": ";for(ll i_ = l;i_<=r;i_++) cerr<<a[i_]<< " ";cerr<<endl;}
#define cer(a) {cerr<<#a<<": ";for(ll x_ : a) cerr<<x_<< " ";cerr<<endl;}

#define daj_mi_malo_vremena ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);

using namespace std;
//using namespace __gnu_pbds;
/*
ll add(ll x,ll y){
    x+=y;
    if(x<0){
        x%=mod;
        x+=mod;
    }else{
        if(x>=mod) x%=mod;
    }
    return x;
}
ll mul(ll a,ll b){
	ll ans = (a*b)%mod;
	if(ans<0) ans+=mod;
	return ans;
}
typedef tree<int,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
typedef tree<int,null_type,less_equal<ll>,rb_tree_tag,tree_order_statistics_node_update> ordered_multiset;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rnd(ll l,ll r){
    return uniform_int_distribution<ll>(l,r)(rng);
}
*/
#define maxn 100005
ll n;
vector<ll> g[maxn];
vector<pll> v[maxn];
pll e[maxn];
ll c[maxn],sub[maxn],par[maxn],b[maxn],pos[maxn],siz[maxn],f[maxn];
ll dfssub(ll u,ll p){
    sub[u] = 1;
    for(ll s : g[u]) if(s!=p) sub[u]+=dfssub(s,u);
    return sub[u];
}
void dfs(ll u,ll p,ll up){
    v[up].pb({1,c[u]});
    ll c = 0;
    par[u] = p;
    b[u] = up;
    siz[up]++;
    pos[u] = sz(v[up]);
    for(ll s : g[u]) if(s!=p&&sub[s]>sub[c]) c = s;
    if(c==0) return;
    dfs(c,u,up);
    for(ll s : g[u]){
        if(s==p||s==c) continue;
        dfs(s,u,s);
    }
}
ll t[2*maxn],ls[2*maxn],rs[2*maxn],tsz = 0,root = 0,mx = 0;
void upd(ll v,ll tl,ll tr,ll i,ll x){
    if(tl==tr){t[v]+=x;return;}
    ll mid = (tl+tr)/2;
    if(i<=mid) upd(ls[v],tl,mid,i,x);
    else upd(rs[v],mid+1,tr,i,x);
    t[v] = t[ls[v]] + t[rs[v]];
}
ll sum(ll v,ll tl,ll tr,ll l,ll r){
    if(l>r||tl>tr||tl>r||tr<l) return 0;
    if(tl>=l&&tr<=r) return t[v];
    ll mid = (tl+tr)/2;
    return sum(ls[v],tl,mid,l,r) + sum(rs[v],mid+1,tr,l,r);
}
void init(ll &v,ll tl,ll tr){
    if(!v) v = ++tsz;
    if(tl==tr) return;
    ll mid = (tl+tr)/2;
    init(ls[v],tl,mid);
    init(rs[v],mid+1,tr);
}
void tc(){
    cin >> n;
    for(ll i = 1;i<=n;i++) cin >> c[i];
    {
        set<ll> s; map<ll,ll> mp; ll it = 0;
        for(ll i = 1;i<=n;i++) s.insert(c[i]);
        for(ll x : s) mp[x] = ++it;
        for(ll i = 1;i<=n;i++) c[i] = mp[c[i]];
        mx = it+1;
    }
    for(ll i = 1;i<=n-1;i++){
        ll x,y; cin >> x >> y;
        e[i] = {x,y};
        g[x].pb(y);
        g[y].pb(x);
    }
    dfssub(1,1);
    dfs(1,1,1);
    init(root,1,mx);
    stack<pll> st;
    for(ll i = 1;i<=n;i++) reverse(all(v[i]));
    for(ll i = 1;i<=n-1;i++){
        ll x = e[i].fi,y = e[i].sc;
        if(par[x]==y) swap(x,y);
        vector<ll> w;
        while(1){
            w.pb(b[x]);
            f[b[x]] = x;
            if(b[x]==1) break;
            x = par[b[x]];
        }
        reverse(all(w));
        x = e[i].fi^e[i].sc^y;
        ll ans = 0;
        for(ll z : w){
            ll cnt = 0;
            ll x = f[z];
            for(ll j = sz(v[z])-1;j>=0;j--){
                pll p = v[z][j];
                cnt+=p.fi;
                if(cnt<pos[x]){
                    ans+=p.fi*sum(root,1,mx,p.sc+1,mx);
                    upd(root,1,mx,p.sc,p.fi);
                    st.push(p);
                }else{
                    ans+=(p.fi-(cnt-pos[x]))*sum(root,1,mx,p.sc+1,mx);
                    upd(root,1,mx,p.sc,p.fi-(cnt-pos[x]));
                    st.push({p.fi-(cnt-pos[x]),p.sc});
                    break;
                }
            }
        }
        for(ll z : w){
            ll x = f[z];
            ll cnt = 0;
            for(ll j = sz(v[z])-1;j>=0;j--){
                pll p = v[z][j];
                cnt+=p.fi;
                v[z].popb();
                if(cnt>=pos[x]){
                    if(cnt!=pos[x]) v[z].pb({cnt-pos[x],p.sc});
                    break;
                }
            }
            v[z].pb({pos[x],c[y]});
        }
        while(sz(st)){
            pll p = st.top();
            st.pop();
            upd(root,1,mx,p.sc,-p.fi);
        }
        cout<<ans<<endl;
    }
}
int main(){
	daj_mi_malo_vremena
    int t; t = 1;
    while(t--){
        tc();
    }
	return 0;
}
/**
5
1 2 3 4 5
1 2
2 3
2 4
3 5

10
1 7 3 4 8 6 2 9 10 5
1 2
1 3
2 4
3 5
2 6
3 7
4 8
5 9
6 10
**/
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5024 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 4 ms 5108 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5156 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 4 ms 5204 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 4 ms 5148 KB Output is correct
13 Correct 3 ms 5160 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 4 ms 5156 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5164 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 3 ms 5160 KB Output is correct
21 Correct 3 ms 5160 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5080 KB Output is correct
24 Correct 3 ms 5164 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 4 ms 5152 KB Output is correct
28 Correct 4 ms 5160 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 5 ms 5152 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5160 KB Output is correct
33 Correct 3 ms 5204 KB Output is correct
34 Correct 3 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 3 ms 5152 KB Output is correct
37 Correct 3 ms 5152 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5204 KB Output is correct
40 Correct 3 ms 5156 KB Output is correct
41 Correct 4 ms 5156 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5156 KB Output is correct
44 Correct 3 ms 5160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5024 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 4 ms 5108 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5156 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 4 ms 5204 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 4 ms 5148 KB Output is correct
13 Correct 3 ms 5160 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 4 ms 5156 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5164 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 3 ms 5160 KB Output is correct
21 Correct 3 ms 5160 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5080 KB Output is correct
24 Correct 3 ms 5164 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 4 ms 5152 KB Output is correct
28 Correct 4 ms 5160 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 5 ms 5152 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5160 KB Output is correct
33 Correct 3 ms 5204 KB Output is correct
34 Correct 3 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 3 ms 5152 KB Output is correct
37 Correct 3 ms 5152 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5204 KB Output is correct
40 Correct 3 ms 5156 KB Output is correct
41 Correct 4 ms 5156 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5156 KB Output is correct
44 Correct 3 ms 5160 KB Output is correct
45 Correct 5 ms 5288 KB Output is correct
46 Correct 13 ms 6124 KB Output is correct
47 Correct 14 ms 6144 KB Output is correct
48 Correct 13 ms 6140 KB Output is correct
49 Correct 9 ms 6356 KB Output is correct
50 Correct 9 ms 6356 KB Output is correct
51 Correct 11 ms 6260 KB Output is correct
52 Correct 14 ms 6168 KB Output is correct
53 Correct 11 ms 6216 KB Output is correct
54 Correct 9 ms 6188 KB Output is correct
55 Correct 10 ms 6188 KB Output is correct
56 Correct 9 ms 6184 KB Output is correct
57 Correct 17 ms 6064 KB Output is correct
58 Correct 21 ms 6088 KB Output is correct
59 Correct 21 ms 6128 KB Output is correct
60 Correct 22 ms 6128 KB Output is correct
61 Correct 9 ms 6228 KB Output is correct
62 Correct 10 ms 6228 KB Output is correct
63 Correct 9 ms 6188 KB Output is correct
64 Correct 7 ms 5588 KB Output is correct
65 Correct 7 ms 5588 KB Output is correct
66 Correct 13 ms 5604 KB Output is correct
67 Correct 12 ms 5768 KB Output is correct
68 Correct 6 ms 5844 KB Output is correct
69 Correct 9 ms 6228 KB Output is correct
70 Correct 6 ms 5844 KB Output is correct
71 Correct 6 ms 5800 KB Output is correct
72 Correct 18 ms 6056 KB Output is correct
73 Correct 12 ms 5676 KB Output is correct
74 Correct 7 ms 5720 KB Output is correct
75 Correct 11 ms 6104 KB Output is correct
76 Correct 10 ms 6104 KB Output is correct
77 Correct 9 ms 6112 KB Output is correct
78 Correct 6 ms 5728 KB Output is correct
79 Correct 8 ms 5684 KB Output is correct
80 Correct 7 ms 5664 KB Output is correct
81 Correct 11 ms 6060 KB Output is correct
82 Correct 11 ms 6124 KB Output is correct
83 Correct 13 ms 6100 KB Output is correct
84 Correct 6 ms 5716 KB Output is correct
85 Correct 7 ms 5716 KB Output is correct
86 Correct 6 ms 5588 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5076 KB Output is correct
2 Correct 4 ms 5024 KB Output is correct
3 Correct 3 ms 5076 KB Output is correct
4 Correct 3 ms 5076 KB Output is correct
5 Correct 4 ms 5204 KB Output is correct
6 Correct 4 ms 5108 KB Output is correct
7 Correct 4 ms 5156 KB Output is correct
8 Correct 3 ms 5156 KB Output is correct
9 Correct 3 ms 5204 KB Output is correct
10 Correct 4 ms 5204 KB Output is correct
11 Correct 3 ms 5204 KB Output is correct
12 Correct 4 ms 5148 KB Output is correct
13 Correct 3 ms 5160 KB Output is correct
14 Correct 3 ms 5204 KB Output is correct
15 Correct 4 ms 5156 KB Output is correct
16 Correct 4 ms 5204 KB Output is correct
17 Correct 4 ms 5164 KB Output is correct
18 Correct 4 ms 5204 KB Output is correct
19 Correct 3 ms 5204 KB Output is correct
20 Correct 3 ms 5160 KB Output is correct
21 Correct 3 ms 5160 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 3 ms 5080 KB Output is correct
24 Correct 3 ms 5164 KB Output is correct
25 Correct 3 ms 5076 KB Output is correct
26 Correct 4 ms 5076 KB Output is correct
27 Correct 4 ms 5152 KB Output is correct
28 Correct 4 ms 5160 KB Output is correct
29 Correct 3 ms 5076 KB Output is correct
30 Correct 5 ms 5152 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5160 KB Output is correct
33 Correct 3 ms 5204 KB Output is correct
34 Correct 3 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 3 ms 5152 KB Output is correct
37 Correct 3 ms 5152 KB Output is correct
38 Correct 3 ms 5076 KB Output is correct
39 Correct 3 ms 5204 KB Output is correct
40 Correct 3 ms 5156 KB Output is correct
41 Correct 4 ms 5156 KB Output is correct
42 Correct 3 ms 5076 KB Output is correct
43 Correct 3 ms 5156 KB Output is correct
44 Correct 3 ms 5160 KB Output is correct
45 Correct 5 ms 5288 KB Output is correct
46 Correct 13 ms 6124 KB Output is correct
47 Correct 14 ms 6144 KB Output is correct
48 Correct 13 ms 6140 KB Output is correct
49 Correct 9 ms 6356 KB Output is correct
50 Correct 9 ms 6356 KB Output is correct
51 Correct 11 ms 6260 KB Output is correct
52 Correct 14 ms 6168 KB Output is correct
53 Correct 11 ms 6216 KB Output is correct
54 Correct 9 ms 6188 KB Output is correct
55 Correct 10 ms 6188 KB Output is correct
56 Correct 9 ms 6184 KB Output is correct
57 Correct 17 ms 6064 KB Output is correct
58 Correct 21 ms 6088 KB Output is correct
59 Correct 21 ms 6128 KB Output is correct
60 Correct 22 ms 6128 KB Output is correct
61 Correct 9 ms 6228 KB Output is correct
62 Correct 10 ms 6228 KB Output is correct
63 Correct 9 ms 6188 KB Output is correct
64 Correct 7 ms 5588 KB Output is correct
65 Correct 7 ms 5588 KB Output is correct
66 Correct 13 ms 5604 KB Output is correct
67 Correct 12 ms 5768 KB Output is correct
68 Correct 6 ms 5844 KB Output is correct
69 Correct 9 ms 6228 KB Output is correct
70 Correct 6 ms 5844 KB Output is correct
71 Correct 6 ms 5800 KB Output is correct
72 Correct 18 ms 6056 KB Output is correct
73 Correct 12 ms 5676 KB Output is correct
74 Correct 7 ms 5720 KB Output is correct
75 Correct 11 ms 6104 KB Output is correct
76 Correct 10 ms 6104 KB Output is correct
77 Correct 9 ms 6112 KB Output is correct
78 Correct 6 ms 5728 KB Output is correct
79 Correct 8 ms 5684 KB Output is correct
80 Correct 7 ms 5664 KB Output is correct
81 Correct 11 ms 6060 KB Output is correct
82 Correct 11 ms 6124 KB Output is correct
83 Correct 13 ms 6100 KB Output is correct
84 Correct 6 ms 5716 KB Output is correct
85 Correct 7 ms 5716 KB Output is correct
86 Correct 6 ms 5588 KB Output is correct
87 Correct 38 ms 7600 KB Output is correct
88 Correct 129 ms 12620 KB Output is correct
89 Correct 608 ms 30196 KB Output is correct
90 Correct 588 ms 30224 KB Output is correct
91 Correct 601 ms 30172 KB Output is correct
92 Correct 301 ms 36456 KB Output is correct
93 Correct 266 ms 36300 KB Output is correct
94 Correct 288 ms 36408 KB Output is correct
95 Correct 226 ms 32880 KB Output is correct
96 Correct 281 ms 33264 KB Output is correct
97 Correct 312 ms 33404 KB Output is correct
98 Correct 275 ms 33192 KB Output is correct
99 Correct 282 ms 33484 KB Output is correct
100 Correct 754 ms 29824 KB Output is correct
101 Correct 1108 ms 30432 KB Output is correct
102 Correct 1123 ms 30344 KB Output is correct
103 Correct 1136 ms 30312 KB Output is correct
104 Correct 308 ms 33344 KB Output is correct
105 Correct 278 ms 33348 KB Output is correct
106 Correct 298 ms 33308 KB Output is correct
107 Correct 173 ms 19924 KB Output is correct
108 Correct 250 ms 20132 KB Output is correct
109 Correct 498 ms 21740 KB Output is correct
110 Correct 150 ms 24812 KB Output is correct
111 Correct 218 ms 32868 KB Output is correct
112 Correct 128 ms 24000 KB Output is correct
113 Correct 119 ms 23308 KB Output is correct
114 Correct 754 ms 29928 KB Output is correct
115 Correct 513 ms 19948 KB Output is correct
116 Correct 139 ms 23332 KB Output is correct
117 Correct 296 ms 30280 KB Output is correct
118 Correct 287 ms 29792 KB Output is correct
119 Correct 279 ms 29300 KB Output is correct
120 Correct 126 ms 21212 KB Output is correct
121 Correct 131 ms 20656 KB Output is correct
122 Correct 135 ms 20340 KB Output is correct
123 Correct 318 ms 30412 KB Output is correct
124 Correct 318 ms 29784 KB Output is correct
125 Correct 325 ms 29420 KB Output is correct
126 Correct 152 ms 21404 KB Output is correct
127 Correct 149 ms 20664 KB Output is correct
128 Correct 161 ms 20312 KB Output is correct