#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e5;
const int P=(1<<17);
int tab[N+10];
int que[N+10];
vector<int> e[N+10];
int par[N+10];
int dep[N+10];
int up[N+10];
int pre[N+10];
int post[N+10];
int pot;
pair<int,int> tr[2*P+10];
long long fen[P+10];
void upd(int x,pair<int,int> c)
{
for(x+=pot-1;x>=1;x/=2)
tr[x]=max(tr[x],c);
return;
}
pair<int,int> read(int l,int r)
{
pair<int,int> ans={0,1};
for(l+=pot-1,r+=pot-1;l<=r;l/=2,r/=2)
{
if(l%2==1)
ans=max(ans,tr[l++]);
if(r%2==0)
ans=max(ans,tr[r--]);
}
return ans;
}
void dfs_pre(int x)
{
static int nr=0;
pre[x]=++nr;
dep[x]=dep[par[x]]+1;
for(auto v:e[x])
dfs_pre(v);
post[x]=nr;
return;
}
int g(int x)
{
return read(pre[x],post[x]).se;
}
int lsb(int x)
{
return x&(-x);
}
long long read_inv(int x)
{
long long ans=0;
for(;x>0;x-=lsb(x))
ans+=fen[x];
return ans;
}
void add_inv(int x,long long c)
{
for(;x<=pot;x+=lsb(x))
fen[x]+=c;
return;
}
long long solve_inv(vector<tuple<int,int,int>> &vec)
{
sort(vec.begin(),vec.end());
long long ans=0;
for(auto [a,b,c]:vec)
{
b=-b;
ans+=read_inv(b-1)*c;
add_inv(b,c);
}
for(auto [a,b,c]:vec)
add_inv(-b,-c);
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>tab[i];
for(int i=1;i<n;i++)
{
int a,b;
cin>>a>>b;
e[a].push_back(b);
par[b]=a;
que[i]=b;
}
dfs_pre(1);
for(pot=1;pot<n;pot*=2);
for(int i=1;i<=2*pot;i++)
tr[i]={0,1};
for(int qi=1;qi<n;qi++)
{
vector<tuple<int,int,int>> vec;
for(int x=par[que[qi]];x>0;)
{
int gr=g(x);
//cerr<<qi<<" "<<x<<" "<<gr<<"\n";
vec.push_back({tab[gr],-vec.size()-1,dep[x]-dep[up[gr]]});
swap(x,up[gr]);
}
cout<<solve_inv(vec)<<"\n";
upd(pre[que[qi]],{qi,que[qi]});
up[que[qi]]=0;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2672 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2676 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
2 ms |
2636 KB |
Output is correct |
18 |
Correct |
3 ms |
2680 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2752 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
2 ms |
2672 KB |
Output is correct |
24 |
Correct |
2 ms |
2676 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2672 KB |
Output is correct |
31 |
Correct |
2 ms |
2672 KB |
Output is correct |
32 |
Correct |
2 ms |
2724 KB |
Output is correct |
33 |
Correct |
2 ms |
2636 KB |
Output is correct |
34 |
Correct |
2 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
2636 KB |
Output is correct |
37 |
Correct |
2 ms |
2636 KB |
Output is correct |
38 |
Correct |
2 ms |
2636 KB |
Output is correct |
39 |
Correct |
2 ms |
2636 KB |
Output is correct |
40 |
Correct |
2 ms |
2672 KB |
Output is correct |
41 |
Correct |
2 ms |
2636 KB |
Output is correct |
42 |
Correct |
2 ms |
2672 KB |
Output is correct |
43 |
Correct |
2 ms |
2680 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2672 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2676 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
2 ms |
2636 KB |
Output is correct |
18 |
Correct |
3 ms |
2680 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2752 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
2 ms |
2672 KB |
Output is correct |
24 |
Correct |
2 ms |
2676 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2672 KB |
Output is correct |
31 |
Correct |
2 ms |
2672 KB |
Output is correct |
32 |
Correct |
2 ms |
2724 KB |
Output is correct |
33 |
Correct |
2 ms |
2636 KB |
Output is correct |
34 |
Correct |
2 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
2636 KB |
Output is correct |
37 |
Correct |
2 ms |
2636 KB |
Output is correct |
38 |
Correct |
2 ms |
2636 KB |
Output is correct |
39 |
Correct |
2 ms |
2636 KB |
Output is correct |
40 |
Correct |
2 ms |
2672 KB |
Output is correct |
41 |
Correct |
2 ms |
2636 KB |
Output is correct |
42 |
Correct |
2 ms |
2672 KB |
Output is correct |
43 |
Correct |
2 ms |
2680 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
3 ms |
2684 KB |
Output is correct |
46 |
Correct |
6 ms |
2944 KB |
Output is correct |
47 |
Correct |
6 ms |
3020 KB |
Output is correct |
48 |
Correct |
7 ms |
2936 KB |
Output is correct |
49 |
Correct |
4 ms |
3200 KB |
Output is correct |
50 |
Correct |
4 ms |
3148 KB |
Output is correct |
51 |
Correct |
4 ms |
3196 KB |
Output is correct |
52 |
Correct |
5 ms |
3020 KB |
Output is correct |
53 |
Correct |
6 ms |
3148 KB |
Output is correct |
54 |
Correct |
6 ms |
3124 KB |
Output is correct |
55 |
Correct |
4 ms |
3148 KB |
Output is correct |
56 |
Correct |
4 ms |
3020 KB |
Output is correct |
57 |
Correct |
7 ms |
2932 KB |
Output is correct |
58 |
Correct |
9 ms |
2988 KB |
Output is correct |
59 |
Correct |
9 ms |
3020 KB |
Output is correct |
60 |
Correct |
8 ms |
3008 KB |
Output is correct |
61 |
Correct |
5 ms |
3020 KB |
Output is correct |
62 |
Correct |
4 ms |
3068 KB |
Output is correct |
63 |
Correct |
4 ms |
3020 KB |
Output is correct |
64 |
Correct |
5 ms |
3020 KB |
Output is correct |
65 |
Correct |
7 ms |
3104 KB |
Output is correct |
66 |
Correct |
7 ms |
3032 KB |
Output is correct |
67 |
Correct |
6 ms |
2936 KB |
Output is correct |
68 |
Correct |
4 ms |
3148 KB |
Output is correct |
69 |
Correct |
4 ms |
3076 KB |
Output is correct |
70 |
Correct |
4 ms |
3020 KB |
Output is correct |
71 |
Correct |
4 ms |
3056 KB |
Output is correct |
72 |
Correct |
8 ms |
3020 KB |
Output is correct |
73 |
Correct |
9 ms |
2988 KB |
Output is correct |
74 |
Correct |
5 ms |
3060 KB |
Output is correct |
75 |
Correct |
5 ms |
3068 KB |
Output is correct |
76 |
Correct |
4 ms |
2940 KB |
Output is correct |
77 |
Correct |
4 ms |
3020 KB |
Output is correct |
78 |
Correct |
4 ms |
2940 KB |
Output is correct |
79 |
Correct |
4 ms |
3020 KB |
Output is correct |
80 |
Correct |
4 ms |
2940 KB |
Output is correct |
81 |
Correct |
4 ms |
3020 KB |
Output is correct |
82 |
Correct |
5 ms |
3020 KB |
Output is correct |
83 |
Correct |
5 ms |
3020 KB |
Output is correct |
84 |
Correct |
5 ms |
2936 KB |
Output is correct |
85 |
Correct |
4 ms |
3020 KB |
Output is correct |
86 |
Correct |
4 ms |
3020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2636 KB |
Output is correct |
2 |
Correct |
2 ms |
2672 KB |
Output is correct |
3 |
Correct |
2 ms |
2676 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
2 ms |
2684 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
2 ms |
2636 KB |
Output is correct |
8 |
Correct |
3 ms |
2764 KB |
Output is correct |
9 |
Correct |
2 ms |
2764 KB |
Output is correct |
10 |
Correct |
2 ms |
2672 KB |
Output is correct |
11 |
Correct |
2 ms |
2636 KB |
Output is correct |
12 |
Correct |
2 ms |
2676 KB |
Output is correct |
13 |
Correct |
2 ms |
2636 KB |
Output is correct |
14 |
Correct |
2 ms |
2636 KB |
Output is correct |
15 |
Correct |
2 ms |
2676 KB |
Output is correct |
16 |
Correct |
2 ms |
2636 KB |
Output is correct |
17 |
Correct |
2 ms |
2636 KB |
Output is correct |
18 |
Correct |
3 ms |
2680 KB |
Output is correct |
19 |
Correct |
2 ms |
2636 KB |
Output is correct |
20 |
Correct |
2 ms |
2636 KB |
Output is correct |
21 |
Correct |
2 ms |
2752 KB |
Output is correct |
22 |
Correct |
2 ms |
2636 KB |
Output is correct |
23 |
Correct |
2 ms |
2672 KB |
Output is correct |
24 |
Correct |
2 ms |
2676 KB |
Output is correct |
25 |
Correct |
3 ms |
2636 KB |
Output is correct |
26 |
Correct |
2 ms |
2764 KB |
Output is correct |
27 |
Correct |
2 ms |
2636 KB |
Output is correct |
28 |
Correct |
2 ms |
2636 KB |
Output is correct |
29 |
Correct |
2 ms |
2636 KB |
Output is correct |
30 |
Correct |
2 ms |
2672 KB |
Output is correct |
31 |
Correct |
2 ms |
2672 KB |
Output is correct |
32 |
Correct |
2 ms |
2724 KB |
Output is correct |
33 |
Correct |
2 ms |
2636 KB |
Output is correct |
34 |
Correct |
2 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2636 KB |
Output is correct |
36 |
Correct |
2 ms |
2636 KB |
Output is correct |
37 |
Correct |
2 ms |
2636 KB |
Output is correct |
38 |
Correct |
2 ms |
2636 KB |
Output is correct |
39 |
Correct |
2 ms |
2636 KB |
Output is correct |
40 |
Correct |
2 ms |
2672 KB |
Output is correct |
41 |
Correct |
2 ms |
2636 KB |
Output is correct |
42 |
Correct |
2 ms |
2672 KB |
Output is correct |
43 |
Correct |
2 ms |
2680 KB |
Output is correct |
44 |
Correct |
2 ms |
2636 KB |
Output is correct |
45 |
Correct |
3 ms |
2684 KB |
Output is correct |
46 |
Correct |
6 ms |
2944 KB |
Output is correct |
47 |
Correct |
6 ms |
3020 KB |
Output is correct |
48 |
Correct |
7 ms |
2936 KB |
Output is correct |
49 |
Correct |
4 ms |
3200 KB |
Output is correct |
50 |
Correct |
4 ms |
3148 KB |
Output is correct |
51 |
Correct |
4 ms |
3196 KB |
Output is correct |
52 |
Correct |
5 ms |
3020 KB |
Output is correct |
53 |
Correct |
6 ms |
3148 KB |
Output is correct |
54 |
Correct |
6 ms |
3124 KB |
Output is correct |
55 |
Correct |
4 ms |
3148 KB |
Output is correct |
56 |
Correct |
4 ms |
3020 KB |
Output is correct |
57 |
Correct |
7 ms |
2932 KB |
Output is correct |
58 |
Correct |
9 ms |
2988 KB |
Output is correct |
59 |
Correct |
9 ms |
3020 KB |
Output is correct |
60 |
Correct |
8 ms |
3008 KB |
Output is correct |
61 |
Correct |
5 ms |
3020 KB |
Output is correct |
62 |
Correct |
4 ms |
3068 KB |
Output is correct |
63 |
Correct |
4 ms |
3020 KB |
Output is correct |
64 |
Correct |
5 ms |
3020 KB |
Output is correct |
65 |
Correct |
7 ms |
3104 KB |
Output is correct |
66 |
Correct |
7 ms |
3032 KB |
Output is correct |
67 |
Correct |
6 ms |
2936 KB |
Output is correct |
68 |
Correct |
4 ms |
3148 KB |
Output is correct |
69 |
Correct |
4 ms |
3076 KB |
Output is correct |
70 |
Correct |
4 ms |
3020 KB |
Output is correct |
71 |
Correct |
4 ms |
3056 KB |
Output is correct |
72 |
Correct |
8 ms |
3020 KB |
Output is correct |
73 |
Correct |
9 ms |
2988 KB |
Output is correct |
74 |
Correct |
5 ms |
3060 KB |
Output is correct |
75 |
Correct |
5 ms |
3068 KB |
Output is correct |
76 |
Correct |
4 ms |
2940 KB |
Output is correct |
77 |
Correct |
4 ms |
3020 KB |
Output is correct |
78 |
Correct |
4 ms |
2940 KB |
Output is correct |
79 |
Correct |
4 ms |
3020 KB |
Output is correct |
80 |
Correct |
4 ms |
2940 KB |
Output is correct |
81 |
Correct |
4 ms |
3020 KB |
Output is correct |
82 |
Correct |
5 ms |
3020 KB |
Output is correct |
83 |
Correct |
5 ms |
3020 KB |
Output is correct |
84 |
Correct |
5 ms |
2936 KB |
Output is correct |
85 |
Correct |
4 ms |
3020 KB |
Output is correct |
86 |
Correct |
4 ms |
3020 KB |
Output is correct |
87 |
Correct |
14 ms |
3624 KB |
Output is correct |
88 |
Correct |
44 ms |
5228 KB |
Output is correct |
89 |
Correct |
236 ms |
11476 KB |
Output is correct |
90 |
Correct |
179 ms |
11484 KB |
Output is correct |
91 |
Correct |
180 ms |
11444 KB |
Output is correct |
92 |
Correct |
72 ms |
16120 KB |
Output is correct |
93 |
Correct |
90 ms |
15992 KB |
Output is correct |
94 |
Correct |
75 ms |
15980 KB |
Output is correct |
95 |
Correct |
80 ms |
13560 KB |
Output is correct |
96 |
Correct |
93 ms |
14008 KB |
Output is correct |
97 |
Correct |
79 ms |
14016 KB |
Output is correct |
98 |
Correct |
81 ms |
13984 KB |
Output is correct |
99 |
Correct |
90 ms |
13032 KB |
Output is correct |
100 |
Correct |
320 ms |
11520 KB |
Output is correct |
101 |
Correct |
348 ms |
11932 KB |
Output is correct |
102 |
Correct |
364 ms |
11948 KB |
Output is correct |
103 |
Correct |
359 ms |
11836 KB |
Output is correct |
104 |
Correct |
93 ms |
12996 KB |
Output is correct |
105 |
Correct |
101 ms |
12948 KB |
Output is correct |
106 |
Correct |
96 ms |
12996 KB |
Output is correct |
107 |
Correct |
223 ms |
10616 KB |
Output is correct |
108 |
Correct |
177 ms |
10784 KB |
Output is correct |
109 |
Correct |
216 ms |
11144 KB |
Output is correct |
110 |
Correct |
67 ms |
15428 KB |
Output is correct |
111 |
Correct |
79 ms |
13592 KB |
Output is correct |
112 |
Correct |
75 ms |
13308 KB |
Output is correct |
113 |
Correct |
87 ms |
12380 KB |
Output is correct |
114 |
Correct |
319 ms |
11588 KB |
Output is correct |
115 |
Correct |
388 ms |
11332 KB |
Output is correct |
116 |
Correct |
89 ms |
12408 KB |
Output is correct |
117 |
Correct |
78 ms |
11844 KB |
Output is correct |
118 |
Correct |
75 ms |
11680 KB |
Output is correct |
119 |
Correct |
76 ms |
11604 KB |
Output is correct |
120 |
Correct |
82 ms |
11468 KB |
Output is correct |
121 |
Correct |
80 ms |
11288 KB |
Output is correct |
122 |
Correct |
84 ms |
11292 KB |
Output is correct |
123 |
Correct |
94 ms |
11904 KB |
Output is correct |
124 |
Correct |
96 ms |
11628 KB |
Output is correct |
125 |
Correct |
98 ms |
11516 KB |
Output is correct |
126 |
Correct |
104 ms |
11636 KB |
Output is correct |
127 |
Correct |
95 ms |
11392 KB |
Output is correct |
128 |
Correct |
94 ms |
11252 KB |
Output is correct |