#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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5148 KB |
Output is correct |
7 |
Correct |
4 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
3 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5212 KB |
Output is correct |
15 |
Correct |
4 ms |
5212 KB |
Output is correct |
16 |
Correct |
4 ms |
5216 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
4 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 |
4 ms |
5208 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 |
4 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
4 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5376 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
4 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5208 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
4 ms |
5212 KB |
Output is correct |
39 |
Correct |
4 ms |
5172 KB |
Output is correct |
40 |
Correct |
4 ms |
5212 KB |
Output is correct |
41 |
Correct |
4 ms |
5176 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
4 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5148 KB |
Output is correct |
7 |
Correct |
4 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
3 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5212 KB |
Output is correct |
15 |
Correct |
4 ms |
5212 KB |
Output is correct |
16 |
Correct |
4 ms |
5216 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
4 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 |
4 ms |
5208 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 |
4 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
4 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5376 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
4 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5208 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
4 ms |
5212 KB |
Output is correct |
39 |
Correct |
4 ms |
5172 KB |
Output is correct |
40 |
Correct |
4 ms |
5212 KB |
Output is correct |
41 |
Correct |
4 ms |
5176 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
4 ms |
5212 KB |
Output is correct |
45 |
Correct |
4 ms |
5148 KB |
Output is correct |
46 |
Correct |
10 ms |
5464 KB |
Output is correct |
47 |
Correct |
8 ms |
5464 KB |
Output is correct |
48 |
Correct |
10 ms |
5464 KB |
Output is correct |
49 |
Correct |
7 ms |
5720 KB |
Output is correct |
50 |
Correct |
7 ms |
5724 KB |
Output is correct |
51 |
Correct |
5 ms |
5720 KB |
Output is correct |
52 |
Correct |
4 ms |
5724 KB |
Output is correct |
53 |
Correct |
5 ms |
5724 KB |
Output is correct |
54 |
Correct |
7 ms |
5724 KB |
Output is correct |
55 |
Correct |
6 ms |
5736 KB |
Output is correct |
56 |
Correct |
7 ms |
5568 KB |
Output is correct |
57 |
Correct |
8 ms |
5468 KB |
Output is correct |
58 |
Correct |
7 ms |
5440 KB |
Output is correct |
59 |
Correct |
8 ms |
5468 KB |
Output is correct |
60 |
Correct |
10 ms |
5468 KB |
Output is correct |
61 |
Correct |
7 ms |
5724 KB |
Output is correct |
62 |
Correct |
8 ms |
5724 KB |
Output is correct |
63 |
Correct |
5 ms |
5724 KB |
Output is correct |
64 |
Correct |
8 ms |
5716 KB |
Output is correct |
65 |
Correct |
9 ms |
5464 KB |
Output is correct |
66 |
Correct |
9 ms |
5468 KB |
Output is correct |
67 |
Correct |
9 ms |
5468 KB |
Output is correct |
68 |
Correct |
6 ms |
5724 KB |
Output is correct |
69 |
Correct |
5 ms |
5724 KB |
Output is correct |
70 |
Correct |
5 ms |
5724 KB |
Output is correct |
71 |
Correct |
7 ms |
5720 KB |
Output is correct |
72 |
Correct |
7 ms |
5468 KB |
Output is correct |
73 |
Correct |
7 ms |
5468 KB |
Output is correct |
74 |
Correct |
6 ms |
5724 KB |
Output is correct |
75 |
Correct |
4 ms |
5468 KB |
Output is correct |
76 |
Correct |
5 ms |
5468 KB |
Output is correct |
77 |
Correct |
7 ms |
5492 KB |
Output is correct |
78 |
Correct |
5 ms |
5468 KB |
Output is correct |
79 |
Correct |
7 ms |
5636 KB |
Output is correct |
80 |
Correct |
7 ms |
5440 KB |
Output is correct |
81 |
Correct |
7 ms |
5720 KB |
Output is correct |
82 |
Correct |
7 ms |
5464 KB |
Output is correct |
83 |
Correct |
7 ms |
5528 KB |
Output is correct |
84 |
Correct |
5 ms |
5468 KB |
Output is correct |
85 |
Correct |
5 ms |
5464 KB |
Output is correct |
86 |
Correct |
5 ms |
5472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
4 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5208 KB |
Output is correct |
6 |
Correct |
3 ms |
5148 KB |
Output is correct |
7 |
Correct |
4 ms |
5212 KB |
Output is correct |
8 |
Correct |
3 ms |
5208 KB |
Output is correct |
9 |
Correct |
3 ms |
5208 KB |
Output is correct |
10 |
Correct |
3 ms |
5212 KB |
Output is correct |
11 |
Correct |
3 ms |
5208 KB |
Output is correct |
12 |
Correct |
3 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5212 KB |
Output is correct |
14 |
Correct |
4 ms |
5212 KB |
Output is correct |
15 |
Correct |
4 ms |
5212 KB |
Output is correct |
16 |
Correct |
4 ms |
5216 KB |
Output is correct |
17 |
Correct |
3 ms |
5208 KB |
Output is correct |
18 |
Correct |
4 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 |
4 ms |
5208 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 |
4 ms |
5212 KB |
Output is correct |
27 |
Correct |
4 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5208 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
4 ms |
5212 KB |
Output is correct |
31 |
Correct |
4 ms |
5376 KB |
Output is correct |
32 |
Correct |
3 ms |
5212 KB |
Output is correct |
33 |
Correct |
3 ms |
5208 KB |
Output is correct |
34 |
Correct |
4 ms |
5208 KB |
Output is correct |
35 |
Correct |
3 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5208 KB |
Output is correct |
37 |
Correct |
4 ms |
5212 KB |
Output is correct |
38 |
Correct |
4 ms |
5212 KB |
Output is correct |
39 |
Correct |
4 ms |
5172 KB |
Output is correct |
40 |
Correct |
4 ms |
5212 KB |
Output is correct |
41 |
Correct |
4 ms |
5176 KB |
Output is correct |
42 |
Correct |
3 ms |
5212 KB |
Output is correct |
43 |
Correct |
3 ms |
5212 KB |
Output is correct |
44 |
Correct |
4 ms |
5212 KB |
Output is correct |
45 |
Correct |
4 ms |
5148 KB |
Output is correct |
46 |
Correct |
10 ms |
5464 KB |
Output is correct |
47 |
Correct |
8 ms |
5464 KB |
Output is correct |
48 |
Correct |
10 ms |
5464 KB |
Output is correct |
49 |
Correct |
7 ms |
5720 KB |
Output is correct |
50 |
Correct |
7 ms |
5724 KB |
Output is correct |
51 |
Correct |
5 ms |
5720 KB |
Output is correct |
52 |
Correct |
4 ms |
5724 KB |
Output is correct |
53 |
Correct |
5 ms |
5724 KB |
Output is correct |
54 |
Correct |
7 ms |
5724 KB |
Output is correct |
55 |
Correct |
6 ms |
5736 KB |
Output is correct |
56 |
Correct |
7 ms |
5568 KB |
Output is correct |
57 |
Correct |
8 ms |
5468 KB |
Output is correct |
58 |
Correct |
7 ms |
5440 KB |
Output is correct |
59 |
Correct |
8 ms |
5468 KB |
Output is correct |
60 |
Correct |
10 ms |
5468 KB |
Output is correct |
61 |
Correct |
7 ms |
5724 KB |
Output is correct |
62 |
Correct |
8 ms |
5724 KB |
Output is correct |
63 |
Correct |
5 ms |
5724 KB |
Output is correct |
64 |
Correct |
8 ms |
5716 KB |
Output is correct |
65 |
Correct |
9 ms |
5464 KB |
Output is correct |
66 |
Correct |
9 ms |
5468 KB |
Output is correct |
67 |
Correct |
9 ms |
5468 KB |
Output is correct |
68 |
Correct |
6 ms |
5724 KB |
Output is correct |
69 |
Correct |
5 ms |
5724 KB |
Output is correct |
70 |
Correct |
5 ms |
5724 KB |
Output is correct |
71 |
Correct |
7 ms |
5720 KB |
Output is correct |
72 |
Correct |
7 ms |
5468 KB |
Output is correct |
73 |
Correct |
7 ms |
5468 KB |
Output is correct |
74 |
Correct |
6 ms |
5724 KB |
Output is correct |
75 |
Correct |
4 ms |
5468 KB |
Output is correct |
76 |
Correct |
5 ms |
5468 KB |
Output is correct |
77 |
Correct |
7 ms |
5492 KB |
Output is correct |
78 |
Correct |
5 ms |
5468 KB |
Output is correct |
79 |
Correct |
7 ms |
5636 KB |
Output is correct |
80 |
Correct |
7 ms |
5440 KB |
Output is correct |
81 |
Correct |
7 ms |
5720 KB |
Output is correct |
82 |
Correct |
7 ms |
5464 KB |
Output is correct |
83 |
Correct |
7 ms |
5528 KB |
Output is correct |
84 |
Correct |
5 ms |
5468 KB |
Output is correct |
85 |
Correct |
5 ms |
5464 KB |
Output is correct |
86 |
Correct |
5 ms |
5472 KB |
Output is correct |
87 |
Correct |
20 ms |
6236 KB |
Output is correct |
88 |
Correct |
45 ms |
8272 KB |
Output is correct |
89 |
Correct |
169 ms |
16500 KB |
Output is correct |
90 |
Correct |
201 ms |
16188 KB |
Output is correct |
91 |
Correct |
189 ms |
16084 KB |
Output is correct |
92 |
Correct |
67 ms |
21816 KB |
Output is correct |
93 |
Correct |
105 ms |
21824 KB |
Output is correct |
94 |
Correct |
65 ms |
21768 KB |
Output is correct |
95 |
Correct |
114 ms |
20820 KB |
Output is correct |
96 |
Correct |
90 ms |
21184 KB |
Output is correct |
97 |
Correct |
91 ms |
21140 KB |
Output is correct |
98 |
Correct |
89 ms |
21332 KB |
Output is correct |
99 |
Correct |
74 ms |
19920 KB |
Output is correct |
100 |
Correct |
213 ms |
15320 KB |
Output is correct |
101 |
Correct |
242 ms |
15416 KB |
Output is correct |
102 |
Correct |
285 ms |
15572 KB |
Output is correct |
103 |
Correct |
242 ms |
15580 KB |
Output is correct |
104 |
Correct |
94 ms |
19756 KB |
Output is correct |
105 |
Correct |
81 ms |
19924 KB |
Output is correct |
106 |
Correct |
77 ms |
19924 KB |
Output is correct |
107 |
Correct |
125 ms |
15064 KB |
Output is correct |
108 |
Correct |
144 ms |
15136 KB |
Output is correct |
109 |
Correct |
156 ms |
15568 KB |
Output is correct |
110 |
Correct |
60 ms |
20944 KB |
Output is correct |
111 |
Correct |
83 ms |
20828 KB |
Output is correct |
112 |
Correct |
79 ms |
20308 KB |
Output is correct |
113 |
Correct |
69 ms |
18900 KB |
Output is correct |
114 |
Correct |
209 ms |
15308 KB |
Output is correct |
115 |
Correct |
185 ms |
14528 KB |
Output is correct |
116 |
Correct |
66 ms |
18896 KB |
Output is correct |
117 |
Correct |
76 ms |
17624 KB |
Output is correct |
118 |
Correct |
83 ms |
17156 KB |
Output is correct |
119 |
Correct |
82 ms |
16912 KB |
Output is correct |
120 |
Correct |
82 ms |
17104 KB |
Output is correct |
121 |
Correct |
70 ms |
16464 KB |
Output is correct |
122 |
Correct |
71 ms |
16228 KB |
Output is correct |
123 |
Correct |
101 ms |
17732 KB |
Output is correct |
124 |
Correct |
87 ms |
17224 KB |
Output is correct |
125 |
Correct |
94 ms |
16912 KB |
Output is correct |
126 |
Correct |
76 ms |
17212 KB |
Output is correct |
127 |
Correct |
83 ms |
16460 KB |
Output is correct |
128 |
Correct |
84 ms |
16344 KB |
Output is correct |