// High above the clouds there is a rainbow...
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn=1e5+10,mod=1e9+7;
const ll inf=1e18;
int a[maxn],fn[maxn],val[maxn],par[maxn];
vector<int>v[maxn];
void add(int pos,int x){
for(pos=pos+3;pos<maxn;pos+=(pos & -pos))
fn[pos]+=x;
}
int ask(int pos){
int ans=0;
for(pos=pos+3;pos>0;pos-=(pos & -pos))
ans+=fn[pos];
return ans;
}
void dfs(int u){
add(a[u],1);
for(int y:v[u]){
val[y]=val[u]+ask(maxn-7)-ask(a[y]);
dfs(y);
}
add(a[u],-1);
}
int arr[maxn],C;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n;cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
arr[i]=a[i];
}
sort(arr,arr+n);
C=unique(arr,arr+n)-arr;
for(int i=0;i<n;i++){
a[i]=lower_bound(arr,arr+C,a[i])-arr;
}
par[0]=-1;
for(int i=0;i<n-1;i++){
int A,B;cin>>A>>B;
par[--B]=--A;
v[A].PB(B);
dfs(0);
int x=B;
while(x>=0){
a[x]=a[B];
x=par[x];
}
cout<<val[A]<<"\n";
}
return 0;
}
// Deathly mistakes:
// * Read the problem curfully.
// * Check maxn.
// * Overflows.
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2816 KB |
Output is correct |
5 |
Correct |
11 ms |
2816 KB |
Output is correct |
6 |
Correct |
11 ms |
2816 KB |
Output is correct |
7 |
Correct |
11 ms |
2688 KB |
Output is correct |
8 |
Correct |
11 ms |
2816 KB |
Output is correct |
9 |
Correct |
10 ms |
2816 KB |
Output is correct |
10 |
Correct |
10 ms |
2816 KB |
Output is correct |
11 |
Correct |
11 ms |
2816 KB |
Output is correct |
12 |
Correct |
11 ms |
2816 KB |
Output is correct |
13 |
Correct |
10 ms |
2688 KB |
Output is correct |
14 |
Correct |
10 ms |
2816 KB |
Output is correct |
15 |
Correct |
10 ms |
2816 KB |
Output is correct |
16 |
Correct |
10 ms |
2688 KB |
Output is correct |
17 |
Correct |
11 ms |
2816 KB |
Output is correct |
18 |
Correct |
11 ms |
2816 KB |
Output is correct |
19 |
Correct |
10 ms |
2816 KB |
Output is correct |
20 |
Correct |
11 ms |
2816 KB |
Output is correct |
21 |
Correct |
10 ms |
2816 KB |
Output is correct |
22 |
Correct |
11 ms |
2816 KB |
Output is correct |
23 |
Correct |
13 ms |
2944 KB |
Output is correct |
24 |
Correct |
11 ms |
2688 KB |
Output is correct |
25 |
Correct |
11 ms |
2816 KB |
Output is correct |
26 |
Correct |
12 ms |
2816 KB |
Output is correct |
27 |
Correct |
10 ms |
2816 KB |
Output is correct |
28 |
Correct |
11 ms |
2688 KB |
Output is correct |
29 |
Correct |
12 ms |
2816 KB |
Output is correct |
30 |
Correct |
10 ms |
2816 KB |
Output is correct |
31 |
Correct |
12 ms |
2816 KB |
Output is correct |
32 |
Correct |
12 ms |
2816 KB |
Output is correct |
33 |
Correct |
10 ms |
2816 KB |
Output is correct |
34 |
Correct |
10 ms |
2816 KB |
Output is correct |
35 |
Correct |
11 ms |
2816 KB |
Output is correct |
36 |
Correct |
10 ms |
2688 KB |
Output is correct |
37 |
Correct |
11 ms |
2688 KB |
Output is correct |
38 |
Correct |
12 ms |
2816 KB |
Output is correct |
39 |
Correct |
12 ms |
2816 KB |
Output is correct |
40 |
Correct |
10 ms |
2688 KB |
Output is correct |
41 |
Correct |
10 ms |
2688 KB |
Output is correct |
42 |
Correct |
12 ms |
2688 KB |
Output is correct |
43 |
Correct |
11 ms |
2688 KB |
Output is correct |
44 |
Correct |
11 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2816 KB |
Output is correct |
5 |
Correct |
11 ms |
2816 KB |
Output is correct |
6 |
Correct |
11 ms |
2816 KB |
Output is correct |
7 |
Correct |
11 ms |
2688 KB |
Output is correct |
8 |
Correct |
11 ms |
2816 KB |
Output is correct |
9 |
Correct |
10 ms |
2816 KB |
Output is correct |
10 |
Correct |
10 ms |
2816 KB |
Output is correct |
11 |
Correct |
11 ms |
2816 KB |
Output is correct |
12 |
Correct |
11 ms |
2816 KB |
Output is correct |
13 |
Correct |
10 ms |
2688 KB |
Output is correct |
14 |
Correct |
10 ms |
2816 KB |
Output is correct |
15 |
Correct |
10 ms |
2816 KB |
Output is correct |
16 |
Correct |
10 ms |
2688 KB |
Output is correct |
17 |
Correct |
11 ms |
2816 KB |
Output is correct |
18 |
Correct |
11 ms |
2816 KB |
Output is correct |
19 |
Correct |
10 ms |
2816 KB |
Output is correct |
20 |
Correct |
11 ms |
2816 KB |
Output is correct |
21 |
Correct |
10 ms |
2816 KB |
Output is correct |
22 |
Correct |
11 ms |
2816 KB |
Output is correct |
23 |
Correct |
13 ms |
2944 KB |
Output is correct |
24 |
Correct |
11 ms |
2688 KB |
Output is correct |
25 |
Correct |
11 ms |
2816 KB |
Output is correct |
26 |
Correct |
12 ms |
2816 KB |
Output is correct |
27 |
Correct |
10 ms |
2816 KB |
Output is correct |
28 |
Correct |
11 ms |
2688 KB |
Output is correct |
29 |
Correct |
12 ms |
2816 KB |
Output is correct |
30 |
Correct |
10 ms |
2816 KB |
Output is correct |
31 |
Correct |
12 ms |
2816 KB |
Output is correct |
32 |
Correct |
12 ms |
2816 KB |
Output is correct |
33 |
Correct |
10 ms |
2816 KB |
Output is correct |
34 |
Correct |
10 ms |
2816 KB |
Output is correct |
35 |
Correct |
11 ms |
2816 KB |
Output is correct |
36 |
Correct |
10 ms |
2688 KB |
Output is correct |
37 |
Correct |
11 ms |
2688 KB |
Output is correct |
38 |
Correct |
12 ms |
2816 KB |
Output is correct |
39 |
Correct |
12 ms |
2816 KB |
Output is correct |
40 |
Correct |
10 ms |
2688 KB |
Output is correct |
41 |
Correct |
10 ms |
2688 KB |
Output is correct |
42 |
Correct |
12 ms |
2688 KB |
Output is correct |
43 |
Correct |
11 ms |
2688 KB |
Output is correct |
44 |
Correct |
11 ms |
2688 KB |
Output is correct |
45 |
Correct |
31 ms |
2816 KB |
Output is correct |
46 |
Correct |
479 ms |
3148 KB |
Output is correct |
47 |
Correct |
484 ms |
2944 KB |
Output is correct |
48 |
Correct |
496 ms |
3064 KB |
Output is correct |
49 |
Correct |
395 ms |
3132 KB |
Output is correct |
50 |
Correct |
395 ms |
3224 KB |
Output is correct |
51 |
Correct |
401 ms |
3320 KB |
Output is correct |
52 |
Correct |
381 ms |
2972 KB |
Output is correct |
53 |
Correct |
419 ms |
3064 KB |
Output is correct |
54 |
Correct |
420 ms |
3064 KB |
Output is correct |
55 |
Correct |
416 ms |
3036 KB |
Output is correct |
56 |
Correct |
406 ms |
3064 KB |
Output is correct |
57 |
Correct |
392 ms |
2936 KB |
Output is correct |
58 |
Correct |
485 ms |
2956 KB |
Output is correct |
59 |
Correct |
487 ms |
2940 KB |
Output is correct |
60 |
Correct |
473 ms |
2936 KB |
Output is correct |
61 |
Correct |
419 ms |
3064 KB |
Output is correct |
62 |
Correct |
417 ms |
2936 KB |
Output is correct |
63 |
Correct |
418 ms |
3108 KB |
Output is correct |
64 |
Correct |
512 ms |
2936 KB |
Output is correct |
65 |
Correct |
539 ms |
3068 KB |
Output is correct |
66 |
Correct |
489 ms |
2936 KB |
Output is correct |
67 |
Correct |
484 ms |
2936 KB |
Output is correct |
68 |
Correct |
473 ms |
3164 KB |
Output is correct |
69 |
Correct |
373 ms |
3080 KB |
Output is correct |
70 |
Correct |
481 ms |
2936 KB |
Output is correct |
71 |
Correct |
473 ms |
3064 KB |
Output is correct |
72 |
Correct |
384 ms |
2936 KB |
Output is correct |
73 |
Correct |
498 ms |
2940 KB |
Output is correct |
74 |
Correct |
477 ms |
3064 KB |
Output is correct |
75 |
Correct |
392 ms |
2936 KB |
Output is correct |
76 |
Correct |
372 ms |
2936 KB |
Output is correct |
77 |
Correct |
378 ms |
2936 KB |
Output is correct |
78 |
Correct |
454 ms |
2944 KB |
Output is correct |
79 |
Correct |
449 ms |
2936 KB |
Output is correct |
80 |
Correct |
460 ms |
3052 KB |
Output is correct |
81 |
Correct |
414 ms |
2944 KB |
Output is correct |
82 |
Correct |
447 ms |
3064 KB |
Output is correct |
83 |
Correct |
412 ms |
3064 KB |
Output is correct |
84 |
Correct |
471 ms |
2936 KB |
Output is correct |
85 |
Correct |
470 ms |
2936 KB |
Output is correct |
86 |
Correct |
462 ms |
3076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2688 KB |
Output is correct |
2 |
Correct |
4 ms |
2688 KB |
Output is correct |
3 |
Correct |
4 ms |
2816 KB |
Output is correct |
4 |
Correct |
7 ms |
2816 KB |
Output is correct |
5 |
Correct |
11 ms |
2816 KB |
Output is correct |
6 |
Correct |
11 ms |
2816 KB |
Output is correct |
7 |
Correct |
11 ms |
2688 KB |
Output is correct |
8 |
Correct |
11 ms |
2816 KB |
Output is correct |
9 |
Correct |
10 ms |
2816 KB |
Output is correct |
10 |
Correct |
10 ms |
2816 KB |
Output is correct |
11 |
Correct |
11 ms |
2816 KB |
Output is correct |
12 |
Correct |
11 ms |
2816 KB |
Output is correct |
13 |
Correct |
10 ms |
2688 KB |
Output is correct |
14 |
Correct |
10 ms |
2816 KB |
Output is correct |
15 |
Correct |
10 ms |
2816 KB |
Output is correct |
16 |
Correct |
10 ms |
2688 KB |
Output is correct |
17 |
Correct |
11 ms |
2816 KB |
Output is correct |
18 |
Correct |
11 ms |
2816 KB |
Output is correct |
19 |
Correct |
10 ms |
2816 KB |
Output is correct |
20 |
Correct |
11 ms |
2816 KB |
Output is correct |
21 |
Correct |
10 ms |
2816 KB |
Output is correct |
22 |
Correct |
11 ms |
2816 KB |
Output is correct |
23 |
Correct |
13 ms |
2944 KB |
Output is correct |
24 |
Correct |
11 ms |
2688 KB |
Output is correct |
25 |
Correct |
11 ms |
2816 KB |
Output is correct |
26 |
Correct |
12 ms |
2816 KB |
Output is correct |
27 |
Correct |
10 ms |
2816 KB |
Output is correct |
28 |
Correct |
11 ms |
2688 KB |
Output is correct |
29 |
Correct |
12 ms |
2816 KB |
Output is correct |
30 |
Correct |
10 ms |
2816 KB |
Output is correct |
31 |
Correct |
12 ms |
2816 KB |
Output is correct |
32 |
Correct |
12 ms |
2816 KB |
Output is correct |
33 |
Correct |
10 ms |
2816 KB |
Output is correct |
34 |
Correct |
10 ms |
2816 KB |
Output is correct |
35 |
Correct |
11 ms |
2816 KB |
Output is correct |
36 |
Correct |
10 ms |
2688 KB |
Output is correct |
37 |
Correct |
11 ms |
2688 KB |
Output is correct |
38 |
Correct |
12 ms |
2816 KB |
Output is correct |
39 |
Correct |
12 ms |
2816 KB |
Output is correct |
40 |
Correct |
10 ms |
2688 KB |
Output is correct |
41 |
Correct |
10 ms |
2688 KB |
Output is correct |
42 |
Correct |
12 ms |
2688 KB |
Output is correct |
43 |
Correct |
11 ms |
2688 KB |
Output is correct |
44 |
Correct |
11 ms |
2688 KB |
Output is correct |
45 |
Correct |
31 ms |
2816 KB |
Output is correct |
46 |
Correct |
479 ms |
3148 KB |
Output is correct |
47 |
Correct |
484 ms |
2944 KB |
Output is correct |
48 |
Correct |
496 ms |
3064 KB |
Output is correct |
49 |
Correct |
395 ms |
3132 KB |
Output is correct |
50 |
Correct |
395 ms |
3224 KB |
Output is correct |
51 |
Correct |
401 ms |
3320 KB |
Output is correct |
52 |
Correct |
381 ms |
2972 KB |
Output is correct |
53 |
Correct |
419 ms |
3064 KB |
Output is correct |
54 |
Correct |
420 ms |
3064 KB |
Output is correct |
55 |
Correct |
416 ms |
3036 KB |
Output is correct |
56 |
Correct |
406 ms |
3064 KB |
Output is correct |
57 |
Correct |
392 ms |
2936 KB |
Output is correct |
58 |
Correct |
485 ms |
2956 KB |
Output is correct |
59 |
Correct |
487 ms |
2940 KB |
Output is correct |
60 |
Correct |
473 ms |
2936 KB |
Output is correct |
61 |
Correct |
419 ms |
3064 KB |
Output is correct |
62 |
Correct |
417 ms |
2936 KB |
Output is correct |
63 |
Correct |
418 ms |
3108 KB |
Output is correct |
64 |
Correct |
512 ms |
2936 KB |
Output is correct |
65 |
Correct |
539 ms |
3068 KB |
Output is correct |
66 |
Correct |
489 ms |
2936 KB |
Output is correct |
67 |
Correct |
484 ms |
2936 KB |
Output is correct |
68 |
Correct |
473 ms |
3164 KB |
Output is correct |
69 |
Correct |
373 ms |
3080 KB |
Output is correct |
70 |
Correct |
481 ms |
2936 KB |
Output is correct |
71 |
Correct |
473 ms |
3064 KB |
Output is correct |
72 |
Correct |
384 ms |
2936 KB |
Output is correct |
73 |
Correct |
498 ms |
2940 KB |
Output is correct |
74 |
Correct |
477 ms |
3064 KB |
Output is correct |
75 |
Correct |
392 ms |
2936 KB |
Output is correct |
76 |
Correct |
372 ms |
2936 KB |
Output is correct |
77 |
Correct |
378 ms |
2936 KB |
Output is correct |
78 |
Correct |
454 ms |
2944 KB |
Output is correct |
79 |
Correct |
449 ms |
2936 KB |
Output is correct |
80 |
Correct |
460 ms |
3052 KB |
Output is correct |
81 |
Correct |
414 ms |
2944 KB |
Output is correct |
82 |
Correct |
447 ms |
3064 KB |
Output is correct |
83 |
Correct |
412 ms |
3064 KB |
Output is correct |
84 |
Correct |
471 ms |
2936 KB |
Output is correct |
85 |
Correct |
470 ms |
2936 KB |
Output is correct |
86 |
Correct |
462 ms |
3076 KB |
Output is correct |
87 |
Execution timed out |
2056 ms |
3320 KB |
Time limit exceeded |
88 |
Halted |
0 ms |
0 KB |
- |