#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vii;
typedef pair<ll,ll> pii;
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pb push_back
#define pow poww
const int M=1e6+10;
const ll mod=998244353;
const ll inf=2e18;
ll pow(ll x,ll y){
ll res=1;
x%=mod;
while(y>0){
if(y%2==1){
res*=x,res%=mod;
}
x*=x,x%=mod;
y/=2;
}
return res;
}
ll n,C[M],a[M],pa[M],b[M];
vii adj[M];
map<ll,ll>mp;
void update(ll x){
for(int i=x;i<=n;i+=(i&-i))
b[i]++;
}
ll query(ll x){
ll res=0;
for(int i=x;i>0;i-=(i&-i))
res+=b[i];
return res;
}
ll calc(ll x){
vii v;
while(x!=1)
v.pb(a[x]),x=pa[x];
v.pb(a[1]);
for(int i=0;i<=2*n;i++)
b[i]=0;
ll ans=0;
for(auto it:v)
update(it),ans+=query(it-1);
return ans;
}
void change(ll x){
ll z=a[x];
while(x!=1)
a[x]=z,x=pa[x];
a[1]=z;
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
ll TC=1;
//cin>>TC;
while(TC--){
cin>>n;
for(int i=0;i<n;i++)
cin>>C[i],mp[C[i]]=1;
ll cnt=1;
for(auto &it:mp)
it.S=cnt++;
for(int i=0;i<n;i++){
a[i+1]=mp[C[i]];
}
for(int i=0;i<n-1;i++){
ll x,y;
cin>>x>>y;
pa[y]=x;
cout<<calc(x)<<'\n';
change(y);
}
}
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
5 ms |
31420 KB |
Output is correct |
3 |
Correct |
5 ms |
31356 KB |
Output is correct |
4 |
Correct |
5 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31336 KB |
Output is correct |
6 |
Correct |
5 ms |
31324 KB |
Output is correct |
7 |
Correct |
6 ms |
31324 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31324 KB |
Output is correct |
10 |
Correct |
6 ms |
31324 KB |
Output is correct |
11 |
Correct |
6 ms |
31324 KB |
Output is correct |
12 |
Correct |
8 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31324 KB |
Output is correct |
14 |
Correct |
6 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31324 KB |
Output is correct |
16 |
Correct |
5 ms |
31344 KB |
Output is correct |
17 |
Correct |
6 ms |
31320 KB |
Output is correct |
18 |
Correct |
5 ms |
31352 KB |
Output is correct |
19 |
Correct |
6 ms |
31324 KB |
Output is correct |
20 |
Correct |
6 ms |
31324 KB |
Output is correct |
21 |
Correct |
5 ms |
31456 KB |
Output is correct |
22 |
Correct |
5 ms |
31324 KB |
Output is correct |
23 |
Correct |
5 ms |
31324 KB |
Output is correct |
24 |
Correct |
6 ms |
31324 KB |
Output is correct |
25 |
Correct |
6 ms |
31324 KB |
Output is correct |
26 |
Correct |
6 ms |
31324 KB |
Output is correct |
27 |
Correct |
6 ms |
31324 KB |
Output is correct |
28 |
Correct |
6 ms |
31324 KB |
Output is correct |
29 |
Correct |
5 ms |
31324 KB |
Output is correct |
30 |
Correct |
7 ms |
31408 KB |
Output is correct |
31 |
Correct |
5 ms |
31320 KB |
Output is correct |
32 |
Correct |
6 ms |
31324 KB |
Output is correct |
33 |
Correct |
6 ms |
31324 KB |
Output is correct |
34 |
Correct |
5 ms |
31324 KB |
Output is correct |
35 |
Correct |
6 ms |
31404 KB |
Output is correct |
36 |
Correct |
6 ms |
31324 KB |
Output is correct |
37 |
Correct |
5 ms |
31404 KB |
Output is correct |
38 |
Correct |
5 ms |
31324 KB |
Output is correct |
39 |
Correct |
5 ms |
31348 KB |
Output is correct |
40 |
Correct |
5 ms |
31408 KB |
Output is correct |
41 |
Correct |
5 ms |
31324 KB |
Output is correct |
42 |
Correct |
5 ms |
31324 KB |
Output is correct |
43 |
Correct |
5 ms |
31324 KB |
Output is correct |
44 |
Correct |
6 ms |
31360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
5 ms |
31420 KB |
Output is correct |
3 |
Correct |
5 ms |
31356 KB |
Output is correct |
4 |
Correct |
5 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31336 KB |
Output is correct |
6 |
Correct |
5 ms |
31324 KB |
Output is correct |
7 |
Correct |
6 ms |
31324 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31324 KB |
Output is correct |
10 |
Correct |
6 ms |
31324 KB |
Output is correct |
11 |
Correct |
6 ms |
31324 KB |
Output is correct |
12 |
Correct |
8 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31324 KB |
Output is correct |
14 |
Correct |
6 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31324 KB |
Output is correct |
16 |
Correct |
5 ms |
31344 KB |
Output is correct |
17 |
Correct |
6 ms |
31320 KB |
Output is correct |
18 |
Correct |
5 ms |
31352 KB |
Output is correct |
19 |
Correct |
6 ms |
31324 KB |
Output is correct |
20 |
Correct |
6 ms |
31324 KB |
Output is correct |
21 |
Correct |
5 ms |
31456 KB |
Output is correct |
22 |
Correct |
5 ms |
31324 KB |
Output is correct |
23 |
Correct |
5 ms |
31324 KB |
Output is correct |
24 |
Correct |
6 ms |
31324 KB |
Output is correct |
25 |
Correct |
6 ms |
31324 KB |
Output is correct |
26 |
Correct |
6 ms |
31324 KB |
Output is correct |
27 |
Correct |
6 ms |
31324 KB |
Output is correct |
28 |
Correct |
6 ms |
31324 KB |
Output is correct |
29 |
Correct |
5 ms |
31324 KB |
Output is correct |
30 |
Correct |
7 ms |
31408 KB |
Output is correct |
31 |
Correct |
5 ms |
31320 KB |
Output is correct |
32 |
Correct |
6 ms |
31324 KB |
Output is correct |
33 |
Correct |
6 ms |
31324 KB |
Output is correct |
34 |
Correct |
5 ms |
31324 KB |
Output is correct |
35 |
Correct |
6 ms |
31404 KB |
Output is correct |
36 |
Correct |
6 ms |
31324 KB |
Output is correct |
37 |
Correct |
5 ms |
31404 KB |
Output is correct |
38 |
Correct |
5 ms |
31324 KB |
Output is correct |
39 |
Correct |
5 ms |
31348 KB |
Output is correct |
40 |
Correct |
5 ms |
31408 KB |
Output is correct |
41 |
Correct |
5 ms |
31324 KB |
Output is correct |
42 |
Correct |
5 ms |
31324 KB |
Output is correct |
43 |
Correct |
5 ms |
31324 KB |
Output is correct |
44 |
Correct |
6 ms |
31360 KB |
Output is correct |
45 |
Correct |
6 ms |
31324 KB |
Output is correct |
46 |
Correct |
12 ms |
33628 KB |
Output is correct |
47 |
Correct |
12 ms |
33628 KB |
Output is correct |
48 |
Correct |
11 ms |
33708 KB |
Output is correct |
49 |
Correct |
149 ms |
33800 KB |
Output is correct |
50 |
Correct |
148 ms |
33832 KB |
Output is correct |
51 |
Correct |
153 ms |
33628 KB |
Output is correct |
52 |
Correct |
76 ms |
33628 KB |
Output is correct |
53 |
Correct |
77 ms |
33628 KB |
Output is correct |
54 |
Correct |
79 ms |
33624 KB |
Output is correct |
55 |
Correct |
76 ms |
33624 KB |
Output is correct |
56 |
Correct |
80 ms |
33884 KB |
Output is correct |
57 |
Correct |
11 ms |
33628 KB |
Output is correct |
58 |
Correct |
11 ms |
33636 KB |
Output is correct |
59 |
Correct |
11 ms |
33628 KB |
Output is correct |
60 |
Correct |
11 ms |
33804 KB |
Output is correct |
61 |
Correct |
78 ms |
33628 KB |
Output is correct |
62 |
Correct |
76 ms |
33624 KB |
Output is correct |
63 |
Correct |
82 ms |
33820 KB |
Output is correct |
64 |
Correct |
14 ms |
33628 KB |
Output is correct |
65 |
Correct |
10 ms |
33372 KB |
Output is correct |
66 |
Correct |
11 ms |
33372 KB |
Output is correct |
67 |
Correct |
10 ms |
33628 KB |
Output is correct |
68 |
Correct |
162 ms |
33372 KB |
Output is correct |
69 |
Correct |
77 ms |
33628 KB |
Output is correct |
70 |
Correct |
82 ms |
33536 KB |
Output is correct |
71 |
Correct |
85 ms |
33544 KB |
Output is correct |
72 |
Correct |
12 ms |
33624 KB |
Output is correct |
73 |
Correct |
11 ms |
33368 KB |
Output is correct |
74 |
Correct |
82 ms |
33544 KB |
Output is correct |
75 |
Correct |
44 ms |
33628 KB |
Output is correct |
76 |
Correct |
31 ms |
33624 KB |
Output is correct |
77 |
Correct |
29 ms |
33804 KB |
Output is correct |
78 |
Correct |
44 ms |
33540 KB |
Output is correct |
79 |
Correct |
32 ms |
33372 KB |
Output is correct |
80 |
Correct |
26 ms |
33368 KB |
Output is correct |
81 |
Correct |
41 ms |
33628 KB |
Output is correct |
82 |
Correct |
31 ms |
33628 KB |
Output is correct |
83 |
Correct |
27 ms |
33624 KB |
Output is correct |
84 |
Correct |
46 ms |
33624 KB |
Output is correct |
85 |
Correct |
33 ms |
33372 KB |
Output is correct |
86 |
Correct |
27 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31324 KB |
Output is correct |
2 |
Correct |
5 ms |
31420 KB |
Output is correct |
3 |
Correct |
5 ms |
31356 KB |
Output is correct |
4 |
Correct |
5 ms |
31324 KB |
Output is correct |
5 |
Correct |
5 ms |
31336 KB |
Output is correct |
6 |
Correct |
5 ms |
31324 KB |
Output is correct |
7 |
Correct |
6 ms |
31324 KB |
Output is correct |
8 |
Correct |
6 ms |
31324 KB |
Output is correct |
9 |
Correct |
6 ms |
31324 KB |
Output is correct |
10 |
Correct |
6 ms |
31324 KB |
Output is correct |
11 |
Correct |
6 ms |
31324 KB |
Output is correct |
12 |
Correct |
8 ms |
31324 KB |
Output is correct |
13 |
Correct |
6 ms |
31324 KB |
Output is correct |
14 |
Correct |
6 ms |
31324 KB |
Output is correct |
15 |
Correct |
6 ms |
31324 KB |
Output is correct |
16 |
Correct |
5 ms |
31344 KB |
Output is correct |
17 |
Correct |
6 ms |
31320 KB |
Output is correct |
18 |
Correct |
5 ms |
31352 KB |
Output is correct |
19 |
Correct |
6 ms |
31324 KB |
Output is correct |
20 |
Correct |
6 ms |
31324 KB |
Output is correct |
21 |
Correct |
5 ms |
31456 KB |
Output is correct |
22 |
Correct |
5 ms |
31324 KB |
Output is correct |
23 |
Correct |
5 ms |
31324 KB |
Output is correct |
24 |
Correct |
6 ms |
31324 KB |
Output is correct |
25 |
Correct |
6 ms |
31324 KB |
Output is correct |
26 |
Correct |
6 ms |
31324 KB |
Output is correct |
27 |
Correct |
6 ms |
31324 KB |
Output is correct |
28 |
Correct |
6 ms |
31324 KB |
Output is correct |
29 |
Correct |
5 ms |
31324 KB |
Output is correct |
30 |
Correct |
7 ms |
31408 KB |
Output is correct |
31 |
Correct |
5 ms |
31320 KB |
Output is correct |
32 |
Correct |
6 ms |
31324 KB |
Output is correct |
33 |
Correct |
6 ms |
31324 KB |
Output is correct |
34 |
Correct |
5 ms |
31324 KB |
Output is correct |
35 |
Correct |
6 ms |
31404 KB |
Output is correct |
36 |
Correct |
6 ms |
31324 KB |
Output is correct |
37 |
Correct |
5 ms |
31404 KB |
Output is correct |
38 |
Correct |
5 ms |
31324 KB |
Output is correct |
39 |
Correct |
5 ms |
31348 KB |
Output is correct |
40 |
Correct |
5 ms |
31408 KB |
Output is correct |
41 |
Correct |
5 ms |
31324 KB |
Output is correct |
42 |
Correct |
5 ms |
31324 KB |
Output is correct |
43 |
Correct |
5 ms |
31324 KB |
Output is correct |
44 |
Correct |
6 ms |
31360 KB |
Output is correct |
45 |
Correct |
6 ms |
31324 KB |
Output is correct |
46 |
Correct |
12 ms |
33628 KB |
Output is correct |
47 |
Correct |
12 ms |
33628 KB |
Output is correct |
48 |
Correct |
11 ms |
33708 KB |
Output is correct |
49 |
Correct |
149 ms |
33800 KB |
Output is correct |
50 |
Correct |
148 ms |
33832 KB |
Output is correct |
51 |
Correct |
153 ms |
33628 KB |
Output is correct |
52 |
Correct |
76 ms |
33628 KB |
Output is correct |
53 |
Correct |
77 ms |
33628 KB |
Output is correct |
54 |
Correct |
79 ms |
33624 KB |
Output is correct |
55 |
Correct |
76 ms |
33624 KB |
Output is correct |
56 |
Correct |
80 ms |
33884 KB |
Output is correct |
57 |
Correct |
11 ms |
33628 KB |
Output is correct |
58 |
Correct |
11 ms |
33636 KB |
Output is correct |
59 |
Correct |
11 ms |
33628 KB |
Output is correct |
60 |
Correct |
11 ms |
33804 KB |
Output is correct |
61 |
Correct |
78 ms |
33628 KB |
Output is correct |
62 |
Correct |
76 ms |
33624 KB |
Output is correct |
63 |
Correct |
82 ms |
33820 KB |
Output is correct |
64 |
Correct |
14 ms |
33628 KB |
Output is correct |
65 |
Correct |
10 ms |
33372 KB |
Output is correct |
66 |
Correct |
11 ms |
33372 KB |
Output is correct |
67 |
Correct |
10 ms |
33628 KB |
Output is correct |
68 |
Correct |
162 ms |
33372 KB |
Output is correct |
69 |
Correct |
77 ms |
33628 KB |
Output is correct |
70 |
Correct |
82 ms |
33536 KB |
Output is correct |
71 |
Correct |
85 ms |
33544 KB |
Output is correct |
72 |
Correct |
12 ms |
33624 KB |
Output is correct |
73 |
Correct |
11 ms |
33368 KB |
Output is correct |
74 |
Correct |
82 ms |
33544 KB |
Output is correct |
75 |
Correct |
44 ms |
33628 KB |
Output is correct |
76 |
Correct |
31 ms |
33624 KB |
Output is correct |
77 |
Correct |
29 ms |
33804 KB |
Output is correct |
78 |
Correct |
44 ms |
33540 KB |
Output is correct |
79 |
Correct |
32 ms |
33372 KB |
Output is correct |
80 |
Correct |
26 ms |
33368 KB |
Output is correct |
81 |
Correct |
41 ms |
33628 KB |
Output is correct |
82 |
Correct |
31 ms |
33628 KB |
Output is correct |
83 |
Correct |
27 ms |
33624 KB |
Output is correct |
84 |
Correct |
46 ms |
33624 KB |
Output is correct |
85 |
Correct |
33 ms |
33372 KB |
Output is correct |
86 |
Correct |
27 ms |
33372 KB |
Output is correct |
87 |
Correct |
32 ms |
34136 KB |
Output is correct |
88 |
Correct |
243 ms |
35840 KB |
Output is correct |
89 |
Execution timed out |
2043 ms |
45896 KB |
Time limit exceeded |
90 |
Halted |
0 ms |
0 KB |
- |