Submission #1076747

# Submission time Handle Problem Language Result Execution time Memory
1076747 2024-08-26T16:07:57 Z Yang8on Construction of Highway (JOI18_construction) C++17
100 / 100
211 ms 19800 KB
#include <bits/stdc++.h>
#define Y8o "Construction of Highway"
#define maxn (int) 1e5 + 5
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r

#define f first
#define s second

using namespace std;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
    return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
    if(fopen(Y8o".inp", "r"))
    {
        freopen(Y8o".inp", "r", stdin);
//        freopen(Y8o".out", "w", stdout);
    }
    ios_base::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
    cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}


int n, a[maxn];
pii ed[maxn];
vector<int> cur, o[maxn];
int sz[maxn], h[maxn], par[maxn];
ll ans;

void dfs(int u = 1, int p = 0) {
    sz[u] = 1;
    for(int v : o[u]) if(v != p) {
        par[v] = u, h[v] = h[u] + 1;
        dfs(v, u);
        sz[u] += sz[v];
    }
}

int chain;
int chainIn[maxn], chainHead[maxn];
void hld(int u = 1, int p = 0) {
    if(!chainHead[chain]) chainHead[chain] = u;
    chainIn[u] = chain;
    int mxchild = -1;
    for(int v : o[u]) if(v != p) {
        if(mxchild == -1 || sz[v] > sz[mxchild]) mxchild = v;
    }
    if(mxchild != -1) hld(mxchild, u);
    for(int v : o[u]) if(v != p && v != mxchild) {
        chain ++, hld(v, u);
    }
}

struct BIT {
    int bit[maxn];

    void update(int x, int val) {
        while(x <= n) bit[x] += val, x += (x & -x);
    }
    int get(int x, int best = 0) {
        while(x) best += bit[x], x -= (x & -x);
        return best;
    }
} B;

vector<pii> child[maxn]; /// f -> node, s -> cl
void prog(int p, int u, int val, vector<pii>& st) {
    vector<pii> tmp; /// f -> cl, s -> sl
    int pre = h[p] - 1;
    while(child[p].size() && h[child[p].back().f] < h[u]) {
        tmp.push_back({ child[p].back().s, h[child[p].back().f] - pre });
        pre = h[child[p].back().f];
        child[p].pop_back();
    }
    if(child[p].size()) tmp.push_back({ child[p].back().s, h[u] - pre });
    child[p].push_back({ u, val });

    reverse(all(tmp));
    for(auto x : tmp) st.push_back(x);
}
int cntqer = 0;
void Upd(int u, int val) {
    cntqer ++;
    vector<pii> st; /// f -> cl, s -> sl
    while(1) {
        if(chainIn[u] == chainIn[1]) {
            prog(1, u, val, st);
            break;
        }
        prog(chainHead[chainIn[u]], u, val, st);
        u = par[chainHead[chainIn[u]]];
    }

    for(auto [cl, sl] : st) ans += 1ll * sl * B.get(cl), B.update(cl + 1, +sl);
    for(auto [cl, sl] : st) B.update(cl + 1, -sl);
}

void solve() {
    cin >> n ;
    for(int i = 1; i <= n; i ++) cin >> a[i], cur.push_back( a[i] );

    sort( all(cur) );
    cur.resize( unique(all(cur)) - cur.begin() );
    for(int i = 1; i <= n; i ++) a[i] = lower_bound(all(cur), a[i]) - cur.begin() + 1;

    for(int i = 1; i <= n - 1; i ++) {
        int u, v; cin >> u >> v;
        o[u].push_back(v), o[v].push_back(u);
        ed[i] = {u, v};
    }

    dfs(), hld();
    Upd(1, a[1]);

    for(int i = 1; i <= n - 1; i ++) {
        int u, v; tie(u, v) = ed[i];
        ans = 0, Upd(v, a[v]);
        cout << ans << '\n';
    }
}


int main() {
    iof();

    int nTest = 1;
//    cin >> nTest;

    while(nTest --) {
        solve();
    }

    ctime();
    return 0;
}

Compilation message

construction.cpp: In function 'void iof()':
construction.cpp:23:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         freopen(Y8o".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5208 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 4 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5036 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 4 ms 5236 KB Output is correct
32 Correct 3 ms 5128 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 3 ms 5212 KB Output is correct
36 Correct 3 ms 5096 KB Output is correct
37 Correct 2 ms 5212 KB Output is correct
38 Correct 3 ms 5212 KB Output is correct
39 Correct 2 ms 5180 KB Output is correct
40 Correct 3 ms 5212 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5212 KB Output is correct
43 Correct 2 ms 5212 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5208 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 4 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5036 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 4 ms 5236 KB Output is correct
32 Correct 3 ms 5128 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 3 ms 5212 KB Output is correct
36 Correct 3 ms 5096 KB Output is correct
37 Correct 2 ms 5212 KB Output is correct
38 Correct 3 ms 5212 KB Output is correct
39 Correct 2 ms 5180 KB Output is correct
40 Correct 3 ms 5212 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5212 KB Output is correct
43 Correct 2 ms 5212 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
45 Correct 3 ms 5208 KB Output is correct
46 Correct 6 ms 5516 KB Output is correct
47 Correct 6 ms 5468 KB Output is correct
48 Correct 6 ms 5720 KB Output is correct
49 Correct 6 ms 5708 KB Output is correct
50 Correct 4 ms 5724 KB Output is correct
51 Correct 4 ms 5720 KB Output is correct
52 Correct 5 ms 5724 KB Output is correct
53 Correct 5 ms 5724 KB Output is correct
54 Correct 4 ms 5724 KB Output is correct
55 Correct 5 ms 5724 KB Output is correct
56 Correct 5 ms 5468 KB Output is correct
57 Correct 7 ms 5468 KB Output is correct
58 Correct 7 ms 5468 KB Output is correct
59 Correct 8 ms 5468 KB Output is correct
60 Correct 7 ms 5368 KB Output is correct
61 Correct 4 ms 5468 KB Output is correct
62 Correct 4 ms 5468 KB Output is correct
63 Correct 5 ms 5464 KB Output is correct
64 Correct 5 ms 5468 KB Output is correct
65 Correct 6 ms 5468 KB Output is correct
66 Correct 6 ms 5468 KB Output is correct
67 Correct 7 ms 5468 KB Output is correct
68 Correct 4 ms 5724 KB Output is correct
69 Correct 4 ms 5724 KB Output is correct
70 Correct 4 ms 5724 KB Output is correct
71 Correct 4 ms 5468 KB Output is correct
72 Correct 7 ms 5468 KB Output is correct
73 Correct 7 ms 5468 KB Output is correct
74 Correct 5 ms 5468 KB Output is correct
75 Correct 5 ms 5464 KB Output is correct
76 Correct 6 ms 5468 KB Output is correct
77 Correct 4 ms 5468 KB Output is correct
78 Correct 5 ms 5464 KB Output is correct
79 Correct 5 ms 5468 KB Output is correct
80 Correct 5 ms 5468 KB Output is correct
81 Correct 5 ms 5468 KB Output is correct
82 Correct 5 ms 5600 KB Output is correct
83 Correct 7 ms 5468 KB Output is correct
84 Correct 5 ms 5464 KB Output is correct
85 Correct 4 ms 5464 KB Output is correct
86 Correct 5 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 5208 KB Output is correct
4 Correct 2 ms 5212 KB Output is correct
5 Correct 3 ms 5212 KB Output is correct
6 Correct 2 ms 5212 KB Output is correct
7 Correct 2 ms 5208 KB Output is correct
8 Correct 2 ms 5212 KB Output is correct
9 Correct 2 ms 5212 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 2 ms 5212 KB Output is correct
13 Correct 2 ms 5212 KB Output is correct
14 Correct 2 ms 5212 KB Output is correct
15 Correct 4 ms 5212 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5212 KB Output is correct
18 Correct 3 ms 5212 KB Output is correct
19 Correct 3 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 4 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 3 ms 5212 KB Output is correct
24 Correct 3 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5036 KB Output is correct
28 Correct 3 ms 5212 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 4 ms 5236 KB Output is correct
32 Correct 3 ms 5128 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 3 ms 5212 KB Output is correct
36 Correct 3 ms 5096 KB Output is correct
37 Correct 2 ms 5212 KB Output is correct
38 Correct 3 ms 5212 KB Output is correct
39 Correct 2 ms 5180 KB Output is correct
40 Correct 3 ms 5212 KB Output is correct
41 Correct 3 ms 5212 KB Output is correct
42 Correct 3 ms 5212 KB Output is correct
43 Correct 2 ms 5212 KB Output is correct
44 Correct 2 ms 5212 KB Output is correct
45 Correct 3 ms 5208 KB Output is correct
46 Correct 6 ms 5516 KB Output is correct
47 Correct 6 ms 5468 KB Output is correct
48 Correct 6 ms 5720 KB Output is correct
49 Correct 6 ms 5708 KB Output is correct
50 Correct 4 ms 5724 KB Output is correct
51 Correct 4 ms 5720 KB Output is correct
52 Correct 5 ms 5724 KB Output is correct
53 Correct 5 ms 5724 KB Output is correct
54 Correct 4 ms 5724 KB Output is correct
55 Correct 5 ms 5724 KB Output is correct
56 Correct 5 ms 5468 KB Output is correct
57 Correct 7 ms 5468 KB Output is correct
58 Correct 7 ms 5468 KB Output is correct
59 Correct 8 ms 5468 KB Output is correct
60 Correct 7 ms 5368 KB Output is correct
61 Correct 4 ms 5468 KB Output is correct
62 Correct 4 ms 5468 KB Output is correct
63 Correct 5 ms 5464 KB Output is correct
64 Correct 5 ms 5468 KB Output is correct
65 Correct 6 ms 5468 KB Output is correct
66 Correct 6 ms 5468 KB Output is correct
67 Correct 7 ms 5468 KB Output is correct
68 Correct 4 ms 5724 KB Output is correct
69 Correct 4 ms 5724 KB Output is correct
70 Correct 4 ms 5724 KB Output is correct
71 Correct 4 ms 5468 KB Output is correct
72 Correct 7 ms 5468 KB Output is correct
73 Correct 7 ms 5468 KB Output is correct
74 Correct 5 ms 5468 KB Output is correct
75 Correct 5 ms 5464 KB Output is correct
76 Correct 6 ms 5468 KB Output is correct
77 Correct 4 ms 5468 KB Output is correct
78 Correct 5 ms 5464 KB Output is correct
79 Correct 5 ms 5468 KB Output is correct
80 Correct 5 ms 5468 KB Output is correct
81 Correct 5 ms 5468 KB Output is correct
82 Correct 5 ms 5600 KB Output is correct
83 Correct 7 ms 5468 KB Output is correct
84 Correct 5 ms 5464 KB Output is correct
85 Correct 4 ms 5464 KB Output is correct
86 Correct 5 ms 5368 KB Output is correct
87 Correct 15 ms 5980 KB Output is correct
88 Correct 41 ms 7820 KB Output is correct
89 Correct 164 ms 14032 KB Output is correct
90 Correct 160 ms 14036 KB Output is correct
91 Correct 168 ms 14120 KB Output is correct
92 Correct 64 ms 19800 KB Output is correct
93 Correct 64 ms 19668 KB Output is correct
94 Correct 75 ms 19668 KB Output is correct
95 Correct 70 ms 19036 KB Output is correct
96 Correct 75 ms 19028 KB Output is correct
97 Correct 74 ms 19028 KB Output is correct
98 Correct 74 ms 19032 KB Output is correct
99 Correct 75 ms 17616 KB Output is correct
100 Correct 189 ms 13356 KB Output is correct
101 Correct 208 ms 13520 KB Output is correct
102 Correct 209 ms 13516 KB Output is correct
103 Correct 211 ms 13516 KB Output is correct
104 Correct 85 ms 17788 KB Output is correct
105 Correct 88 ms 17616 KB Output is correct
106 Correct 83 ms 17816 KB Output is correct
107 Correct 142 ms 13648 KB Output is correct
108 Correct 146 ms 13776 KB Output is correct
109 Correct 152 ms 13872 KB Output is correct
110 Correct 62 ms 19412 KB Output is correct
111 Correct 81 ms 19232 KB Output is correct
112 Correct 69 ms 18776 KB Output is correct
113 Correct 64 ms 17364 KB Output is correct
114 Correct 188 ms 13520 KB Output is correct
115 Correct 200 ms 13012 KB Output is correct
116 Correct 100 ms 17316 KB Output is correct
117 Correct 79 ms 15928 KB Output is correct
118 Correct 80 ms 15440 KB Output is correct
119 Correct 91 ms 15120 KB Output is correct
120 Correct 74 ms 15420 KB Output is correct
121 Correct 66 ms 14924 KB Output is correct
122 Correct 69 ms 14604 KB Output is correct
123 Correct 81 ms 15936 KB Output is correct
124 Correct 97 ms 15312 KB Output is correct
125 Correct 89 ms 15116 KB Output is correct
126 Correct 78 ms 15676 KB Output is correct
127 Correct 82 ms 15128 KB Output is correct
128 Correct 106 ms 14628 KB Output is correct