// ~Be name khoda~ //
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define cl clear
#define endll '\n'
const int maxn = 1e6 + 10;
const int maxn5 = 1e5 + 10;
const int maxnt = 1.2e6 + 10;
const int maxn3 = 1e3 + 10;
const int mod = 1e9 + 7;
const ll inf = 2e18;
int ind = 1;
set <pair<int, int>> av[maxn5];
vector <int> have, liv, req, adj[maxn5];
ll fen[maxn5];
int sz[maxn5], big[maxn5], h[maxn5];
int par[maxn5], f[maxn5], fid[maxn5];
int fir[maxn5], a[maxn5];
inline void add(int id, ll val){
for(; id < maxn5; id += id & -id){
fen[id] += val;
have.pb(id);
}
return;
}
inline ll get(int id){
ll ret = 0;
for(; id; id -= id & -id)
ret += fen[id];
return ret;
}
inline void dfs_det(int v){
sz[v] = 1;
big[v] = -1;
for(auto u : adj[v]){
h[u] = h[v] + 1;
dfs_det(u);
sz[v] += sz[u];
par[u] = v;
if(big[v] == -1 || sz[big[v]] <= sz[u])
big[v] = u;
}
return;
}
inline void dfs_hld(int v, int last, int id){
f[v] = last;
fid[v] = id;
//cout << v << ' ' << f[v] << ' ' << fid[v] << endl;
if(last == v)
fir[id] = -h[v] + 1;
av[id].insert({-h[v], maxn5 - 5});
//cout << "then why ? " << v << ' ' << av[id].size() << endl;
if(big[v] != -1){
dfs_hld(big[v], last, id);
}
for(auto u : adj[v]) if(u != big[v]){
dfs_hld(u, u, ind++);
}
return;
}
inline ll solve(int v, int val){
if(v == -1) return 0;
//cout << "aha " << v << ' ' << val << endl;
int id = fid[v];
auto it = av[id].lower_bound(mp(-h[v], -1));
auto itt = it;
itt--;
ll ret = 0;
if(h[v] > (it->fi) * -1){
int len = (it == av[id].end() ? fir[id] : (it->fi)) + h[v];
if((itt->se) > 1)
ret += get((itt->se) - 1) * len;
add(itt->se, len);
//cout << "begor " << ' ' << (itt->fi) << ' ' << (itt->se) << ' ' << len << ' ' << ret << endl;
}
for(; it != av[id].end();){
itt = it;
int len = 0;
itt++;
if(itt != av[id].end())
len = (itt->fi) - (it->fi);
else
len = fir[id] - (it->fi);
itt--;
if((it -> se) > 1)
ret += get((it->se) - 1) * len;
add(it->se, len);
//cout << "Look " << (it->fi) << ' ' << (it->se) << ' ' << len << ' ' << ret << ' ' << fir[id] << endl;
it++;
av[id].erase(itt);
}
av[id].insert({-h[v], val});
ret += solve(par[f[v]], val);
return ret;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n; cin >> n;
for(int i = 0; i < n; i++){
cin >> a[i];
liv.pb(a[i]);
}
sort(all(liv));
liv.resize(unique(all(liv)) - liv.begin());
for(int i = 0; i < n; i++)
a[i] = (lower_bound(all(liv), a[i]) - liv.begin()) + 1;
for(int i = 0; i < n - 1; i++){
int a, b; cin >> a >> b;
a--; b--;
adj[a].pb(b);
req.pb(b);
}
par[0] = -1;
dfs_det(0);
dfs_hld(0, 0, 0);
for(auto u : req){
have.clear();
ll ans = solve(u, a[u]);
cout << ans << '\n';
for(auto v : have)
fen[v] = 0;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
4 ms |
7356 KB |
Output is correct |
3 |
Correct |
4 ms |
7356 KB |
Output is correct |
4 |
Correct |
4 ms |
7364 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7376 KB |
Output is correct |
8 |
Correct |
4 ms |
7504 KB |
Output is correct |
9 |
Correct |
4 ms |
7632 KB |
Output is correct |
10 |
Correct |
4 ms |
7496 KB |
Output is correct |
11 |
Correct |
4 ms |
7508 KB |
Output is correct |
12 |
Correct |
4 ms |
7504 KB |
Output is correct |
13 |
Correct |
4 ms |
7508 KB |
Output is correct |
14 |
Correct |
4 ms |
7500 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7368 KB |
Output is correct |
17 |
Correct |
5 ms |
7372 KB |
Output is correct |
18 |
Correct |
5 ms |
7376 KB |
Output is correct |
19 |
Correct |
5 ms |
7500 KB |
Output is correct |
20 |
Correct |
4 ms |
7500 KB |
Output is correct |
21 |
Correct |
4 ms |
7500 KB |
Output is correct |
22 |
Correct |
4 ms |
7368 KB |
Output is correct |
23 |
Correct |
4 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7368 KB |
Output is correct |
26 |
Correct |
4 ms |
7624 KB |
Output is correct |
27 |
Correct |
4 ms |
7500 KB |
Output is correct |
28 |
Correct |
6 ms |
7496 KB |
Output is correct |
29 |
Correct |
4 ms |
7500 KB |
Output is correct |
30 |
Correct |
4 ms |
7368 KB |
Output is correct |
31 |
Correct |
4 ms |
7372 KB |
Output is correct |
32 |
Correct |
5 ms |
7500 KB |
Output is correct |
33 |
Correct |
6 ms |
7500 KB |
Output is correct |
34 |
Correct |
4 ms |
7564 KB |
Output is correct |
35 |
Correct |
4 ms |
7500 KB |
Output is correct |
36 |
Correct |
5 ms |
7516 KB |
Output is correct |
37 |
Correct |
5 ms |
7500 KB |
Output is correct |
38 |
Correct |
4 ms |
7496 KB |
Output is correct |
39 |
Correct |
4 ms |
7500 KB |
Output is correct |
40 |
Correct |
4 ms |
7500 KB |
Output is correct |
41 |
Correct |
5 ms |
7500 KB |
Output is correct |
42 |
Correct |
4 ms |
7500 KB |
Output is correct |
43 |
Correct |
4 ms |
7500 KB |
Output is correct |
44 |
Correct |
4 ms |
7500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
4 ms |
7356 KB |
Output is correct |
3 |
Correct |
4 ms |
7356 KB |
Output is correct |
4 |
Correct |
4 ms |
7364 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7376 KB |
Output is correct |
8 |
Correct |
4 ms |
7504 KB |
Output is correct |
9 |
Correct |
4 ms |
7632 KB |
Output is correct |
10 |
Correct |
4 ms |
7496 KB |
Output is correct |
11 |
Correct |
4 ms |
7508 KB |
Output is correct |
12 |
Correct |
4 ms |
7504 KB |
Output is correct |
13 |
Correct |
4 ms |
7508 KB |
Output is correct |
14 |
Correct |
4 ms |
7500 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7368 KB |
Output is correct |
17 |
Correct |
5 ms |
7372 KB |
Output is correct |
18 |
Correct |
5 ms |
7376 KB |
Output is correct |
19 |
Correct |
5 ms |
7500 KB |
Output is correct |
20 |
Correct |
4 ms |
7500 KB |
Output is correct |
21 |
Correct |
4 ms |
7500 KB |
Output is correct |
22 |
Correct |
4 ms |
7368 KB |
Output is correct |
23 |
Correct |
4 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7368 KB |
Output is correct |
26 |
Correct |
4 ms |
7624 KB |
Output is correct |
27 |
Correct |
4 ms |
7500 KB |
Output is correct |
28 |
Correct |
6 ms |
7496 KB |
Output is correct |
29 |
Correct |
4 ms |
7500 KB |
Output is correct |
30 |
Correct |
4 ms |
7368 KB |
Output is correct |
31 |
Correct |
4 ms |
7372 KB |
Output is correct |
32 |
Correct |
5 ms |
7500 KB |
Output is correct |
33 |
Correct |
6 ms |
7500 KB |
Output is correct |
34 |
Correct |
4 ms |
7564 KB |
Output is correct |
35 |
Correct |
4 ms |
7500 KB |
Output is correct |
36 |
Correct |
5 ms |
7516 KB |
Output is correct |
37 |
Correct |
5 ms |
7500 KB |
Output is correct |
38 |
Correct |
4 ms |
7496 KB |
Output is correct |
39 |
Correct |
4 ms |
7500 KB |
Output is correct |
40 |
Correct |
4 ms |
7500 KB |
Output is correct |
41 |
Correct |
5 ms |
7500 KB |
Output is correct |
42 |
Correct |
4 ms |
7500 KB |
Output is correct |
43 |
Correct |
4 ms |
7500 KB |
Output is correct |
44 |
Correct |
4 ms |
7500 KB |
Output is correct |
45 |
Correct |
5 ms |
7508 KB |
Output is correct |
46 |
Correct |
12 ms |
7860 KB |
Output is correct |
47 |
Correct |
10 ms |
7936 KB |
Output is correct |
48 |
Correct |
11 ms |
7928 KB |
Output is correct |
49 |
Correct |
8 ms |
9044 KB |
Output is correct |
50 |
Correct |
7 ms |
9036 KB |
Output is correct |
51 |
Correct |
7 ms |
9044 KB |
Output is correct |
52 |
Correct |
7 ms |
8568 KB |
Output is correct |
53 |
Correct |
8 ms |
8656 KB |
Output is correct |
54 |
Correct |
8 ms |
8564 KB |
Output is correct |
55 |
Correct |
10 ms |
8580 KB |
Output is correct |
56 |
Correct |
7 ms |
8388 KB |
Output is correct |
57 |
Correct |
11 ms |
7884 KB |
Output is correct |
58 |
Correct |
14 ms |
7884 KB |
Output is correct |
59 |
Correct |
12 ms |
7888 KB |
Output is correct |
60 |
Correct |
13 ms |
7928 KB |
Output is correct |
61 |
Correct |
8 ms |
8412 KB |
Output is correct |
62 |
Correct |
8 ms |
8396 KB |
Output is correct |
63 |
Correct |
7 ms |
8356 KB |
Output is correct |
64 |
Correct |
9 ms |
7884 KB |
Output is correct |
65 |
Correct |
10 ms |
7884 KB |
Output is correct |
66 |
Correct |
9 ms |
7892 KB |
Output is correct |
67 |
Correct |
9 ms |
7884 KB |
Output is correct |
68 |
Correct |
7 ms |
8908 KB |
Output is correct |
69 |
Correct |
7 ms |
8528 KB |
Output is correct |
70 |
Correct |
7 ms |
8536 KB |
Output is correct |
71 |
Correct |
7 ms |
8396 KB |
Output is correct |
72 |
Correct |
11 ms |
7916 KB |
Output is correct |
73 |
Correct |
12 ms |
7892 KB |
Output is correct |
74 |
Correct |
8 ms |
8404 KB |
Output is correct |
75 |
Correct |
7 ms |
8144 KB |
Output is correct |
76 |
Correct |
9 ms |
8020 KB |
Output is correct |
77 |
Correct |
10 ms |
8012 KB |
Output is correct |
78 |
Correct |
7 ms |
8140 KB |
Output is correct |
79 |
Correct |
8 ms |
7976 KB |
Output is correct |
80 |
Correct |
8 ms |
8012 KB |
Output is correct |
81 |
Correct |
8 ms |
8124 KB |
Output is correct |
82 |
Correct |
8 ms |
8000 KB |
Output is correct |
83 |
Correct |
8 ms |
8020 KB |
Output is correct |
84 |
Correct |
7 ms |
8140 KB |
Output is correct |
85 |
Correct |
8 ms |
8012 KB |
Output is correct |
86 |
Correct |
7 ms |
8012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7364 KB |
Output is correct |
2 |
Correct |
4 ms |
7356 KB |
Output is correct |
3 |
Correct |
4 ms |
7356 KB |
Output is correct |
4 |
Correct |
4 ms |
7364 KB |
Output is correct |
5 |
Correct |
4 ms |
7372 KB |
Output is correct |
6 |
Correct |
4 ms |
7372 KB |
Output is correct |
7 |
Correct |
5 ms |
7376 KB |
Output is correct |
8 |
Correct |
4 ms |
7504 KB |
Output is correct |
9 |
Correct |
4 ms |
7632 KB |
Output is correct |
10 |
Correct |
4 ms |
7496 KB |
Output is correct |
11 |
Correct |
4 ms |
7508 KB |
Output is correct |
12 |
Correct |
4 ms |
7504 KB |
Output is correct |
13 |
Correct |
4 ms |
7508 KB |
Output is correct |
14 |
Correct |
4 ms |
7500 KB |
Output is correct |
15 |
Correct |
5 ms |
7372 KB |
Output is correct |
16 |
Correct |
5 ms |
7368 KB |
Output is correct |
17 |
Correct |
5 ms |
7372 KB |
Output is correct |
18 |
Correct |
5 ms |
7376 KB |
Output is correct |
19 |
Correct |
5 ms |
7500 KB |
Output is correct |
20 |
Correct |
4 ms |
7500 KB |
Output is correct |
21 |
Correct |
4 ms |
7500 KB |
Output is correct |
22 |
Correct |
4 ms |
7368 KB |
Output is correct |
23 |
Correct |
4 ms |
7372 KB |
Output is correct |
24 |
Correct |
4 ms |
7372 KB |
Output is correct |
25 |
Correct |
4 ms |
7368 KB |
Output is correct |
26 |
Correct |
4 ms |
7624 KB |
Output is correct |
27 |
Correct |
4 ms |
7500 KB |
Output is correct |
28 |
Correct |
6 ms |
7496 KB |
Output is correct |
29 |
Correct |
4 ms |
7500 KB |
Output is correct |
30 |
Correct |
4 ms |
7368 KB |
Output is correct |
31 |
Correct |
4 ms |
7372 KB |
Output is correct |
32 |
Correct |
5 ms |
7500 KB |
Output is correct |
33 |
Correct |
6 ms |
7500 KB |
Output is correct |
34 |
Correct |
4 ms |
7564 KB |
Output is correct |
35 |
Correct |
4 ms |
7500 KB |
Output is correct |
36 |
Correct |
5 ms |
7516 KB |
Output is correct |
37 |
Correct |
5 ms |
7500 KB |
Output is correct |
38 |
Correct |
4 ms |
7496 KB |
Output is correct |
39 |
Correct |
4 ms |
7500 KB |
Output is correct |
40 |
Correct |
4 ms |
7500 KB |
Output is correct |
41 |
Correct |
5 ms |
7500 KB |
Output is correct |
42 |
Correct |
4 ms |
7500 KB |
Output is correct |
43 |
Correct |
4 ms |
7500 KB |
Output is correct |
44 |
Correct |
4 ms |
7500 KB |
Output is correct |
45 |
Correct |
5 ms |
7508 KB |
Output is correct |
46 |
Correct |
12 ms |
7860 KB |
Output is correct |
47 |
Correct |
10 ms |
7936 KB |
Output is correct |
48 |
Correct |
11 ms |
7928 KB |
Output is correct |
49 |
Correct |
8 ms |
9044 KB |
Output is correct |
50 |
Correct |
7 ms |
9036 KB |
Output is correct |
51 |
Correct |
7 ms |
9044 KB |
Output is correct |
52 |
Correct |
7 ms |
8568 KB |
Output is correct |
53 |
Correct |
8 ms |
8656 KB |
Output is correct |
54 |
Correct |
8 ms |
8564 KB |
Output is correct |
55 |
Correct |
10 ms |
8580 KB |
Output is correct |
56 |
Correct |
7 ms |
8388 KB |
Output is correct |
57 |
Correct |
11 ms |
7884 KB |
Output is correct |
58 |
Correct |
14 ms |
7884 KB |
Output is correct |
59 |
Correct |
12 ms |
7888 KB |
Output is correct |
60 |
Correct |
13 ms |
7928 KB |
Output is correct |
61 |
Correct |
8 ms |
8412 KB |
Output is correct |
62 |
Correct |
8 ms |
8396 KB |
Output is correct |
63 |
Correct |
7 ms |
8356 KB |
Output is correct |
64 |
Correct |
9 ms |
7884 KB |
Output is correct |
65 |
Correct |
10 ms |
7884 KB |
Output is correct |
66 |
Correct |
9 ms |
7892 KB |
Output is correct |
67 |
Correct |
9 ms |
7884 KB |
Output is correct |
68 |
Correct |
7 ms |
8908 KB |
Output is correct |
69 |
Correct |
7 ms |
8528 KB |
Output is correct |
70 |
Correct |
7 ms |
8536 KB |
Output is correct |
71 |
Correct |
7 ms |
8396 KB |
Output is correct |
72 |
Correct |
11 ms |
7916 KB |
Output is correct |
73 |
Correct |
12 ms |
7892 KB |
Output is correct |
74 |
Correct |
8 ms |
8404 KB |
Output is correct |
75 |
Correct |
7 ms |
8144 KB |
Output is correct |
76 |
Correct |
9 ms |
8020 KB |
Output is correct |
77 |
Correct |
10 ms |
8012 KB |
Output is correct |
78 |
Correct |
7 ms |
8140 KB |
Output is correct |
79 |
Correct |
8 ms |
7976 KB |
Output is correct |
80 |
Correct |
8 ms |
8012 KB |
Output is correct |
81 |
Correct |
8 ms |
8124 KB |
Output is correct |
82 |
Correct |
8 ms |
8000 KB |
Output is correct |
83 |
Correct |
8 ms |
8020 KB |
Output is correct |
84 |
Correct |
7 ms |
8140 KB |
Output is correct |
85 |
Correct |
8 ms |
8012 KB |
Output is correct |
86 |
Correct |
7 ms |
8012 KB |
Output is correct |
87 |
Correct |
23 ms |
8604 KB |
Output is correct |
88 |
Correct |
63 ms |
11324 KB |
Output is correct |
89 |
Correct |
249 ms |
20544 KB |
Output is correct |
90 |
Correct |
250 ms |
20544 KB |
Output is correct |
91 |
Correct |
267 ms |
20620 KB |
Output is correct |
92 |
Correct |
138 ms |
48324 KB |
Output is correct |
93 |
Correct |
126 ms |
48348 KB |
Output is correct |
94 |
Correct |
132 ms |
48396 KB |
Output is correct |
95 |
Correct |
120 ms |
35520 KB |
Output is correct |
96 |
Correct |
130 ms |
35900 KB |
Output is correct |
97 |
Correct |
132 ms |
35904 KB |
Output is correct |
98 |
Correct |
122 ms |
35908 KB |
Output is correct |
99 |
Correct |
119 ms |
33828 KB |
Output is correct |
100 |
Correct |
338 ms |
20676 KB |
Output is correct |
101 |
Correct |
396 ms |
20960 KB |
Output is correct |
102 |
Correct |
390 ms |
21060 KB |
Output is correct |
103 |
Correct |
406 ms |
21068 KB |
Output is correct |
104 |
Correct |
137 ms |
33956 KB |
Output is correct |
105 |
Correct |
143 ms |
33752 KB |
Output is correct |
106 |
Correct |
133 ms |
33788 KB |
Output is correct |
107 |
Correct |
232 ms |
19044 KB |
Output is correct |
108 |
Correct |
246 ms |
19080 KB |
Output is correct |
109 |
Correct |
232 ms |
19532 KB |
Output is correct |
110 |
Correct |
125 ms |
47040 KB |
Output is correct |
111 |
Correct |
127 ms |
35592 KB |
Output is correct |
112 |
Correct |
115 ms |
36520 KB |
Output is correct |
113 |
Correct |
112 ms |
32456 KB |
Output is correct |
114 |
Correct |
335 ms |
20608 KB |
Output is correct |
115 |
Correct |
375 ms |
19536 KB |
Output is correct |
116 |
Correct |
140 ms |
32588 KB |
Output is correct |
117 |
Correct |
123 ms |
26840 KB |
Output is correct |
118 |
Correct |
121 ms |
24568 KB |
Output is correct |
119 |
Correct |
124 ms |
23504 KB |
Output is correct |
120 |
Correct |
112 ms |
25652 KB |
Output is correct |
121 |
Correct |
115 ms |
23540 KB |
Output is correct |
122 |
Correct |
112 ms |
22392 KB |
Output is correct |
123 |
Correct |
150 ms |
26840 KB |
Output is correct |
124 |
Correct |
138 ms |
24648 KB |
Output is correct |
125 |
Correct |
136 ms |
23496 KB |
Output is correct |
126 |
Correct |
141 ms |
25804 KB |
Output is correct |
127 |
Correct |
138 ms |
23632 KB |
Output is correct |
128 |
Correct |
131 ms |
22552 KB |
Output is correct |