#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 = 29;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 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 |
484 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
600 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 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 |
484 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
600 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 |
344 KB |
Output is correct |
48 |
Correct |
3 ms |
580 KB |
Output is correct |
49 |
Correct |
113 ms |
580 KB |
Output is correct |
50 |
Correct |
141 ms |
600 KB |
Output is correct |
51 |
Correct |
124 ms |
552 KB |
Output is correct |
52 |
Correct |
59 ms |
472 KB |
Output is correct |
53 |
Correct |
59 ms |
552 KB |
Output is correct |
54 |
Correct |
57 ms |
600 KB |
Output is correct |
55 |
Correct |
73 ms |
600 KB |
Output is correct |
56 |
Correct |
56 ms |
604 KB |
Output is correct |
57 |
Correct |
3 ms |
604 KB |
Output is correct |
58 |
Correct |
6 ms |
604 KB |
Output is correct |
59 |
Correct |
3 ms |
544 KB |
Output is correct |
60 |
Correct |
3 ms |
604 KB |
Output is correct |
61 |
Correct |
73 ms |
636 KB |
Output is correct |
62 |
Correct |
65 ms |
636 KB |
Output is correct |
63 |
Correct |
56 ms |
600 KB |
Output is correct |
64 |
Correct |
3 ms |
600 KB |
Output is correct |
65 |
Correct |
3 ms |
604 KB |
Output is correct |
66 |
Correct |
3 ms |
604 KB |
Output is correct |
67 |
Correct |
3 ms |
604 KB |
Output is correct |
68 |
Correct |
84 ms |
604 KB |
Output is correct |
69 |
Correct |
58 ms |
636 KB |
Output is correct |
70 |
Correct |
40 ms |
604 KB |
Output is correct |
71 |
Correct |
41 ms |
604 KB |
Output is correct |
72 |
Correct |
3 ms |
532 KB |
Output is correct |
73 |
Correct |
4 ms |
604 KB |
Output is correct |
74 |
Correct |
41 ms |
628 KB |
Output is correct |
75 |
Correct |
30 ms |
600 KB |
Output is correct |
76 |
Correct |
21 ms |
604 KB |
Output is correct |
77 |
Correct |
16 ms |
544 KB |
Output is correct |
78 |
Correct |
23 ms |
600 KB |
Output is correct |
79 |
Correct |
16 ms |
600 KB |
Output is correct |
80 |
Correct |
13 ms |
604 KB |
Output is correct |
81 |
Correct |
30 ms |
644 KB |
Output is correct |
82 |
Correct |
21 ms |
600 KB |
Output is correct |
83 |
Correct |
18 ms |
604 KB |
Output is correct |
84 |
Correct |
23 ms |
632 KB |
Output is correct |
85 |
Correct |
16 ms |
604 KB |
Output is correct |
86 |
Correct |
13 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 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 |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
2 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
2 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 |
484 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
1 ms |
600 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 |
344 KB |
Output is correct |
48 |
Correct |
3 ms |
580 KB |
Output is correct |
49 |
Correct |
113 ms |
580 KB |
Output is correct |
50 |
Correct |
141 ms |
600 KB |
Output is correct |
51 |
Correct |
124 ms |
552 KB |
Output is correct |
52 |
Correct |
59 ms |
472 KB |
Output is correct |
53 |
Correct |
59 ms |
552 KB |
Output is correct |
54 |
Correct |
57 ms |
600 KB |
Output is correct |
55 |
Correct |
73 ms |
600 KB |
Output is correct |
56 |
Correct |
56 ms |
604 KB |
Output is correct |
57 |
Correct |
3 ms |
604 KB |
Output is correct |
58 |
Correct |
6 ms |
604 KB |
Output is correct |
59 |
Correct |
3 ms |
544 KB |
Output is correct |
60 |
Correct |
3 ms |
604 KB |
Output is correct |
61 |
Correct |
73 ms |
636 KB |
Output is correct |
62 |
Correct |
65 ms |
636 KB |
Output is correct |
63 |
Correct |
56 ms |
600 KB |
Output is correct |
64 |
Correct |
3 ms |
600 KB |
Output is correct |
65 |
Correct |
3 ms |
604 KB |
Output is correct |
66 |
Correct |
3 ms |
604 KB |
Output is correct |
67 |
Correct |
3 ms |
604 KB |
Output is correct |
68 |
Correct |
84 ms |
604 KB |
Output is correct |
69 |
Correct |
58 ms |
636 KB |
Output is correct |
70 |
Correct |
40 ms |
604 KB |
Output is correct |
71 |
Correct |
41 ms |
604 KB |
Output is correct |
72 |
Correct |
3 ms |
532 KB |
Output is correct |
73 |
Correct |
4 ms |
604 KB |
Output is correct |
74 |
Correct |
41 ms |
628 KB |
Output is correct |
75 |
Correct |
30 ms |
600 KB |
Output is correct |
76 |
Correct |
21 ms |
604 KB |
Output is correct |
77 |
Correct |
16 ms |
544 KB |
Output is correct |
78 |
Correct |
23 ms |
600 KB |
Output is correct |
79 |
Correct |
16 ms |
600 KB |
Output is correct |
80 |
Correct |
13 ms |
604 KB |
Output is correct |
81 |
Correct |
30 ms |
644 KB |
Output is correct |
82 |
Correct |
21 ms |
600 KB |
Output is correct |
83 |
Correct |
18 ms |
604 KB |
Output is correct |
84 |
Correct |
23 ms |
632 KB |
Output is correct |
85 |
Correct |
16 ms |
604 KB |
Output is correct |
86 |
Correct |
13 ms |
600 KB |
Output is correct |
87 |
Correct |
13 ms |
988 KB |
Output is correct |
88 |
Correct |
63 ms |
1832 KB |
Output is correct |
89 |
Correct |
618 ms |
4804 KB |
Output is correct |
90 |
Correct |
588 ms |
4808 KB |
Output is correct |
91 |
Correct |
599 ms |
4808 KB |
Output is correct |
92 |
Execution timed out |
2062 ms |
3792 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |