# pragma GCC optimize ("O3")
# pragma GCC optimize ("Ofast")
# pragma GCC optimize ("unroll-loops")
# pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
#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 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) {
seg.resize(x+1);
sz=x;
}
int sum(int i) {
int ret=0;
while (i>0) {
assert(i<seg.size());
ret+=seg[i];
i-=(i&-i);
}
return ret;
}
void update(int i,int val) {
while (i<=sz) {
assert(i<seg.size());
seg[i]+=val;
i+=(i&-i);
}
}
};
long long cntinv(vector<P> v) {
vector<int> press;
press.push_back(-1);
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();
assert(v[i].first!=0);
ret+=1LL*v[i].second*st.sum(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;
int deep=sum(ind[now],ind[now]+siz[now]-1).second;
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==deep) {
rec=nt;
}
}
vec.push_back(P(arr[deep],depth[now]-depth[rec]+1));
now=p[rec];
}
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:24:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for(int i=0;i<adj[v].size();i++) {
| ~^~~~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
from construction.cpp:5:
construction.cpp: In member function 'int SegmentTree::sum(int)':
construction.cpp:72:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | assert(i<seg.size());
| ~^~~~~~~~~~~
construction.cpp: In member function 'void SegmentTree::update(int, int)':
construction.cpp:80:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
80 | assert(i<seg.size());
| ~^~~~~~~~~~~
construction.cpp: In function 'long long int cntinv(std::vector<std::pair<int, int> >)':
construction.cpp:90: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]
90 | for(int i=0;i<v.size();i++) {
| ~^~~~~~~~~
construction.cpp:99: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]
99 | for(int i=0;i<v.size();i++) {
| ~^~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:109:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
construction.cpp:111:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | scanf("%d",&arr[i]);
| ~~~~~^~~~~~~~~~~~~~
construction.cpp:124:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | scanf("%d %d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9292 KB |
Output is correct |
2 |
Correct |
5 ms |
9372 KB |
Output is correct |
3 |
Correct |
5 ms |
9292 KB |
Output is correct |
4 |
Correct |
6 ms |
9292 KB |
Output is correct |
5 |
Correct |
7 ms |
9404 KB |
Output is correct |
6 |
Correct |
6 ms |
9292 KB |
Output is correct |
7 |
Correct |
7 ms |
9292 KB |
Output is correct |
8 |
Correct |
6 ms |
9420 KB |
Output is correct |
9 |
Correct |
7 ms |
9420 KB |
Output is correct |
10 |
Correct |
8 ms |
9420 KB |
Output is correct |
11 |
Correct |
6 ms |
9420 KB |
Output is correct |
12 |
Correct |
6 ms |
9420 KB |
Output is correct |
13 |
Correct |
7 ms |
9420 KB |
Output is correct |
14 |
Correct |
6 ms |
9292 KB |
Output is correct |
15 |
Correct |
7 ms |
9288 KB |
Output is correct |
16 |
Correct |
8 ms |
9292 KB |
Output is correct |
17 |
Correct |
8 ms |
9292 KB |
Output is correct |
18 |
Correct |
8 ms |
9292 KB |
Output is correct |
19 |
Correct |
7 ms |
9292 KB |
Output is correct |
20 |
Correct |
7 ms |
9528 KB |
Output is correct |
21 |
Correct |
7 ms |
9292 KB |
Output is correct |
22 |
Correct |
7 ms |
9412 KB |
Output is correct |
23 |
Correct |
6 ms |
9292 KB |
Output is correct |
24 |
Correct |
7 ms |
9292 KB |
Output is correct |
25 |
Correct |
8 ms |
9336 KB |
Output is correct |
26 |
Correct |
8 ms |
9436 KB |
Output is correct |
27 |
Correct |
7 ms |
9420 KB |
Output is correct |
28 |
Correct |
6 ms |
9292 KB |
Output is correct |
29 |
Correct |
6 ms |
9292 KB |
Output is correct |
30 |
Correct |
8 ms |
9292 KB |
Output is correct |
31 |
Correct |
8 ms |
9292 KB |
Output is correct |
32 |
Correct |
7 ms |
9324 KB |
Output is correct |
33 |
Correct |
6 ms |
9292 KB |
Output is correct |
34 |
Correct |
6 ms |
9292 KB |
Output is correct |
35 |
Correct |
6 ms |
9292 KB |
Output is correct |
36 |
Correct |
7 ms |
9292 KB |
Output is correct |
37 |
Correct |
6 ms |
9292 KB |
Output is correct |
38 |
Correct |
6 ms |
9292 KB |
Output is correct |
39 |
Correct |
7 ms |
9292 KB |
Output is correct |
40 |
Correct |
7 ms |
9292 KB |
Output is correct |
41 |
Correct |
6 ms |
9292 KB |
Output is correct |
42 |
Correct |
7 ms |
9292 KB |
Output is correct |
43 |
Correct |
7 ms |
9292 KB |
Output is correct |
44 |
Correct |
6 ms |
9292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9292 KB |
Output is correct |
2 |
Correct |
5 ms |
9372 KB |
Output is correct |
3 |
Correct |
5 ms |
9292 KB |
Output is correct |
4 |
Correct |
6 ms |
9292 KB |
Output is correct |
5 |
Correct |
7 ms |
9404 KB |
Output is correct |
6 |
Correct |
6 ms |
9292 KB |
Output is correct |
7 |
Correct |
7 ms |
9292 KB |
Output is correct |
8 |
Correct |
6 ms |
9420 KB |
Output is correct |
9 |
Correct |
7 ms |
9420 KB |
Output is correct |
10 |
Correct |
8 ms |
9420 KB |
Output is correct |
11 |
Correct |
6 ms |
9420 KB |
Output is correct |
12 |
Correct |
6 ms |
9420 KB |
Output is correct |
13 |
Correct |
7 ms |
9420 KB |
Output is correct |
14 |
Correct |
6 ms |
9292 KB |
Output is correct |
15 |
Correct |
7 ms |
9288 KB |
Output is correct |
16 |
Correct |
8 ms |
9292 KB |
Output is correct |
17 |
Correct |
8 ms |
9292 KB |
Output is correct |
18 |
Correct |
8 ms |
9292 KB |
Output is correct |
19 |
Correct |
7 ms |
9292 KB |
Output is correct |
20 |
Correct |
7 ms |
9528 KB |
Output is correct |
21 |
Correct |
7 ms |
9292 KB |
Output is correct |
22 |
Correct |
7 ms |
9412 KB |
Output is correct |
23 |
Correct |
6 ms |
9292 KB |
Output is correct |
24 |
Correct |
7 ms |
9292 KB |
Output is correct |
25 |
Correct |
8 ms |
9336 KB |
Output is correct |
26 |
Correct |
8 ms |
9436 KB |
Output is correct |
27 |
Correct |
7 ms |
9420 KB |
Output is correct |
28 |
Correct |
6 ms |
9292 KB |
Output is correct |
29 |
Correct |
6 ms |
9292 KB |
Output is correct |
30 |
Correct |
8 ms |
9292 KB |
Output is correct |
31 |
Correct |
8 ms |
9292 KB |
Output is correct |
32 |
Correct |
7 ms |
9324 KB |
Output is correct |
33 |
Correct |
6 ms |
9292 KB |
Output is correct |
34 |
Correct |
6 ms |
9292 KB |
Output is correct |
35 |
Correct |
6 ms |
9292 KB |
Output is correct |
36 |
Correct |
7 ms |
9292 KB |
Output is correct |
37 |
Correct |
6 ms |
9292 KB |
Output is correct |
38 |
Correct |
6 ms |
9292 KB |
Output is correct |
39 |
Correct |
7 ms |
9292 KB |
Output is correct |
40 |
Correct |
7 ms |
9292 KB |
Output is correct |
41 |
Correct |
6 ms |
9292 KB |
Output is correct |
42 |
Correct |
7 ms |
9292 KB |
Output is correct |
43 |
Correct |
7 ms |
9292 KB |
Output is correct |
44 |
Correct |
6 ms |
9292 KB |
Output is correct |
45 |
Correct |
9 ms |
9404 KB |
Output is correct |
46 |
Correct |
23 ms |
9688 KB |
Output is correct |
47 |
Correct |
24 ms |
9680 KB |
Output is correct |
48 |
Correct |
22 ms |
9692 KB |
Output is correct |
49 |
Correct |
15 ms |
9880 KB |
Output is correct |
50 |
Correct |
15 ms |
9932 KB |
Output is correct |
51 |
Correct |
15 ms |
10044 KB |
Output is correct |
52 |
Correct |
20 ms |
9800 KB |
Output is correct |
53 |
Correct |
20 ms |
9804 KB |
Output is correct |
54 |
Correct |
20 ms |
9860 KB |
Output is correct |
55 |
Correct |
20 ms |
9844 KB |
Output is correct |
56 |
Correct |
21 ms |
9832 KB |
Output is correct |
57 |
Correct |
39 ms |
9684 KB |
Output is correct |
58 |
Correct |
40 ms |
9684 KB |
Output is correct |
59 |
Correct |
41 ms |
9668 KB |
Output is correct |
60 |
Correct |
43 ms |
9676 KB |
Output is correct |
61 |
Correct |
22 ms |
9736 KB |
Output is correct |
62 |
Correct |
22 ms |
9844 KB |
Output is correct |
63 |
Correct |
22 ms |
9836 KB |
Output is correct |
64 |
Correct |
23 ms |
9688 KB |
Output is correct |
65 |
Correct |
23 ms |
9676 KB |
Output is correct |
66 |
Correct |
24 ms |
9684 KB |
Output is correct |
67 |
Correct |
22 ms |
9688 KB |
Output is correct |
68 |
Correct |
14 ms |
9932 KB |
Output is correct |
69 |
Correct |
20 ms |
9816 KB |
Output is correct |
70 |
Correct |
20 ms |
9832 KB |
Output is correct |
71 |
Correct |
20 ms |
9820 KB |
Output is correct |
72 |
Correct |
40 ms |
9676 KB |
Output is correct |
73 |
Correct |
40 ms |
9676 KB |
Output is correct |
74 |
Correct |
22 ms |
9832 KB |
Output is correct |
75 |
Correct |
20 ms |
9676 KB |
Output is correct |
76 |
Correct |
18 ms |
9676 KB |
Output is correct |
77 |
Correct |
20 ms |
9724 KB |
Output is correct |
78 |
Correct |
19 ms |
9764 KB |
Output is correct |
79 |
Correct |
19 ms |
9696 KB |
Output is correct |
80 |
Correct |
19 ms |
9716 KB |
Output is correct |
81 |
Correct |
23 ms |
9676 KB |
Output is correct |
82 |
Correct |
21 ms |
9736 KB |
Output is correct |
83 |
Correct |
20 ms |
9676 KB |
Output is correct |
84 |
Correct |
21 ms |
9720 KB |
Output is correct |
85 |
Correct |
21 ms |
9736 KB |
Output is correct |
86 |
Correct |
20 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9292 KB |
Output is correct |
2 |
Correct |
5 ms |
9372 KB |
Output is correct |
3 |
Correct |
5 ms |
9292 KB |
Output is correct |
4 |
Correct |
6 ms |
9292 KB |
Output is correct |
5 |
Correct |
7 ms |
9404 KB |
Output is correct |
6 |
Correct |
6 ms |
9292 KB |
Output is correct |
7 |
Correct |
7 ms |
9292 KB |
Output is correct |
8 |
Correct |
6 ms |
9420 KB |
Output is correct |
9 |
Correct |
7 ms |
9420 KB |
Output is correct |
10 |
Correct |
8 ms |
9420 KB |
Output is correct |
11 |
Correct |
6 ms |
9420 KB |
Output is correct |
12 |
Correct |
6 ms |
9420 KB |
Output is correct |
13 |
Correct |
7 ms |
9420 KB |
Output is correct |
14 |
Correct |
6 ms |
9292 KB |
Output is correct |
15 |
Correct |
7 ms |
9288 KB |
Output is correct |
16 |
Correct |
8 ms |
9292 KB |
Output is correct |
17 |
Correct |
8 ms |
9292 KB |
Output is correct |
18 |
Correct |
8 ms |
9292 KB |
Output is correct |
19 |
Correct |
7 ms |
9292 KB |
Output is correct |
20 |
Correct |
7 ms |
9528 KB |
Output is correct |
21 |
Correct |
7 ms |
9292 KB |
Output is correct |
22 |
Correct |
7 ms |
9412 KB |
Output is correct |
23 |
Correct |
6 ms |
9292 KB |
Output is correct |
24 |
Correct |
7 ms |
9292 KB |
Output is correct |
25 |
Correct |
8 ms |
9336 KB |
Output is correct |
26 |
Correct |
8 ms |
9436 KB |
Output is correct |
27 |
Correct |
7 ms |
9420 KB |
Output is correct |
28 |
Correct |
6 ms |
9292 KB |
Output is correct |
29 |
Correct |
6 ms |
9292 KB |
Output is correct |
30 |
Correct |
8 ms |
9292 KB |
Output is correct |
31 |
Correct |
8 ms |
9292 KB |
Output is correct |
32 |
Correct |
7 ms |
9324 KB |
Output is correct |
33 |
Correct |
6 ms |
9292 KB |
Output is correct |
34 |
Correct |
6 ms |
9292 KB |
Output is correct |
35 |
Correct |
6 ms |
9292 KB |
Output is correct |
36 |
Correct |
7 ms |
9292 KB |
Output is correct |
37 |
Correct |
6 ms |
9292 KB |
Output is correct |
38 |
Correct |
6 ms |
9292 KB |
Output is correct |
39 |
Correct |
7 ms |
9292 KB |
Output is correct |
40 |
Correct |
7 ms |
9292 KB |
Output is correct |
41 |
Correct |
6 ms |
9292 KB |
Output is correct |
42 |
Correct |
7 ms |
9292 KB |
Output is correct |
43 |
Correct |
7 ms |
9292 KB |
Output is correct |
44 |
Correct |
6 ms |
9292 KB |
Output is correct |
45 |
Correct |
9 ms |
9404 KB |
Output is correct |
46 |
Correct |
23 ms |
9688 KB |
Output is correct |
47 |
Correct |
24 ms |
9680 KB |
Output is correct |
48 |
Correct |
22 ms |
9692 KB |
Output is correct |
49 |
Correct |
15 ms |
9880 KB |
Output is correct |
50 |
Correct |
15 ms |
9932 KB |
Output is correct |
51 |
Correct |
15 ms |
10044 KB |
Output is correct |
52 |
Correct |
20 ms |
9800 KB |
Output is correct |
53 |
Correct |
20 ms |
9804 KB |
Output is correct |
54 |
Correct |
20 ms |
9860 KB |
Output is correct |
55 |
Correct |
20 ms |
9844 KB |
Output is correct |
56 |
Correct |
21 ms |
9832 KB |
Output is correct |
57 |
Correct |
39 ms |
9684 KB |
Output is correct |
58 |
Correct |
40 ms |
9684 KB |
Output is correct |
59 |
Correct |
41 ms |
9668 KB |
Output is correct |
60 |
Correct |
43 ms |
9676 KB |
Output is correct |
61 |
Correct |
22 ms |
9736 KB |
Output is correct |
62 |
Correct |
22 ms |
9844 KB |
Output is correct |
63 |
Correct |
22 ms |
9836 KB |
Output is correct |
64 |
Correct |
23 ms |
9688 KB |
Output is correct |
65 |
Correct |
23 ms |
9676 KB |
Output is correct |
66 |
Correct |
24 ms |
9684 KB |
Output is correct |
67 |
Correct |
22 ms |
9688 KB |
Output is correct |
68 |
Correct |
14 ms |
9932 KB |
Output is correct |
69 |
Correct |
20 ms |
9816 KB |
Output is correct |
70 |
Correct |
20 ms |
9832 KB |
Output is correct |
71 |
Correct |
20 ms |
9820 KB |
Output is correct |
72 |
Correct |
40 ms |
9676 KB |
Output is correct |
73 |
Correct |
40 ms |
9676 KB |
Output is correct |
74 |
Correct |
22 ms |
9832 KB |
Output is correct |
75 |
Correct |
20 ms |
9676 KB |
Output is correct |
76 |
Correct |
18 ms |
9676 KB |
Output is correct |
77 |
Correct |
20 ms |
9724 KB |
Output is correct |
78 |
Correct |
19 ms |
9764 KB |
Output is correct |
79 |
Correct |
19 ms |
9696 KB |
Output is correct |
80 |
Correct |
19 ms |
9716 KB |
Output is correct |
81 |
Correct |
23 ms |
9676 KB |
Output is correct |
82 |
Correct |
21 ms |
9736 KB |
Output is correct |
83 |
Correct |
20 ms |
9676 KB |
Output is correct |
84 |
Correct |
21 ms |
9720 KB |
Output is correct |
85 |
Correct |
21 ms |
9736 KB |
Output is correct |
86 |
Correct |
20 ms |
9720 KB |
Output is correct |
87 |
Correct |
58 ms |
10164 KB |
Output is correct |
88 |
Correct |
200 ms |
11736 KB |
Output is correct |
89 |
Correct |
899 ms |
17584 KB |
Output is correct |
90 |
Correct |
878 ms |
17492 KB |
Output is correct |
91 |
Correct |
896 ms |
17668 KB |
Output is correct |
92 |
Correct |
401 ms |
25160 KB |
Output is correct |
93 |
Correct |
394 ms |
25048 KB |
Output is correct |
94 |
Correct |
386 ms |
25196 KB |
Output is correct |
95 |
Correct |
628 ms |
22440 KB |
Output is correct |
96 |
Correct |
649 ms |
22428 KB |
Output is correct |
97 |
Correct |
638 ms |
22476 KB |
Output is correct |
98 |
Correct |
647 ms |
22620 KB |
Output is correct |
99 |
Correct |
634 ms |
21496 KB |
Output is correct |
100 |
Execution timed out |
2021 ms |
17412 KB |
Time limit exceeded |
101 |
Halted |
0 ms |
0 KB |
- |