#include "bits/stdc++.h"
using namespace std;
#define ar array
typedef int64_t ll;
#define int ll
#define her cout<<"here"<<endl;
const int N = 1e5 + 5;
vector<int> edges[N];
int a[N], v[N], par[N][20];
int tin[N], tout[N], t, d[N];
void dfs(int u){
tin[u] = ++t;
for(int j=1;j<20;j++){
par[u][j] = par[par[u][j-1]][j-1];
}
for(auto x : edges[u]){
if(x == par[u][0]) continue;
d[x] = d[u] + 1;
par[x][0] = u;
dfs(x);
}
tout[u] = t;
}
bool is(int a, int b){
return tin[a] <= tin[b] && tout[b] <= tout[a];
}
int lca(int a, int b){
assert(!is(a, b) && !is(b, a));
for(int j=19;~j;j--){
if(!is(par[a][j], b)) a = par[a][j];
}
return a;
}
struct BIT{
int tree[N];
void add(int i, int v){
for(;i<N;i+=(i&-i)) tree[i] += v;
}
int get(int i){
int r = 0;
for(;i>0;i-=(i&-i)) r += tree[i];
return r;
}
int get(int l, int r){
return get(r) - get(--l);
}
}tree;
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin >> n;
for(int i=1;i<=n;i++) cin >> a[i];
vector<int> p(n); iota(p.begin(), p.end(), 1);
sort(p.begin(), p.end(), [&](int i, int j){
return (a[i] < a[j]);
});
for(int i=0,last=0,j=0;i<n;){
while(j<n && a[p[j]] == a[p[i]]) j++;
last++;
while(i < j) a[p[i]] = last, i++;
}
vector<ar<int, 2>> e;
for(int i=1;i<n;i++){
int a, b; cin >> a >> b;
e.push_back({a, b});
edges[a].push_back(b);
edges[b].push_back(a);
}
par[1][0] = 1;
dfs(1);
v[1] = 1;
for(auto [a, b] : e){
int c = 1;
vector<ar<int, 2>> t;
while(!is(v[c], b) && c != b){
//~ cout<<c<<" "<<v[c]<<" "<<b<<endl;
int u = lca(b, v[c]);
int x = lca(v[c], b);
v[x] = v[c];
t.push_back({d[u] - d[c], ::a[v[c]]});
c = u;
}
//~ assert(v[c] == a);
if(c != b) t.push_back({d[b] - d[c], ::a[v[c]]});
v[1] = b;
int res = 0;
//~ for(auto x : t) cout<<x[0]<<" ";
//~ cout<<"\n";
//~ for(auto x : t) cout<<x[1]<<" ";
//~ cout<<"\n";
for(auto x : t){
tree.add(x[1], x[0]);
res += tree.get(x[1] + 1, N) * x[0];
}
cout<<res<<"\n";
for(auto x : t){
tree.add(x[1], -x[0]);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2820 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2888 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Correct |
3 ms |
2820 KB |
Output is correct |
10 |
Correct |
3 ms |
2900 KB |
Output is correct |
11 |
Correct |
3 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2780 KB |
Output is correct |
16 |
Correct |
3 ms |
2884 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
3 ms |
2820 KB |
Output is correct |
21 |
Correct |
3 ms |
2772 KB |
Output is correct |
22 |
Correct |
4 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2876 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2848 KB |
Output is correct |
27 |
Correct |
2 ms |
2900 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
4 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2816 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
3 ms |
2772 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
3 ms |
2812 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2816 KB |
Output is correct |
40 |
Correct |
3 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2816 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2820 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2888 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Correct |
3 ms |
2820 KB |
Output is correct |
10 |
Correct |
3 ms |
2900 KB |
Output is correct |
11 |
Correct |
3 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2780 KB |
Output is correct |
16 |
Correct |
3 ms |
2884 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
3 ms |
2820 KB |
Output is correct |
21 |
Correct |
3 ms |
2772 KB |
Output is correct |
22 |
Correct |
4 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2876 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2848 KB |
Output is correct |
27 |
Correct |
2 ms |
2900 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
4 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2816 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
3 ms |
2772 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
3 ms |
2812 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2816 KB |
Output is correct |
40 |
Correct |
3 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2816 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2812 KB |
Output is correct |
45 |
Correct |
3 ms |
3028 KB |
Output is correct |
46 |
Correct |
10 ms |
3844 KB |
Output is correct |
47 |
Correct |
10 ms |
3876 KB |
Output is correct |
48 |
Correct |
10 ms |
3876 KB |
Output is correct |
49 |
Correct |
6 ms |
3944 KB |
Output is correct |
50 |
Correct |
5 ms |
3976 KB |
Output is correct |
51 |
Correct |
6 ms |
4052 KB |
Output is correct |
52 |
Correct |
7 ms |
3972 KB |
Output is correct |
53 |
Correct |
7 ms |
4048 KB |
Output is correct |
54 |
Correct |
7 ms |
4052 KB |
Output is correct |
55 |
Correct |
6 ms |
4052 KB |
Output is correct |
56 |
Correct |
6 ms |
3924 KB |
Output is correct |
57 |
Correct |
19 ms |
3780 KB |
Output is correct |
58 |
Correct |
16 ms |
3796 KB |
Output is correct |
59 |
Correct |
17 ms |
3856 KB |
Output is correct |
60 |
Correct |
21 ms |
3840 KB |
Output is correct |
61 |
Correct |
7 ms |
3924 KB |
Output is correct |
62 |
Correct |
8 ms |
3924 KB |
Output is correct |
63 |
Correct |
7 ms |
3924 KB |
Output is correct |
64 |
Correct |
10 ms |
3796 KB |
Output is correct |
65 |
Correct |
11 ms |
3796 KB |
Output is correct |
66 |
Correct |
10 ms |
3824 KB |
Output is correct |
67 |
Correct |
10 ms |
3796 KB |
Output is correct |
68 |
Correct |
6 ms |
3924 KB |
Output is correct |
69 |
Correct |
6 ms |
3972 KB |
Output is correct |
70 |
Correct |
6 ms |
3972 KB |
Output is correct |
71 |
Correct |
6 ms |
3924 KB |
Output is correct |
72 |
Correct |
18 ms |
3844 KB |
Output is correct |
73 |
Correct |
19 ms |
3796 KB |
Output is correct |
74 |
Correct |
7 ms |
3924 KB |
Output is correct |
75 |
Correct |
8 ms |
3924 KB |
Output is correct |
76 |
Correct |
6 ms |
3796 KB |
Output is correct |
77 |
Correct |
7 ms |
3796 KB |
Output is correct |
78 |
Correct |
6 ms |
3796 KB |
Output is correct |
79 |
Correct |
6 ms |
3796 KB |
Output is correct |
80 |
Correct |
6 ms |
3836 KB |
Output is correct |
81 |
Correct |
7 ms |
3924 KB |
Output is correct |
82 |
Correct |
8 ms |
3796 KB |
Output is correct |
83 |
Correct |
10 ms |
3840 KB |
Output is correct |
84 |
Correct |
6 ms |
3796 KB |
Output is correct |
85 |
Correct |
8 ms |
3796 KB |
Output is correct |
86 |
Correct |
10 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2680 KB |
Output is correct |
4 |
Correct |
2 ms |
2820 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
3 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2888 KB |
Output is correct |
8 |
Correct |
3 ms |
2900 KB |
Output is correct |
9 |
Correct |
3 ms |
2820 KB |
Output is correct |
10 |
Correct |
3 ms |
2900 KB |
Output is correct |
11 |
Correct |
3 ms |
2900 KB |
Output is correct |
12 |
Correct |
2 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2900 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2780 KB |
Output is correct |
16 |
Correct |
3 ms |
2884 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2900 KB |
Output is correct |
20 |
Correct |
3 ms |
2820 KB |
Output is correct |
21 |
Correct |
3 ms |
2772 KB |
Output is correct |
22 |
Correct |
4 ms |
2772 KB |
Output is correct |
23 |
Correct |
3 ms |
2876 KB |
Output is correct |
24 |
Correct |
4 ms |
2772 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
2 ms |
2848 KB |
Output is correct |
27 |
Correct |
2 ms |
2900 KB |
Output is correct |
28 |
Correct |
2 ms |
2900 KB |
Output is correct |
29 |
Correct |
2 ms |
2772 KB |
Output is correct |
30 |
Correct |
4 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2816 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
3 ms |
2772 KB |
Output is correct |
34 |
Correct |
3 ms |
2772 KB |
Output is correct |
35 |
Correct |
3 ms |
2812 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
2 ms |
2772 KB |
Output is correct |
39 |
Correct |
2 ms |
2816 KB |
Output is correct |
40 |
Correct |
3 ms |
2772 KB |
Output is correct |
41 |
Correct |
2 ms |
2772 KB |
Output is correct |
42 |
Correct |
2 ms |
2816 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2812 KB |
Output is correct |
45 |
Correct |
3 ms |
3028 KB |
Output is correct |
46 |
Correct |
10 ms |
3844 KB |
Output is correct |
47 |
Correct |
10 ms |
3876 KB |
Output is correct |
48 |
Correct |
10 ms |
3876 KB |
Output is correct |
49 |
Correct |
6 ms |
3944 KB |
Output is correct |
50 |
Correct |
5 ms |
3976 KB |
Output is correct |
51 |
Correct |
6 ms |
4052 KB |
Output is correct |
52 |
Correct |
7 ms |
3972 KB |
Output is correct |
53 |
Correct |
7 ms |
4048 KB |
Output is correct |
54 |
Correct |
7 ms |
4052 KB |
Output is correct |
55 |
Correct |
6 ms |
4052 KB |
Output is correct |
56 |
Correct |
6 ms |
3924 KB |
Output is correct |
57 |
Correct |
19 ms |
3780 KB |
Output is correct |
58 |
Correct |
16 ms |
3796 KB |
Output is correct |
59 |
Correct |
17 ms |
3856 KB |
Output is correct |
60 |
Correct |
21 ms |
3840 KB |
Output is correct |
61 |
Correct |
7 ms |
3924 KB |
Output is correct |
62 |
Correct |
8 ms |
3924 KB |
Output is correct |
63 |
Correct |
7 ms |
3924 KB |
Output is correct |
64 |
Correct |
10 ms |
3796 KB |
Output is correct |
65 |
Correct |
11 ms |
3796 KB |
Output is correct |
66 |
Correct |
10 ms |
3824 KB |
Output is correct |
67 |
Correct |
10 ms |
3796 KB |
Output is correct |
68 |
Correct |
6 ms |
3924 KB |
Output is correct |
69 |
Correct |
6 ms |
3972 KB |
Output is correct |
70 |
Correct |
6 ms |
3972 KB |
Output is correct |
71 |
Correct |
6 ms |
3924 KB |
Output is correct |
72 |
Correct |
18 ms |
3844 KB |
Output is correct |
73 |
Correct |
19 ms |
3796 KB |
Output is correct |
74 |
Correct |
7 ms |
3924 KB |
Output is correct |
75 |
Correct |
8 ms |
3924 KB |
Output is correct |
76 |
Correct |
6 ms |
3796 KB |
Output is correct |
77 |
Correct |
7 ms |
3796 KB |
Output is correct |
78 |
Correct |
6 ms |
3796 KB |
Output is correct |
79 |
Correct |
6 ms |
3796 KB |
Output is correct |
80 |
Correct |
6 ms |
3836 KB |
Output is correct |
81 |
Correct |
7 ms |
3924 KB |
Output is correct |
82 |
Correct |
8 ms |
3796 KB |
Output is correct |
83 |
Correct |
10 ms |
3840 KB |
Output is correct |
84 |
Correct |
6 ms |
3796 KB |
Output is correct |
85 |
Correct |
8 ms |
3796 KB |
Output is correct |
86 |
Correct |
10 ms |
3796 KB |
Output is correct |
87 |
Correct |
28 ms |
5584 KB |
Output is correct |
88 |
Correct |
100 ms |
11308 KB |
Output is correct |
89 |
Correct |
425 ms |
31492 KB |
Output is correct |
90 |
Correct |
498 ms |
31468 KB |
Output is correct |
91 |
Correct |
439 ms |
31588 KB |
Output is correct |
92 |
Correct |
139 ms |
36272 KB |
Output is correct |
93 |
Correct |
114 ms |
36248 KB |
Output is correct |
94 |
Correct |
117 ms |
36220 KB |
Output is correct |
95 |
Correct |
228 ms |
36156 KB |
Output is correct |
96 |
Correct |
192 ms |
36452 KB |
Output is correct |
97 |
Correct |
206 ms |
36540 KB |
Output is correct |
98 |
Correct |
198 ms |
36476 KB |
Output is correct |
99 |
Correct |
199 ms |
34876 KB |
Output is correct |
100 |
Correct |
792 ms |
31124 KB |
Output is correct |
101 |
Correct |
918 ms |
31436 KB |
Output is correct |
102 |
Correct |
825 ms |
31464 KB |
Output is correct |
103 |
Correct |
789 ms |
31352 KB |
Output is correct |
104 |
Correct |
297 ms |
34776 KB |
Output is correct |
105 |
Correct |
255 ms |
34764 KB |
Output is correct |
106 |
Correct |
290 ms |
34788 KB |
Output is correct |
107 |
Correct |
372 ms |
29904 KB |
Output is correct |
108 |
Correct |
407 ms |
30088 KB |
Output is correct |
109 |
Correct |
375 ms |
30600 KB |
Output is correct |
110 |
Correct |
123 ms |
34840 KB |
Output is correct |
111 |
Correct |
187 ms |
36148 KB |
Output is correct |
112 |
Correct |
174 ms |
35360 KB |
Output is correct |
113 |
Correct |
185 ms |
33508 KB |
Output is correct |
114 |
Correct |
758 ms |
31124 KB |
Output is correct |
115 |
Correct |
753 ms |
30160 KB |
Output is correct |
116 |
Correct |
261 ms |
33468 KB |
Output is correct |
117 |
Correct |
168 ms |
32920 KB |
Output is correct |
118 |
Correct |
180 ms |
32400 KB |
Output is correct |
119 |
Correct |
166 ms |
32104 KB |
Output is correct |
120 |
Correct |
168 ms |
31836 KB |
Output is correct |
121 |
Correct |
185 ms |
31448 KB |
Output is correct |
122 |
Correct |
172 ms |
31016 KB |
Output is correct |
123 |
Correct |
274 ms |
33048 KB |
Output is correct |
124 |
Correct |
294 ms |
32520 KB |
Output is correct |
125 |
Correct |
278 ms |
32220 KB |
Output is correct |
126 |
Correct |
294 ms |
31892 KB |
Output is correct |
127 |
Correct |
265 ms |
31428 KB |
Output is correct |
128 |
Correct |
260 ms |
31112 KB |
Output is correct |