Submission #631908

# Submission time Handle Problem Language Result Execution time Memory
631908 2022-08-19T06:26:02 Z radal Construction of Highway (JOI18_construction) C++17
100 / 100
1935 ms 28064 KB
#include <bits/stdc++.h>
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define pb push_back
#define X first
#define Y second
#define debug(x) cerr << #x << " : " << x << endl;
#define all(x) x.begin() , x.end()

using namespace std;

typedef pair<int,int> pll;
typedef long long ll;

constexpr int N = 1e5+10,lg = 18;

int tin[N],sz[N],seg[N*4],T,n;
int h[N],par[N][lg],c[N],fen[N];
vector<int> adj[N],ti,ci;
vector<pll> e;
void dfs(int v){
    ti.pb(v);
    tin[v] = T++;
    sz[v] = 1;
    for (int u : adj[v]){
        h[u] = h[v]+1;
        par[u][0] = v;
        dfs(u);
        sz[v] += sz[u];
    }
}

void upd(int l,int r,int p,int x,int v = 1){
    if (r-l == 1){
        seg[v] = x;
        return;
    }
    int m = (l+r) >> 1,u = (v << 1);
    if (p < m) upd(l,m,p,x,u);
    else upd(m,r,p,x,u|1);
    seg[v] = max(seg[u],seg[u|1]);
}

int que(int l,int r,int s,int e,int v = 1){
    if (l >= e || s >= r) return 0;
    if (s <= l && r <= e) return seg[v];
    int m = (l+r) >> 1,u = (v << 1);
    return max(que(l,m,s,e,u),que(m,r,s,e,u|1));
}

void upd(int r,int x){
    for (; r < n; r |= (r+1))
        fen[r] += x;
}

int que(int l){
    int ans = 0;
    for (; l >= 0; l = (l&(l+1))-1)
        ans += fen[l];
    return ans;
}
int main(){
    ios_base :: sync_with_stdio(0); cin.tie(0);
    cin >> n;
    rep(i,1,n+1){
        cin >> c[i];
        ci.pb(c[i]);
    }
    sort(all(ci));
    rep(i,1,n+1){
        c[i] = lower_bound(all(ci),c[i])-ci.begin();
    }
    e.pb({1,1});
    rep(i,1,n){
        int u,v;
        cin >> u >> v;
        e.pb({u,v});
        adj[u].pb(v);
    }
    dfs(1);
    rep(j,1,lg)
        rep(i,2,n+1)
            par[i][j] = par[par[i][j-1]][j-1];

    rep(j,1,n){
        int u = e[j].X,v = e[j].Y;
        vector<pll> ve;
        int cur = u;
        while (cur){
            int beg = cur,mx = que(0,n,tin[cur],tin[cur]+sz[cur]);
            repr(i,lg-1,0){
                if (!par[cur][i]) continue;
                int w = par[cur][i];
                if (que(0,n,tin[w],tin[w]+sz[w]) == mx) cur = w;
            }
            ve.pb({-h[cur]+h[beg]+1,c[e[mx].Y]});
            cur = par[cur][0];
        }
        ll ans = 0;
        for (auto a : ve){
            ans += 1ll*a.X*que(a.Y-1);
            upd(a.Y,a.X);
        }
        for (auto a : ve) upd(a.Y,-a.X);
        upd(0,n,tin[v],j);
        cout << ans << endl;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2688 KB Output is correct
6 Correct 3 ms 2684 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 3 ms 2824 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 7 ms 2772 KB Output is correct
14 Correct 4 ms 2772 KB Output is correct
15 Correct 4 ms 2772 KB Output is correct
16 Correct 5 ms 2772 KB Output is correct
17 Correct 6 ms 2772 KB Output is correct
18 Correct 4 ms 2684 KB Output is correct
19 Correct 3 ms 2716 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 3 ms 2684 KB Output is correct
25 Correct 3 ms 2772 KB Output is correct
26 Correct 3 ms 2772 KB Output is correct
27 Correct 5 ms 2772 KB Output is correct
28 Correct 4 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 4 ms 2784 KB Output is correct
31 Correct 3 ms 2684 KB Output is correct
32 Correct 3 ms 2772 KB Output is correct
33 Correct 3 ms 2772 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 3 ms 2772 KB Output is correct
36 Correct 4 ms 2688 KB Output is correct
37 Correct 4 ms 2724 KB Output is correct
38 Correct 3 ms 2772 KB Output is correct
39 Correct 3 ms 2772 KB Output is correct
40 Correct 3 ms 2684 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 3 ms 2812 KB Output is correct
43 Correct 3 ms 2680 KB Output is correct
44 Correct 5 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2688 KB Output is correct
6 Correct 3 ms 2684 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 3 ms 2824 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 7 ms 2772 KB Output is correct
14 Correct 4 ms 2772 KB Output is correct
15 Correct 4 ms 2772 KB Output is correct
16 Correct 5 ms 2772 KB Output is correct
17 Correct 6 ms 2772 KB Output is correct
18 Correct 4 ms 2684 KB Output is correct
19 Correct 3 ms 2716 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 3 ms 2684 KB Output is correct
25 Correct 3 ms 2772 KB Output is correct
26 Correct 3 ms 2772 KB Output is correct
27 Correct 5 ms 2772 KB Output is correct
28 Correct 4 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 4 ms 2784 KB Output is correct
31 Correct 3 ms 2684 KB Output is correct
32 Correct 3 ms 2772 KB Output is correct
33 Correct 3 ms 2772 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 3 ms 2772 KB Output is correct
36 Correct 4 ms 2688 KB Output is correct
37 Correct 4 ms 2724 KB Output is correct
38 Correct 3 ms 2772 KB Output is correct
39 Correct 3 ms 2772 KB Output is correct
40 Correct 3 ms 2684 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 3 ms 2812 KB Output is correct
43 Correct 3 ms 2680 KB Output is correct
44 Correct 5 ms 2772 KB Output is correct
45 Correct 6 ms 2772 KB Output is correct
46 Correct 19 ms 3284 KB Output is correct
47 Correct 21 ms 3204 KB Output is correct
48 Correct 17 ms 3320 KB Output is correct
49 Correct 11 ms 3592 KB Output is correct
50 Correct 11 ms 3668 KB Output is correct
51 Correct 12 ms 3592 KB Output is correct
52 Correct 20 ms 3456 KB Output is correct
53 Correct 18 ms 3460 KB Output is correct
54 Correct 18 ms 3412 KB Output is correct
55 Correct 18 ms 3460 KB Output is correct
56 Correct 18 ms 3456 KB Output is correct
57 Correct 30 ms 3284 KB Output is correct
58 Correct 29 ms 3284 KB Output is correct
59 Correct 29 ms 3224 KB Output is correct
60 Correct 54 ms 3312 KB Output is correct
61 Correct 18 ms 3460 KB Output is correct
62 Correct 18 ms 3412 KB Output is correct
63 Correct 18 ms 3416 KB Output is correct
64 Correct 21 ms 3284 KB Output is correct
65 Correct 17 ms 3276 KB Output is correct
66 Correct 19 ms 3276 KB Output is correct
67 Correct 17 ms 3304 KB Output is correct
68 Correct 11 ms 3540 KB Output is correct
69 Correct 17 ms 3444 KB Output is correct
70 Correct 16 ms 3460 KB Output is correct
71 Correct 17 ms 3412 KB Output is correct
72 Correct 27 ms 3280 KB Output is correct
73 Correct 36 ms 3284 KB Output is correct
74 Correct 19 ms 3412 KB Output is correct
75 Correct 16 ms 3392 KB Output is correct
76 Correct 24 ms 3284 KB Output is correct
77 Correct 19 ms 3336 KB Output is correct
78 Correct 25 ms 3292 KB Output is correct
79 Correct 17 ms 3284 KB Output is correct
80 Correct 16 ms 3328 KB Output is correct
81 Correct 18 ms 3288 KB Output is correct
82 Correct 17 ms 3352 KB Output is correct
83 Correct 16 ms 3288 KB Output is correct
84 Correct 17 ms 3284 KB Output is correct
85 Correct 17 ms 3328 KB Output is correct
86 Correct 19 ms 3284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2680 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 3 ms 2688 KB Output is correct
6 Correct 3 ms 2684 KB Output is correct
7 Correct 3 ms 2772 KB Output is correct
8 Correct 3 ms 2772 KB Output is correct
9 Correct 3 ms 2824 KB Output is correct
10 Correct 4 ms 2772 KB Output is correct
11 Correct 3 ms 2772 KB Output is correct
12 Correct 3 ms 2772 KB Output is correct
13 Correct 7 ms 2772 KB Output is correct
14 Correct 4 ms 2772 KB Output is correct
15 Correct 4 ms 2772 KB Output is correct
16 Correct 5 ms 2772 KB Output is correct
17 Correct 6 ms 2772 KB Output is correct
18 Correct 4 ms 2684 KB Output is correct
19 Correct 3 ms 2716 KB Output is correct
20 Correct 5 ms 2688 KB Output is correct
21 Correct 3 ms 2772 KB Output is correct
22 Correct 3 ms 2772 KB Output is correct
23 Correct 3 ms 2772 KB Output is correct
24 Correct 3 ms 2684 KB Output is correct
25 Correct 3 ms 2772 KB Output is correct
26 Correct 3 ms 2772 KB Output is correct
27 Correct 5 ms 2772 KB Output is correct
28 Correct 4 ms 2772 KB Output is correct
29 Correct 3 ms 2772 KB Output is correct
30 Correct 4 ms 2784 KB Output is correct
31 Correct 3 ms 2684 KB Output is correct
32 Correct 3 ms 2772 KB Output is correct
33 Correct 3 ms 2772 KB Output is correct
34 Correct 3 ms 2772 KB Output is correct
35 Correct 3 ms 2772 KB Output is correct
36 Correct 4 ms 2688 KB Output is correct
37 Correct 4 ms 2724 KB Output is correct
38 Correct 3 ms 2772 KB Output is correct
39 Correct 3 ms 2772 KB Output is correct
40 Correct 3 ms 2684 KB Output is correct
41 Correct 3 ms 2772 KB Output is correct
42 Correct 3 ms 2812 KB Output is correct
43 Correct 3 ms 2680 KB Output is correct
44 Correct 5 ms 2772 KB Output is correct
45 Correct 6 ms 2772 KB Output is correct
46 Correct 19 ms 3284 KB Output is correct
47 Correct 21 ms 3204 KB Output is correct
48 Correct 17 ms 3320 KB Output is correct
49 Correct 11 ms 3592 KB Output is correct
50 Correct 11 ms 3668 KB Output is correct
51 Correct 12 ms 3592 KB Output is correct
52 Correct 20 ms 3456 KB Output is correct
53 Correct 18 ms 3460 KB Output is correct
54 Correct 18 ms 3412 KB Output is correct
55 Correct 18 ms 3460 KB Output is correct
56 Correct 18 ms 3456 KB Output is correct
57 Correct 30 ms 3284 KB Output is correct
58 Correct 29 ms 3284 KB Output is correct
59 Correct 29 ms 3224 KB Output is correct
60 Correct 54 ms 3312 KB Output is correct
61 Correct 18 ms 3460 KB Output is correct
62 Correct 18 ms 3412 KB Output is correct
63 Correct 18 ms 3416 KB Output is correct
64 Correct 21 ms 3284 KB Output is correct
65 Correct 17 ms 3276 KB Output is correct
66 Correct 19 ms 3276 KB Output is correct
67 Correct 17 ms 3304 KB Output is correct
68 Correct 11 ms 3540 KB Output is correct
69 Correct 17 ms 3444 KB Output is correct
70 Correct 16 ms 3460 KB Output is correct
71 Correct 17 ms 3412 KB Output is correct
72 Correct 27 ms 3280 KB Output is correct
73 Correct 36 ms 3284 KB Output is correct
74 Correct 19 ms 3412 KB Output is correct
75 Correct 16 ms 3392 KB Output is correct
76 Correct 24 ms 3284 KB Output is correct
77 Correct 19 ms 3336 KB Output is correct
78 Correct 25 ms 3292 KB Output is correct
79 Correct 17 ms 3284 KB Output is correct
80 Correct 16 ms 3328 KB Output is correct
81 Correct 18 ms 3288 KB Output is correct
82 Correct 17 ms 3352 KB Output is correct
83 Correct 16 ms 3288 KB Output is correct
84 Correct 17 ms 3284 KB Output is correct
85 Correct 17 ms 3328 KB Output is correct
86 Correct 19 ms 3284 KB Output is correct
87 Correct 57 ms 4340 KB Output is correct
88 Correct 173 ms 7524 KB Output is correct
89 Correct 780 ms 18812 KB Output is correct
90 Correct 807 ms 18976 KB Output is correct
91 Correct 882 ms 18728 KB Output is correct
92 Correct 443 ms 27984 KB Output is correct
93 Correct 422 ms 27968 KB Output is correct
94 Correct 427 ms 28064 KB Output is correct
95 Correct 874 ms 22540 KB Output is correct
96 Correct 638 ms 22960 KB Output is correct
97 Correct 774 ms 22952 KB Output is correct
98 Correct 694 ms 22860 KB Output is correct
99 Correct 667 ms 22796 KB Output is correct
100 Correct 1523 ms 18968 KB Output is correct
101 Correct 1565 ms 19256 KB Output is correct
102 Correct 1502 ms 19344 KB Output is correct
103 Correct 1541 ms 19196 KB Output is correct
104 Correct 729 ms 22684 KB Output is correct
105 Correct 767 ms 22696 KB Output is correct
106 Correct 722 ms 22664 KB Output is correct
107 Correct 826 ms 17740 KB Output is correct
108 Correct 726 ms 18224 KB Output is correct
109 Correct 1000 ms 18504 KB Output is correct
110 Correct 388 ms 27436 KB Output is correct
111 Correct 881 ms 22624 KB Output is correct
112 Correct 689 ms 22356 KB Output is correct
113 Correct 642 ms 22008 KB Output is correct
114 Correct 1584 ms 19016 KB Output is correct
115 Correct 1935 ms 18592 KB Output is correct
116 Correct 740 ms 22088 KB Output is correct
117 Correct 640 ms 20340 KB Output is correct
118 Correct 660 ms 19760 KB Output is correct
119 Correct 622 ms 19428 KB Output is correct
120 Correct 649 ms 20016 KB Output is correct
121 Correct 628 ms 19444 KB Output is correct
122 Correct 660 ms 19016 KB Output is correct
123 Correct 732 ms 20392 KB Output is correct
124 Correct 734 ms 19908 KB Output is correct
125 Correct 827 ms 19480 KB Output is correct
126 Correct 772 ms 20168 KB Output is correct
127 Correct 791 ms 19600 KB Output is correct
128 Correct 701 ms 19332 KB Output is correct