#include<bits/stdc++.h>
#define INF 1e18
#define fi first
#define se second
#define FOR(i, k, n) for(ll i = k; i <= n; i++)
#define FOR1(i, k, n) for(ll i = k; i >= n; i--)
#define pb push_back
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
#define vii vector<pii>
#define ll long long
#define vll vector<ll>
#define pll pair<ll, ll>
#define re return 0
#define mii map<int, int>
#define input "BAI1.inp"
#define output "BAI1.out"
#define rf freopen(input, "r", stdin); freopen(output, "w", stdout)
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int base = 31;
void add(int &a, int b)
{
a += b;
if(a >= mod) a -= mod;
if(a < 0) a += mod;
}
int par[maxn];
int a[maxn], bit[maxn];
int t;
void update(int x)
{
while(x <= t)
{
bit[x]++;
x += x & -x;
}
}
int get(int x)
{
int ans = 0;
while(x > 0)
{
ans += bit[x];
x -= x & -x;
}
return ans;
}
int main()
{
fastio;
int n;
cin >> n;
FOR(i, 1, n)
cin >> a[i];
vii vvv;
FOR(i, 1, n)
vvv.pb({a[i], i});
sort(vvv.begin(), vvv.end());
int truoc = -1;
for(auto x : vvv)
{
if(x.fi > truoc)
t++;
a[x.se] = t;
truoc = x.fi;
}
FOR(i, 1, n - 1)
{
int u, v;
cin >> u >> v;
vi vv;
par[v] = u;
while(u != 1)
{
vv.pb(u);
u = par[u];
}
vv.pb(1);
if(vv.size() == 1)
cout << "0\n";
else
{
int ans = 0;
FOR(i, 1, n)
bit[i] = 0;
for(auto x : vv)
{
ans += get(a[x] - 1);
update(a[x]);
}
cout << ans << "\n";
}
for(auto x : vv)
a[x] = a[v];
}
re;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
3 ms |
344 KB |
Output is correct |
47 |
Correct |
3 ms |
584 KB |
Output is correct |
48 |
Correct |
3 ms |
348 KB |
Output is correct |
49 |
Correct |
115 ms |
568 KB |
Output is correct |
50 |
Correct |
122 ms |
348 KB |
Output is correct |
51 |
Correct |
113 ms |
348 KB |
Output is correct |
52 |
Correct |
60 ms |
576 KB |
Output is correct |
53 |
Correct |
56 ms |
348 KB |
Output is correct |
54 |
Correct |
56 ms |
348 KB |
Output is correct |
55 |
Correct |
56 ms |
348 KB |
Output is correct |
56 |
Correct |
56 ms |
344 KB |
Output is correct |
57 |
Correct |
3 ms |
600 KB |
Output is correct |
58 |
Correct |
3 ms |
348 KB |
Output is correct |
59 |
Correct |
3 ms |
580 KB |
Output is correct |
60 |
Correct |
3 ms |
580 KB |
Output is correct |
61 |
Correct |
60 ms |
348 KB |
Output is correct |
62 |
Correct |
56 ms |
348 KB |
Output is correct |
63 |
Correct |
57 ms |
344 KB |
Output is correct |
64 |
Correct |
3 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
348 KB |
Output is correct |
66 |
Correct |
3 ms |
348 KB |
Output is correct |
67 |
Correct |
3 ms |
348 KB |
Output is correct |
68 |
Correct |
82 ms |
348 KB |
Output is correct |
69 |
Correct |
57 ms |
344 KB |
Output is correct |
70 |
Correct |
40 ms |
600 KB |
Output is correct |
71 |
Correct |
41 ms |
348 KB |
Output is correct |
72 |
Correct |
3 ms |
348 KB |
Output is correct |
73 |
Correct |
3 ms |
588 KB |
Output is correct |
74 |
Correct |
41 ms |
348 KB |
Output is correct |
75 |
Correct |
30 ms |
344 KB |
Output is correct |
76 |
Correct |
21 ms |
344 KB |
Output is correct |
77 |
Correct |
17 ms |
348 KB |
Output is correct |
78 |
Correct |
23 ms |
580 KB |
Output is correct |
79 |
Correct |
18 ms |
580 KB |
Output is correct |
80 |
Correct |
13 ms |
348 KB |
Output is correct |
81 |
Correct |
30 ms |
344 KB |
Output is correct |
82 |
Correct |
21 ms |
344 KB |
Output is correct |
83 |
Correct |
18 ms |
348 KB |
Output is correct |
84 |
Correct |
22 ms |
580 KB |
Output is correct |
85 |
Correct |
16 ms |
592 KB |
Output is correct |
86 |
Correct |
13 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
424 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
600 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
3 ms |
344 KB |
Output is correct |
47 |
Correct |
3 ms |
584 KB |
Output is correct |
48 |
Correct |
3 ms |
348 KB |
Output is correct |
49 |
Correct |
115 ms |
568 KB |
Output is correct |
50 |
Correct |
122 ms |
348 KB |
Output is correct |
51 |
Correct |
113 ms |
348 KB |
Output is correct |
52 |
Correct |
60 ms |
576 KB |
Output is correct |
53 |
Correct |
56 ms |
348 KB |
Output is correct |
54 |
Correct |
56 ms |
348 KB |
Output is correct |
55 |
Correct |
56 ms |
348 KB |
Output is correct |
56 |
Correct |
56 ms |
344 KB |
Output is correct |
57 |
Correct |
3 ms |
600 KB |
Output is correct |
58 |
Correct |
3 ms |
348 KB |
Output is correct |
59 |
Correct |
3 ms |
580 KB |
Output is correct |
60 |
Correct |
3 ms |
580 KB |
Output is correct |
61 |
Correct |
60 ms |
348 KB |
Output is correct |
62 |
Correct |
56 ms |
348 KB |
Output is correct |
63 |
Correct |
57 ms |
344 KB |
Output is correct |
64 |
Correct |
3 ms |
348 KB |
Output is correct |
65 |
Correct |
3 ms |
348 KB |
Output is correct |
66 |
Correct |
3 ms |
348 KB |
Output is correct |
67 |
Correct |
3 ms |
348 KB |
Output is correct |
68 |
Correct |
82 ms |
348 KB |
Output is correct |
69 |
Correct |
57 ms |
344 KB |
Output is correct |
70 |
Correct |
40 ms |
600 KB |
Output is correct |
71 |
Correct |
41 ms |
348 KB |
Output is correct |
72 |
Correct |
3 ms |
348 KB |
Output is correct |
73 |
Correct |
3 ms |
588 KB |
Output is correct |
74 |
Correct |
41 ms |
348 KB |
Output is correct |
75 |
Correct |
30 ms |
344 KB |
Output is correct |
76 |
Correct |
21 ms |
344 KB |
Output is correct |
77 |
Correct |
17 ms |
348 KB |
Output is correct |
78 |
Correct |
23 ms |
580 KB |
Output is correct |
79 |
Correct |
18 ms |
580 KB |
Output is correct |
80 |
Correct |
13 ms |
348 KB |
Output is correct |
81 |
Correct |
30 ms |
344 KB |
Output is correct |
82 |
Correct |
21 ms |
344 KB |
Output is correct |
83 |
Correct |
18 ms |
348 KB |
Output is correct |
84 |
Correct |
22 ms |
580 KB |
Output is correct |
85 |
Correct |
16 ms |
592 KB |
Output is correct |
86 |
Correct |
13 ms |
348 KB |
Output is correct |
87 |
Correct |
11 ms |
604 KB |
Output is correct |
88 |
Correct |
65 ms |
1204 KB |
Output is correct |
89 |
Correct |
605 ms |
2804 KB |
Output is correct |
90 |
Correct |
631 ms |
2768 KB |
Output is correct |
91 |
Correct |
594 ms |
2792 KB |
Output is correct |
92 |
Execution timed out |
2021 ms |
2556 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |