#include <bits/stdc++.h>
using namespace std;
int n;
int arr[100000];
vector<int> adj[100000];
typedef pair<int,int> P;
P edge[100000];
int p[100000];
int ind[100000];
int siz[100000];
int f=0;
int depth[100000];
int save[100000];
int par[100000][17];
void dfs(int v,int prev) {
ind[v]=f++;
siz[v]=1;
p[v]=prev;
for(int i=0;i<adj[v].size();i++) {
int nt=adj[v][i];
if (nt==prev) {
continue;
}
depth[nt]=depth[v]+1;
dfs(nt,v);
siz[v]+=siz[nt];
}
}
const int sz=131072;
P seg[sz*2];
P pmax(P a,P b) {
return a<b?b:a;
}
P sum(int l,int r,int node=1,int nodel=0,int noder=sz-1) {
if (r<nodel||l>noder) {
return P(0,0);
}
if (l<=nodel&&noder<=r) {
return seg[node];
}
int mid=(nodel+noder)/2;
return pmax(sum(l,r,node*2,nodel,mid),sum(l,r,node*2+1,mid+1,noder));
}
void update(int i,P val) {
i+=sz;
seg[i]=val;
while (i>1) {
i/=2;
seg[i]=pmax(seg[i*2],seg[i*2+1]);
}
}
struct SegmentTree {
vector<int> seg;
int sz=1;
void init(int x) {
while (sz<x) {
sz*=2;
}
seg.resize(sz*2);
}
int sum(int l,int r,int node=1,int nodel=0,int noder=0) {
if (node==1) {
noder=sz-1;
}
if (r<nodel||l>noder) {
return 0;
}
if (l<=nodel&&noder<=r) {
return seg[node];
}
int mid=(nodel+noder)/2;
return sum(l,r,node*2,nodel,mid)+sum(l,r,node*2+1,mid+1,noder);
}
void update(int i,int val) {
i+=sz;
seg[i]+=val;
while (i>1) {
i/=2;
seg[i]=seg[i*2]+seg[i*2+1];
}
}
};
long long cntinv(vector<P> v) {
vector<int> press;
for(int i=0;i<v.size();i++) {
//printf("%d %d\n",v[i].first,v[i].second);
press.push_back(v[i].first);
}
sort(press.begin(),press.end());
press.erase(unique(press.begin(),press.end()),press.end());
SegmentTree st;
st.init(press.size());
long long ret=0;
for(int i=0;i<v.size();i++) {
v[i].first=lower_bound(press.begin(),press.end(),v[i].first)-press.begin();
if (v[i].first!=0)
ret+=1LL*v[i].second*st.sum(0,v[i].first-1);
st.update(v[i].first,v[i].second);
}
return ret;
}
int main(void) {
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
vector<int> press;
for(int i=0;i<n;i++) {
press.push_back(arr[i]);
}
sort(press.begin(),press.end());
press.erase(unique(press.begin(),press.end()),press.end());
for(int i=0;i<n;i++) {
arr[i]=lower_bound(press.begin(),press.end(),arr[i])-press.begin();
}
for(int i=1;i<n;i++) {
int u,v;
scanf("%d %d",&u,&v);
u--;
v--;
edge[i]=P(u,v);
adj[u].push_back(v);
adj[v].push_back(u);
}
dfs(0,-1);
memset(par,-1,sizeof(par));
for(int i=0;i<n;i++) {
par[i][0]=p[i];
}
for(int j=1;j<17;j++) {
for(int i=0;i<n;i++) {
if (par[i][j-1]!=-1) {
par[i][j]=par[par[i][j-1]][j-1];
}
}
}
for(int i=1;i<n;i++) {
int now=edge[i].first;
vector<P> vec;
while (now!=-1) {
int rec=now;
for(int j=16;j>=0;j--) {
int nt=par[rec][j];
if (nt==-1) {
continue;
}
if (sum(ind[nt],ind[nt]+siz[nt]-1).second==sum(ind[now],ind[now]+siz[now]-1).second) {
rec=nt;
}
}
int deep=sum(ind[now],ind[now]+siz[now]-1).second;
vec.push_back(P(arr[deep],depth[now]-depth[rec]+1));
now=p[rec];
}
save[edge[i].second]=save[edge[i].first];
update(ind[edge[i].second],P(i,edge[i].second));
printf("%lld\n",cntinv(vec));
}
}
Compilation message
construction.cpp: In function 'void dfs(int, int)':
construction.cpp:21:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
21 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
construction.cpp: In function 'long long int cntinv(std::vector<std::pair<int, int> >)':
construction.cpp:93:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for(int i=0;i<v.size();i++) {
| ~^~~~~~~~~
construction.cpp:102:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i=0;i<v.size();i++) {
| ~^~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:112:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
112 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
construction.cpp:114:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | scanf("%d",&arr[i]);
| ~~~~~^~~~~~~~~~~~~~
construction.cpp:127:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | scanf("%d %d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9292 KB |
Output is correct |
2 |
Correct |
5 ms |
9292 KB |
Output is correct |
3 |
Correct |
5 ms |
9292 KB |
Output is correct |
4 |
Correct |
6 ms |
9292 KB |
Output is correct |
5 |
Correct |
8 ms |
9420 KB |
Output is correct |
6 |
Correct |
7 ms |
9420 KB |
Output is correct |
7 |
Correct |
8 ms |
9420 KB |
Output is correct |
8 |
Correct |
7 ms |
9452 KB |
Output is correct |
9 |
Correct |
7 ms |
9544 KB |
Output is correct |
10 |
Correct |
7 ms |
9452 KB |
Output is correct |
11 |
Correct |
8 ms |
9440 KB |
Output is correct |
12 |
Correct |
7 ms |
9420 KB |
Output is correct |
13 |
Correct |
8 ms |
9420 KB |
Output is correct |
14 |
Correct |
8 ms |
9420 KB |
Output is correct |
15 |
Correct |
9 ms |
9420 KB |
Output is correct |
16 |
Correct |
10 ms |
9428 KB |
Output is correct |
17 |
Correct |
10 ms |
9324 KB |
Output is correct |
18 |
Correct |
10 ms |
9420 KB |
Output is correct |
19 |
Correct |
8 ms |
9420 KB |
Output is correct |
20 |
Correct |
8 ms |
9420 KB |
Output is correct |
21 |
Correct |
8 ms |
9420 KB |
Output is correct |
22 |
Correct |
8 ms |
9420 KB |
Output is correct |
23 |
Correct |
7 ms |
9420 KB |
Output is correct |
24 |
Correct |
8 ms |
9420 KB |
Output is correct |
25 |
Correct |
8 ms |
9420 KB |
Output is correct |
26 |
Correct |
7 ms |
9444 KB |
Output is correct |
27 |
Correct |
8 ms |
9444 KB |
Output is correct |
28 |
Correct |
8 ms |
9420 KB |
Output is correct |
29 |
Correct |
8 ms |
9320 KB |
Output is correct |
30 |
Correct |
10 ms |
9424 KB |
Output is correct |
31 |
Correct |
10 ms |
9416 KB |
Output is correct |
32 |
Correct |
8 ms |
9316 KB |
Output is correct |
33 |
Correct |
8 ms |
9428 KB |
Output is correct |
34 |
Correct |
7 ms |
9420 KB |
Output is correct |
35 |
Correct |
9 ms |
9424 KB |
Output is correct |
36 |
Correct |
7 ms |
9324 KB |
Output is correct |
37 |
Correct |
7 ms |
9316 KB |
Output is correct |
38 |
Correct |
8 ms |
9324 KB |
Output is correct |
39 |
Correct |
7 ms |
9420 KB |
Output is correct |
40 |
Correct |
7 ms |
9420 KB |
Output is correct |
41 |
Correct |
7 ms |
9428 KB |
Output is correct |
42 |
Correct |
7 ms |
9420 KB |
Output is correct |
43 |
Correct |
8 ms |
9424 KB |
Output is correct |
44 |
Correct |
7 ms |
9320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9292 KB |
Output is correct |
2 |
Correct |
5 ms |
9292 KB |
Output is correct |
3 |
Correct |
5 ms |
9292 KB |
Output is correct |
4 |
Correct |
6 ms |
9292 KB |
Output is correct |
5 |
Correct |
8 ms |
9420 KB |
Output is correct |
6 |
Correct |
7 ms |
9420 KB |
Output is correct |
7 |
Correct |
8 ms |
9420 KB |
Output is correct |
8 |
Correct |
7 ms |
9452 KB |
Output is correct |
9 |
Correct |
7 ms |
9544 KB |
Output is correct |
10 |
Correct |
7 ms |
9452 KB |
Output is correct |
11 |
Correct |
8 ms |
9440 KB |
Output is correct |
12 |
Correct |
7 ms |
9420 KB |
Output is correct |
13 |
Correct |
8 ms |
9420 KB |
Output is correct |
14 |
Correct |
8 ms |
9420 KB |
Output is correct |
15 |
Correct |
9 ms |
9420 KB |
Output is correct |
16 |
Correct |
10 ms |
9428 KB |
Output is correct |
17 |
Correct |
10 ms |
9324 KB |
Output is correct |
18 |
Correct |
10 ms |
9420 KB |
Output is correct |
19 |
Correct |
8 ms |
9420 KB |
Output is correct |
20 |
Correct |
8 ms |
9420 KB |
Output is correct |
21 |
Correct |
8 ms |
9420 KB |
Output is correct |
22 |
Correct |
8 ms |
9420 KB |
Output is correct |
23 |
Correct |
7 ms |
9420 KB |
Output is correct |
24 |
Correct |
8 ms |
9420 KB |
Output is correct |
25 |
Correct |
8 ms |
9420 KB |
Output is correct |
26 |
Correct |
7 ms |
9444 KB |
Output is correct |
27 |
Correct |
8 ms |
9444 KB |
Output is correct |
28 |
Correct |
8 ms |
9420 KB |
Output is correct |
29 |
Correct |
8 ms |
9320 KB |
Output is correct |
30 |
Correct |
10 ms |
9424 KB |
Output is correct |
31 |
Correct |
10 ms |
9416 KB |
Output is correct |
32 |
Correct |
8 ms |
9316 KB |
Output is correct |
33 |
Correct |
8 ms |
9428 KB |
Output is correct |
34 |
Correct |
7 ms |
9420 KB |
Output is correct |
35 |
Correct |
9 ms |
9424 KB |
Output is correct |
36 |
Correct |
7 ms |
9324 KB |
Output is correct |
37 |
Correct |
7 ms |
9316 KB |
Output is correct |
38 |
Correct |
8 ms |
9324 KB |
Output is correct |
39 |
Correct |
7 ms |
9420 KB |
Output is correct |
40 |
Correct |
7 ms |
9420 KB |
Output is correct |
41 |
Correct |
7 ms |
9428 KB |
Output is correct |
42 |
Correct |
7 ms |
9420 KB |
Output is correct |
43 |
Correct |
8 ms |
9424 KB |
Output is correct |
44 |
Correct |
7 ms |
9320 KB |
Output is correct |
45 |
Correct |
11 ms |
9420 KB |
Output is correct |
46 |
Correct |
38 ms |
9772 KB |
Output is correct |
47 |
Correct |
40 ms |
9784 KB |
Output is correct |
48 |
Correct |
32 ms |
9784 KB |
Output is correct |
49 |
Correct |
21 ms |
9984 KB |
Output is correct |
50 |
Correct |
22 ms |
10080 KB |
Output is correct |
51 |
Correct |
21 ms |
10064 KB |
Output is correct |
52 |
Correct |
33 ms |
9932 KB |
Output is correct |
53 |
Correct |
34 ms |
9924 KB |
Output is correct |
54 |
Correct |
34 ms |
9904 KB |
Output is correct |
55 |
Correct |
34 ms |
9956 KB |
Output is correct |
56 |
Correct |
35 ms |
9916 KB |
Output is correct |
57 |
Correct |
72 ms |
9768 KB |
Output is correct |
58 |
Correct |
74 ms |
9776 KB |
Output is correct |
59 |
Correct |
87 ms |
9712 KB |
Output is correct |
60 |
Correct |
74 ms |
9676 KB |
Output is correct |
61 |
Correct |
36 ms |
9892 KB |
Output is correct |
62 |
Correct |
36 ms |
9804 KB |
Output is correct |
63 |
Correct |
36 ms |
9884 KB |
Output is correct |
64 |
Correct |
36 ms |
9676 KB |
Output is correct |
65 |
Correct |
35 ms |
9760 KB |
Output is correct |
66 |
Correct |
39 ms |
9760 KB |
Output is correct |
67 |
Correct |
34 ms |
9768 KB |
Output is correct |
68 |
Correct |
21 ms |
9972 KB |
Output is correct |
69 |
Correct |
34 ms |
9952 KB |
Output is correct |
70 |
Correct |
33 ms |
9896 KB |
Output is correct |
71 |
Correct |
34 ms |
9856 KB |
Output is correct |
72 |
Correct |
72 ms |
9768 KB |
Output is correct |
73 |
Correct |
73 ms |
9748 KB |
Output is correct |
74 |
Correct |
36 ms |
9920 KB |
Output is correct |
75 |
Correct |
33 ms |
9840 KB |
Output is correct |
76 |
Correct |
30 ms |
9828 KB |
Output is correct |
77 |
Correct |
29 ms |
9772 KB |
Output is correct |
78 |
Correct |
31 ms |
9804 KB |
Output is correct |
79 |
Correct |
29 ms |
9676 KB |
Output is correct |
80 |
Correct |
28 ms |
9740 KB |
Output is correct |
81 |
Correct |
34 ms |
9840 KB |
Output is correct |
82 |
Correct |
35 ms |
9792 KB |
Output is correct |
83 |
Correct |
31 ms |
9776 KB |
Output is correct |
84 |
Correct |
33 ms |
9804 KB |
Output is correct |
85 |
Correct |
33 ms |
9808 KB |
Output is correct |
86 |
Correct |
31 ms |
9784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9292 KB |
Output is correct |
2 |
Correct |
5 ms |
9292 KB |
Output is correct |
3 |
Correct |
5 ms |
9292 KB |
Output is correct |
4 |
Correct |
6 ms |
9292 KB |
Output is correct |
5 |
Correct |
8 ms |
9420 KB |
Output is correct |
6 |
Correct |
7 ms |
9420 KB |
Output is correct |
7 |
Correct |
8 ms |
9420 KB |
Output is correct |
8 |
Correct |
7 ms |
9452 KB |
Output is correct |
9 |
Correct |
7 ms |
9544 KB |
Output is correct |
10 |
Correct |
7 ms |
9452 KB |
Output is correct |
11 |
Correct |
8 ms |
9440 KB |
Output is correct |
12 |
Correct |
7 ms |
9420 KB |
Output is correct |
13 |
Correct |
8 ms |
9420 KB |
Output is correct |
14 |
Correct |
8 ms |
9420 KB |
Output is correct |
15 |
Correct |
9 ms |
9420 KB |
Output is correct |
16 |
Correct |
10 ms |
9428 KB |
Output is correct |
17 |
Correct |
10 ms |
9324 KB |
Output is correct |
18 |
Correct |
10 ms |
9420 KB |
Output is correct |
19 |
Correct |
8 ms |
9420 KB |
Output is correct |
20 |
Correct |
8 ms |
9420 KB |
Output is correct |
21 |
Correct |
8 ms |
9420 KB |
Output is correct |
22 |
Correct |
8 ms |
9420 KB |
Output is correct |
23 |
Correct |
7 ms |
9420 KB |
Output is correct |
24 |
Correct |
8 ms |
9420 KB |
Output is correct |
25 |
Correct |
8 ms |
9420 KB |
Output is correct |
26 |
Correct |
7 ms |
9444 KB |
Output is correct |
27 |
Correct |
8 ms |
9444 KB |
Output is correct |
28 |
Correct |
8 ms |
9420 KB |
Output is correct |
29 |
Correct |
8 ms |
9320 KB |
Output is correct |
30 |
Correct |
10 ms |
9424 KB |
Output is correct |
31 |
Correct |
10 ms |
9416 KB |
Output is correct |
32 |
Correct |
8 ms |
9316 KB |
Output is correct |
33 |
Correct |
8 ms |
9428 KB |
Output is correct |
34 |
Correct |
7 ms |
9420 KB |
Output is correct |
35 |
Correct |
9 ms |
9424 KB |
Output is correct |
36 |
Correct |
7 ms |
9324 KB |
Output is correct |
37 |
Correct |
7 ms |
9316 KB |
Output is correct |
38 |
Correct |
8 ms |
9324 KB |
Output is correct |
39 |
Correct |
7 ms |
9420 KB |
Output is correct |
40 |
Correct |
7 ms |
9420 KB |
Output is correct |
41 |
Correct |
7 ms |
9428 KB |
Output is correct |
42 |
Correct |
7 ms |
9420 KB |
Output is correct |
43 |
Correct |
8 ms |
9424 KB |
Output is correct |
44 |
Correct |
7 ms |
9320 KB |
Output is correct |
45 |
Correct |
11 ms |
9420 KB |
Output is correct |
46 |
Correct |
38 ms |
9772 KB |
Output is correct |
47 |
Correct |
40 ms |
9784 KB |
Output is correct |
48 |
Correct |
32 ms |
9784 KB |
Output is correct |
49 |
Correct |
21 ms |
9984 KB |
Output is correct |
50 |
Correct |
22 ms |
10080 KB |
Output is correct |
51 |
Correct |
21 ms |
10064 KB |
Output is correct |
52 |
Correct |
33 ms |
9932 KB |
Output is correct |
53 |
Correct |
34 ms |
9924 KB |
Output is correct |
54 |
Correct |
34 ms |
9904 KB |
Output is correct |
55 |
Correct |
34 ms |
9956 KB |
Output is correct |
56 |
Correct |
35 ms |
9916 KB |
Output is correct |
57 |
Correct |
72 ms |
9768 KB |
Output is correct |
58 |
Correct |
74 ms |
9776 KB |
Output is correct |
59 |
Correct |
87 ms |
9712 KB |
Output is correct |
60 |
Correct |
74 ms |
9676 KB |
Output is correct |
61 |
Correct |
36 ms |
9892 KB |
Output is correct |
62 |
Correct |
36 ms |
9804 KB |
Output is correct |
63 |
Correct |
36 ms |
9884 KB |
Output is correct |
64 |
Correct |
36 ms |
9676 KB |
Output is correct |
65 |
Correct |
35 ms |
9760 KB |
Output is correct |
66 |
Correct |
39 ms |
9760 KB |
Output is correct |
67 |
Correct |
34 ms |
9768 KB |
Output is correct |
68 |
Correct |
21 ms |
9972 KB |
Output is correct |
69 |
Correct |
34 ms |
9952 KB |
Output is correct |
70 |
Correct |
33 ms |
9896 KB |
Output is correct |
71 |
Correct |
34 ms |
9856 KB |
Output is correct |
72 |
Correct |
72 ms |
9768 KB |
Output is correct |
73 |
Correct |
73 ms |
9748 KB |
Output is correct |
74 |
Correct |
36 ms |
9920 KB |
Output is correct |
75 |
Correct |
33 ms |
9840 KB |
Output is correct |
76 |
Correct |
30 ms |
9828 KB |
Output is correct |
77 |
Correct |
29 ms |
9772 KB |
Output is correct |
78 |
Correct |
31 ms |
9804 KB |
Output is correct |
79 |
Correct |
29 ms |
9676 KB |
Output is correct |
80 |
Correct |
28 ms |
9740 KB |
Output is correct |
81 |
Correct |
34 ms |
9840 KB |
Output is correct |
82 |
Correct |
35 ms |
9792 KB |
Output is correct |
83 |
Correct |
31 ms |
9776 KB |
Output is correct |
84 |
Correct |
33 ms |
9804 KB |
Output is correct |
85 |
Correct |
33 ms |
9808 KB |
Output is correct |
86 |
Correct |
31 ms |
9784 KB |
Output is correct |
87 |
Correct |
96 ms |
10376 KB |
Output is correct |
88 |
Correct |
314 ms |
12484 KB |
Output is correct |
89 |
Correct |
1482 ms |
20296 KB |
Output is correct |
90 |
Correct |
1454 ms |
20176 KB |
Output is correct |
91 |
Correct |
1514 ms |
20188 KB |
Output is correct |
92 |
Correct |
656 ms |
27704 KB |
Output is correct |
93 |
Correct |
687 ms |
27784 KB |
Output is correct |
94 |
Correct |
676 ms |
27684 KB |
Output is correct |
95 |
Correct |
1223 ms |
25180 KB |
Output is correct |
96 |
Correct |
1253 ms |
25572 KB |
Output is correct |
97 |
Correct |
1238 ms |
25728 KB |
Output is correct |
98 |
Correct |
1254 ms |
25608 KB |
Output is correct |
99 |
Correct |
1276 ms |
23912 KB |
Output is correct |
100 |
Execution timed out |
2090 ms |
19648 KB |
Time limit exceeded |
101 |
Halted |
0 ms |
0 KB |
- |