#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 |
3832 KB |
Output is correct |
2 |
Correct |
7 ms |
3832 KB |
Output is correct |
3 |
Correct |
8 ms |
4016 KB |
Output is correct |
4 |
Correct |
8 ms |
4016 KB |
Output is correct |
5 |
Correct |
8 ms |
4068 KB |
Output is correct |
6 |
Correct |
8 ms |
4144 KB |
Output is correct |
7 |
Correct |
8 ms |
4164 KB |
Output is correct |
8 |
Correct |
7 ms |
4176 KB |
Output is correct |
9 |
Correct |
7 ms |
4176 KB |
Output is correct |
10 |
Correct |
9 ms |
4176 KB |
Output is correct |
11 |
Correct |
7 ms |
4176 KB |
Output is correct |
12 |
Correct |
7 ms |
4176 KB |
Output is correct |
13 |
Correct |
7 ms |
4176 KB |
Output is correct |
14 |
Correct |
7 ms |
4176 KB |
Output is correct |
15 |
Correct |
8 ms |
4176 KB |
Output is correct |
16 |
Correct |
8 ms |
4176 KB |
Output is correct |
17 |
Correct |
11 ms |
4204 KB |
Output is correct |
18 |
Correct |
10 ms |
4204 KB |
Output is correct |
19 |
Correct |
8 ms |
4204 KB |
Output is correct |
20 |
Correct |
10 ms |
4204 KB |
Output is correct |
21 |
Correct |
10 ms |
4204 KB |
Output is correct |
22 |
Correct |
8 ms |
4204 KB |
Output is correct |
23 |
Correct |
9 ms |
4204 KB |
Output is correct |
24 |
Correct |
8 ms |
4204 KB |
Output is correct |
25 |
Correct |
8 ms |
4204 KB |
Output is correct |
26 |
Correct |
8 ms |
4204 KB |
Output is correct |
27 |
Correct |
7 ms |
4204 KB |
Output is correct |
28 |
Correct |
8 ms |
4204 KB |
Output is correct |
29 |
Correct |
7 ms |
4204 KB |
Output is correct |
30 |
Correct |
7 ms |
4204 KB |
Output is correct |
31 |
Correct |
4 ms |
4204 KB |
Output is correct |
32 |
Correct |
9 ms |
4204 KB |
Output is correct |
33 |
Correct |
7 ms |
4204 KB |
Output is correct |
34 |
Correct |
8 ms |
4256 KB |
Output is correct |
35 |
Correct |
7 ms |
4256 KB |
Output is correct |
36 |
Correct |
8 ms |
4256 KB |
Output is correct |
37 |
Correct |
7 ms |
4256 KB |
Output is correct |
38 |
Correct |
6 ms |
4256 KB |
Output is correct |
39 |
Correct |
8 ms |
4256 KB |
Output is correct |
40 |
Correct |
7 ms |
4256 KB |
Output is correct |
41 |
Correct |
9 ms |
4256 KB |
Output is correct |
42 |
Correct |
8 ms |
4256 KB |
Output is correct |
43 |
Correct |
7 ms |
4256 KB |
Output is correct |
44 |
Correct |
7 ms |
4256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3832 KB |
Output is correct |
2 |
Correct |
7 ms |
3832 KB |
Output is correct |
3 |
Correct |
8 ms |
4016 KB |
Output is correct |
4 |
Correct |
8 ms |
4016 KB |
Output is correct |
5 |
Correct |
8 ms |
4068 KB |
Output is correct |
6 |
Correct |
8 ms |
4144 KB |
Output is correct |
7 |
Correct |
8 ms |
4164 KB |
Output is correct |
8 |
Correct |
7 ms |
4176 KB |
Output is correct |
9 |
Correct |
7 ms |
4176 KB |
Output is correct |
10 |
Correct |
9 ms |
4176 KB |
Output is correct |
11 |
Correct |
7 ms |
4176 KB |
Output is correct |
12 |
Correct |
7 ms |
4176 KB |
Output is correct |
13 |
Correct |
7 ms |
4176 KB |
Output is correct |
14 |
Correct |
7 ms |
4176 KB |
Output is correct |
15 |
Correct |
8 ms |
4176 KB |
Output is correct |
16 |
Correct |
8 ms |
4176 KB |
Output is correct |
17 |
Correct |
11 ms |
4204 KB |
Output is correct |
18 |
Correct |
10 ms |
4204 KB |
Output is correct |
19 |
Correct |
8 ms |
4204 KB |
Output is correct |
20 |
Correct |
10 ms |
4204 KB |
Output is correct |
21 |
Correct |
10 ms |
4204 KB |
Output is correct |
22 |
Correct |
8 ms |
4204 KB |
Output is correct |
23 |
Correct |
9 ms |
4204 KB |
Output is correct |
24 |
Correct |
8 ms |
4204 KB |
Output is correct |
25 |
Correct |
8 ms |
4204 KB |
Output is correct |
26 |
Correct |
8 ms |
4204 KB |
Output is correct |
27 |
Correct |
7 ms |
4204 KB |
Output is correct |
28 |
Correct |
8 ms |
4204 KB |
Output is correct |
29 |
Correct |
7 ms |
4204 KB |
Output is correct |
30 |
Correct |
7 ms |
4204 KB |
Output is correct |
31 |
Correct |
4 ms |
4204 KB |
Output is correct |
32 |
Correct |
9 ms |
4204 KB |
Output is correct |
33 |
Correct |
7 ms |
4204 KB |
Output is correct |
34 |
Correct |
8 ms |
4256 KB |
Output is correct |
35 |
Correct |
7 ms |
4256 KB |
Output is correct |
36 |
Correct |
8 ms |
4256 KB |
Output is correct |
37 |
Correct |
7 ms |
4256 KB |
Output is correct |
38 |
Correct |
6 ms |
4256 KB |
Output is correct |
39 |
Correct |
8 ms |
4256 KB |
Output is correct |
40 |
Correct |
7 ms |
4256 KB |
Output is correct |
41 |
Correct |
9 ms |
4256 KB |
Output is correct |
42 |
Correct |
8 ms |
4256 KB |
Output is correct |
43 |
Correct |
7 ms |
4256 KB |
Output is correct |
44 |
Correct |
7 ms |
4256 KB |
Output is correct |
45 |
Correct |
12 ms |
4256 KB |
Output is correct |
46 |
Correct |
20 ms |
4476 KB |
Output is correct |
47 |
Correct |
21 ms |
4604 KB |
Output is correct |
48 |
Correct |
19 ms |
4620 KB |
Output is correct |
49 |
Correct |
18 ms |
4772 KB |
Output is correct |
50 |
Correct |
18 ms |
4772 KB |
Output is correct |
51 |
Correct |
20 ms |
4984 KB |
Output is correct |
52 |
Correct |
14 ms |
4988 KB |
Output is correct |
53 |
Correct |
22 ms |
5092 KB |
Output is correct |
54 |
Correct |
19 ms |
5092 KB |
Output is correct |
55 |
Correct |
16 ms |
5220 KB |
Output is correct |
56 |
Correct |
18 ms |
5296 KB |
Output is correct |
57 |
Correct |
20 ms |
5296 KB |
Output is correct |
58 |
Correct |
22 ms |
5300 KB |
Output is correct |
59 |
Correct |
26 ms |
5428 KB |
Output is correct |
60 |
Correct |
25 ms |
5452 KB |
Output is correct |
61 |
Correct |
24 ms |
5656 KB |
Output is correct |
62 |
Correct |
16 ms |
5748 KB |
Output is correct |
63 |
Correct |
16 ms |
5808 KB |
Output is correct |
64 |
Correct |
20 ms |
5808 KB |
Output is correct |
65 |
Correct |
18 ms |
5808 KB |
Output is correct |
66 |
Correct |
22 ms |
5832 KB |
Output is correct |
67 |
Correct |
20 ms |
6016 KB |
Output is correct |
68 |
Correct |
19 ms |
6092 KB |
Output is correct |
69 |
Correct |
20 ms |
6092 KB |
Output is correct |
70 |
Correct |
14 ms |
6092 KB |
Output is correct |
71 |
Correct |
14 ms |
6116 KB |
Output is correct |
72 |
Correct |
26 ms |
6164 KB |
Output is correct |
73 |
Correct |
18 ms |
6164 KB |
Output is correct |
74 |
Correct |
14 ms |
6268 KB |
Output is correct |
75 |
Correct |
16 ms |
6348 KB |
Output is correct |
76 |
Correct |
16 ms |
6412 KB |
Output is correct |
77 |
Correct |
21 ms |
6476 KB |
Output is correct |
78 |
Correct |
14 ms |
6540 KB |
Output is correct |
79 |
Correct |
21 ms |
6588 KB |
Output is correct |
80 |
Correct |
19 ms |
6636 KB |
Output is correct |
81 |
Correct |
22 ms |
6684 KB |
Output is correct |
82 |
Correct |
16 ms |
6748 KB |
Output is correct |
83 |
Correct |
16 ms |
6812 KB |
Output is correct |
84 |
Correct |
20 ms |
6876 KB |
Output is correct |
85 |
Correct |
15 ms |
6924 KB |
Output is correct |
86 |
Correct |
15 ms |
6972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3832 KB |
Output is correct |
2 |
Correct |
7 ms |
3832 KB |
Output is correct |
3 |
Correct |
8 ms |
4016 KB |
Output is correct |
4 |
Correct |
8 ms |
4016 KB |
Output is correct |
5 |
Correct |
8 ms |
4068 KB |
Output is correct |
6 |
Correct |
8 ms |
4144 KB |
Output is correct |
7 |
Correct |
8 ms |
4164 KB |
Output is correct |
8 |
Correct |
7 ms |
4176 KB |
Output is correct |
9 |
Correct |
7 ms |
4176 KB |
Output is correct |
10 |
Correct |
9 ms |
4176 KB |
Output is correct |
11 |
Correct |
7 ms |
4176 KB |
Output is correct |
12 |
Correct |
7 ms |
4176 KB |
Output is correct |
13 |
Correct |
7 ms |
4176 KB |
Output is correct |
14 |
Correct |
7 ms |
4176 KB |
Output is correct |
15 |
Correct |
8 ms |
4176 KB |
Output is correct |
16 |
Correct |
8 ms |
4176 KB |
Output is correct |
17 |
Correct |
11 ms |
4204 KB |
Output is correct |
18 |
Correct |
10 ms |
4204 KB |
Output is correct |
19 |
Correct |
8 ms |
4204 KB |
Output is correct |
20 |
Correct |
10 ms |
4204 KB |
Output is correct |
21 |
Correct |
10 ms |
4204 KB |
Output is correct |
22 |
Correct |
8 ms |
4204 KB |
Output is correct |
23 |
Correct |
9 ms |
4204 KB |
Output is correct |
24 |
Correct |
8 ms |
4204 KB |
Output is correct |
25 |
Correct |
8 ms |
4204 KB |
Output is correct |
26 |
Correct |
8 ms |
4204 KB |
Output is correct |
27 |
Correct |
7 ms |
4204 KB |
Output is correct |
28 |
Correct |
8 ms |
4204 KB |
Output is correct |
29 |
Correct |
7 ms |
4204 KB |
Output is correct |
30 |
Correct |
7 ms |
4204 KB |
Output is correct |
31 |
Correct |
4 ms |
4204 KB |
Output is correct |
32 |
Correct |
9 ms |
4204 KB |
Output is correct |
33 |
Correct |
7 ms |
4204 KB |
Output is correct |
34 |
Correct |
8 ms |
4256 KB |
Output is correct |
35 |
Correct |
7 ms |
4256 KB |
Output is correct |
36 |
Correct |
8 ms |
4256 KB |
Output is correct |
37 |
Correct |
7 ms |
4256 KB |
Output is correct |
38 |
Correct |
6 ms |
4256 KB |
Output is correct |
39 |
Correct |
8 ms |
4256 KB |
Output is correct |
40 |
Correct |
7 ms |
4256 KB |
Output is correct |
41 |
Correct |
9 ms |
4256 KB |
Output is correct |
42 |
Correct |
8 ms |
4256 KB |
Output is correct |
43 |
Correct |
7 ms |
4256 KB |
Output is correct |
44 |
Correct |
7 ms |
4256 KB |
Output is correct |
45 |
Correct |
12 ms |
4256 KB |
Output is correct |
46 |
Correct |
20 ms |
4476 KB |
Output is correct |
47 |
Correct |
21 ms |
4604 KB |
Output is correct |
48 |
Correct |
19 ms |
4620 KB |
Output is correct |
49 |
Correct |
18 ms |
4772 KB |
Output is correct |
50 |
Correct |
18 ms |
4772 KB |
Output is correct |
51 |
Correct |
20 ms |
4984 KB |
Output is correct |
52 |
Correct |
14 ms |
4988 KB |
Output is correct |
53 |
Correct |
22 ms |
5092 KB |
Output is correct |
54 |
Correct |
19 ms |
5092 KB |
Output is correct |
55 |
Correct |
16 ms |
5220 KB |
Output is correct |
56 |
Correct |
18 ms |
5296 KB |
Output is correct |
57 |
Correct |
20 ms |
5296 KB |
Output is correct |
58 |
Correct |
22 ms |
5300 KB |
Output is correct |
59 |
Correct |
26 ms |
5428 KB |
Output is correct |
60 |
Correct |
25 ms |
5452 KB |
Output is correct |
61 |
Correct |
24 ms |
5656 KB |
Output is correct |
62 |
Correct |
16 ms |
5748 KB |
Output is correct |
63 |
Correct |
16 ms |
5808 KB |
Output is correct |
64 |
Correct |
20 ms |
5808 KB |
Output is correct |
65 |
Correct |
18 ms |
5808 KB |
Output is correct |
66 |
Correct |
22 ms |
5832 KB |
Output is correct |
67 |
Correct |
20 ms |
6016 KB |
Output is correct |
68 |
Correct |
19 ms |
6092 KB |
Output is correct |
69 |
Correct |
20 ms |
6092 KB |
Output is correct |
70 |
Correct |
14 ms |
6092 KB |
Output is correct |
71 |
Correct |
14 ms |
6116 KB |
Output is correct |
72 |
Correct |
26 ms |
6164 KB |
Output is correct |
73 |
Correct |
18 ms |
6164 KB |
Output is correct |
74 |
Correct |
14 ms |
6268 KB |
Output is correct |
75 |
Correct |
16 ms |
6348 KB |
Output is correct |
76 |
Correct |
16 ms |
6412 KB |
Output is correct |
77 |
Correct |
21 ms |
6476 KB |
Output is correct |
78 |
Correct |
14 ms |
6540 KB |
Output is correct |
79 |
Correct |
21 ms |
6588 KB |
Output is correct |
80 |
Correct |
19 ms |
6636 KB |
Output is correct |
81 |
Correct |
22 ms |
6684 KB |
Output is correct |
82 |
Correct |
16 ms |
6748 KB |
Output is correct |
83 |
Correct |
16 ms |
6812 KB |
Output is correct |
84 |
Correct |
20 ms |
6876 KB |
Output is correct |
85 |
Correct |
15 ms |
6924 KB |
Output is correct |
86 |
Correct |
15 ms |
6972 KB |
Output is correct |
87 |
Correct |
52 ms |
7784 KB |
Output is correct |
88 |
Correct |
155 ms |
10540 KB |
Output is correct |
89 |
Correct |
660 ms |
20160 KB |
Output is correct |
90 |
Correct |
671 ms |
22204 KB |
Output is correct |
91 |
Correct |
684 ms |
24260 KB |
Output is correct |
92 |
Correct |
436 ms |
30960 KB |
Output is correct |
93 |
Correct |
415 ms |
33084 KB |
Output is correct |
94 |
Correct |
445 ms |
35304 KB |
Output is correct |
95 |
Correct |
423 ms |
35616 KB |
Output is correct |
96 |
Correct |
456 ms |
37832 KB |
Output is correct |
97 |
Correct |
451 ms |
39856 KB |
Output is correct |
98 |
Correct |
547 ms |
41968 KB |
Output is correct |
99 |
Correct |
444 ms |
43080 KB |
Output is correct |
100 |
Correct |
829 ms |
43080 KB |
Output is correct |
101 |
Correct |
1002 ms |
44368 KB |
Output is correct |
102 |
Correct |
1029 ms |
46488 KB |
Output is correct |
103 |
Correct |
937 ms |
48536 KB |
Output is correct |
104 |
Correct |
411 ms |
53212 KB |
Output is correct |
105 |
Correct |
488 ms |
55416 KB |
Output is correct |
106 |
Correct |
451 ms |
57512 KB |
Output is correct |
107 |
Correct |
506 ms |
57512 KB |
Output is correct |
108 |
Correct |
492 ms |
57992 KB |
Output is correct |
109 |
Correct |
492 ms |
59852 KB |
Output is correct |
110 |
Correct |
238 ms |
64524 KB |
Output is correct |
111 |
Correct |
386 ms |
66412 KB |
Output is correct |
112 |
Correct |
299 ms |
67644 KB |
Output is correct |
113 |
Correct |
337 ms |
68244 KB |
Output is correct |
114 |
Correct |
828 ms |
68244 KB |
Output is correct |
115 |
Correct |
600 ms |
68700 KB |
Output is correct |
116 |
Correct |
337 ms |
73004 KB |
Output is correct |
117 |
Correct |
475 ms |
73764 KB |
Output is correct |
118 |
Correct |
527 ms |
75372 KB |
Output is correct |
119 |
Correct |
474 ms |
76872 KB |
Output is correct |
120 |
Correct |
319 ms |
78608 KB |
Output is correct |
121 |
Correct |
252 ms |
79880 KB |
Output is correct |
122 |
Correct |
262 ms |
81192 KB |
Output is correct |
123 |
Correct |
442 ms |
83732 KB |
Output is correct |
124 |
Correct |
454 ms |
85572 KB |
Output is correct |
125 |
Correct |
458 ms |
86940 KB |
Output is correct |
126 |
Correct |
315 ms |
88920 KB |
Output is correct |
127 |
Correct |
336 ms |
89888 KB |
Output is correct |
128 |
Correct |
288 ms |
91272 KB |
Output is correct |