답안 #113094

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
113094 2019-05-23T15:12:14 Z popovicirobert Construction of Highway (JOI18_construction) C++14
100 / 100
1967 ms 27816 KB
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

struct Fenwick {
    vector <int> aib;
    int n;

    inline void init(int _n) {
        n = _n;
        aib.resize(n + 1);
    }

    inline void update(int pos, int val) {
        for(int i = pos; i <= n; i += lsb(i)) {
            aib[i] += val;
        }
    }

    inline int query(int pos) {
        int ans = 0;
        for(int i = pos; i > 0; i -= lsb(i)) {
            ans += aib[i];
        }
        return ans;
    }
};

struct SegTree {
    vector <int> aint;
    int n;

    inline void init(int _n) {
        n = _n;
        aint.resize(4 * n + 1, 0);
    }

    inline void refresh(int nod) {
        aint[nod] = max(aint[2 * nod], aint[2 * nod + 1]);
    }

    void update(int nod, int left, int right, int pos, int val) {
        if(left == right) {
            aint[nod] = val;
        }
        else {
            int mid = (left + right) / 2;

            if(pos <= mid) update(2 * nod, left, mid, pos, val);
            else update(2 * nod + 1, mid + 1, right, pos, val);

            refresh(nod);
        }
    }

    int query(int nod, int left, int right, int l, int r) {
        if(l <= left && right <= r) {
            return aint[nod];
        }
        else {
            int mid = (left + right) / 2;
            int ans = 0;

            if(l <= mid) ans = max(ans, query(2 * nod, left, mid, l, r));
            if(mid < r) ans = max(ans, query(2 * nod + 1, mid + 1, right, l, r));

            return ans;
        }
    }
};

vector < vector <int> > g, anc;
vector <int> idl, idr;
int sz, n;

void dfs(int nod, int par) {

    anc[nod][0] = par;
    for(int bit = 1; bit <= 17; bit++) {
        anc[nod][bit] = anc[anc[nod][bit - 1]][bit - 1];
    }

    idl[nod] = ++sz;
    for(auto it : g[nod]) {
        if(it != par) {
            dfs(it, nod);
        }
    }
    idr[nod] = sz;
}

int main() {
    //ifstream cin("A.in");
    //ofstream cout("A.out");
    int i;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n;

    vector < pair <int, int> > aux(n + 1);

    for(i = 1; i <= n; i++) {
        cin >> aux[i].first;
        aux[i].second = i;
    }

    sort(aux.begin(), aux.end());

    vector <int> c(n + 1);
    int cur = 0;

    for(i = 1; i <= n; i++) {
        if(aux[i].first != aux[i - 1].first) {
            cur++;
        }
        c[aux[i].second] = cur;
    }

    vector < pair <int, int> > edges(n);
    g.resize(n + 1);

    for(i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        edges[i] = {x, y};
        g[x].push_back(y);
        g[y].push_back(x);
    }

    idl.resize(n + 1), idr.resize(n + 1);
    anc.resize(n + 1, vector <int>(18));

    dfs(1, 0);

    SegTree st; st.init(n);
    Fenwick fen; fen.init(n);

    st.update(1, 1, n, 1, 1);
    edges[0].second = 1;

    for(i = 1; i < n; i++) {

        int x = edges[i].first, y = edges[i].second;

        vector < pair <int, int> > vals;
        int nod = x;

        ll ans = 0;

        while(nod != 0) {

            int cur = st.query(1, 1, n, idl[nod], idr[nod]);
            int len = 1;

            for(int bit = 17; bit >= 0; bit--) {
                if(anc[nod][bit] > 0 && st.query(1, 1, n, idl[anc[nod][bit]], idr[anc[nod][bit]]) == cur) {
                    nod = anc[nod][bit];
                    len += (1 << bit);
                }
            }

            cur = edges[cur - 1].second;

            ans += 1LL * len * fen.query(c[cur] - 1);

            fen.update(c[cur], len);
            vals.push_back({c[cur], len});

            nod = anc[nod][0];
        }

        cout << ans << "\n";

        for(auto it : vals) {
            fen.update(it.first, -it.second);
        }

        st.update(1, 1, n, idl[y], i + 1);
    }

    //cin.close();
    //cout.close();
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 420 KB Output is correct
8 Correct 2 ms 520 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 480 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 4 ms 436 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 3 ms 484 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 428 KB Output is correct
28 Correct 3 ms 384 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 4 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 3 ms 432 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 512 KB Output is correct
35 Correct 3 ms 396 KB Output is correct
36 Correct 3 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 3 ms 484 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 428 KB Output is correct
44 Correct 3 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 420 KB Output is correct
8 Correct 2 ms 520 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 480 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 4 ms 436 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 3 ms 484 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 428 KB Output is correct
28 Correct 3 ms 384 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 4 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 3 ms 432 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 512 KB Output is correct
35 Correct 3 ms 396 KB Output is correct
36 Correct 3 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 3 ms 484 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 428 KB Output is correct
44 Correct 3 ms 384 KB Output is correct
45 Correct 4 ms 512 KB Output is correct
46 Correct 16 ms 1280 KB Output is correct
47 Correct 16 ms 1280 KB Output is correct
48 Correct 14 ms 1280 KB Output is correct
49 Correct 8 ms 1456 KB Output is correct
50 Correct 9 ms 1408 KB Output is correct
51 Correct 9 ms 1392 KB Output is correct
52 Correct 13 ms 1280 KB Output is correct
53 Correct 13 ms 1384 KB Output is correct
54 Correct 13 ms 1280 KB Output is correct
55 Correct 13 ms 1408 KB Output is correct
56 Correct 13 ms 1408 KB Output is correct
57 Correct 27 ms 1152 KB Output is correct
58 Correct 27 ms 1320 KB Output is correct
59 Correct 28 ms 1272 KB Output is correct
60 Correct 27 ms 1280 KB Output is correct
61 Correct 14 ms 1280 KB Output is correct
62 Correct 15 ms 1376 KB Output is correct
63 Correct 14 ms 1388 KB Output is correct
64 Correct 15 ms 1280 KB Output is correct
65 Correct 15 ms 1200 KB Output is correct
66 Correct 16 ms 1280 KB Output is correct
67 Correct 14 ms 1280 KB Output is correct
68 Correct 9 ms 1408 KB Output is correct
69 Correct 13 ms 1280 KB Output is correct
70 Correct 13 ms 1280 KB Output is correct
71 Correct 13 ms 1280 KB Output is correct
72 Correct 29 ms 1272 KB Output is correct
73 Correct 28 ms 1360 KB Output is correct
74 Correct 14 ms 1256 KB Output is correct
75 Correct 12 ms 1408 KB Output is correct
76 Correct 11 ms 1280 KB Output is correct
77 Correct 11 ms 1288 KB Output is correct
78 Correct 12 ms 1280 KB Output is correct
79 Correct 12 ms 1200 KB Output is correct
80 Correct 11 ms 1280 KB Output is correct
81 Correct 14 ms 1332 KB Output is correct
82 Correct 13 ms 1280 KB Output is correct
83 Correct 13 ms 1280 KB Output is correct
84 Correct 13 ms 1280 KB Output is correct
85 Correct 13 ms 1280 KB Output is correct
86 Correct 13 ms 1280 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 356 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 384 KB Output is correct
7 Correct 3 ms 420 KB Output is correct
8 Correct 2 ms 520 KB Output is correct
9 Correct 2 ms 512 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 3 ms 512 KB Output is correct
12 Correct 3 ms 512 KB Output is correct
13 Correct 3 ms 480 KB Output is correct
14 Correct 3 ms 512 KB Output is correct
15 Correct 3 ms 384 KB Output is correct
16 Correct 4 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 4 ms 436 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 3 ms 512 KB Output is correct
21 Correct 3 ms 484 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 3 ms 384 KB Output is correct
26 Correct 2 ms 512 KB Output is correct
27 Correct 3 ms 428 KB Output is correct
28 Correct 3 ms 384 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 4 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 3 ms 432 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 3 ms 512 KB Output is correct
35 Correct 3 ms 396 KB Output is correct
36 Correct 3 ms 384 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 3 ms 484 KB Output is correct
40 Correct 3 ms 384 KB Output is correct
41 Correct 3 ms 384 KB Output is correct
42 Correct 3 ms 384 KB Output is correct
43 Correct 3 ms 428 KB Output is correct
44 Correct 3 ms 384 KB Output is correct
45 Correct 4 ms 512 KB Output is correct
46 Correct 16 ms 1280 KB Output is correct
47 Correct 16 ms 1280 KB Output is correct
48 Correct 14 ms 1280 KB Output is correct
49 Correct 8 ms 1456 KB Output is correct
50 Correct 9 ms 1408 KB Output is correct
51 Correct 9 ms 1392 KB Output is correct
52 Correct 13 ms 1280 KB Output is correct
53 Correct 13 ms 1384 KB Output is correct
54 Correct 13 ms 1280 KB Output is correct
55 Correct 13 ms 1408 KB Output is correct
56 Correct 13 ms 1408 KB Output is correct
57 Correct 27 ms 1152 KB Output is correct
58 Correct 27 ms 1320 KB Output is correct
59 Correct 28 ms 1272 KB Output is correct
60 Correct 27 ms 1280 KB Output is correct
61 Correct 14 ms 1280 KB Output is correct
62 Correct 15 ms 1376 KB Output is correct
63 Correct 14 ms 1388 KB Output is correct
64 Correct 15 ms 1280 KB Output is correct
65 Correct 15 ms 1200 KB Output is correct
66 Correct 16 ms 1280 KB Output is correct
67 Correct 14 ms 1280 KB Output is correct
68 Correct 9 ms 1408 KB Output is correct
69 Correct 13 ms 1280 KB Output is correct
70 Correct 13 ms 1280 KB Output is correct
71 Correct 13 ms 1280 KB Output is correct
72 Correct 29 ms 1272 KB Output is correct
73 Correct 28 ms 1360 KB Output is correct
74 Correct 14 ms 1256 KB Output is correct
75 Correct 12 ms 1408 KB Output is correct
76 Correct 11 ms 1280 KB Output is correct
77 Correct 11 ms 1288 KB Output is correct
78 Correct 12 ms 1280 KB Output is correct
79 Correct 12 ms 1200 KB Output is correct
80 Correct 11 ms 1280 KB Output is correct
81 Correct 14 ms 1332 KB Output is correct
82 Correct 13 ms 1280 KB Output is correct
83 Correct 13 ms 1280 KB Output is correct
84 Correct 13 ms 1280 KB Output is correct
85 Correct 13 ms 1280 KB Output is correct
86 Correct 13 ms 1280 KB Output is correct
87 Correct 44 ms 2668 KB Output is correct
88 Correct 163 ms 7160 KB Output is correct
89 Correct 882 ms 23376 KB Output is correct
90 Correct 884 ms 23280 KB Output is correct
91 Correct 889 ms 23212 KB Output is correct
92 Correct 490 ms 27816 KB Output is correct
93 Correct 424 ms 27672 KB Output is correct
94 Correct 464 ms 27720 KB Output is correct
95 Correct 752 ms 25676 KB Output is correct
96 Correct 677 ms 26088 KB Output is correct
97 Correct 716 ms 25960 KB Output is correct
98 Correct 709 ms 26020 KB Output is correct
99 Correct 702 ms 25604 KB Output is correct
100 Correct 1865 ms 22960 KB Output is correct
101 Correct 1903 ms 23240 KB Output is correct
102 Correct 1967 ms 23180 KB Output is correct
103 Correct 1827 ms 23196 KB Output is correct
104 Correct 868 ms 25540 KB Output is correct
105 Correct 928 ms 25620 KB Output is correct
106 Correct 923 ms 25528 KB Output is correct
107 Correct 788 ms 22488 KB Output is correct
108 Correct 790 ms 22564 KB Output is correct
109 Correct 878 ms 22884 KB Output is correct
110 Correct 405 ms 27216 KB Output is correct
111 Correct 719 ms 25716 KB Output is correct
112 Correct 732 ms 25388 KB Output is correct
113 Correct 664 ms 25040 KB Output is correct
114 Correct 1760 ms 22876 KB Output is correct
115 Correct 1782 ms 22616 KB Output is correct
116 Correct 856 ms 24992 KB Output is correct
117 Correct 607 ms 24064 KB Output is correct
118 Correct 604 ms 23884 KB Output is correct
119 Correct 529 ms 23368 KB Output is correct
120 Correct 645 ms 23760 KB Output is correct
121 Correct 582 ms 23304 KB Output is correct
122 Correct 557 ms 23160 KB Output is correct
123 Correct 827 ms 24136 KB Output is correct
124 Correct 819 ms 23740 KB Output is correct
125 Correct 812 ms 23416 KB Output is correct
126 Correct 851 ms 23856 KB Output is correct
127 Correct 732 ms 23548 KB Output is correct
128 Correct 800 ms 23296 KB Output is correct