//whatever
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
template<class T, int SZ> struct BIT {
T bit[SZ+1];
vpi v;
BIT() { memset(bit,0,sizeof bit); }
void upd(int k, T val) { // add val to index k
v.pb({k,val});
for( ;k <= SZ; k += (k&-k)) bit[k] += val;
}
void clr() {
for (auto a: v) upd(a.f,-a.s);
v.clear();
}
T query(int k) {
T temp = 0;
for (;k > 0;k -= (k&-k)) temp += bit[k];
return temp;
}
T query(int l, int r) { return query(r)-query(l-1); } // range query [l,r]
};
BIT<int,MX> B;
vector<vi> graph;
template <int V> struct HeavyLight { // sum queries, sum updates
int parent[V], heavy[V], depth[V];
int root[V], treePos[V];
vpi dat[V];
void init() {
int n = sz(graph)-1;
FOR(i,1,n+1) heavy[i] = -1;
parent[1] = -1, depth[1] = 0;
dfs(1);
for (int i = 1; i <= n; ++i)
if (parent[i] == -1 || heavy[parent[i]] != i)
for (int j = i; j != -1; j = heavy[j]) {
root[j] = i;
dat[i].pb({0,1});
}
}
int dfs(int v) {
int size = 1, maxSubtree = 0;
for (auto u : graph[v]) if (u != parent[v]) {
parent[u] = v;
depth[u] = depth[v] + 1;
int subtree = dfs(u);
if (subtree > maxSubtree) heavy[v] = u, maxSubtree = subtree;
size += subtree;
}
return size;
}
ll query(int v) {
int r = root[v], tot = depth[v]-depth[r]+1;
vpi tmp;
int TOT = tot;
F0Rd(i,sz(dat[r])) {
auto a = dat[r][i];
if (a.s >= TOT) {
tmp.pb({a.f,TOT});
break;
} else {
TOT -= a.s;
tmp.pb(a);
}
}
/*cout << v << " " << r << " " << dat[r].back().f << "\n";
for (auto a: tmp) cout << a.f << " " << a.s << "\n";
exit(0);*/
reverse(all(tmp));
ll ret = 0;
for (auto x: tmp) {
ret += (ll)x.s*B.query(x.f-1);
B.upd(x.f,x.s);
}
return ret;
}
void upd(int v, int val) {
int r = root[v], tot = depth[v]-depth[r]+1;
// cout << v << " " << tot << "\n";
int TOT = tot;
while (dat[r].back().s < TOT) {
TOT -= dat[r].back().s;
dat[r].pop_back();
}
dat[r].back().s -= TOT;
if (dat[r].back().s == 0) dat[r].pop_back();
dat[r].pb({val,tot});
// cout << "AH " << val << " " << tot << "\n";
// exit(0);
}
ll queryPath(int v) {
//cout << v << " " << root[v] << " " << sz(dat[v]) << "\n";
// exit(0);
ll ans = 0;
B.clr();
for (; v != -1; v = parent[root[v]]) ans += query(v);
return ans;
}
void updPath(int v, int val) {
for (; v != -1; v = parent[root[v]]) upd(v,val);
}
};
HeavyLight<1<<17> H;
int N,M,A[MX];
pi p[MX];
void compress() {
map<int,int> m;
FOR(i,1,N+1) m[A[i]] = 0;
int co = 0;
for (auto& a: m) a.s = ++co;
FOR(i,1,N+1) {
A[i] = m[A[i]];
// cout << A[i] << "\n";
}
// exit(0);
}
int main() {
cin >> N; graph.resize(N+1);
FOR(i,1,N+1) cin >> A[i];
compress();
F0R(i,N-1) {
cin >> p[i].f >> p[i].s;
graph[p[i].f].pb(p[i].s);
graph[p[i].s].pb(p[i].f);
}
H.init();
H.updPath(1,A[1]);
// exit(0);
F0R(i,N-1) {
cout << H.queryPath(p[i].f) << "\n";
// exit(0);
H.updPath(p[i].s,A[p[i].s]);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3836 KB |
Output is correct |
2 |
Correct |
9 ms |
3892 KB |
Output is correct |
3 |
Correct |
6 ms |
3892 KB |
Output is correct |
4 |
Correct |
8 ms |
4128 KB |
Output is correct |
5 |
Correct |
9 ms |
4128 KB |
Output is correct |
6 |
Correct |
9 ms |
4128 KB |
Output is correct |
7 |
Correct |
8 ms |
4128 KB |
Output is correct |
8 |
Correct |
7 ms |
4128 KB |
Output is correct |
9 |
Correct |
7 ms |
4128 KB |
Output is correct |
10 |
Correct |
7 ms |
4128 KB |
Output is correct |
11 |
Correct |
9 ms |
4140 KB |
Output is correct |
12 |
Correct |
8 ms |
4140 KB |
Output is correct |
13 |
Correct |
6 ms |
4140 KB |
Output is correct |
14 |
Correct |
6 ms |
4140 KB |
Output is correct |
15 |
Correct |
10 ms |
4140 KB |
Output is correct |
16 |
Correct |
6 ms |
4140 KB |
Output is correct |
17 |
Correct |
7 ms |
4140 KB |
Output is correct |
18 |
Correct |
7 ms |
4180 KB |
Output is correct |
19 |
Correct |
9 ms |
4180 KB |
Output is correct |
20 |
Correct |
6 ms |
4180 KB |
Output is correct |
21 |
Correct |
7 ms |
4180 KB |
Output is correct |
22 |
Correct |
6 ms |
4180 KB |
Output is correct |
23 |
Correct |
6 ms |
4180 KB |
Output is correct |
24 |
Correct |
8 ms |
4180 KB |
Output is correct |
25 |
Correct |
7 ms |
4180 KB |
Output is correct |
26 |
Correct |
8 ms |
4180 KB |
Output is correct |
27 |
Correct |
7 ms |
4180 KB |
Output is correct |
28 |
Correct |
6 ms |
4180 KB |
Output is correct |
29 |
Correct |
6 ms |
4180 KB |
Output is correct |
30 |
Correct |
7 ms |
4204 KB |
Output is correct |
31 |
Correct |
7 ms |
4204 KB |
Output is correct |
32 |
Correct |
7 ms |
4204 KB |
Output is correct |
33 |
Correct |
9 ms |
4204 KB |
Output is correct |
34 |
Correct |
7 ms |
4204 KB |
Output is correct |
35 |
Correct |
8 ms |
4204 KB |
Output is correct |
36 |
Correct |
9 ms |
4204 KB |
Output is correct |
37 |
Correct |
6 ms |
4204 KB |
Output is correct |
38 |
Correct |
7 ms |
4204 KB |
Output is correct |
39 |
Correct |
9 ms |
4204 KB |
Output is correct |
40 |
Correct |
7 ms |
4204 KB |
Output is correct |
41 |
Correct |
8 ms |
4220 KB |
Output is correct |
42 |
Correct |
8 ms |
4220 KB |
Output is correct |
43 |
Correct |
7 ms |
4220 KB |
Output is correct |
44 |
Correct |
7 ms |
4220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3836 KB |
Output is correct |
2 |
Correct |
9 ms |
3892 KB |
Output is correct |
3 |
Correct |
6 ms |
3892 KB |
Output is correct |
4 |
Correct |
8 ms |
4128 KB |
Output is correct |
5 |
Correct |
9 ms |
4128 KB |
Output is correct |
6 |
Correct |
9 ms |
4128 KB |
Output is correct |
7 |
Correct |
8 ms |
4128 KB |
Output is correct |
8 |
Correct |
7 ms |
4128 KB |
Output is correct |
9 |
Correct |
7 ms |
4128 KB |
Output is correct |
10 |
Correct |
7 ms |
4128 KB |
Output is correct |
11 |
Correct |
9 ms |
4140 KB |
Output is correct |
12 |
Correct |
8 ms |
4140 KB |
Output is correct |
13 |
Correct |
6 ms |
4140 KB |
Output is correct |
14 |
Correct |
6 ms |
4140 KB |
Output is correct |
15 |
Correct |
10 ms |
4140 KB |
Output is correct |
16 |
Correct |
6 ms |
4140 KB |
Output is correct |
17 |
Correct |
7 ms |
4140 KB |
Output is correct |
18 |
Correct |
7 ms |
4180 KB |
Output is correct |
19 |
Correct |
9 ms |
4180 KB |
Output is correct |
20 |
Correct |
6 ms |
4180 KB |
Output is correct |
21 |
Correct |
7 ms |
4180 KB |
Output is correct |
22 |
Correct |
6 ms |
4180 KB |
Output is correct |
23 |
Correct |
6 ms |
4180 KB |
Output is correct |
24 |
Correct |
8 ms |
4180 KB |
Output is correct |
25 |
Correct |
7 ms |
4180 KB |
Output is correct |
26 |
Correct |
8 ms |
4180 KB |
Output is correct |
27 |
Correct |
7 ms |
4180 KB |
Output is correct |
28 |
Correct |
6 ms |
4180 KB |
Output is correct |
29 |
Correct |
6 ms |
4180 KB |
Output is correct |
30 |
Correct |
7 ms |
4204 KB |
Output is correct |
31 |
Correct |
7 ms |
4204 KB |
Output is correct |
32 |
Correct |
7 ms |
4204 KB |
Output is correct |
33 |
Correct |
9 ms |
4204 KB |
Output is correct |
34 |
Correct |
7 ms |
4204 KB |
Output is correct |
35 |
Correct |
8 ms |
4204 KB |
Output is correct |
36 |
Correct |
9 ms |
4204 KB |
Output is correct |
37 |
Correct |
6 ms |
4204 KB |
Output is correct |
38 |
Correct |
7 ms |
4204 KB |
Output is correct |
39 |
Correct |
9 ms |
4204 KB |
Output is correct |
40 |
Correct |
7 ms |
4204 KB |
Output is correct |
41 |
Correct |
8 ms |
4220 KB |
Output is correct |
42 |
Correct |
8 ms |
4220 KB |
Output is correct |
43 |
Correct |
7 ms |
4220 KB |
Output is correct |
44 |
Correct |
7 ms |
4220 KB |
Output is correct |
45 |
Correct |
10 ms |
4220 KB |
Output is correct |
46 |
Correct |
18 ms |
4476 KB |
Output is correct |
47 |
Correct |
21 ms |
4608 KB |
Output is correct |
48 |
Correct |
25 ms |
4616 KB |
Output is correct |
49 |
Correct |
20 ms |
4764 KB |
Output is correct |
50 |
Correct |
18 ms |
4860 KB |
Output is correct |
51 |
Correct |
19 ms |
4860 KB |
Output is correct |
52 |
Correct |
16 ms |
4860 KB |
Output is correct |
53 |
Correct |
16 ms |
4860 KB |
Output is correct |
54 |
Correct |
16 ms |
4860 KB |
Output is correct |
55 |
Correct |
16 ms |
4860 KB |
Output is correct |
56 |
Correct |
18 ms |
4860 KB |
Output is correct |
57 |
Correct |
22 ms |
4860 KB |
Output is correct |
58 |
Correct |
26 ms |
4860 KB |
Output is correct |
59 |
Correct |
29 ms |
4860 KB |
Output is correct |
60 |
Correct |
34 ms |
4860 KB |
Output is correct |
61 |
Correct |
19 ms |
4860 KB |
Output is correct |
62 |
Correct |
21 ms |
4860 KB |
Output is correct |
63 |
Correct |
20 ms |
4860 KB |
Output is correct |
64 |
Correct |
15 ms |
4860 KB |
Output is correct |
65 |
Correct |
16 ms |
4860 KB |
Output is correct |
66 |
Correct |
17 ms |
4860 KB |
Output is correct |
67 |
Correct |
16 ms |
4860 KB |
Output is correct |
68 |
Correct |
18 ms |
4860 KB |
Output is correct |
69 |
Correct |
17 ms |
4860 KB |
Output is correct |
70 |
Correct |
19 ms |
4860 KB |
Output is correct |
71 |
Correct |
18 ms |
4860 KB |
Output is correct |
72 |
Correct |
22 ms |
4860 KB |
Output is correct |
73 |
Correct |
22 ms |
4860 KB |
Output is correct |
74 |
Correct |
14 ms |
4860 KB |
Output is correct |
75 |
Correct |
17 ms |
4860 KB |
Output is correct |
76 |
Correct |
18 ms |
4860 KB |
Output is correct |
77 |
Correct |
22 ms |
4860 KB |
Output is correct |
78 |
Correct |
13 ms |
4860 KB |
Output is correct |
79 |
Correct |
13 ms |
4860 KB |
Output is correct |
80 |
Correct |
17 ms |
4860 KB |
Output is correct |
81 |
Correct |
17 ms |
4860 KB |
Output is correct |
82 |
Correct |
23 ms |
4860 KB |
Output is correct |
83 |
Correct |
25 ms |
4860 KB |
Output is correct |
84 |
Correct |
17 ms |
4860 KB |
Output is correct |
85 |
Correct |
15 ms |
4860 KB |
Output is correct |
86 |
Correct |
17 ms |
4860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3836 KB |
Output is correct |
2 |
Correct |
9 ms |
3892 KB |
Output is correct |
3 |
Correct |
6 ms |
3892 KB |
Output is correct |
4 |
Correct |
8 ms |
4128 KB |
Output is correct |
5 |
Correct |
9 ms |
4128 KB |
Output is correct |
6 |
Correct |
9 ms |
4128 KB |
Output is correct |
7 |
Correct |
8 ms |
4128 KB |
Output is correct |
8 |
Correct |
7 ms |
4128 KB |
Output is correct |
9 |
Correct |
7 ms |
4128 KB |
Output is correct |
10 |
Correct |
7 ms |
4128 KB |
Output is correct |
11 |
Correct |
9 ms |
4140 KB |
Output is correct |
12 |
Correct |
8 ms |
4140 KB |
Output is correct |
13 |
Correct |
6 ms |
4140 KB |
Output is correct |
14 |
Correct |
6 ms |
4140 KB |
Output is correct |
15 |
Correct |
10 ms |
4140 KB |
Output is correct |
16 |
Correct |
6 ms |
4140 KB |
Output is correct |
17 |
Correct |
7 ms |
4140 KB |
Output is correct |
18 |
Correct |
7 ms |
4180 KB |
Output is correct |
19 |
Correct |
9 ms |
4180 KB |
Output is correct |
20 |
Correct |
6 ms |
4180 KB |
Output is correct |
21 |
Correct |
7 ms |
4180 KB |
Output is correct |
22 |
Correct |
6 ms |
4180 KB |
Output is correct |
23 |
Correct |
6 ms |
4180 KB |
Output is correct |
24 |
Correct |
8 ms |
4180 KB |
Output is correct |
25 |
Correct |
7 ms |
4180 KB |
Output is correct |
26 |
Correct |
8 ms |
4180 KB |
Output is correct |
27 |
Correct |
7 ms |
4180 KB |
Output is correct |
28 |
Correct |
6 ms |
4180 KB |
Output is correct |
29 |
Correct |
6 ms |
4180 KB |
Output is correct |
30 |
Correct |
7 ms |
4204 KB |
Output is correct |
31 |
Correct |
7 ms |
4204 KB |
Output is correct |
32 |
Correct |
7 ms |
4204 KB |
Output is correct |
33 |
Correct |
9 ms |
4204 KB |
Output is correct |
34 |
Correct |
7 ms |
4204 KB |
Output is correct |
35 |
Correct |
8 ms |
4204 KB |
Output is correct |
36 |
Correct |
9 ms |
4204 KB |
Output is correct |
37 |
Correct |
6 ms |
4204 KB |
Output is correct |
38 |
Correct |
7 ms |
4204 KB |
Output is correct |
39 |
Correct |
9 ms |
4204 KB |
Output is correct |
40 |
Correct |
7 ms |
4204 KB |
Output is correct |
41 |
Correct |
8 ms |
4220 KB |
Output is correct |
42 |
Correct |
8 ms |
4220 KB |
Output is correct |
43 |
Correct |
7 ms |
4220 KB |
Output is correct |
44 |
Correct |
7 ms |
4220 KB |
Output is correct |
45 |
Correct |
10 ms |
4220 KB |
Output is correct |
46 |
Correct |
18 ms |
4476 KB |
Output is correct |
47 |
Correct |
21 ms |
4608 KB |
Output is correct |
48 |
Correct |
25 ms |
4616 KB |
Output is correct |
49 |
Correct |
20 ms |
4764 KB |
Output is correct |
50 |
Correct |
18 ms |
4860 KB |
Output is correct |
51 |
Correct |
19 ms |
4860 KB |
Output is correct |
52 |
Correct |
16 ms |
4860 KB |
Output is correct |
53 |
Correct |
16 ms |
4860 KB |
Output is correct |
54 |
Correct |
16 ms |
4860 KB |
Output is correct |
55 |
Correct |
16 ms |
4860 KB |
Output is correct |
56 |
Correct |
18 ms |
4860 KB |
Output is correct |
57 |
Correct |
22 ms |
4860 KB |
Output is correct |
58 |
Correct |
26 ms |
4860 KB |
Output is correct |
59 |
Correct |
29 ms |
4860 KB |
Output is correct |
60 |
Correct |
34 ms |
4860 KB |
Output is correct |
61 |
Correct |
19 ms |
4860 KB |
Output is correct |
62 |
Correct |
21 ms |
4860 KB |
Output is correct |
63 |
Correct |
20 ms |
4860 KB |
Output is correct |
64 |
Correct |
15 ms |
4860 KB |
Output is correct |
65 |
Correct |
16 ms |
4860 KB |
Output is correct |
66 |
Correct |
17 ms |
4860 KB |
Output is correct |
67 |
Correct |
16 ms |
4860 KB |
Output is correct |
68 |
Correct |
18 ms |
4860 KB |
Output is correct |
69 |
Correct |
17 ms |
4860 KB |
Output is correct |
70 |
Correct |
19 ms |
4860 KB |
Output is correct |
71 |
Correct |
18 ms |
4860 KB |
Output is correct |
72 |
Correct |
22 ms |
4860 KB |
Output is correct |
73 |
Correct |
22 ms |
4860 KB |
Output is correct |
74 |
Correct |
14 ms |
4860 KB |
Output is correct |
75 |
Correct |
17 ms |
4860 KB |
Output is correct |
76 |
Correct |
18 ms |
4860 KB |
Output is correct |
77 |
Correct |
22 ms |
4860 KB |
Output is correct |
78 |
Correct |
13 ms |
4860 KB |
Output is correct |
79 |
Correct |
13 ms |
4860 KB |
Output is correct |
80 |
Correct |
17 ms |
4860 KB |
Output is correct |
81 |
Correct |
17 ms |
4860 KB |
Output is correct |
82 |
Correct |
23 ms |
4860 KB |
Output is correct |
83 |
Correct |
25 ms |
4860 KB |
Output is correct |
84 |
Correct |
17 ms |
4860 KB |
Output is correct |
85 |
Correct |
15 ms |
4860 KB |
Output is correct |
86 |
Correct |
17 ms |
4860 KB |
Output is correct |
87 |
Correct |
46 ms |
5308 KB |
Output is correct |
88 |
Correct |
150 ms |
7356 KB |
Output is correct |
89 |
Correct |
667 ms |
14804 KB |
Output is correct |
90 |
Correct |
731 ms |
14908 KB |
Output is correct |
91 |
Correct |
641 ms |
14908 KB |
Output is correct |
92 |
Correct |
502 ms |
19432 KB |
Output is correct |
93 |
Correct |
451 ms |
19460 KB |
Output is correct |
94 |
Correct |
484 ms |
19508 KB |
Output is correct |
95 |
Correct |
407 ms |
19508 KB |
Output is correct |
96 |
Correct |
461 ms |
19508 KB |
Output is correct |
97 |
Correct |
558 ms |
19508 KB |
Output is correct |
98 |
Correct |
539 ms |
19508 KB |
Output is correct |
99 |
Correct |
461 ms |
19508 KB |
Output is correct |
100 |
Correct |
912 ms |
19508 KB |
Output is correct |
101 |
Correct |
923 ms |
19508 KB |
Output is correct |
102 |
Correct |
818 ms |
19508 KB |
Output is correct |
103 |
Correct |
889 ms |
20860 KB |
Output is correct |
104 |
Correct |
405 ms |
25532 KB |
Output is correct |
105 |
Correct |
451 ms |
27692 KB |
Output is correct |
106 |
Correct |
591 ms |
29820 KB |
Output is correct |
107 |
Correct |
403 ms |
29820 KB |
Output is correct |
108 |
Correct |
424 ms |
30192 KB |
Output is correct |
109 |
Correct |
562 ms |
32140 KB |
Output is correct |
110 |
Correct |
255 ms |
36656 KB |
Output is correct |
111 |
Correct |
418 ms |
38432 KB |
Output is correct |
112 |
Correct |
341 ms |
39832 KB |
Output is correct |
113 |
Correct |
246 ms |
40396 KB |
Output is correct |
114 |
Correct |
750 ms |
40396 KB |
Output is correct |
115 |
Correct |
646 ms |
41004 KB |
Output is correct |
116 |
Correct |
379 ms |
45208 KB |
Output is correct |
117 |
Correct |
480 ms |
46020 KB |
Output is correct |
118 |
Correct |
507 ms |
47588 KB |
Output is correct |
119 |
Correct |
463 ms |
49156 KB |
Output is correct |
120 |
Correct |
310 ms |
50936 KB |
Output is correct |
121 |
Correct |
403 ms |
52124 KB |
Output is correct |
122 |
Correct |
291 ms |
53500 KB |
Output is correct |
123 |
Correct |
496 ms |
56052 KB |
Output is correct |
124 |
Correct |
491 ms |
57620 KB |
Output is correct |
125 |
Correct |
510 ms |
59204 KB |
Output is correct |
126 |
Correct |
359 ms |
61020 KB |
Output is correct |
127 |
Correct |
314 ms |
62172 KB |
Output is correct |
128 |
Correct |
429 ms |
63676 KB |
Output is correct |