#include<bits/stdc++.h>
#define printv(a, b) { \
for(auto pv : a) b << pv << " "; \
b << "\n"; \
}
#define eb emplace_back
#define pob pop_back()
#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
#define lc (2 * id + 1)
#define rc (2 * id + 2)
int n;
vector<int> c, tme;
vector<vector<int>> g;
vector<int> pr;
vector<int> in, out, eu;
int ts = 0;
void init(){
g.resize(n + 1);
pr.resize(n + 1);
c.resize(n + 1);
tme.resize(n + 1);
in.resize(n + 1);
out.resize(n + 1);
eu.resize(n + 1);
}
struct SegmentTree{
vector<int> st;
int argmax(int a, int b){
if(tme[a] >= tme[b]) return a;
else return b;
}
void pull(int id){
st[id] = argmax(st[lc], st[rc]);
}
void build(int L, int R, int id){
if(L == R){
st[id] = L;
return;
}
int M = (L + R) / 2;
build(L, M, lc);
build(M + 1, R, rc);
pull(id);
}
void init(){
st.resize(4 * n);
}
void upd(int x, int L = 1, int R = n, int id = 0){
if(L == R) return;
int M = (L + R) / 2;
if(x <= M) upd(x, L, M, lc);
else upd(x, M + 1, R, rc);
pull(id);
}
int query(int l, int r, int L = 1, int R = n, int id = 0){
if(l <= L && R <= r) return st[id];
int M = (L + R) / 2;
if(r <= M) return query(l, r, L, M, lc);
else if(l > M) return query(l, r, M + 1, R, rc);
else return argmax(query(l, r, L, M, lc), query(l, r, M + 1, R, rc));
}
};
int lowbit(int x){
return x & -x;
}
struct BIT{
vector<ll> bit;
void init(){
bit.resize(n + 1);
}
void modify(int x, ll v){
for(; x < bit.size(); x += lowbit(x)) bit[x] += v;
}
ll query(int x){
ll ans = 0;
for(; x > 0; x -= lowbit(x)) ans += bit[x];
return ans;
}
};
void dfs(int now){
in[now] = ++ts;
eu[ts] = now;
for(int i : g[now]) dfs(i);
out[now] = ts;
}
SegmentTree st;
int getv(int v){
int pos = st.query(in[v], out[v]);
//cerr << "getv " << v << " " << pos << "\n";
return c[eu[pos]];
}
ll calc(int v){
//cerr << "calc " << v << "\n";
vector<int> path;
v = pr[v];
while(true){
path.eb(v);
if(v == 1) break;
v = pr[v];
}
//printv(path, cerr);
BIT bit;
bit.init();
ll ans = 0;
reverse(iter(path));
for(int i : path){
int cv = getv(i);
ans += bit.query(n) - bit.query(cv);
//cerr << "cv " << i << " " << cv << "\n";
bit.modify(cv, 1);
}
//cerr << "ok " << ans << "\n";
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cerr.tie(0);
cin >> n;
init();
for(int i = 1; i <= n; i++){
cin >> c[i];
}
vector<int> tmp = c;
lsort(tmp);
uni(tmp);
for(int i = 1; i <= n; i++){
c[i] = lower_bound(iter(tmp), c[i]) - tmp.begin();
}
//printv(c, cerr);
vector<int> qry(n + 1);
for(int i = 2; i <= n; i++){
int u, v;
cin >> u >> v;
g[u].eb(v);
pr[v] = u;
qry[i] = v;
}
dfs(1);
st.init();
st.build(1, n, 0);
for(int i = 2; i <= n; i++){
int now = qry[i];
cout << calc(now) << "\n";
tme[in[now]] = i;
st.upd(in[now]);
}
return 0;
}
Compilation message
construction.cpp: In member function 'void BIT::modify(int, ll)':
construction.cpp:93:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(; x < bit.size(); x += lowbit(x)) bit[x] += v;
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
10 ms |
332 KB |
Output is correct |
9 |
Correct |
10 ms |
396 KB |
Output is correct |
10 |
Correct |
10 ms |
332 KB |
Output is correct |
11 |
Correct |
7 ms |
324 KB |
Output is correct |
12 |
Correct |
7 ms |
372 KB |
Output is correct |
13 |
Correct |
7 ms |
332 KB |
Output is correct |
14 |
Correct |
7 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
8 ms |
372 KB |
Output is correct |
20 |
Correct |
8 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
308 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
368 KB |
Output is correct |
28 |
Correct |
7 ms |
372 KB |
Output is correct |
29 |
Correct |
7 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
7 ms |
332 KB |
Output is correct |
33 |
Correct |
5 ms |
332 KB |
Output is correct |
34 |
Correct |
3 ms |
332 KB |
Output is correct |
35 |
Correct |
2 ms |
312 KB |
Output is correct |
36 |
Correct |
4 ms |
332 KB |
Output is correct |
37 |
Correct |
4 ms |
320 KB |
Output is correct |
38 |
Correct |
3 ms |
332 KB |
Output is correct |
39 |
Correct |
4 ms |
332 KB |
Output is correct |
40 |
Correct |
3 ms |
312 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
4 ms |
332 KB |
Output is correct |
43 |
Correct |
3 ms |
312 KB |
Output is correct |
44 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
10 ms |
332 KB |
Output is correct |
9 |
Correct |
10 ms |
396 KB |
Output is correct |
10 |
Correct |
10 ms |
332 KB |
Output is correct |
11 |
Correct |
7 ms |
324 KB |
Output is correct |
12 |
Correct |
7 ms |
372 KB |
Output is correct |
13 |
Correct |
7 ms |
332 KB |
Output is correct |
14 |
Correct |
7 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
8 ms |
372 KB |
Output is correct |
20 |
Correct |
8 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
308 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
368 KB |
Output is correct |
28 |
Correct |
7 ms |
372 KB |
Output is correct |
29 |
Correct |
7 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
7 ms |
332 KB |
Output is correct |
33 |
Correct |
5 ms |
332 KB |
Output is correct |
34 |
Correct |
3 ms |
332 KB |
Output is correct |
35 |
Correct |
2 ms |
312 KB |
Output is correct |
36 |
Correct |
4 ms |
332 KB |
Output is correct |
37 |
Correct |
4 ms |
320 KB |
Output is correct |
38 |
Correct |
3 ms |
332 KB |
Output is correct |
39 |
Correct |
4 ms |
332 KB |
Output is correct |
40 |
Correct |
3 ms |
312 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
4 ms |
332 KB |
Output is correct |
43 |
Correct |
3 ms |
312 KB |
Output is correct |
44 |
Correct |
2 ms |
332 KB |
Output is correct |
45 |
Correct |
2 ms |
332 KB |
Output is correct |
46 |
Correct |
11 ms |
756 KB |
Output is correct |
47 |
Correct |
11 ms |
716 KB |
Output is correct |
48 |
Correct |
10 ms |
716 KB |
Output is correct |
49 |
Correct |
665 ms |
960 KB |
Output is correct |
50 |
Correct |
689 ms |
1072 KB |
Output is correct |
51 |
Correct |
706 ms |
968 KB |
Output is correct |
52 |
Correct |
523 ms |
716 KB |
Output is correct |
53 |
Correct |
588 ms |
824 KB |
Output is correct |
54 |
Correct |
536 ms |
716 KB |
Output is correct |
55 |
Correct |
536 ms |
816 KB |
Output is correct |
56 |
Correct |
590 ms |
824 KB |
Output is correct |
57 |
Correct |
11 ms |
716 KB |
Output is correct |
58 |
Correct |
11 ms |
716 KB |
Output is correct |
59 |
Correct |
12 ms |
700 KB |
Output is correct |
60 |
Correct |
12 ms |
716 KB |
Output is correct |
61 |
Correct |
548 ms |
828 KB |
Output is correct |
62 |
Correct |
628 ms |
812 KB |
Output is correct |
63 |
Correct |
565 ms |
940 KB |
Output is correct |
64 |
Correct |
10 ms |
716 KB |
Output is correct |
65 |
Correct |
10 ms |
708 KB |
Output is correct |
66 |
Correct |
11 ms |
744 KB |
Output is correct |
67 |
Correct |
11 ms |
716 KB |
Output is correct |
68 |
Correct |
641 ms |
932 KB |
Output is correct |
69 |
Correct |
542 ms |
964 KB |
Output is correct |
70 |
Correct |
549 ms |
792 KB |
Output is correct |
71 |
Correct |
542 ms |
800 KB |
Output is correct |
72 |
Correct |
11 ms |
716 KB |
Output is correct |
73 |
Correct |
13 ms |
740 KB |
Output is correct |
74 |
Correct |
562 ms |
796 KB |
Output is correct |
75 |
Correct |
290 ms |
836 KB |
Output is correct |
76 |
Correct |
164 ms |
708 KB |
Output is correct |
77 |
Correct |
121 ms |
716 KB |
Output is correct |
78 |
Correct |
256 ms |
880 KB |
Output is correct |
79 |
Correct |
174 ms |
836 KB |
Output is correct |
80 |
Correct |
121 ms |
748 KB |
Output is correct |
81 |
Correct |
299 ms |
836 KB |
Output is correct |
82 |
Correct |
175 ms |
952 KB |
Output is correct |
83 |
Correct |
128 ms |
752 KB |
Output is correct |
84 |
Correct |
261 ms |
656 KB |
Output is correct |
85 |
Correct |
180 ms |
752 KB |
Output is correct |
86 |
Correct |
139 ms |
736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
10 ms |
332 KB |
Output is correct |
9 |
Correct |
10 ms |
396 KB |
Output is correct |
10 |
Correct |
10 ms |
332 KB |
Output is correct |
11 |
Correct |
7 ms |
324 KB |
Output is correct |
12 |
Correct |
7 ms |
372 KB |
Output is correct |
13 |
Correct |
7 ms |
332 KB |
Output is correct |
14 |
Correct |
7 ms |
316 KB |
Output is correct |
15 |
Correct |
1 ms |
308 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
324 KB |
Output is correct |
19 |
Correct |
8 ms |
372 KB |
Output is correct |
20 |
Correct |
8 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
376 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
308 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
9 ms |
344 KB |
Output is correct |
27 |
Correct |
7 ms |
368 KB |
Output is correct |
28 |
Correct |
7 ms |
372 KB |
Output is correct |
29 |
Correct |
7 ms |
308 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
7 ms |
332 KB |
Output is correct |
33 |
Correct |
5 ms |
332 KB |
Output is correct |
34 |
Correct |
3 ms |
332 KB |
Output is correct |
35 |
Correct |
2 ms |
312 KB |
Output is correct |
36 |
Correct |
4 ms |
332 KB |
Output is correct |
37 |
Correct |
4 ms |
320 KB |
Output is correct |
38 |
Correct |
3 ms |
332 KB |
Output is correct |
39 |
Correct |
4 ms |
332 KB |
Output is correct |
40 |
Correct |
3 ms |
312 KB |
Output is correct |
41 |
Correct |
2 ms |
332 KB |
Output is correct |
42 |
Correct |
4 ms |
332 KB |
Output is correct |
43 |
Correct |
3 ms |
312 KB |
Output is correct |
44 |
Correct |
2 ms |
332 KB |
Output is correct |
45 |
Correct |
2 ms |
332 KB |
Output is correct |
46 |
Correct |
11 ms |
756 KB |
Output is correct |
47 |
Correct |
11 ms |
716 KB |
Output is correct |
48 |
Correct |
10 ms |
716 KB |
Output is correct |
49 |
Correct |
665 ms |
960 KB |
Output is correct |
50 |
Correct |
689 ms |
1072 KB |
Output is correct |
51 |
Correct |
706 ms |
968 KB |
Output is correct |
52 |
Correct |
523 ms |
716 KB |
Output is correct |
53 |
Correct |
588 ms |
824 KB |
Output is correct |
54 |
Correct |
536 ms |
716 KB |
Output is correct |
55 |
Correct |
536 ms |
816 KB |
Output is correct |
56 |
Correct |
590 ms |
824 KB |
Output is correct |
57 |
Correct |
11 ms |
716 KB |
Output is correct |
58 |
Correct |
11 ms |
716 KB |
Output is correct |
59 |
Correct |
12 ms |
700 KB |
Output is correct |
60 |
Correct |
12 ms |
716 KB |
Output is correct |
61 |
Correct |
548 ms |
828 KB |
Output is correct |
62 |
Correct |
628 ms |
812 KB |
Output is correct |
63 |
Correct |
565 ms |
940 KB |
Output is correct |
64 |
Correct |
10 ms |
716 KB |
Output is correct |
65 |
Correct |
10 ms |
708 KB |
Output is correct |
66 |
Correct |
11 ms |
744 KB |
Output is correct |
67 |
Correct |
11 ms |
716 KB |
Output is correct |
68 |
Correct |
641 ms |
932 KB |
Output is correct |
69 |
Correct |
542 ms |
964 KB |
Output is correct |
70 |
Correct |
549 ms |
792 KB |
Output is correct |
71 |
Correct |
542 ms |
800 KB |
Output is correct |
72 |
Correct |
11 ms |
716 KB |
Output is correct |
73 |
Correct |
13 ms |
740 KB |
Output is correct |
74 |
Correct |
562 ms |
796 KB |
Output is correct |
75 |
Correct |
290 ms |
836 KB |
Output is correct |
76 |
Correct |
164 ms |
708 KB |
Output is correct |
77 |
Correct |
121 ms |
716 KB |
Output is correct |
78 |
Correct |
256 ms |
880 KB |
Output is correct |
79 |
Correct |
174 ms |
836 KB |
Output is correct |
80 |
Correct |
121 ms |
748 KB |
Output is correct |
81 |
Correct |
299 ms |
836 KB |
Output is correct |
82 |
Correct |
175 ms |
952 KB |
Output is correct |
83 |
Correct |
128 ms |
752 KB |
Output is correct |
84 |
Correct |
261 ms |
656 KB |
Output is correct |
85 |
Correct |
180 ms |
752 KB |
Output is correct |
86 |
Correct |
139 ms |
736 KB |
Output is correct |
87 |
Correct |
40 ms |
1468 KB |
Output is correct |
88 |
Correct |
245 ms |
3772 KB |
Output is correct |
89 |
Execution timed out |
2015 ms |
12184 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |