#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll L = 18;
const ll sq = 400;
const ll N = 1e5 + 50;
const ll Mod = 1e9 + 7;
ll n, c[N], par[N][L], h[N], mx[N];
vector<int> edge, adj[N];
void dfs(int v, int p = 0){
par[v][0] = p;
for(int i = 1; i < L; i++){
if(!par[v][i-1]) break;
par[v][i] = par[par[v][i-1]][i-1];
}
for(int u: adj[v]){
h[u] = h[v] + 1; dfs(u, v);
}
}
int getPar(int v, int k){
for(int i = 0; i < L; i++){
if(k >> i & 1) v = par[v][i];
}
return v;
}
int lca(int v, int u){
if(h[v] < h[u]) swap(u, v);
v = getPar(v, h[v] - h[u]);
if(u == v) return v;
for(int i = L-1; i >= 0; i--){
if(par[v][i] != par[u][i]){
v = par[v][i]; u = par[u][i];
}
}
return par[v][0];
}
void prep(int v){
for(int u: adj[v]){
prep(u); mx[v] = max(mx[v], mx[u]);
}
if(mx[v]) c[v] = c[edge[mx[v]]];
}
vector<int> vec;
vector<pii> block;
int bit[N], m;
void upd(int x, int y){
for(; x <= m; x += x & (-x)) bit[x] += y;
}
int get(int x){
int out = 0;
for(; x; x -= x & (-x)) out += bit[x];
return out;
}
ll solve(){
vec.clear();
for(auto i: block) vec.pb(i.F);
sort(all(vec)); vec.resize(unique(all(vec)) - vec.begin());
m = vec.size();
int sum = 0; ll res = 0;
for(auto [x, y]: block){
int id = lower_bound(all(vec), x) - vec.begin() + 1;
res += 1ll * y * (sum - get(id)); upd(id, y); sum += y;
}
for(auto [x, y]: block){
int id = lower_bound(all(vec), x) - vec.begin() + 1; upd(id, -y);
}
return res;
}
int main(){
fast_io;
cin >> n;
for(int i = 1; i <= n; i++) cin >> c[i];
int u, v; edge.pb(1);
for(int i = 1; i < n; i++){
cin >> u >> v;
adj[u].pb(v); edge.pb(v);
}
dfs(1);
for(int i = 1; i < n; i++){
block.clear();
int tah = -1; v = edge[i];
for(int j = i-1; j % sq != 0; j--){
u = lca(v, edge[j]);
if(h[u] > tah){
block.pb({c[edge[j]], h[u] - tah});
tah = h[u];
}
}
vec.clear();
while(h[v] - 1 > tah){
v = par[v][0]; vec.pb(c[v]);
}
reverse(all(vec));
for(int j: vec) block.pb({j, 1});
cout << solve() << endl;
if(i % sq == 0){
fill(mx, mx + N, 0);
for(int j = i; j > i - sq; j--) mx[edge[j]] = j;
prep(1);
}
}
}
Compilation message
construction.cpp: In function 'll solve()':
construction.cpp:96:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
96 | for(auto [x, y]: block){
| ^
construction.cpp:101:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
101 | for(auto [x, y]: block){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
6 ms |
8540 KB |
Output is correct |
6 |
Correct |
6 ms |
8540 KB |
Output is correct |
7 |
Correct |
5 ms |
8540 KB |
Output is correct |
8 |
Correct |
3 ms |
8540 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
4 ms |
8540 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
4 ms |
8696 KB |
Output is correct |
15 |
Correct |
4 ms |
8688 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8536 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
4 ms |
8540 KB |
Output is correct |
22 |
Correct |
5 ms |
8540 KB |
Output is correct |
23 |
Correct |
5 ms |
8540 KB |
Output is correct |
24 |
Correct |
5 ms |
8540 KB |
Output is correct |
25 |
Correct |
5 ms |
8540 KB |
Output is correct |
26 |
Correct |
3 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
3 ms |
8536 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
6 ms |
8796 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
5 ms |
8688 KB |
Output is correct |
34 |
Correct |
6 ms |
8540 KB |
Output is correct |
35 |
Correct |
5 ms |
8540 KB |
Output is correct |
36 |
Correct |
5 ms |
8536 KB |
Output is correct |
37 |
Correct |
5 ms |
8540 KB |
Output is correct |
38 |
Correct |
5 ms |
8540 KB |
Output is correct |
39 |
Correct |
6 ms |
8540 KB |
Output is correct |
40 |
Correct |
6 ms |
8688 KB |
Output is correct |
41 |
Correct |
6 ms |
8540 KB |
Output is correct |
42 |
Correct |
6 ms |
8536 KB |
Output is correct |
43 |
Correct |
7 ms |
8536 KB |
Output is correct |
44 |
Correct |
6 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
6 ms |
8540 KB |
Output is correct |
6 |
Correct |
6 ms |
8540 KB |
Output is correct |
7 |
Correct |
5 ms |
8540 KB |
Output is correct |
8 |
Correct |
3 ms |
8540 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
4 ms |
8540 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
4 ms |
8696 KB |
Output is correct |
15 |
Correct |
4 ms |
8688 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8536 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
4 ms |
8540 KB |
Output is correct |
22 |
Correct |
5 ms |
8540 KB |
Output is correct |
23 |
Correct |
5 ms |
8540 KB |
Output is correct |
24 |
Correct |
5 ms |
8540 KB |
Output is correct |
25 |
Correct |
5 ms |
8540 KB |
Output is correct |
26 |
Correct |
3 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
3 ms |
8536 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
6 ms |
8796 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
5 ms |
8688 KB |
Output is correct |
34 |
Correct |
6 ms |
8540 KB |
Output is correct |
35 |
Correct |
5 ms |
8540 KB |
Output is correct |
36 |
Correct |
5 ms |
8536 KB |
Output is correct |
37 |
Correct |
5 ms |
8540 KB |
Output is correct |
38 |
Correct |
5 ms |
8540 KB |
Output is correct |
39 |
Correct |
6 ms |
8540 KB |
Output is correct |
40 |
Correct |
6 ms |
8688 KB |
Output is correct |
41 |
Correct |
6 ms |
8540 KB |
Output is correct |
42 |
Correct |
6 ms |
8536 KB |
Output is correct |
43 |
Correct |
7 ms |
8536 KB |
Output is correct |
44 |
Correct |
6 ms |
8540 KB |
Output is correct |
45 |
Correct |
11 ms |
8708 KB |
Output is correct |
46 |
Correct |
44 ms |
8756 KB |
Output is correct |
47 |
Correct |
45 ms |
8752 KB |
Output is correct |
48 |
Correct |
42 ms |
8540 KB |
Output is correct |
49 |
Correct |
16 ms |
8796 KB |
Output is correct |
50 |
Correct |
16 ms |
8980 KB |
Output is correct |
51 |
Correct |
16 ms |
8788 KB |
Output is correct |
52 |
Correct |
25 ms |
8792 KB |
Output is correct |
53 |
Correct |
26 ms |
8852 KB |
Output is correct |
54 |
Correct |
25 ms |
8792 KB |
Output is correct |
55 |
Correct |
26 ms |
8796 KB |
Output is correct |
56 |
Correct |
24 ms |
8796 KB |
Output is correct |
57 |
Correct |
30 ms |
8540 KB |
Output is correct |
58 |
Correct |
31 ms |
8536 KB |
Output is correct |
59 |
Correct |
31 ms |
8536 KB |
Output is correct |
60 |
Correct |
31 ms |
8540 KB |
Output is correct |
61 |
Correct |
40 ms |
8796 KB |
Output is correct |
62 |
Correct |
40 ms |
8796 KB |
Output is correct |
63 |
Correct |
43 ms |
8860 KB |
Output is correct |
64 |
Correct |
45 ms |
8540 KB |
Output is correct |
65 |
Correct |
45 ms |
8540 KB |
Output is correct |
66 |
Correct |
58 ms |
8540 KB |
Output is correct |
67 |
Correct |
43 ms |
8540 KB |
Output is correct |
68 |
Correct |
15 ms |
9012 KB |
Output is correct |
69 |
Correct |
25 ms |
8864 KB |
Output is correct |
70 |
Correct |
25 ms |
8796 KB |
Output is correct |
71 |
Correct |
25 ms |
8792 KB |
Output is correct |
72 |
Correct |
30 ms |
8540 KB |
Output is correct |
73 |
Correct |
31 ms |
8540 KB |
Output is correct |
74 |
Correct |
39 ms |
8792 KB |
Output is correct |
75 |
Correct |
28 ms |
8796 KB |
Output is correct |
76 |
Correct |
31 ms |
8788 KB |
Output is correct |
77 |
Correct |
33 ms |
8792 KB |
Output is correct |
78 |
Correct |
27 ms |
8796 KB |
Output is correct |
79 |
Correct |
31 ms |
8540 KB |
Output is correct |
80 |
Correct |
32 ms |
8540 KB |
Output is correct |
81 |
Correct |
47 ms |
8796 KB |
Output is correct |
82 |
Correct |
48 ms |
8796 KB |
Output is correct |
83 |
Correct |
50 ms |
8780 KB |
Output is correct |
84 |
Correct |
51 ms |
8804 KB |
Output is correct |
85 |
Correct |
48 ms |
8540 KB |
Output is correct |
86 |
Correct |
49 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
3 ms |
8540 KB |
Output is correct |
5 |
Correct |
6 ms |
8540 KB |
Output is correct |
6 |
Correct |
6 ms |
8540 KB |
Output is correct |
7 |
Correct |
5 ms |
8540 KB |
Output is correct |
8 |
Correct |
3 ms |
8540 KB |
Output is correct |
9 |
Correct |
3 ms |
8540 KB |
Output is correct |
10 |
Correct |
3 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
4 ms |
8540 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
4 ms |
8696 KB |
Output is correct |
15 |
Correct |
4 ms |
8688 KB |
Output is correct |
16 |
Correct |
4 ms |
8540 KB |
Output is correct |
17 |
Correct |
4 ms |
8540 KB |
Output is correct |
18 |
Correct |
4 ms |
8540 KB |
Output is correct |
19 |
Correct |
4 ms |
8536 KB |
Output is correct |
20 |
Correct |
4 ms |
8540 KB |
Output is correct |
21 |
Correct |
4 ms |
8540 KB |
Output is correct |
22 |
Correct |
5 ms |
8540 KB |
Output is correct |
23 |
Correct |
5 ms |
8540 KB |
Output is correct |
24 |
Correct |
5 ms |
8540 KB |
Output is correct |
25 |
Correct |
5 ms |
8540 KB |
Output is correct |
26 |
Correct |
3 ms |
8540 KB |
Output is correct |
27 |
Correct |
4 ms |
8540 KB |
Output is correct |
28 |
Correct |
3 ms |
8536 KB |
Output is correct |
29 |
Correct |
4 ms |
8540 KB |
Output is correct |
30 |
Correct |
4 ms |
8540 KB |
Output is correct |
31 |
Correct |
6 ms |
8796 KB |
Output is correct |
32 |
Correct |
4 ms |
8540 KB |
Output is correct |
33 |
Correct |
5 ms |
8688 KB |
Output is correct |
34 |
Correct |
6 ms |
8540 KB |
Output is correct |
35 |
Correct |
5 ms |
8540 KB |
Output is correct |
36 |
Correct |
5 ms |
8536 KB |
Output is correct |
37 |
Correct |
5 ms |
8540 KB |
Output is correct |
38 |
Correct |
5 ms |
8540 KB |
Output is correct |
39 |
Correct |
6 ms |
8540 KB |
Output is correct |
40 |
Correct |
6 ms |
8688 KB |
Output is correct |
41 |
Correct |
6 ms |
8540 KB |
Output is correct |
42 |
Correct |
6 ms |
8536 KB |
Output is correct |
43 |
Correct |
7 ms |
8536 KB |
Output is correct |
44 |
Correct |
6 ms |
8540 KB |
Output is correct |
45 |
Correct |
11 ms |
8708 KB |
Output is correct |
46 |
Correct |
44 ms |
8756 KB |
Output is correct |
47 |
Correct |
45 ms |
8752 KB |
Output is correct |
48 |
Correct |
42 ms |
8540 KB |
Output is correct |
49 |
Correct |
16 ms |
8796 KB |
Output is correct |
50 |
Correct |
16 ms |
8980 KB |
Output is correct |
51 |
Correct |
16 ms |
8788 KB |
Output is correct |
52 |
Correct |
25 ms |
8792 KB |
Output is correct |
53 |
Correct |
26 ms |
8852 KB |
Output is correct |
54 |
Correct |
25 ms |
8792 KB |
Output is correct |
55 |
Correct |
26 ms |
8796 KB |
Output is correct |
56 |
Correct |
24 ms |
8796 KB |
Output is correct |
57 |
Correct |
30 ms |
8540 KB |
Output is correct |
58 |
Correct |
31 ms |
8536 KB |
Output is correct |
59 |
Correct |
31 ms |
8536 KB |
Output is correct |
60 |
Correct |
31 ms |
8540 KB |
Output is correct |
61 |
Correct |
40 ms |
8796 KB |
Output is correct |
62 |
Correct |
40 ms |
8796 KB |
Output is correct |
63 |
Correct |
43 ms |
8860 KB |
Output is correct |
64 |
Correct |
45 ms |
8540 KB |
Output is correct |
65 |
Correct |
45 ms |
8540 KB |
Output is correct |
66 |
Correct |
58 ms |
8540 KB |
Output is correct |
67 |
Correct |
43 ms |
8540 KB |
Output is correct |
68 |
Correct |
15 ms |
9012 KB |
Output is correct |
69 |
Correct |
25 ms |
8864 KB |
Output is correct |
70 |
Correct |
25 ms |
8796 KB |
Output is correct |
71 |
Correct |
25 ms |
8792 KB |
Output is correct |
72 |
Correct |
30 ms |
8540 KB |
Output is correct |
73 |
Correct |
31 ms |
8540 KB |
Output is correct |
74 |
Correct |
39 ms |
8792 KB |
Output is correct |
75 |
Correct |
28 ms |
8796 KB |
Output is correct |
76 |
Correct |
31 ms |
8788 KB |
Output is correct |
77 |
Correct |
33 ms |
8792 KB |
Output is correct |
78 |
Correct |
27 ms |
8796 KB |
Output is correct |
79 |
Correct |
31 ms |
8540 KB |
Output is correct |
80 |
Correct |
32 ms |
8540 KB |
Output is correct |
81 |
Correct |
47 ms |
8796 KB |
Output is correct |
82 |
Correct |
48 ms |
8796 KB |
Output is correct |
83 |
Correct |
50 ms |
8780 KB |
Output is correct |
84 |
Correct |
51 ms |
8804 KB |
Output is correct |
85 |
Correct |
48 ms |
8540 KB |
Output is correct |
86 |
Correct |
49 ms |
8540 KB |
Output is correct |
87 |
Correct |
115 ms |
8792 KB |
Output is correct |
88 |
Correct |
379 ms |
13404 KB |
Output is correct |
89 |
Correct |
1749 ms |
21964 KB |
Output is correct |
90 |
Correct |
1745 ms |
22052 KB |
Output is correct |
91 |
Correct |
1781 ms |
21764 KB |
Output is correct |
92 |
Execution timed out |
2069 ms |
27960 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |