// vaziat meshki-ghermeze !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 1e5 + 10;
const ll Inf = 2242545357980376863LL;
const int Log = 19;
int fen[N];
void Add(int idx, int x){
for(; idx < N; idx += idx & (-idx))
fen[idx] += x;
}
int Get(int idx){
int res = 0;
for(; idx; idx -= idx & (-idx))
res += fen[idx];
return res;
}
int par[N][Log], dep[N];
int val[N], ep[N];
int C[N];
int Blift(int u, int h){
for(int i = 0; i < Log; i++)
if(h >> i & 1)
u = par[u][i];
return u;
}
typedef pair<int, int> pii;
vector<pii> A;
void Solve(int rt, int u, int v){
// cerr << "! " << rt << ' ' << u << ' ' << v << '\n';
if(rt == v) return ;
if(ep[rt] == u){
A.pb({val[rt], dep[v] - dep[rt]});
return ;
}
int v1 = v;
int v2 = ep[rt];
if(dep[v1] > dep[v2])
v1 = Blift(v1, dep[v1] - dep[v2]);
else
v2 = Blift(v2, dep[v2] - dep[v1]);
// assert(v1 != v2);
for(int l = Log - 1; l >= 0; l--){
if(par[v1][l] != par[v2][l])
v1 = par[v1][l], v2 = par[v2][l];
}
A.pb({val[rt], dep[v2] - dep[rt]});
ep[v2] = ep[rt];
val[v2] = val[rt];
Solve(v1, u, v);
return ;
}
ll Calc(){
reverse(all(A));
ll res = 0;
for(auto X : A){
res += 1ll * X.S * Get(X.F - 1);
Add(X.F, X.S);
}
for(auto X : A)
Add(X.F, -X.S);
return res;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
vector<int> V;
for(int i = 1; i <= n; i++){
cin >> C[i];
V.pb(C[i]);
}
sort(all(V));
for(int i = 1; i <= n; i++){
C[i] = lower_bound(all(V), C[i]) - V.begin();
C[i] += 3;
}
dep[1] = 0;
val[1] = C[1];
ep[1] = 1;
int u, v;
for(int i = 1; i < n; i++){
cin >> u >> v;
par[v][0] = u;
dep[v] = dep[u] + 1;
for(int i = 1; i < Log; i++)
par[v][i] = par[par[v][i - 1]][i - 1];
A.clear();
Solve(1, u, v);
val[1] = C[v];
ep[1] = v;
cout << Calc() << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
408 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
492 KB |
Output is correct |
37 |
Correct |
1 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
492 KB |
Output is correct |
42 |
Correct |
1 ms |
492 KB |
Output is correct |
43 |
Correct |
1 ms |
492 KB |
Output is correct |
44 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
408 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
492 KB |
Output is correct |
37 |
Correct |
1 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
492 KB |
Output is correct |
42 |
Correct |
1 ms |
492 KB |
Output is correct |
43 |
Correct |
1 ms |
492 KB |
Output is correct |
44 |
Correct |
1 ms |
492 KB |
Output is correct |
45 |
Correct |
2 ms |
492 KB |
Output is correct |
46 |
Correct |
6 ms |
876 KB |
Output is correct |
47 |
Correct |
6 ms |
876 KB |
Output is correct |
48 |
Correct |
6 ms |
876 KB |
Output is correct |
49 |
Correct |
4 ms |
876 KB |
Output is correct |
50 |
Correct |
4 ms |
876 KB |
Output is correct |
51 |
Correct |
4 ms |
876 KB |
Output is correct |
52 |
Correct |
4 ms |
1004 KB |
Output is correct |
53 |
Correct |
5 ms |
1004 KB |
Output is correct |
54 |
Correct |
5 ms |
1004 KB |
Output is correct |
55 |
Correct |
5 ms |
1004 KB |
Output is correct |
56 |
Correct |
4 ms |
876 KB |
Output is correct |
57 |
Correct |
10 ms |
876 KB |
Output is correct |
58 |
Correct |
10 ms |
876 KB |
Output is correct |
59 |
Correct |
10 ms |
876 KB |
Output is correct |
60 |
Correct |
10 ms |
876 KB |
Output is correct |
61 |
Correct |
5 ms |
876 KB |
Output is correct |
62 |
Correct |
5 ms |
876 KB |
Output is correct |
63 |
Correct |
5 ms |
876 KB |
Output is correct |
64 |
Correct |
5 ms |
876 KB |
Output is correct |
65 |
Correct |
6 ms |
876 KB |
Output is correct |
66 |
Correct |
6 ms |
876 KB |
Output is correct |
67 |
Correct |
6 ms |
876 KB |
Output is correct |
68 |
Correct |
3 ms |
876 KB |
Output is correct |
69 |
Correct |
4 ms |
1132 KB |
Output is correct |
70 |
Correct |
5 ms |
1004 KB |
Output is correct |
71 |
Correct |
4 ms |
876 KB |
Output is correct |
72 |
Correct |
10 ms |
876 KB |
Output is correct |
73 |
Correct |
10 ms |
876 KB |
Output is correct |
74 |
Correct |
4 ms |
876 KB |
Output is correct |
75 |
Correct |
4 ms |
876 KB |
Output is correct |
76 |
Correct |
5 ms |
876 KB |
Output is correct |
77 |
Correct |
4 ms |
876 KB |
Output is correct |
78 |
Correct |
4 ms |
876 KB |
Output is correct |
79 |
Correct |
4 ms |
896 KB |
Output is correct |
80 |
Correct |
4 ms |
876 KB |
Output is correct |
81 |
Correct |
5 ms |
876 KB |
Output is correct |
82 |
Correct |
5 ms |
876 KB |
Output is correct |
83 |
Correct |
6 ms |
876 KB |
Output is correct |
84 |
Correct |
5 ms |
876 KB |
Output is correct |
85 |
Correct |
4 ms |
876 KB |
Output is correct |
86 |
Correct |
4 ms |
876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
492 KB |
Output is correct |
8 |
Correct |
1 ms |
408 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
1 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
492 KB |
Output is correct |
14 |
Correct |
1 ms |
492 KB |
Output is correct |
15 |
Correct |
1 ms |
492 KB |
Output is correct |
16 |
Correct |
1 ms |
492 KB |
Output is correct |
17 |
Correct |
1 ms |
492 KB |
Output is correct |
18 |
Correct |
1 ms |
492 KB |
Output is correct |
19 |
Correct |
1 ms |
492 KB |
Output is correct |
20 |
Correct |
1 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
512 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
492 KB |
Output is correct |
28 |
Correct |
1 ms |
492 KB |
Output is correct |
29 |
Correct |
1 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
31 |
Correct |
1 ms |
492 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
1 ms |
512 KB |
Output is correct |
36 |
Correct |
1 ms |
492 KB |
Output is correct |
37 |
Correct |
1 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
492 KB |
Output is correct |
39 |
Correct |
1 ms |
492 KB |
Output is correct |
40 |
Correct |
1 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
492 KB |
Output is correct |
42 |
Correct |
1 ms |
492 KB |
Output is correct |
43 |
Correct |
1 ms |
492 KB |
Output is correct |
44 |
Correct |
1 ms |
492 KB |
Output is correct |
45 |
Correct |
2 ms |
492 KB |
Output is correct |
46 |
Correct |
6 ms |
876 KB |
Output is correct |
47 |
Correct |
6 ms |
876 KB |
Output is correct |
48 |
Correct |
6 ms |
876 KB |
Output is correct |
49 |
Correct |
4 ms |
876 KB |
Output is correct |
50 |
Correct |
4 ms |
876 KB |
Output is correct |
51 |
Correct |
4 ms |
876 KB |
Output is correct |
52 |
Correct |
4 ms |
1004 KB |
Output is correct |
53 |
Correct |
5 ms |
1004 KB |
Output is correct |
54 |
Correct |
5 ms |
1004 KB |
Output is correct |
55 |
Correct |
5 ms |
1004 KB |
Output is correct |
56 |
Correct |
4 ms |
876 KB |
Output is correct |
57 |
Correct |
10 ms |
876 KB |
Output is correct |
58 |
Correct |
10 ms |
876 KB |
Output is correct |
59 |
Correct |
10 ms |
876 KB |
Output is correct |
60 |
Correct |
10 ms |
876 KB |
Output is correct |
61 |
Correct |
5 ms |
876 KB |
Output is correct |
62 |
Correct |
5 ms |
876 KB |
Output is correct |
63 |
Correct |
5 ms |
876 KB |
Output is correct |
64 |
Correct |
5 ms |
876 KB |
Output is correct |
65 |
Correct |
6 ms |
876 KB |
Output is correct |
66 |
Correct |
6 ms |
876 KB |
Output is correct |
67 |
Correct |
6 ms |
876 KB |
Output is correct |
68 |
Correct |
3 ms |
876 KB |
Output is correct |
69 |
Correct |
4 ms |
1132 KB |
Output is correct |
70 |
Correct |
5 ms |
1004 KB |
Output is correct |
71 |
Correct |
4 ms |
876 KB |
Output is correct |
72 |
Correct |
10 ms |
876 KB |
Output is correct |
73 |
Correct |
10 ms |
876 KB |
Output is correct |
74 |
Correct |
4 ms |
876 KB |
Output is correct |
75 |
Correct |
4 ms |
876 KB |
Output is correct |
76 |
Correct |
5 ms |
876 KB |
Output is correct |
77 |
Correct |
4 ms |
876 KB |
Output is correct |
78 |
Correct |
4 ms |
876 KB |
Output is correct |
79 |
Correct |
4 ms |
896 KB |
Output is correct |
80 |
Correct |
4 ms |
876 KB |
Output is correct |
81 |
Correct |
5 ms |
876 KB |
Output is correct |
82 |
Correct |
5 ms |
876 KB |
Output is correct |
83 |
Correct |
6 ms |
876 KB |
Output is correct |
84 |
Correct |
5 ms |
876 KB |
Output is correct |
85 |
Correct |
4 ms |
876 KB |
Output is correct |
86 |
Correct |
4 ms |
876 KB |
Output is correct |
87 |
Correct |
17 ms |
1644 KB |
Output is correct |
88 |
Correct |
56 ms |
4204 KB |
Output is correct |
89 |
Correct |
271 ms |
12652 KB |
Output is correct |
90 |
Correct |
269 ms |
12780 KB |
Output is correct |
91 |
Correct |
265 ms |
12652 KB |
Output is correct |
92 |
Correct |
148 ms |
11756 KB |
Output is correct |
93 |
Correct |
156 ms |
11756 KB |
Output is correct |
94 |
Correct |
146 ms |
11756 KB |
Output is correct |
95 |
Correct |
175 ms |
16496 KB |
Output is correct |
96 |
Correct |
180 ms |
16880 KB |
Output is correct |
97 |
Correct |
182 ms |
16880 KB |
Output is correct |
98 |
Correct |
188 ms |
17136 KB |
Output is correct |
99 |
Correct |
173 ms |
12780 KB |
Output is correct |
100 |
Correct |
483 ms |
12396 KB |
Output is correct |
101 |
Correct |
532 ms |
12908 KB |
Output is correct |
102 |
Correct |
539 ms |
12780 KB |
Output is correct |
103 |
Correct |
534 ms |
12652 KB |
Output is correct |
104 |
Correct |
224 ms |
12780 KB |
Output is correct |
105 |
Correct |
224 ms |
12652 KB |
Output is correct |
106 |
Correct |
235 ms |
12780 KB |
Output is correct |
107 |
Correct |
237 ms |
11628 KB |
Output is correct |
108 |
Correct |
248 ms |
12104 KB |
Output is correct |
109 |
Correct |
267 ms |
12396 KB |
Output is correct |
110 |
Correct |
135 ms |
11244 KB |
Output is correct |
111 |
Correct |
173 ms |
16496 KB |
Output is correct |
112 |
Correct |
173 ms |
16240 KB |
Output is correct |
113 |
Correct |
168 ms |
12116 KB |
Output is correct |
114 |
Correct |
483 ms |
12352 KB |
Output is correct |
115 |
Correct |
528 ms |
12140 KB |
Output is correct |
116 |
Correct |
223 ms |
12140 KB |
Output is correct |
117 |
Correct |
167 ms |
12396 KB |
Output is correct |
118 |
Correct |
159 ms |
12396 KB |
Output is correct |
119 |
Correct |
161 ms |
12396 KB |
Output is correct |
120 |
Correct |
155 ms |
12140 KB |
Output is correct |
121 |
Correct |
151 ms |
12140 KB |
Output is correct |
122 |
Correct |
152 ms |
12140 KB |
Output is correct |
123 |
Correct |
245 ms |
12396 KB |
Output is correct |
124 |
Correct |
236 ms |
12396 KB |
Output is correct |
125 |
Correct |
239 ms |
12396 KB |
Output is correct |
126 |
Correct |
233 ms |
12140 KB |
Output is correct |
127 |
Correct |
235 ms |
12268 KB |
Output is correct |
128 |
Correct |
234 ms |
12268 KB |
Output is correct |