답안 #915299

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
915299 2024-01-23T16:12:06 Z Amirreza_Fakhri Construction of Highway (JOI18_construction) C++17
100 / 100
221 ms 52048 KB
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;

const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 5e5+5;

int n, c[maxn], par[maxn], he[maxn], node[maxn], heavy[maxn], head[maxn], fen[maxn];
vector <int> uni, adj[maxn];
vector <pii> ch[maxn], vec1, vec2;

int dfs1(int v = 0) {
    int sz = 1, mx = 0;
    heavy[v] = -1;
    for (int u : adj[v]) {
        int csz = dfs1(u);
        sz += csz;
        if (csz > mx) mx = csz, heavy[v] = u;
    }
    return sz;
}

void dfs2(int v = 0) {
    for (int u : adj[v]) {
        if (u == heavy[v]) head[u] = head[v];
        else head[u] = u;
        dfs2(u);
    }
}

int get(int i) {
    int ans = 0;
    for (i++; i; i -= i&-i) ans += fen[i-1];
    return ans;
}

void upd(int i, int x) {
    for (i++; i <= n; i += i&-i) fen[i-1] += x;
}

int processs(int v) {
    int ans = 0;
    while (v != -1) {
        int last = he[head[v]]-1;
        for (int i = ch[head[v]].size()-1; i >= 0; i--) {
            vec1.pb(mp(min(ch[head[v]][i].F, he[v])-last, ch[head[v]][i].S));
            last = ch[head[v]][i].F;
            if (last >= he[v]) break;
        }
        for (int i = vec1.size()-1; i >= 0; i--) {
            // cout << vec1[i].S << ' ' << vec1[i].F << " FF\n";
            vec2.pb(vec1[i]);
            ans += get(vec1[i].S-1)*vec1[i].F;
            upd(vec1[i].S, vec1[i].F);
        }
        vec1.clear();
        v = par[head[v]];
    }
    for (int i = 0; i < vec2.size(); i++) upd(vec2[i].S, -vec2[i].F);
    vec2.clear();
    return ans;
}

void amir(int v) {
    int x = c[v];
    while (v != -1) {
        // cout << v << "\n";
        for (int i = ch[head[v]].size()-1; i >= 0; i--) {
            if (ch[head[v]][i].F > he[v]) break;
            ch[head[v]].pop_back();
        }
        ch[head[v]].pb(mp(he[v], x));
        v = par[head[v]];
    }
    // for (int i = 0; i < ch[0].size(); i++) {
    //     cout << ch[0].size() << ' ' << ch[0][i].F << ' ' << ch[0][i].S << '\n';
    // }
    // cout << "_____\n";
}

int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> c[i];
        uni.pb(c[i]);
    }
    sort(all(uni)); uni.resize(unique(all(uni))-uni.begin());
    for (int i = 0; i < n; i++) c[i] = lower_bound(all(uni), c[i])-uni.begin();
    par[0] = -1;
    for (int i = 0; i < n-1; i++) {
        int v, u; cin >> v >> u;
        adj[--v].pb(--u);
        par[u] = v;
        he[u] = he[v]+1;
        node[i] = u;
    }
    dfs1(), dfs2();
    ch[0].pb(mp(0, c[0]));
    for (int i = 0; i < n-1; i++) {
        cout << processs(par[node[i]]) << '\n';
        amir(node[i]);
        // for (int i = 0
    }
    // cout << processs(2) << '\n';
    // cout << heavy[1] << '\n';
    return 0;
}


/* 
5
1 2 3 4 5
1 2
2 3
2 4
3 5 
*/

Compilation message

construction.cpp: In function 'long long int processs(long long int)':
construction.cpp:70:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |     for (int i = 0; i < vec2.size(); i++) upd(vec2[i].S, -vec2[i].F);
      |                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33372 KB Output is correct
2 Correct 10 ms 35420 KB Output is correct
3 Correct 11 ms 35420 KB Output is correct
4 Correct 10 ms 35420 KB Output is correct
5 Correct 10 ms 33372 KB Output is correct
6 Correct 10 ms 35420 KB Output is correct
7 Correct 10 ms 33372 KB Output is correct
8 Correct 11 ms 33372 KB Output is correct
9 Correct 10 ms 33372 KB Output is correct
10 Correct 10 ms 33372 KB Output is correct
11 Correct 11 ms 33368 KB Output is correct
12 Correct 10 ms 33372 KB Output is correct
13 Correct 10 ms 33368 KB Output is correct
14 Correct 9 ms 33532 KB Output is correct
15 Correct 10 ms 33372 KB Output is correct
16 Correct 9 ms 33372 KB Output is correct
17 Correct 10 ms 33372 KB Output is correct
18 Correct 9 ms 33464 KB Output is correct
19 Correct 10 ms 33372 KB Output is correct
20 Correct 10 ms 33368 KB Output is correct
21 Correct 10 ms 33372 KB Output is correct
22 Correct 10 ms 33532 KB Output is correct
23 Correct 11 ms 33372 KB Output is correct
24 Correct 10 ms 33372 KB Output is correct
25 Correct 10 ms 33372 KB Output is correct
26 Correct 10 ms 33544 KB Output is correct
27 Correct 10 ms 33372 KB Output is correct
28 Correct 11 ms 33412 KB Output is correct
29 Correct 10 ms 33372 KB Output is correct
30 Correct 10 ms 33372 KB Output is correct
31 Correct 9 ms 33372 KB Output is correct
32 Correct 10 ms 33372 KB Output is correct
33 Correct 10 ms 33368 KB Output is correct
34 Correct 9 ms 33372 KB Output is correct
35 Correct 11 ms 33372 KB Output is correct
36 Correct 10 ms 33456 KB Output is correct
37 Correct 10 ms 33368 KB Output is correct
38 Correct 11 ms 33368 KB Output is correct
39 Correct 10 ms 33372 KB Output is correct
40 Correct 10 ms 33368 KB Output is correct
41 Correct 11 ms 33372 KB Output is correct
42 Correct 10 ms 33528 KB Output is correct
43 Correct 10 ms 33372 KB Output is correct
44 Correct 10 ms 33372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33372 KB Output is correct
2 Correct 10 ms 35420 KB Output is correct
3 Correct 11 ms 35420 KB Output is correct
4 Correct 10 ms 35420 KB Output is correct
5 Correct 10 ms 33372 KB Output is correct
6 Correct 10 ms 35420 KB Output is correct
7 Correct 10 ms 33372 KB Output is correct
8 Correct 11 ms 33372 KB Output is correct
9 Correct 10 ms 33372 KB Output is correct
10 Correct 10 ms 33372 KB Output is correct
11 Correct 11 ms 33368 KB Output is correct
12 Correct 10 ms 33372 KB Output is correct
13 Correct 10 ms 33368 KB Output is correct
14 Correct 9 ms 33532 KB Output is correct
15 Correct 10 ms 33372 KB Output is correct
16 Correct 9 ms 33372 KB Output is correct
17 Correct 10 ms 33372 KB Output is correct
18 Correct 9 ms 33464 KB Output is correct
19 Correct 10 ms 33372 KB Output is correct
20 Correct 10 ms 33368 KB Output is correct
21 Correct 10 ms 33372 KB Output is correct
22 Correct 10 ms 33532 KB Output is correct
23 Correct 11 ms 33372 KB Output is correct
24 Correct 10 ms 33372 KB Output is correct
25 Correct 10 ms 33372 KB Output is correct
26 Correct 10 ms 33544 KB Output is correct
27 Correct 10 ms 33372 KB Output is correct
28 Correct 11 ms 33412 KB Output is correct
29 Correct 10 ms 33372 KB Output is correct
30 Correct 10 ms 33372 KB Output is correct
31 Correct 9 ms 33372 KB Output is correct
32 Correct 10 ms 33372 KB Output is correct
33 Correct 10 ms 33368 KB Output is correct
34 Correct 9 ms 33372 KB Output is correct
35 Correct 11 ms 33372 KB Output is correct
36 Correct 10 ms 33456 KB Output is correct
37 Correct 10 ms 33368 KB Output is correct
38 Correct 11 ms 33368 KB Output is correct
39 Correct 10 ms 33372 KB Output is correct
40 Correct 10 ms 33368 KB Output is correct
41 Correct 11 ms 33372 KB Output is correct
42 Correct 10 ms 33528 KB Output is correct
43 Correct 10 ms 33372 KB Output is correct
44 Correct 10 ms 33372 KB Output is correct
45 Correct 11 ms 33372 KB Output is correct
46 Correct 13 ms 35820 KB Output is correct
47 Correct 15 ms 35820 KB Output is correct
48 Correct 14 ms 37696 KB Output is correct
49 Correct 15 ms 35932 KB Output is correct
50 Correct 12 ms 36012 KB Output is correct
51 Correct 13 ms 36020 KB Output is correct
52 Correct 12 ms 35932 KB Output is correct
53 Correct 13 ms 36028 KB Output is correct
54 Correct 12 ms 35932 KB Output is correct
55 Correct 13 ms 36044 KB Output is correct
56 Correct 13 ms 35676 KB Output is correct
57 Correct 14 ms 35676 KB Output is correct
58 Correct 15 ms 35672 KB Output is correct
59 Correct 14 ms 35676 KB Output is correct
60 Correct 16 ms 35672 KB Output is correct
61 Correct 13 ms 35676 KB Output is correct
62 Correct 12 ms 35932 KB Output is correct
63 Correct 13 ms 35928 KB Output is correct
64 Correct 14 ms 35672 KB Output is correct
65 Correct 14 ms 35676 KB Output is correct
66 Correct 13 ms 35676 KB Output is correct
67 Correct 14 ms 35928 KB Output is correct
68 Correct 12 ms 35932 KB Output is correct
69 Correct 12 ms 35932 KB Output is correct
70 Correct 13 ms 35928 KB Output is correct
71 Correct 12 ms 35676 KB Output is correct
72 Correct 15 ms 35672 KB Output is correct
73 Correct 15 ms 35676 KB Output is correct
74 Correct 15 ms 36176 KB Output is correct
75 Correct 13 ms 35676 KB Output is correct
76 Correct 12 ms 35676 KB Output is correct
77 Correct 12 ms 35676 KB Output is correct
78 Correct 12 ms 35780 KB Output is correct
79 Correct 12 ms 35676 KB Output is correct
80 Correct 12 ms 35676 KB Output is correct
81 Correct 12 ms 35676 KB Output is correct
82 Correct 13 ms 35672 KB Output is correct
83 Correct 13 ms 35672 KB Output is correct
84 Correct 12 ms 35676 KB Output is correct
85 Correct 13 ms 35676 KB Output is correct
86 Correct 13 ms 35676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 33372 KB Output is correct
2 Correct 10 ms 35420 KB Output is correct
3 Correct 11 ms 35420 KB Output is correct
4 Correct 10 ms 35420 KB Output is correct
5 Correct 10 ms 33372 KB Output is correct
6 Correct 10 ms 35420 KB Output is correct
7 Correct 10 ms 33372 KB Output is correct
8 Correct 11 ms 33372 KB Output is correct
9 Correct 10 ms 33372 KB Output is correct
10 Correct 10 ms 33372 KB Output is correct
11 Correct 11 ms 33368 KB Output is correct
12 Correct 10 ms 33372 KB Output is correct
13 Correct 10 ms 33368 KB Output is correct
14 Correct 9 ms 33532 KB Output is correct
15 Correct 10 ms 33372 KB Output is correct
16 Correct 9 ms 33372 KB Output is correct
17 Correct 10 ms 33372 KB Output is correct
18 Correct 9 ms 33464 KB Output is correct
19 Correct 10 ms 33372 KB Output is correct
20 Correct 10 ms 33368 KB Output is correct
21 Correct 10 ms 33372 KB Output is correct
22 Correct 10 ms 33532 KB Output is correct
23 Correct 11 ms 33372 KB Output is correct
24 Correct 10 ms 33372 KB Output is correct
25 Correct 10 ms 33372 KB Output is correct
26 Correct 10 ms 33544 KB Output is correct
27 Correct 10 ms 33372 KB Output is correct
28 Correct 11 ms 33412 KB Output is correct
29 Correct 10 ms 33372 KB Output is correct
30 Correct 10 ms 33372 KB Output is correct
31 Correct 9 ms 33372 KB Output is correct
32 Correct 10 ms 33372 KB Output is correct
33 Correct 10 ms 33368 KB Output is correct
34 Correct 9 ms 33372 KB Output is correct
35 Correct 11 ms 33372 KB Output is correct
36 Correct 10 ms 33456 KB Output is correct
37 Correct 10 ms 33368 KB Output is correct
38 Correct 11 ms 33368 KB Output is correct
39 Correct 10 ms 33372 KB Output is correct
40 Correct 10 ms 33368 KB Output is correct
41 Correct 11 ms 33372 KB Output is correct
42 Correct 10 ms 33528 KB Output is correct
43 Correct 10 ms 33372 KB Output is correct
44 Correct 10 ms 33372 KB Output is correct
45 Correct 11 ms 33372 KB Output is correct
46 Correct 13 ms 35820 KB Output is correct
47 Correct 15 ms 35820 KB Output is correct
48 Correct 14 ms 37696 KB Output is correct
49 Correct 15 ms 35932 KB Output is correct
50 Correct 12 ms 36012 KB Output is correct
51 Correct 13 ms 36020 KB Output is correct
52 Correct 12 ms 35932 KB Output is correct
53 Correct 13 ms 36028 KB Output is correct
54 Correct 12 ms 35932 KB Output is correct
55 Correct 13 ms 36044 KB Output is correct
56 Correct 13 ms 35676 KB Output is correct
57 Correct 14 ms 35676 KB Output is correct
58 Correct 15 ms 35672 KB Output is correct
59 Correct 14 ms 35676 KB Output is correct
60 Correct 16 ms 35672 KB Output is correct
61 Correct 13 ms 35676 KB Output is correct
62 Correct 12 ms 35932 KB Output is correct
63 Correct 13 ms 35928 KB Output is correct
64 Correct 14 ms 35672 KB Output is correct
65 Correct 14 ms 35676 KB Output is correct
66 Correct 13 ms 35676 KB Output is correct
67 Correct 14 ms 35928 KB Output is correct
68 Correct 12 ms 35932 KB Output is correct
69 Correct 12 ms 35932 KB Output is correct
70 Correct 13 ms 35928 KB Output is correct
71 Correct 12 ms 35676 KB Output is correct
72 Correct 15 ms 35672 KB Output is correct
73 Correct 15 ms 35676 KB Output is correct
74 Correct 15 ms 36176 KB Output is correct
75 Correct 13 ms 35676 KB Output is correct
76 Correct 12 ms 35676 KB Output is correct
77 Correct 12 ms 35676 KB Output is correct
78 Correct 12 ms 35780 KB Output is correct
79 Correct 12 ms 35676 KB Output is correct
80 Correct 12 ms 35676 KB Output is correct
81 Correct 12 ms 35676 KB Output is correct
82 Correct 13 ms 35672 KB Output is correct
83 Correct 13 ms 35672 KB Output is correct
84 Correct 12 ms 35676 KB Output is correct
85 Correct 13 ms 35676 KB Output is correct
86 Correct 13 ms 35676 KB Output is correct
87 Correct 20 ms 38236 KB Output is correct
88 Correct 45 ms 39900 KB Output is correct
89 Correct 151 ms 47308 KB Output is correct
90 Correct 145 ms 48584 KB Output is correct
91 Correct 140 ms 47232 KB Output is correct
92 Correct 79 ms 51152 KB Output is correct
93 Correct 82 ms 51324 KB Output is correct
94 Correct 83 ms 51276 KB Output is correct
95 Correct 84 ms 51556 KB Output is correct
96 Correct 88 ms 52048 KB Output is correct
97 Correct 89 ms 51872 KB Output is correct
98 Correct 93 ms 52024 KB Output is correct
99 Correct 90 ms 49156 KB Output is correct
100 Correct 160 ms 46972 KB Output is correct
101 Correct 202 ms 47304 KB Output is correct
102 Correct 221 ms 47204 KB Output is correct
103 Correct 208 ms 47340 KB Output is correct
104 Correct 91 ms 48936 KB Output is correct
105 Correct 95 ms 49360 KB Output is correct
106 Correct 97 ms 49060 KB Output is correct
107 Correct 120 ms 46576 KB Output is correct
108 Correct 124 ms 47940 KB Output is correct
109 Correct 144 ms 47072 KB Output is correct
110 Correct 68 ms 50640 KB Output is correct
111 Correct 82 ms 51636 KB Output is correct
112 Correct 78 ms 51284 KB Output is correct
113 Correct 82 ms 48332 KB Output is correct
114 Correct 168 ms 47056 KB Output is correct
115 Correct 177 ms 46708 KB Output is correct
116 Correct 76 ms 48472 KB Output is correct
117 Correct 97 ms 47564 KB Output is correct
118 Correct 86 ms 47052 KB Output is correct
119 Correct 85 ms 46792 KB Output is correct
120 Correct 73 ms 47048 KB Output is correct
121 Correct 74 ms 46708 KB Output is correct
122 Correct 74 ms 46568 KB Output is correct
123 Correct 95 ms 47564 KB Output is correct
124 Correct 92 ms 47072 KB Output is correct
125 Correct 92 ms 47056 KB Output is correct
126 Correct 81 ms 47308 KB Output is correct
127 Correct 85 ms 46840 KB Output is correct
128 Correct 103 ms 46732 KB Output is correct