// Code by Parsa Eslami
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORR(i,a,b) for(int i=a;i>=b;i--)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define all(x) x.begin(),x.end()
#define SZ(x) ((int)x.size())
#define bit(i,j) ((j>>i)&1)
#define pb push_back
#define lc 2*id
#define rc 2*id+1
#define dmid int mid=(r+l)/2
#define err(x) cerr<#x<<" : "<<x<<'\n'
#define F first
#define S second
using namespace std;
const int N=4000+4;
int par[N],val[N];
struct fenwick{
int fen[N];
void add(int k,int x){
while(k>0){
fen[k]+=x;
k-=(k&(-k));
}
}
int get(int k){
int rt=0;
while(k<N){
rt+=fen[k];
k+=(k&(-k));
}
return rt;
}
};
fenwick fen;
int Get(){
int x; cin>>x; return x;
}
void Set(int v,int x){
val[v]=x;
if(v==1) return;
Set(par[v],x);
}
ll get(int u){
ll rt=0;
int v=u;
vector<int> vc;
while(1){
vc.pb(v);
if(v==1) break;
v=par[v];
}
reverse(all(vc));
for(int x:vc){
rt+=fen.get(val[x]+1);
fen.add(val[x],+1);
}
for(int x:vc) fen.add(val[x],-1);
return rt;
}
int32_t main(){
iostream::sync_with_stdio(0); cin.tie(0);
int n=Get();
FOR(i,1,n) val[i]=Get();
vector<pii> vc;
FOR(i,1,n) vc.pb({val[i],i});
sort(all(vc)); int x0=0;
FOR(i,0,SZ(vc)-1){
if(i==0||vc[i].F!=vc[i-1].F) x0++;
val[vc[i].S]=x0;
}
FOR(i,1,n-1){
int u=Get(),v=Get();
cout<<get(u)<<'\n';
par[v]=u;
Set(u,val[v]);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
332 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
3 ms |
328 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
352 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
344 KB |
Output is correct |
35 |
Correct |
2 ms |
328 KB |
Output is correct |
36 |
Correct |
2 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
2 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
332 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
3 ms |
328 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
352 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
344 KB |
Output is correct |
35 |
Correct |
2 ms |
328 KB |
Output is correct |
36 |
Correct |
2 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
2 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
328 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
4 ms |
468 KB |
Output is correct |
47 |
Correct |
4 ms |
468 KB |
Output is correct |
48 |
Correct |
5 ms |
408 KB |
Output is correct |
49 |
Correct |
232 ms |
568 KB |
Output is correct |
50 |
Correct |
234 ms |
488 KB |
Output is correct |
51 |
Correct |
231 ms |
488 KB |
Output is correct |
52 |
Correct |
122 ms |
476 KB |
Output is correct |
53 |
Correct |
128 ms |
488 KB |
Output is correct |
54 |
Correct |
121 ms |
508 KB |
Output is correct |
55 |
Correct |
124 ms |
492 KB |
Output is correct |
56 |
Correct |
118 ms |
488 KB |
Output is correct |
57 |
Correct |
4 ms |
468 KB |
Output is correct |
58 |
Correct |
5 ms |
464 KB |
Output is correct |
59 |
Correct |
4 ms |
468 KB |
Output is correct |
60 |
Correct |
5 ms |
468 KB |
Output is correct |
61 |
Correct |
121 ms |
500 KB |
Output is correct |
62 |
Correct |
122 ms |
468 KB |
Output is correct |
63 |
Correct |
117 ms |
488 KB |
Output is correct |
64 |
Correct |
3 ms |
464 KB |
Output is correct |
65 |
Correct |
4 ms |
468 KB |
Output is correct |
66 |
Correct |
4 ms |
468 KB |
Output is correct |
67 |
Correct |
4 ms |
464 KB |
Output is correct |
68 |
Correct |
217 ms |
464 KB |
Output is correct |
69 |
Correct |
130 ms |
472 KB |
Output is correct |
70 |
Correct |
113 ms |
452 KB |
Output is correct |
71 |
Correct |
109 ms |
464 KB |
Output is correct |
72 |
Correct |
4 ms |
512 KB |
Output is correct |
73 |
Correct |
4 ms |
468 KB |
Output is correct |
74 |
Correct |
109 ms |
448 KB |
Output is correct |
75 |
Correct |
64 ms |
468 KB |
Output is correct |
76 |
Correct |
47 ms |
464 KB |
Output is correct |
77 |
Correct |
33 ms |
496 KB |
Output is correct |
78 |
Correct |
56 ms |
340 KB |
Output is correct |
79 |
Correct |
38 ms |
468 KB |
Output is correct |
80 |
Correct |
30 ms |
448 KB |
Output is correct |
81 |
Correct |
61 ms |
476 KB |
Output is correct |
82 |
Correct |
43 ms |
468 KB |
Output is correct |
83 |
Correct |
36 ms |
468 KB |
Output is correct |
84 |
Correct |
64 ms |
504 KB |
Output is correct |
85 |
Correct |
39 ms |
340 KB |
Output is correct |
86 |
Correct |
30 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
316 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
4 ms |
332 KB |
Output is correct |
10 |
Correct |
4 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
340 KB |
Output is correct |
13 |
Correct |
3 ms |
340 KB |
Output is correct |
14 |
Correct |
4 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
336 KB |
Output is correct |
20 |
Correct |
3 ms |
328 KB |
Output is correct |
21 |
Correct |
3 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
340 KB |
Output is correct |
27 |
Correct |
3 ms |
352 KB |
Output is correct |
28 |
Correct |
3 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
324 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
344 KB |
Output is correct |
35 |
Correct |
2 ms |
328 KB |
Output is correct |
36 |
Correct |
2 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
2 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
328 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
328 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
4 ms |
468 KB |
Output is correct |
47 |
Correct |
4 ms |
468 KB |
Output is correct |
48 |
Correct |
5 ms |
408 KB |
Output is correct |
49 |
Correct |
232 ms |
568 KB |
Output is correct |
50 |
Correct |
234 ms |
488 KB |
Output is correct |
51 |
Correct |
231 ms |
488 KB |
Output is correct |
52 |
Correct |
122 ms |
476 KB |
Output is correct |
53 |
Correct |
128 ms |
488 KB |
Output is correct |
54 |
Correct |
121 ms |
508 KB |
Output is correct |
55 |
Correct |
124 ms |
492 KB |
Output is correct |
56 |
Correct |
118 ms |
488 KB |
Output is correct |
57 |
Correct |
4 ms |
468 KB |
Output is correct |
58 |
Correct |
5 ms |
464 KB |
Output is correct |
59 |
Correct |
4 ms |
468 KB |
Output is correct |
60 |
Correct |
5 ms |
468 KB |
Output is correct |
61 |
Correct |
121 ms |
500 KB |
Output is correct |
62 |
Correct |
122 ms |
468 KB |
Output is correct |
63 |
Correct |
117 ms |
488 KB |
Output is correct |
64 |
Correct |
3 ms |
464 KB |
Output is correct |
65 |
Correct |
4 ms |
468 KB |
Output is correct |
66 |
Correct |
4 ms |
468 KB |
Output is correct |
67 |
Correct |
4 ms |
464 KB |
Output is correct |
68 |
Correct |
217 ms |
464 KB |
Output is correct |
69 |
Correct |
130 ms |
472 KB |
Output is correct |
70 |
Correct |
113 ms |
452 KB |
Output is correct |
71 |
Correct |
109 ms |
464 KB |
Output is correct |
72 |
Correct |
4 ms |
512 KB |
Output is correct |
73 |
Correct |
4 ms |
468 KB |
Output is correct |
74 |
Correct |
109 ms |
448 KB |
Output is correct |
75 |
Correct |
64 ms |
468 KB |
Output is correct |
76 |
Correct |
47 ms |
464 KB |
Output is correct |
77 |
Correct |
33 ms |
496 KB |
Output is correct |
78 |
Correct |
56 ms |
340 KB |
Output is correct |
79 |
Correct |
38 ms |
468 KB |
Output is correct |
80 |
Correct |
30 ms |
448 KB |
Output is correct |
81 |
Correct |
61 ms |
476 KB |
Output is correct |
82 |
Correct |
43 ms |
468 KB |
Output is correct |
83 |
Correct |
36 ms |
468 KB |
Output is correct |
84 |
Correct |
64 ms |
504 KB |
Output is correct |
85 |
Correct |
39 ms |
340 KB |
Output is correct |
86 |
Correct |
30 ms |
460 KB |
Output is correct |
87 |
Runtime error |
2 ms |
724 KB |
Execution killed with signal 11 |
88 |
Halted |
0 ms |
0 KB |
- |