답안 #1049154

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1049154 2024-08-08T14:17:10 Z fryingduc Construction of Highway (JOI18_construction) C++17
100 / 100
1305 ms 32804 KB
#include "bits/stdc++.h"
using namespace std;

const int maxn = 1e5 + 5;
const int lg = 18;
int n, a[maxn];
vector<int> g[maxn];
int pos[maxn], cur_pos, head[maxn];
int up[maxn][lg + 1];
int sz[maxn], par[maxn];
int h[maxn];
int eu[maxn], ev[maxn];
int rev[maxn];

pair<int, int> tree[maxn * 4];
int lazy[maxn * 4];
pair<int, int> operator+(const pair<int, int> &a, const pair<int, int> &b) {
    return {max(a.first, b.first), min(a.second, b.second)};
}
struct fenwick_tree {
    #define gb(x) (x) & (-x)
    int n;
    vector<long long> bit;
    fenwick_tree() {}
    fenwick_tree(int n) : n(n), bit(n + 5, 0) {}
    void update(int x, long long val) {
        for(int i = x; i <= n; i += gb(i)) bit[i] += val;
    }
    long long get(int x) {
        int ans = 0;
        for(int i = x; i > 0; i -= gb(i)) ans += bit[i];
        
        return ans;
    }
} fen;
struct segment_tree {
    int n;
    segment_tree() {}
    segment_tree(int n) : n(n) {
        for(int i = 1; i <= n * 4; ++i) {
            tree[i] = {0, 2e9};
        }
    }
    void build(int ind, int l, int r) {
        if(l == r) {
            tree[ind] = {a[rev[l]], a[rev[l]]};
            return;
        }
        int mid = (l + r) / 2;
        build(ind * 2, l, mid);
        build(ind * 2 + 1, mid + 1, r);
        tree[ind] = tree[ind * 2] + tree[ind * 2 + 1];
    }
    void down(int ind, int l, int r) {
        tree[ind].first = tree[ind].second = lazy[ind];
        if(l != r) {
            lazy[ind * 2] = lazy[ind * 2 + 1] = lazy[ind];
        }
        lazy[ind] = 0;
    }
    void update(int ind, int l, int r, int x, int y, int val) {
        if(lazy[ind]) down(ind, l, r);
        if(l > y || r < x) return;
        if(x <= l and r <= y) {
            lazy[ind] = val;
            down(ind, l, r);
            return;
        }
        int mid = (l + r) / 2;
        update(ind * 2, l, mid, x, y, val);
        update(ind * 2 + 1, mid + 1, r, x, y, val);
        tree[ind] = tree[ind * 2] + tree[ind * 2 + 1];
    }
    pair<int, int> get(int ind, int l, int r, int x, int y) {
        if(lazy[ind]) down(ind, l, r);
        if(l > y || r < x) return {0, 2e9};
        if(x <= l and r <= y) return tree[ind];
        int mid = (l + r) / 2;
        return get(ind * 2, l, mid, x, y) + get(ind * 2 + 1, mid + 1, r, x, y);
    }
    void update(int x, int y, int val) {
        update(1, 1, n, x, y, val);
    }
    pair<int, int> get(int x, int y) {
        return get(1, 1, n, x, y);
    }
} st;
void pre_dfs(int u, int prev) {
    sz[u] = 1;
    for(auto v:g[u]) {
        if(v == prev) continue;
        par[v] = u;
        h[v] = h[u] + 1;
        up[v][0] = u;
        pre_dfs(v, u);
        sz[u] += sz[v];
    }
}
void hld(int u, int prev) {
    if(!head[u]) {
        head[u] = u;
    }
    pos[u] = ++cur_pos;
    rev[cur_pos] = u;
    int big_child = -1;
    for(auto v:g[u]) {
        if(v == prev) continue;
        if(big_child == -1 || sz[big_child] < sz[v]) big_child = v;
    }
    if(big_child != -1) {
        head[big_child] = head[u];
        hld(big_child, u);
    }
    for(auto v:g[u]) {
        if(v == prev || v == big_child) continue;
        hld(v, u);
    }
}
void update(int u, int v, int val) {
    while(head[u] != head[v]) {
        if(h[u] > h[v]) swap(u, v);
        st.update(pos[head[v]], pos[v], val);
        
        v = par[head[v]];
    }
    if(h[u] > h[v]) swap(u, v);
    st.update(pos[u], pos[v], val);
}
pair<int, int> get(int u, int v) {
    pair<int, int> ans = {0, 2e9};
    while(head[u] != head[v]) {
        if(h[u] > h[v]) swap(u, v);
        ans = ans + st.get(pos[head[v]], pos[v]);
        v = par[head[v]];
    }
    if(h[u] > h[v]) swap(u, v);
    ans = ans + st.get(pos[u], pos[v]);
    
    return ans;
}
void solve() {
    cin >> n;
    vector<int> cpr;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        cpr.push_back(a[i]);
    }
    sort(cpr.begin(), cpr.end());
    cpr.erase(unique(cpr.begin(), cpr.end()), cpr.end());
    
    for(int i = 1; i <= n; ++i)  {
        a[i] = lower_bound(cpr.begin(), cpr.end(), a[i]) - cpr.begin() + 1;
    }
    fen = fenwick_tree((int)cpr.size() + 5);
    for(int i = 1; i < n; ++i) {
        int u, v; cin >> u >> v;
        eu[i] = u, ev[i] = v;
        g[u].push_back(v);
//        g[v].push_back(u);
    }
    pre_dfs(1, 0);
    for(int i = 1; i <= lg; ++i) {
        for(int u = 1; u <= n; ++u) {
            up[u][i] = up[up[u][i - 1]][i - 1];
        }
    }
    hld(1, 0);
    st = segment_tree(n);
    st.build(1, 1, n);
    
    
    for(int t = 1; t < n; ++t) {
        int u = eu[t], v = ev[t];
        vector<pair<int, int>> vec;
        
        int ver = u;
        while(ver) {
            vec.push_back({st.get(pos[ver], pos[ver]).first, 1});
            for(int i = lg; i >= 0; --i) {
                if(!up[ver][i]) continue;
                int nxt = up[ver][i];
                pair<int, int> dit = get(ver, nxt);
                if(dit.first == dit.second) {
                    ver = nxt;
                    vec.back().second += (1 << i);
                }
            }
            ver = par[ver];
        }
        long long ans = 0;
        for(int i = 0; i < (int)vec.size(); ++i) {
            ans += vec[i].second * fen.get(vec[i].first - 1);
            
            fen.update(vec[i].first, vec[i].second);
        }                    
        for(auto i:vec) {
            fen.update(i.first, -i.second);
        }
        cout << ans << '\n';
        
        update(1, u, st.get(pos[v], pos[v]).first);
    }
    
}
signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    solve();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 1 ms 9564 KB Output is correct
3 Correct 1 ms 9564 KB Output is correct
4 Correct 1 ms 9740 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9644 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9624 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9568 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 3 ms 9752 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9564 KB Output is correct
20 Correct 2 ms 9564 KB Output is correct
21 Correct 2 ms 9564 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 2 ms 9564 KB Output is correct
24 Correct 2 ms 9560 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 1 ms 9564 KB Output is correct
27 Correct 2 ms 9564 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 2 ms 9564 KB Output is correct
30 Correct 2 ms 9564 KB Output is correct
31 Correct 3 ms 9740 KB Output is correct
32 Correct 2 ms 9692 KB Output is correct
33 Correct 2 ms 9704 KB Output is correct
34 Correct 2 ms 9564 KB Output is correct
35 Correct 2 ms 9564 KB Output is correct
36 Correct 2 ms 9564 KB Output is correct
37 Correct 2 ms 9564 KB Output is correct
38 Correct 2 ms 9564 KB Output is correct
39 Correct 2 ms 9720 KB Output is correct
40 Correct 2 ms 9564 KB Output is correct
41 Correct 2 ms 9564 KB Output is correct
42 Correct 2 ms 9564 KB Output is correct
43 Correct 2 ms 9564 KB Output is correct
44 Correct 2 ms 9564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 1 ms 9564 KB Output is correct
3 Correct 1 ms 9564 KB Output is correct
4 Correct 1 ms 9740 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9644 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9624 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9568 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 3 ms 9752 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9564 KB Output is correct
20 Correct 2 ms 9564 KB Output is correct
21 Correct 2 ms 9564 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 2 ms 9564 KB Output is correct
24 Correct 2 ms 9560 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 1 ms 9564 KB Output is correct
27 Correct 2 ms 9564 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 2 ms 9564 KB Output is correct
30 Correct 2 ms 9564 KB Output is correct
31 Correct 3 ms 9740 KB Output is correct
32 Correct 2 ms 9692 KB Output is correct
33 Correct 2 ms 9704 KB Output is correct
34 Correct 2 ms 9564 KB Output is correct
35 Correct 2 ms 9564 KB Output is correct
36 Correct 2 ms 9564 KB Output is correct
37 Correct 2 ms 9564 KB Output is correct
38 Correct 2 ms 9564 KB Output is correct
39 Correct 2 ms 9720 KB Output is correct
40 Correct 2 ms 9564 KB Output is correct
41 Correct 2 ms 9564 KB Output is correct
42 Correct 2 ms 9564 KB Output is correct
43 Correct 2 ms 9564 KB Output is correct
44 Correct 2 ms 9564 KB Output is correct
45 Correct 3 ms 9564 KB Output is correct
46 Correct 11 ms 9976 KB Output is correct
47 Correct 12 ms 9976 KB Output is correct
48 Correct 11 ms 9820 KB Output is correct
49 Correct 6 ms 10332 KB Output is correct
50 Correct 6 ms 10332 KB Output is correct
51 Correct 6 ms 10344 KB Output is correct
52 Correct 8 ms 10076 KB Output is correct
53 Correct 8 ms 10076 KB Output is correct
54 Correct 8 ms 10076 KB Output is correct
55 Correct 8 ms 10072 KB Output is correct
56 Correct 8 ms 10076 KB Output is correct
57 Correct 22 ms 9960 KB Output is correct
58 Correct 25 ms 9972 KB Output is correct
59 Correct 24 ms 9816 KB Output is correct
60 Correct 23 ms 9816 KB Output is correct
61 Correct 9 ms 10076 KB Output is correct
62 Correct 9 ms 9876 KB Output is correct
63 Correct 10 ms 10076 KB Output is correct
64 Correct 8 ms 9820 KB Output is correct
65 Correct 10 ms 9820 KB Output is correct
66 Correct 12 ms 9820 KB Output is correct
67 Correct 11 ms 9948 KB Output is correct
68 Correct 6 ms 10076 KB Output is correct
69 Correct 7 ms 10120 KB Output is correct
70 Correct 7 ms 10076 KB Output is correct
71 Correct 7 ms 10072 KB Output is correct
72 Correct 22 ms 9816 KB Output is correct
73 Correct 22 ms 10068 KB Output is correct
74 Correct 9 ms 9820 KB Output is correct
75 Correct 8 ms 9820 KB Output is correct
76 Correct 9 ms 9820 KB Output is correct
77 Correct 7 ms 9992 KB Output is correct
78 Correct 7 ms 9992 KB Output is correct
79 Correct 7 ms 9820 KB Output is correct
80 Correct 7 ms 9948 KB Output is correct
81 Correct 9 ms 10040 KB Output is correct
82 Correct 9 ms 9820 KB Output is correct
83 Correct 8 ms 9820 KB Output is correct
84 Correct 9 ms 9816 KB Output is correct
85 Correct 9 ms 9976 KB Output is correct
86 Correct 8 ms 9820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 1 ms 9564 KB Output is correct
3 Correct 1 ms 9564 KB Output is correct
4 Correct 1 ms 9740 KB Output is correct
5 Correct 2 ms 9564 KB Output is correct
6 Correct 2 ms 9564 KB Output is correct
7 Correct 2 ms 9564 KB Output is correct
8 Correct 2 ms 9644 KB Output is correct
9 Correct 2 ms 9564 KB Output is correct
10 Correct 2 ms 9564 KB Output is correct
11 Correct 2 ms 9564 KB Output is correct
12 Correct 2 ms 9624 KB Output is correct
13 Correct 2 ms 9564 KB Output is correct
14 Correct 2 ms 9568 KB Output is correct
15 Correct 2 ms 9564 KB Output is correct
16 Correct 3 ms 9752 KB Output is correct
17 Correct 2 ms 9564 KB Output is correct
18 Correct 2 ms 9564 KB Output is correct
19 Correct 2 ms 9564 KB Output is correct
20 Correct 2 ms 9564 KB Output is correct
21 Correct 2 ms 9564 KB Output is correct
22 Correct 2 ms 9564 KB Output is correct
23 Correct 2 ms 9564 KB Output is correct
24 Correct 2 ms 9560 KB Output is correct
25 Correct 2 ms 9564 KB Output is correct
26 Correct 1 ms 9564 KB Output is correct
27 Correct 2 ms 9564 KB Output is correct
28 Correct 2 ms 9564 KB Output is correct
29 Correct 2 ms 9564 KB Output is correct
30 Correct 2 ms 9564 KB Output is correct
31 Correct 3 ms 9740 KB Output is correct
32 Correct 2 ms 9692 KB Output is correct
33 Correct 2 ms 9704 KB Output is correct
34 Correct 2 ms 9564 KB Output is correct
35 Correct 2 ms 9564 KB Output is correct
36 Correct 2 ms 9564 KB Output is correct
37 Correct 2 ms 9564 KB Output is correct
38 Correct 2 ms 9564 KB Output is correct
39 Correct 2 ms 9720 KB Output is correct
40 Correct 2 ms 9564 KB Output is correct
41 Correct 2 ms 9564 KB Output is correct
42 Correct 2 ms 9564 KB Output is correct
43 Correct 2 ms 9564 KB Output is correct
44 Correct 2 ms 9564 KB Output is correct
45 Correct 3 ms 9564 KB Output is correct
46 Correct 11 ms 9976 KB Output is correct
47 Correct 12 ms 9976 KB Output is correct
48 Correct 11 ms 9820 KB Output is correct
49 Correct 6 ms 10332 KB Output is correct
50 Correct 6 ms 10332 KB Output is correct
51 Correct 6 ms 10344 KB Output is correct
52 Correct 8 ms 10076 KB Output is correct
53 Correct 8 ms 10076 KB Output is correct
54 Correct 8 ms 10076 KB Output is correct
55 Correct 8 ms 10072 KB Output is correct
56 Correct 8 ms 10076 KB Output is correct
57 Correct 22 ms 9960 KB Output is correct
58 Correct 25 ms 9972 KB Output is correct
59 Correct 24 ms 9816 KB Output is correct
60 Correct 23 ms 9816 KB Output is correct
61 Correct 9 ms 10076 KB Output is correct
62 Correct 9 ms 9876 KB Output is correct
63 Correct 10 ms 10076 KB Output is correct
64 Correct 8 ms 9820 KB Output is correct
65 Correct 10 ms 9820 KB Output is correct
66 Correct 12 ms 9820 KB Output is correct
67 Correct 11 ms 9948 KB Output is correct
68 Correct 6 ms 10076 KB Output is correct
69 Correct 7 ms 10120 KB Output is correct
70 Correct 7 ms 10076 KB Output is correct
71 Correct 7 ms 10072 KB Output is correct
72 Correct 22 ms 9816 KB Output is correct
73 Correct 22 ms 10068 KB Output is correct
74 Correct 9 ms 9820 KB Output is correct
75 Correct 8 ms 9820 KB Output is correct
76 Correct 9 ms 9820 KB Output is correct
77 Correct 7 ms 9992 KB Output is correct
78 Correct 7 ms 9992 KB Output is correct
79 Correct 7 ms 9820 KB Output is correct
80 Correct 7 ms 9948 KB Output is correct
81 Correct 9 ms 10040 KB Output is correct
82 Correct 9 ms 9820 KB Output is correct
83 Correct 8 ms 9820 KB Output is correct
84 Correct 9 ms 9816 KB Output is correct
85 Correct 9 ms 9976 KB Output is correct
86 Correct 8 ms 9820 KB Output is correct
87 Correct 34 ms 10332 KB Output is correct
88 Correct 117 ms 15612 KB Output is correct
89 Correct 533 ms 23548 KB Output is correct
90 Correct 524 ms 23512 KB Output is correct
91 Correct 541 ms 23456 KB Output is correct
92 Correct 230 ms 32716 KB Output is correct
93 Correct 210 ms 32804 KB Output is correct
94 Correct 207 ms 32720 KB Output is correct
95 Correct 269 ms 27652 KB Output is correct
96 Correct 291 ms 28112 KB Output is correct
97 Correct 289 ms 28120 KB Output is correct
98 Correct 278 ms 28116 KB Output is correct
99 Correct 310 ms 27416 KB Output is correct
100 Correct 1217 ms 23760 KB Output is correct
101 Correct 1305 ms 24016 KB Output is correct
102 Correct 1302 ms 23812 KB Output is correct
103 Correct 1296 ms 24020 KB Output is correct
104 Correct 360 ms 27348 KB Output is correct
105 Correct 373 ms 27348 KB Output is correct
106 Correct 364 ms 27312 KB Output is correct
107 Correct 335 ms 22028 KB Output is correct
108 Correct 485 ms 21968 KB Output is correct
109 Correct 578 ms 22740 KB Output is correct
110 Correct 187 ms 31480 KB Output is correct
111 Correct 272 ms 27784 KB Output is correct
112 Correct 263 ms 26580 KB Output is correct
113 Correct 282 ms 25980 KB Output is correct
114 Correct 1224 ms 23504 KB Output is correct
115 Correct 1239 ms 22484 KB Output is correct
116 Correct 358 ms 26060 KB Output is correct
117 Correct 284 ms 25036 KB Output is correct
118 Correct 285 ms 24528 KB Output is correct
119 Correct 268 ms 24180 KB Output is correct
120 Correct 296 ms 24020 KB Output is correct
121 Correct 290 ms 23404 KB Output is correct
122 Correct 256 ms 23172 KB Output is correct
123 Correct 387 ms 25064 KB Output is correct
124 Correct 340 ms 24548 KB Output is correct
125 Correct 337 ms 24276 KB Output is correct
126 Correct 333 ms 24016 KB Output is correct
127 Correct 323 ms 23304 KB Output is correct
128 Correct 319 ms 22992 KB Output is correct