#include <bits/stdc++.h>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Construction of Highway"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 1e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
int n;
int a[maxn], h[maxn], par[maxn];
int nex[maxn], In[maxn], siz[maxn];
vector<int> edges[maxn];
vector<pair<int, int>> P[maxn];
pair<int, int> E[maxn];
/// DATA STRUCTURE
struct BIT {
int bit[maxn];
void update(int x, int val) {
for(; x < maxn; x += (x & -x)) bit[x] += val;
}
int get(int x, int ans = 0) {
for(; x > 0; x -= (x & -x)) ans += bit[x];
return ans;
}
} Bit;
/// PRE DFS
int dfs_siz(int u) {
siz[u] = 1;
for(int &v : edges[u]) {
edges[v].erase(find(all(edges[v]), u));
h[v] = h[u] + 1, par[v] = u;
siz[u] += dfs_siz(v);
if(siz[v] > siz[edges[u][0]]) swap(v, edges[u][0]);
}
return siz[u];
}
void dfs_hld(int u) {
static int Time = 0;
In[u] = ++Time;
for(int v : edges[u]) {
if(v == edges[u][0]) nex[v] = nex[u];
else nex[v] = v;
dfs_hld(v);
}
}
/// HLD DECOMP <3
ll get(int u, int val) {
ll ans = 0;
vector<pair<int, int>> S, tmp;
auto prog = [&](int u, int p) {
int prv = h[p] - 1;
tmp.clear();
while(!P[p].empty() && h[P[p].back().first] < h[u]) {
tmp.push_back({P[p].back().second, h[P[p].back().first] - prv});
prv = h[P[p].back().first];
P[p].pop_back();
}
if(!P[p].empty()) tmp.push_back({P[p].back().second, h[u] - prv});
P[p].push_back({u, val});
reverse(all(tmp));
for(auto x : tmp) S.push_back(x);
};
for(; u != 0; u = par[nex[u]]) {
prog(u, nex[u]);
}
for(auto [cl, w] : S) ans += 1ll * w * Bit.get(cl - 1), Bit.update(cl, w);
for(auto [cl, w] : S) Bit.update(cl, -w);
return ans;
}
void solve()
{
vector<int> Comp;
cin >> n;
fi(i, 1, n) cin >> a[i], Comp.push_back(a[i]);
fi(i, 1, n - 1) {
int x, y; cin >> x >> y;
E[i] = {x, y};
edges[x].push_back(y), edges[y].push_back(x);
}
resp(Comp);
fi(i, 1, n) a[i] = upper_bound(all(Comp), a[i]) - Comp.begin();
nex[1] = h[1] = 1;
dfs_siz(1), dfs_hld(1);
get(1, a[1]);
fi(i, 1, n - 1) {
int x, y; tie(x, y) = E[i];
cout << get(y, a[y]) << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
construction.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5004 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5244 KB |
Output is correct |
6 |
Correct |
3 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5152 KB |
Output is correct |
15 |
Correct |
2 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 |
5272 KB |
Output is correct |
19 |
Correct |
3 ms |
5212 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 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 |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5096 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5140 KB |
Output is correct |
33 |
Correct |
2 ms |
5168 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 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 |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5004 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5244 KB |
Output is correct |
6 |
Correct |
3 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5152 KB |
Output is correct |
15 |
Correct |
2 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 |
5272 KB |
Output is correct |
19 |
Correct |
3 ms |
5212 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 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 |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5096 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5140 KB |
Output is correct |
33 |
Correct |
2 ms |
5168 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 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 |
5444 KB |
Output is correct |
47 |
Correct |
6 ms |
5468 KB |
Output is correct |
48 |
Correct |
7 ms |
5468 KB |
Output is correct |
49 |
Correct |
4 ms |
5724 KB |
Output is correct |
50 |
Correct |
5 ms |
5724 KB |
Output is correct |
51 |
Correct |
4 ms |
5692 KB |
Output is correct |
52 |
Correct |
4 ms |
5724 KB |
Output is correct |
53 |
Correct |
5 ms |
5724 KB |
Output is correct |
54 |
Correct |
5 ms |
5976 KB |
Output is correct |
55 |
Correct |
4 ms |
5628 KB |
Output is correct |
56 |
Correct |
5 ms |
5656 KB |
Output is correct |
57 |
Correct |
7 ms |
5624 KB |
Output is correct |
58 |
Correct |
7 ms |
5468 KB |
Output is correct |
59 |
Correct |
7 ms |
5532 KB |
Output is correct |
60 |
Correct |
7 ms |
5464 KB |
Output is correct |
61 |
Correct |
5 ms |
5720 KB |
Output is correct |
62 |
Correct |
5 ms |
5724 KB |
Output is correct |
63 |
Correct |
5 ms |
5724 KB |
Output is correct |
64 |
Correct |
7 ms |
5464 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 |
6 ms |
5724 KB |
Output is correct |
70 |
Correct |
4 ms |
5724 KB |
Output is correct |
71 |
Correct |
4 ms |
5724 KB |
Output is correct |
72 |
Correct |
6 ms |
5468 KB |
Output is correct |
73 |
Correct |
6 ms |
5468 KB |
Output is correct |
74 |
Correct |
4 ms |
5724 KB |
Output is correct |
75 |
Correct |
5 ms |
5468 KB |
Output is correct |
76 |
Correct |
5 ms |
5468 KB |
Output is correct |
77 |
Correct |
4 ms |
5468 KB |
Output is correct |
78 |
Correct |
5 ms |
5468 KB |
Output is correct |
79 |
Correct |
5 ms |
5464 KB |
Output is correct |
80 |
Correct |
5 ms |
5468 KB |
Output is correct |
81 |
Correct |
5 ms |
5464 KB |
Output is correct |
82 |
Correct |
5 ms |
5692 KB |
Output is correct |
83 |
Correct |
5 ms |
5416 KB |
Output is correct |
84 |
Correct |
5 ms |
5468 KB |
Output is correct |
85 |
Correct |
4 ms |
5468 KB |
Output is correct |
86 |
Correct |
5 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5212 KB |
Output is correct |
2 |
Correct |
2 ms |
5004 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
3 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5244 KB |
Output is correct |
6 |
Correct |
3 ms |
5208 KB |
Output is correct |
7 |
Correct |
2 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
3 ms |
5212 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
2 ms |
5208 KB |
Output is correct |
14 |
Correct |
3 ms |
5152 KB |
Output is correct |
15 |
Correct |
2 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 |
5272 KB |
Output is correct |
19 |
Correct |
3 ms |
5212 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
2 ms |
5212 KB |
Output is correct |
22 |
Correct |
2 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 |
5212 KB |
Output is correct |
28 |
Correct |
2 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5212 KB |
Output is correct |
30 |
Correct |
3 ms |
5096 KB |
Output is correct |
31 |
Correct |
3 ms |
5212 KB |
Output is correct |
32 |
Correct |
3 ms |
5140 KB |
Output is correct |
33 |
Correct |
2 ms |
5168 KB |
Output is correct |
34 |
Correct |
3 ms |
5212 KB |
Output is correct |
35 |
Correct |
2 ms |
5212 KB |
Output is correct |
36 |
Correct |
3 ms |
5212 KB |
Output is correct |
37 |
Correct |
3 ms |
5212 KB |
Output is correct |
38 |
Correct |
3 ms |
5212 KB |
Output is correct |
39 |
Correct |
2 ms |
5212 KB |
Output is correct |
40 |
Correct |
3 ms |
5212 KB |
Output is correct |
41 |
Correct |
2 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 |
5444 KB |
Output is correct |
47 |
Correct |
6 ms |
5468 KB |
Output is correct |
48 |
Correct |
7 ms |
5468 KB |
Output is correct |
49 |
Correct |
4 ms |
5724 KB |
Output is correct |
50 |
Correct |
5 ms |
5724 KB |
Output is correct |
51 |
Correct |
4 ms |
5692 KB |
Output is correct |
52 |
Correct |
4 ms |
5724 KB |
Output is correct |
53 |
Correct |
5 ms |
5724 KB |
Output is correct |
54 |
Correct |
5 ms |
5976 KB |
Output is correct |
55 |
Correct |
4 ms |
5628 KB |
Output is correct |
56 |
Correct |
5 ms |
5656 KB |
Output is correct |
57 |
Correct |
7 ms |
5624 KB |
Output is correct |
58 |
Correct |
7 ms |
5468 KB |
Output is correct |
59 |
Correct |
7 ms |
5532 KB |
Output is correct |
60 |
Correct |
7 ms |
5464 KB |
Output is correct |
61 |
Correct |
5 ms |
5720 KB |
Output is correct |
62 |
Correct |
5 ms |
5724 KB |
Output is correct |
63 |
Correct |
5 ms |
5724 KB |
Output is correct |
64 |
Correct |
7 ms |
5464 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 |
6 ms |
5724 KB |
Output is correct |
70 |
Correct |
4 ms |
5724 KB |
Output is correct |
71 |
Correct |
4 ms |
5724 KB |
Output is correct |
72 |
Correct |
6 ms |
5468 KB |
Output is correct |
73 |
Correct |
6 ms |
5468 KB |
Output is correct |
74 |
Correct |
4 ms |
5724 KB |
Output is correct |
75 |
Correct |
5 ms |
5468 KB |
Output is correct |
76 |
Correct |
5 ms |
5468 KB |
Output is correct |
77 |
Correct |
4 ms |
5468 KB |
Output is correct |
78 |
Correct |
5 ms |
5468 KB |
Output is correct |
79 |
Correct |
5 ms |
5464 KB |
Output is correct |
80 |
Correct |
5 ms |
5468 KB |
Output is correct |
81 |
Correct |
5 ms |
5464 KB |
Output is correct |
82 |
Correct |
5 ms |
5692 KB |
Output is correct |
83 |
Correct |
5 ms |
5416 KB |
Output is correct |
84 |
Correct |
5 ms |
5468 KB |
Output is correct |
85 |
Correct |
4 ms |
5468 KB |
Output is correct |
86 |
Correct |
5 ms |
5468 KB |
Output is correct |
87 |
Correct |
13 ms |
6200 KB |
Output is correct |
88 |
Correct |
48 ms |
8532 KB |
Output is correct |
89 |
Correct |
152 ms |
16336 KB |
Output is correct |
90 |
Correct |
170 ms |
16436 KB |
Output is correct |
91 |
Correct |
157 ms |
16324 KB |
Output is correct |
92 |
Correct |
70 ms |
22212 KB |
Output is correct |
93 |
Correct |
71 ms |
22224 KB |
Output is correct |
94 |
Correct |
79 ms |
22224 KB |
Output is correct |
95 |
Correct |
74 ms |
20948 KB |
Output is correct |
96 |
Correct |
80 ms |
21320 KB |
Output is correct |
97 |
Correct |
79 ms |
21332 KB |
Output is correct |
98 |
Correct |
116 ms |
21332 KB |
Output is correct |
99 |
Correct |
79 ms |
19920 KB |
Output is correct |
100 |
Correct |
168 ms |
15316 KB |
Output is correct |
101 |
Correct |
209 ms |
15636 KB |
Output is correct |
102 |
Correct |
202 ms |
15784 KB |
Output is correct |
103 |
Correct |
235 ms |
15648 KB |
Output is correct |
104 |
Correct |
95 ms |
20000 KB |
Output is correct |
105 |
Correct |
96 ms |
19928 KB |
Output is correct |
106 |
Correct |
99 ms |
19972 KB |
Output is correct |
107 |
Correct |
158 ms |
15148 KB |
Output is correct |
108 |
Correct |
136 ms |
15304 KB |
Output is correct |
109 |
Correct |
153 ms |
15832 KB |
Output is correct |
110 |
Correct |
69 ms |
21164 KB |
Output is correct |
111 |
Correct |
86 ms |
21140 KB |
Output is correct |
112 |
Correct |
64 ms |
20456 KB |
Output is correct |
113 |
Correct |
80 ms |
19152 KB |
Output is correct |
114 |
Correct |
185 ms |
15500 KB |
Output is correct |
115 |
Correct |
177 ms |
14872 KB |
Output is correct |
116 |
Correct |
77 ms |
18916 KB |
Output is correct |
117 |
Correct |
87 ms |
17804 KB |
Output is correct |
118 |
Correct |
92 ms |
17456 KB |
Output is correct |
119 |
Correct |
82 ms |
17128 KB |
Output is correct |
120 |
Correct |
76 ms |
17284 KB |
Output is correct |
121 |
Correct |
68 ms |
16716 KB |
Output is correct |
122 |
Correct |
70 ms |
16436 KB |
Output is correct |
123 |
Correct |
91 ms |
17844 KB |
Output is correct |
124 |
Correct |
83 ms |
17484 KB |
Output is correct |
125 |
Correct |
81 ms |
17164 KB |
Output is correct |
126 |
Correct |
69 ms |
17220 KB |
Output is correct |
127 |
Correct |
77 ms |
16716 KB |
Output is correct |
128 |
Correct |
73 ms |
16396 KB |
Output is correct |