#include <bits/stdc++.h>
#define endl '\n'
using namespace std;
const int N = 100007;
int n,a[N];
vector < int > v[N];
pair < int, int > e[N];
int size_of_subtree[N];
deque < pair < int, int > > d[N];
int chains,chain_of_node[N],head_of_chain[N],index_in_chain[N],parent[N];
int it[N];
int p[N];
map < int, int > code;
int all;
void update(int pos, int value) {
for(;pos<=all;pos+=pos&(-pos)) it[pos]+=value;
}
int query(int pos) {
int ans=0;
for(;pos>=1;pos-=pos&(-pos)) ans+=it[pos];
return ans;
}
void dfs(int node) {
size_of_subtree[node]=1;
if(v[node].empty()) return;
int i,max_size=-1,which=-1;
for(i=0;i<(int)(v[node].size());i++) {
dfs(v[node][i]);
size_of_subtree[node]+=size_of_subtree[v[node][i]];
if(size_of_subtree[v[node][i]]>max_size) {
max_size=size_of_subtree[v[node][i]];
which=i;
}
}
swap(v[node][0],v[node][which]);
}
void build_hld(int node, int chain, int idx) {
chain_of_node[node]=chain;
index_in_chain[node]=idx;
if(v[node].empty()) return;
int i;
build_hld(v[node][0],chain,idx+1);
for(i=1;i<(int)(v[node].size());i++) {
head_of_chain[++chains]=v[node][i];
build_hld(v[node][i],chains,1);
}
}
void append(int chain, int value) {
if(!d[chain].empty() && d[chain].back().first==value) {
++d[chain][d[chain].size()-1].second;
}
else {
d[chain].push_back(make_pair(value,1));
}
}
void extract(deque < pair < int, int > > d, int cnt, vector < pair < int, int > > &f) {
int i;
vector < pair < int, int > > v;
for(i=0;i<(int)(d.size()) && cnt>0;i++) {
int curr=min(cnt,d[i].second);
cnt-=curr;
v.push_back(make_pair(d[i].first,curr));
}
reverse(v.begin(),v.end());
for(i=0;i<(int)(v.size());i++) f.push_back(v[i]);
}
long long count_inversions(int node) {
vector < pair < int, int > > v;
long long ans=0;
int i;
while(node) {
extract(d[chain_of_node[node]],index_in_chain[node],v);
node=parent[head_of_chain[chain_of_node[node]]];
}
for(i=0;i<(int)(v.size());i++) {
ans+=query(v[i].first-1)*1ll*v[i].second;
update(v[i].first,v[i].second);
}
for(i=0;i<(int)(v.size());i++) {
update(v[i].first,-v[i].second);
}
return ans;
}
void eliminate(deque < pair < int, int > > &d, int cnt) {
while(!d.empty() && cnt>=d.front().second) {
cnt-=d.front().second;
d.pop_front();
}
if(cnt>0) d[0].second-=cnt;
}
void assign(int node, int value) {
while(node) {
eliminate(d[chain_of_node[node]],index_in_chain[node]);
if(!d[chain_of_node[node]].empty() && d[chain_of_node[node]].front().first==value) {
d[chain_of_node[node]][0].second+=index_in_chain[node];
}
else {
d[chain_of_node[node]].push_front(make_pair(value,index_in_chain[node]));
}
node=parent[head_of_chain[chain_of_node[node]]];
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i;
scanf("%d", &n);
for(i=1;i<=n;i++) {
scanf("%d", &a[i]);
p[i]=a[i];
}
sort(p+1,p+1+n);
code[p[1]]=1;
for(i=2;i<=n;i++) if(p[i]!=p[i-1]) {
code[p[i]]=code[p[i-1]]+1;
}
all=code[p[n]];
for(i=1;i<=n;i++) {
a[i]=code[a[i]];
}
for(i=1;i<n;i++) {
scanf("%d %d", &e[i].first, &e[i].second);
parent[e[i].second]=e[i].first;
v[e[i].first].push_back(e[i].second);
}
dfs(1);
chains=1;
head_of_chain[1]=1;
build_hld(1,1,1);
append(1,a[1]);
for(i=1;i<n;i++) {
printf("%lld\n", count_inversions(e[i].first));
append(chain_of_node[e[i].second],a[e[i].second]);
assign(e[i].second,a[e[i].second]);
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:135:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:137:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
construction.cpp:153:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &e[i].first, &e[i].second);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
70008 KB |
Output is correct |
2 |
Correct |
58 ms |
70248 KB |
Output is correct |
3 |
Correct |
63 ms |
70248 KB |
Output is correct |
4 |
Correct |
63 ms |
70248 KB |
Output is correct |
5 |
Correct |
66 ms |
70368 KB |
Output is correct |
6 |
Correct |
62 ms |
70368 KB |
Output is correct |
7 |
Correct |
61 ms |
70368 KB |
Output is correct |
8 |
Correct |
62 ms |
70476 KB |
Output is correct |
9 |
Correct |
63 ms |
70476 KB |
Output is correct |
10 |
Correct |
61 ms |
70484 KB |
Output is correct |
11 |
Correct |
78 ms |
70636 KB |
Output is correct |
12 |
Correct |
67 ms |
70704 KB |
Output is correct |
13 |
Correct |
65 ms |
70704 KB |
Output is correct |
14 |
Correct |
64 ms |
70704 KB |
Output is correct |
15 |
Correct |
62 ms |
70756 KB |
Output is correct |
16 |
Correct |
61 ms |
70756 KB |
Output is correct |
17 |
Correct |
62 ms |
70756 KB |
Output is correct |
18 |
Correct |
63 ms |
70756 KB |
Output is correct |
19 |
Correct |
64 ms |
70756 KB |
Output is correct |
20 |
Correct |
67 ms |
70756 KB |
Output is correct |
21 |
Correct |
64 ms |
70756 KB |
Output is correct |
22 |
Correct |
68 ms |
70880 KB |
Output is correct |
23 |
Correct |
62 ms |
70880 KB |
Output is correct |
24 |
Correct |
60 ms |
70880 KB |
Output is correct |
25 |
Correct |
63 ms |
70880 KB |
Output is correct |
26 |
Correct |
64 ms |
70880 KB |
Output is correct |
27 |
Correct |
80 ms |
70880 KB |
Output is correct |
28 |
Correct |
66 ms |
70940 KB |
Output is correct |
29 |
Correct |
62 ms |
70940 KB |
Output is correct |
30 |
Correct |
64 ms |
70940 KB |
Output is correct |
31 |
Correct |
63 ms |
70940 KB |
Output is correct |
32 |
Correct |
59 ms |
70940 KB |
Output is correct |
33 |
Correct |
60 ms |
70940 KB |
Output is correct |
34 |
Correct |
60 ms |
70940 KB |
Output is correct |
35 |
Correct |
62 ms |
71000 KB |
Output is correct |
36 |
Correct |
62 ms |
71000 KB |
Output is correct |
37 |
Correct |
64 ms |
71000 KB |
Output is correct |
38 |
Correct |
63 ms |
71000 KB |
Output is correct |
39 |
Correct |
64 ms |
71000 KB |
Output is correct |
40 |
Correct |
61 ms |
71000 KB |
Output is correct |
41 |
Correct |
62 ms |
71000 KB |
Output is correct |
42 |
Correct |
61 ms |
71000 KB |
Output is correct |
43 |
Correct |
60 ms |
71000 KB |
Output is correct |
44 |
Correct |
64 ms |
71000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
70008 KB |
Output is correct |
2 |
Correct |
58 ms |
70248 KB |
Output is correct |
3 |
Correct |
63 ms |
70248 KB |
Output is correct |
4 |
Correct |
63 ms |
70248 KB |
Output is correct |
5 |
Correct |
66 ms |
70368 KB |
Output is correct |
6 |
Correct |
62 ms |
70368 KB |
Output is correct |
7 |
Correct |
61 ms |
70368 KB |
Output is correct |
8 |
Correct |
62 ms |
70476 KB |
Output is correct |
9 |
Correct |
63 ms |
70476 KB |
Output is correct |
10 |
Correct |
61 ms |
70484 KB |
Output is correct |
11 |
Correct |
78 ms |
70636 KB |
Output is correct |
12 |
Correct |
67 ms |
70704 KB |
Output is correct |
13 |
Correct |
65 ms |
70704 KB |
Output is correct |
14 |
Correct |
64 ms |
70704 KB |
Output is correct |
15 |
Correct |
62 ms |
70756 KB |
Output is correct |
16 |
Correct |
61 ms |
70756 KB |
Output is correct |
17 |
Correct |
62 ms |
70756 KB |
Output is correct |
18 |
Correct |
63 ms |
70756 KB |
Output is correct |
19 |
Correct |
64 ms |
70756 KB |
Output is correct |
20 |
Correct |
67 ms |
70756 KB |
Output is correct |
21 |
Correct |
64 ms |
70756 KB |
Output is correct |
22 |
Correct |
68 ms |
70880 KB |
Output is correct |
23 |
Correct |
62 ms |
70880 KB |
Output is correct |
24 |
Correct |
60 ms |
70880 KB |
Output is correct |
25 |
Correct |
63 ms |
70880 KB |
Output is correct |
26 |
Correct |
64 ms |
70880 KB |
Output is correct |
27 |
Correct |
80 ms |
70880 KB |
Output is correct |
28 |
Correct |
66 ms |
70940 KB |
Output is correct |
29 |
Correct |
62 ms |
70940 KB |
Output is correct |
30 |
Correct |
64 ms |
70940 KB |
Output is correct |
31 |
Correct |
63 ms |
70940 KB |
Output is correct |
32 |
Correct |
59 ms |
70940 KB |
Output is correct |
33 |
Correct |
60 ms |
70940 KB |
Output is correct |
34 |
Correct |
60 ms |
70940 KB |
Output is correct |
35 |
Correct |
62 ms |
71000 KB |
Output is correct |
36 |
Correct |
62 ms |
71000 KB |
Output is correct |
37 |
Correct |
64 ms |
71000 KB |
Output is correct |
38 |
Correct |
63 ms |
71000 KB |
Output is correct |
39 |
Correct |
64 ms |
71000 KB |
Output is correct |
40 |
Correct |
61 ms |
71000 KB |
Output is correct |
41 |
Correct |
62 ms |
71000 KB |
Output is correct |
42 |
Correct |
61 ms |
71000 KB |
Output is correct |
43 |
Correct |
60 ms |
71000 KB |
Output is correct |
44 |
Correct |
64 ms |
71000 KB |
Output is correct |
45 |
Correct |
61 ms |
71000 KB |
Output is correct |
46 |
Correct |
68 ms |
71352 KB |
Output is correct |
47 |
Correct |
71 ms |
71432 KB |
Output is correct |
48 |
Correct |
70 ms |
71508 KB |
Output is correct |
49 |
Correct |
67 ms |
71952 KB |
Output is correct |
50 |
Correct |
69 ms |
72132 KB |
Output is correct |
51 |
Correct |
65 ms |
72132 KB |
Output is correct |
52 |
Correct |
78 ms |
72132 KB |
Output is correct |
53 |
Correct |
77 ms |
72132 KB |
Output is correct |
54 |
Correct |
79 ms |
72200 KB |
Output is correct |
55 |
Correct |
76 ms |
72268 KB |
Output is correct |
56 |
Correct |
67 ms |
72268 KB |
Output is correct |
57 |
Correct |
71 ms |
72268 KB |
Output is correct |
58 |
Correct |
73 ms |
72300 KB |
Output is correct |
59 |
Correct |
86 ms |
72436 KB |
Output is correct |
60 |
Correct |
74 ms |
72508 KB |
Output is correct |
61 |
Correct |
66 ms |
72604 KB |
Output is correct |
62 |
Correct |
68 ms |
72692 KB |
Output is correct |
63 |
Correct |
64 ms |
72868 KB |
Output is correct |
64 |
Correct |
66 ms |
72868 KB |
Output is correct |
65 |
Correct |
69 ms |
72868 KB |
Output is correct |
66 |
Correct |
69 ms |
72868 KB |
Output is correct |
67 |
Correct |
78 ms |
72868 KB |
Output is correct |
68 |
Correct |
76 ms |
73020 KB |
Output is correct |
69 |
Correct |
81 ms |
73068 KB |
Output is correct |
70 |
Correct |
98 ms |
73068 KB |
Output is correct |
71 |
Correct |
65 ms |
73068 KB |
Output is correct |
72 |
Correct |
91 ms |
73108 KB |
Output is correct |
73 |
Correct |
92 ms |
73108 KB |
Output is correct |
74 |
Correct |
76 ms |
73188 KB |
Output is correct |
75 |
Correct |
85 ms |
73244 KB |
Output is correct |
76 |
Correct |
74 ms |
73308 KB |
Output is correct |
77 |
Correct |
77 ms |
73372 KB |
Output is correct |
78 |
Correct |
77 ms |
73436 KB |
Output is correct |
79 |
Correct |
74 ms |
73436 KB |
Output is correct |
80 |
Correct |
74 ms |
73436 KB |
Output is correct |
81 |
Correct |
82 ms |
73584 KB |
Output is correct |
82 |
Correct |
91 ms |
73648 KB |
Output is correct |
83 |
Correct |
74 ms |
73712 KB |
Output is correct |
84 |
Correct |
79 ms |
73712 KB |
Output is correct |
85 |
Correct |
79 ms |
73848 KB |
Output is correct |
86 |
Correct |
82 ms |
73848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
70008 KB |
Output is correct |
2 |
Correct |
58 ms |
70248 KB |
Output is correct |
3 |
Correct |
63 ms |
70248 KB |
Output is correct |
4 |
Correct |
63 ms |
70248 KB |
Output is correct |
5 |
Correct |
66 ms |
70368 KB |
Output is correct |
6 |
Correct |
62 ms |
70368 KB |
Output is correct |
7 |
Correct |
61 ms |
70368 KB |
Output is correct |
8 |
Correct |
62 ms |
70476 KB |
Output is correct |
9 |
Correct |
63 ms |
70476 KB |
Output is correct |
10 |
Correct |
61 ms |
70484 KB |
Output is correct |
11 |
Correct |
78 ms |
70636 KB |
Output is correct |
12 |
Correct |
67 ms |
70704 KB |
Output is correct |
13 |
Correct |
65 ms |
70704 KB |
Output is correct |
14 |
Correct |
64 ms |
70704 KB |
Output is correct |
15 |
Correct |
62 ms |
70756 KB |
Output is correct |
16 |
Correct |
61 ms |
70756 KB |
Output is correct |
17 |
Correct |
62 ms |
70756 KB |
Output is correct |
18 |
Correct |
63 ms |
70756 KB |
Output is correct |
19 |
Correct |
64 ms |
70756 KB |
Output is correct |
20 |
Correct |
67 ms |
70756 KB |
Output is correct |
21 |
Correct |
64 ms |
70756 KB |
Output is correct |
22 |
Correct |
68 ms |
70880 KB |
Output is correct |
23 |
Correct |
62 ms |
70880 KB |
Output is correct |
24 |
Correct |
60 ms |
70880 KB |
Output is correct |
25 |
Correct |
63 ms |
70880 KB |
Output is correct |
26 |
Correct |
64 ms |
70880 KB |
Output is correct |
27 |
Correct |
80 ms |
70880 KB |
Output is correct |
28 |
Correct |
66 ms |
70940 KB |
Output is correct |
29 |
Correct |
62 ms |
70940 KB |
Output is correct |
30 |
Correct |
64 ms |
70940 KB |
Output is correct |
31 |
Correct |
63 ms |
70940 KB |
Output is correct |
32 |
Correct |
59 ms |
70940 KB |
Output is correct |
33 |
Correct |
60 ms |
70940 KB |
Output is correct |
34 |
Correct |
60 ms |
70940 KB |
Output is correct |
35 |
Correct |
62 ms |
71000 KB |
Output is correct |
36 |
Correct |
62 ms |
71000 KB |
Output is correct |
37 |
Correct |
64 ms |
71000 KB |
Output is correct |
38 |
Correct |
63 ms |
71000 KB |
Output is correct |
39 |
Correct |
64 ms |
71000 KB |
Output is correct |
40 |
Correct |
61 ms |
71000 KB |
Output is correct |
41 |
Correct |
62 ms |
71000 KB |
Output is correct |
42 |
Correct |
61 ms |
71000 KB |
Output is correct |
43 |
Correct |
60 ms |
71000 KB |
Output is correct |
44 |
Correct |
64 ms |
71000 KB |
Output is correct |
45 |
Correct |
61 ms |
71000 KB |
Output is correct |
46 |
Correct |
68 ms |
71352 KB |
Output is correct |
47 |
Correct |
71 ms |
71432 KB |
Output is correct |
48 |
Correct |
70 ms |
71508 KB |
Output is correct |
49 |
Correct |
67 ms |
71952 KB |
Output is correct |
50 |
Correct |
69 ms |
72132 KB |
Output is correct |
51 |
Correct |
65 ms |
72132 KB |
Output is correct |
52 |
Correct |
78 ms |
72132 KB |
Output is correct |
53 |
Correct |
77 ms |
72132 KB |
Output is correct |
54 |
Correct |
79 ms |
72200 KB |
Output is correct |
55 |
Correct |
76 ms |
72268 KB |
Output is correct |
56 |
Correct |
67 ms |
72268 KB |
Output is correct |
57 |
Correct |
71 ms |
72268 KB |
Output is correct |
58 |
Correct |
73 ms |
72300 KB |
Output is correct |
59 |
Correct |
86 ms |
72436 KB |
Output is correct |
60 |
Correct |
74 ms |
72508 KB |
Output is correct |
61 |
Correct |
66 ms |
72604 KB |
Output is correct |
62 |
Correct |
68 ms |
72692 KB |
Output is correct |
63 |
Correct |
64 ms |
72868 KB |
Output is correct |
64 |
Correct |
66 ms |
72868 KB |
Output is correct |
65 |
Correct |
69 ms |
72868 KB |
Output is correct |
66 |
Correct |
69 ms |
72868 KB |
Output is correct |
67 |
Correct |
78 ms |
72868 KB |
Output is correct |
68 |
Correct |
76 ms |
73020 KB |
Output is correct |
69 |
Correct |
81 ms |
73068 KB |
Output is correct |
70 |
Correct |
98 ms |
73068 KB |
Output is correct |
71 |
Correct |
65 ms |
73068 KB |
Output is correct |
72 |
Correct |
91 ms |
73108 KB |
Output is correct |
73 |
Correct |
92 ms |
73108 KB |
Output is correct |
74 |
Correct |
76 ms |
73188 KB |
Output is correct |
75 |
Correct |
85 ms |
73244 KB |
Output is correct |
76 |
Correct |
74 ms |
73308 KB |
Output is correct |
77 |
Correct |
77 ms |
73372 KB |
Output is correct |
78 |
Correct |
77 ms |
73436 KB |
Output is correct |
79 |
Correct |
74 ms |
73436 KB |
Output is correct |
80 |
Correct |
74 ms |
73436 KB |
Output is correct |
81 |
Correct |
82 ms |
73584 KB |
Output is correct |
82 |
Correct |
91 ms |
73648 KB |
Output is correct |
83 |
Correct |
74 ms |
73712 KB |
Output is correct |
84 |
Correct |
79 ms |
73712 KB |
Output is correct |
85 |
Correct |
79 ms |
73848 KB |
Output is correct |
86 |
Correct |
82 ms |
73848 KB |
Output is correct |
87 |
Correct |
107 ms |
74660 KB |
Output is correct |
88 |
Correct |
157 ms |
77348 KB |
Output is correct |
89 |
Correct |
517 ms |
86796 KB |
Output is correct |
90 |
Correct |
523 ms |
88952 KB |
Output is correct |
91 |
Correct |
547 ms |
91012 KB |
Output is correct |
92 |
Correct |
373 ms |
100520 KB |
Output is correct |
93 |
Correct |
446 ms |
102752 KB |
Output is correct |
94 |
Correct |
344 ms |
104812 KB |
Output is correct |
95 |
Execution timed out |
1072 ms |
104812 KB |
Time limit exceeded |
96 |
Halted |
0 ms |
0 KB |
- |