#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fi first
#define se second
#define ll long long
using namespace std ;
using namespace __gnu_pbds ;
const ll N = 1e5 ;
tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag, tree_order_statistics_node_update> s;
ll ans, c[N + 1], a[N + 1], b[N + 1], dp[N + 1], p[N + 1] ;
vector<ll> now ;
vector<pair<ll, ll>> v[N + 1] ;
void dfs(ll city, ll last, ll abu)
{
dp[abu] = ans ;
s.insert({c[city], city}) ;
ans += s.order_of_key({c[city], 0}) ;
for(auto i : v[city])
{
if(i.fi == last)
continue ;
dp[i.se] ;
dfs(i.fi, city, i.se) ;
}
s.erase({c[city], city}) ;
ans -= s.order_of_key({c[city], 0}) ;
}
signed main()
{
ios_base::sync_with_stdio( 0 ) ;
cin.tie( 0 ) ;
cout.tie( 0 ) ;
ll n ;
cin >> n ;
for(ll i = 1 ; i <= n ; i++)
cin >> c[i] ;
for(ll i = 1 ; i < n ; i++)
{
cin >> a[i] >> b[i] ;
p[b[i]] = a[i] ;
v[a[i]].push_back({b[i], i}) ;
v[b[i]].push_back({a[i], i}) ;
}
if(n <= 4000)
{
for(ll i = 1 ; i < n ; i++)
{
ll num = a[i] ;
ans = 0 ;
s.clear() ;
while(num)
{
ans += s.order_of_key({c[num], 0}) ;
s.insert({c[num], num}) ;
c[num] = c[b[i]] ;
num = p[num] ;
}
cout << ans << '\n' ;
}
return 0 ;
}
dfs(1, 0, 0) ;
for(ll i = 1 ; i < n ; i++)
cout << dp[i] << '\n' ;
return 0 ;
}
//s.find_by_order(k); k-й по величине элемент в множестве
//s.order_of_key(x); сколько элементов в множестве меньше
Compilation message
construction.cpp: In function 'void dfs(long long int, long long int, long long int)':
construction.cpp:22:16: warning: statement has no effect [-Wunused-value]
22 | dp[i.se] ;
| ~~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
18 ms |
2644 KB |
Output is correct |
9 |
Correct |
18 ms |
2756 KB |
Output is correct |
10 |
Correct |
18 ms |
2760 KB |
Output is correct |
11 |
Correct |
9 ms |
2768 KB |
Output is correct |
12 |
Correct |
9 ms |
2736 KB |
Output is correct |
13 |
Correct |
9 ms |
2644 KB |
Output is correct |
14 |
Correct |
9 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
10 ms |
2760 KB |
Output is correct |
20 |
Correct |
10 ms |
2644 KB |
Output is correct |
21 |
Correct |
9 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
18 ms |
2764 KB |
Output is correct |
27 |
Correct |
9 ms |
2764 KB |
Output is correct |
28 |
Correct |
9 ms |
2768 KB |
Output is correct |
29 |
Correct |
9 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
9 ms |
2764 KB |
Output is correct |
33 |
Correct |
5 ms |
2644 KB |
Output is correct |
34 |
Correct |
4 ms |
2644 KB |
Output is correct |
35 |
Correct |
3 ms |
2644 KB |
Output is correct |
36 |
Correct |
5 ms |
2644 KB |
Output is correct |
37 |
Correct |
4 ms |
2644 KB |
Output is correct |
38 |
Correct |
3 ms |
2644 KB |
Output is correct |
39 |
Correct |
5 ms |
2644 KB |
Output is correct |
40 |
Correct |
4 ms |
2644 KB |
Output is correct |
41 |
Correct |
4 ms |
2644 KB |
Output is correct |
42 |
Correct |
5 ms |
2644 KB |
Output is correct |
43 |
Correct |
4 ms |
2752 KB |
Output is correct |
44 |
Correct |
3 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
18 ms |
2644 KB |
Output is correct |
9 |
Correct |
18 ms |
2756 KB |
Output is correct |
10 |
Correct |
18 ms |
2760 KB |
Output is correct |
11 |
Correct |
9 ms |
2768 KB |
Output is correct |
12 |
Correct |
9 ms |
2736 KB |
Output is correct |
13 |
Correct |
9 ms |
2644 KB |
Output is correct |
14 |
Correct |
9 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
10 ms |
2760 KB |
Output is correct |
20 |
Correct |
10 ms |
2644 KB |
Output is correct |
21 |
Correct |
9 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
18 ms |
2764 KB |
Output is correct |
27 |
Correct |
9 ms |
2764 KB |
Output is correct |
28 |
Correct |
9 ms |
2768 KB |
Output is correct |
29 |
Correct |
9 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
9 ms |
2764 KB |
Output is correct |
33 |
Correct |
5 ms |
2644 KB |
Output is correct |
34 |
Correct |
4 ms |
2644 KB |
Output is correct |
35 |
Correct |
3 ms |
2644 KB |
Output is correct |
36 |
Correct |
5 ms |
2644 KB |
Output is correct |
37 |
Correct |
4 ms |
2644 KB |
Output is correct |
38 |
Correct |
3 ms |
2644 KB |
Output is correct |
39 |
Correct |
5 ms |
2644 KB |
Output is correct |
40 |
Correct |
4 ms |
2644 KB |
Output is correct |
41 |
Correct |
4 ms |
2644 KB |
Output is correct |
42 |
Correct |
5 ms |
2644 KB |
Output is correct |
43 |
Correct |
4 ms |
2752 KB |
Output is correct |
44 |
Correct |
3 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2772 KB |
Output is correct |
46 |
Correct |
6 ms |
3036 KB |
Output is correct |
47 |
Correct |
5 ms |
3028 KB |
Output is correct |
48 |
Correct |
4 ms |
3028 KB |
Output is correct |
49 |
Correct |
1390 ms |
3268 KB |
Output is correct |
50 |
Correct |
1390 ms |
3304 KB |
Output is correct |
51 |
Correct |
1394 ms |
3244 KB |
Output is correct |
52 |
Correct |
658 ms |
3152 KB |
Output is correct |
53 |
Correct |
643 ms |
3300 KB |
Output is correct |
54 |
Correct |
664 ms |
3152 KB |
Output is correct |
55 |
Correct |
660 ms |
3208 KB |
Output is correct |
56 |
Correct |
661 ms |
3244 KB |
Output is correct |
57 |
Correct |
5 ms |
3028 KB |
Output is correct |
58 |
Correct |
6 ms |
3156 KB |
Output is correct |
59 |
Correct |
5 ms |
3028 KB |
Output is correct |
60 |
Correct |
5 ms |
3064 KB |
Output is correct |
61 |
Correct |
659 ms |
3180 KB |
Output is correct |
62 |
Correct |
658 ms |
3284 KB |
Output is correct |
63 |
Correct |
661 ms |
3176 KB |
Output is correct |
64 |
Correct |
5 ms |
3032 KB |
Output is correct |
65 |
Correct |
5 ms |
3028 KB |
Output is correct |
66 |
Correct |
5 ms |
3048 KB |
Output is correct |
67 |
Correct |
4 ms |
3028 KB |
Output is correct |
68 |
Correct |
1409 ms |
3320 KB |
Output is correct |
69 |
Correct |
637 ms |
3148 KB |
Output is correct |
70 |
Correct |
645 ms |
3148 KB |
Output is correct |
71 |
Correct |
652 ms |
3148 KB |
Output is correct |
72 |
Correct |
5 ms |
3028 KB |
Output is correct |
73 |
Correct |
5 ms |
3052 KB |
Output is correct |
74 |
Correct |
652 ms |
3288 KB |
Output is correct |
75 |
Correct |
297 ms |
3064 KB |
Output is correct |
76 |
Correct |
186 ms |
3072 KB |
Output is correct |
77 |
Correct |
136 ms |
3072 KB |
Output is correct |
78 |
Correct |
296 ms |
3088 KB |
Output is correct |
79 |
Correct |
189 ms |
3064 KB |
Output is correct |
80 |
Correct |
139 ms |
3072 KB |
Output is correct |
81 |
Correct |
305 ms |
3096 KB |
Output is correct |
82 |
Correct |
194 ms |
3092 KB |
Output is correct |
83 |
Correct |
146 ms |
3076 KB |
Output is correct |
84 |
Correct |
303 ms |
3104 KB |
Output is correct |
85 |
Correct |
193 ms |
3100 KB |
Output is correct |
86 |
Correct |
146 ms |
3208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
1 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
1 ms |
2644 KB |
Output is correct |
8 |
Correct |
18 ms |
2644 KB |
Output is correct |
9 |
Correct |
18 ms |
2756 KB |
Output is correct |
10 |
Correct |
18 ms |
2760 KB |
Output is correct |
11 |
Correct |
9 ms |
2768 KB |
Output is correct |
12 |
Correct |
9 ms |
2736 KB |
Output is correct |
13 |
Correct |
9 ms |
2644 KB |
Output is correct |
14 |
Correct |
9 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
10 ms |
2760 KB |
Output is correct |
20 |
Correct |
10 ms |
2644 KB |
Output is correct |
21 |
Correct |
9 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
18 ms |
2764 KB |
Output is correct |
27 |
Correct |
9 ms |
2764 KB |
Output is correct |
28 |
Correct |
9 ms |
2768 KB |
Output is correct |
29 |
Correct |
9 ms |
2764 KB |
Output is correct |
30 |
Correct |
2 ms |
2644 KB |
Output is correct |
31 |
Correct |
2 ms |
2644 KB |
Output is correct |
32 |
Correct |
9 ms |
2764 KB |
Output is correct |
33 |
Correct |
5 ms |
2644 KB |
Output is correct |
34 |
Correct |
4 ms |
2644 KB |
Output is correct |
35 |
Correct |
3 ms |
2644 KB |
Output is correct |
36 |
Correct |
5 ms |
2644 KB |
Output is correct |
37 |
Correct |
4 ms |
2644 KB |
Output is correct |
38 |
Correct |
3 ms |
2644 KB |
Output is correct |
39 |
Correct |
5 ms |
2644 KB |
Output is correct |
40 |
Correct |
4 ms |
2644 KB |
Output is correct |
41 |
Correct |
4 ms |
2644 KB |
Output is correct |
42 |
Correct |
5 ms |
2644 KB |
Output is correct |
43 |
Correct |
4 ms |
2752 KB |
Output is correct |
44 |
Correct |
3 ms |
2644 KB |
Output is correct |
45 |
Correct |
2 ms |
2772 KB |
Output is correct |
46 |
Correct |
6 ms |
3036 KB |
Output is correct |
47 |
Correct |
5 ms |
3028 KB |
Output is correct |
48 |
Correct |
4 ms |
3028 KB |
Output is correct |
49 |
Correct |
1390 ms |
3268 KB |
Output is correct |
50 |
Correct |
1390 ms |
3304 KB |
Output is correct |
51 |
Correct |
1394 ms |
3244 KB |
Output is correct |
52 |
Correct |
658 ms |
3152 KB |
Output is correct |
53 |
Correct |
643 ms |
3300 KB |
Output is correct |
54 |
Correct |
664 ms |
3152 KB |
Output is correct |
55 |
Correct |
660 ms |
3208 KB |
Output is correct |
56 |
Correct |
661 ms |
3244 KB |
Output is correct |
57 |
Correct |
5 ms |
3028 KB |
Output is correct |
58 |
Correct |
6 ms |
3156 KB |
Output is correct |
59 |
Correct |
5 ms |
3028 KB |
Output is correct |
60 |
Correct |
5 ms |
3064 KB |
Output is correct |
61 |
Correct |
659 ms |
3180 KB |
Output is correct |
62 |
Correct |
658 ms |
3284 KB |
Output is correct |
63 |
Correct |
661 ms |
3176 KB |
Output is correct |
64 |
Correct |
5 ms |
3032 KB |
Output is correct |
65 |
Correct |
5 ms |
3028 KB |
Output is correct |
66 |
Correct |
5 ms |
3048 KB |
Output is correct |
67 |
Correct |
4 ms |
3028 KB |
Output is correct |
68 |
Correct |
1409 ms |
3320 KB |
Output is correct |
69 |
Correct |
637 ms |
3148 KB |
Output is correct |
70 |
Correct |
645 ms |
3148 KB |
Output is correct |
71 |
Correct |
652 ms |
3148 KB |
Output is correct |
72 |
Correct |
5 ms |
3028 KB |
Output is correct |
73 |
Correct |
5 ms |
3052 KB |
Output is correct |
74 |
Correct |
652 ms |
3288 KB |
Output is correct |
75 |
Correct |
297 ms |
3064 KB |
Output is correct |
76 |
Correct |
186 ms |
3072 KB |
Output is correct |
77 |
Correct |
136 ms |
3072 KB |
Output is correct |
78 |
Correct |
296 ms |
3088 KB |
Output is correct |
79 |
Correct |
189 ms |
3064 KB |
Output is correct |
80 |
Correct |
139 ms |
3072 KB |
Output is correct |
81 |
Correct |
305 ms |
3096 KB |
Output is correct |
82 |
Correct |
194 ms |
3092 KB |
Output is correct |
83 |
Correct |
146 ms |
3076 KB |
Output is correct |
84 |
Correct |
303 ms |
3104 KB |
Output is correct |
85 |
Correct |
193 ms |
3100 KB |
Output is correct |
86 |
Correct |
146 ms |
3208 KB |
Output is correct |
87 |
Incorrect |
6 ms |
3540 KB |
Output isn't correct |
88 |
Halted |
0 ms |
0 KB |
- |