#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int l[100000];
int r[100000];
int p[100000];
int sub[100000];
int pri[100000];
int val[100000];
int par[100000];
void pull(int t){
sub[t] = 1;
if(l[t]!=-1){
sub[t] += sub[l[t]];
p[l[t]] = t;
}
if(r[t]!=-1){
sub[t] += sub[r[t]];
p[r[t]] = t;
}
}
void push(int t){
if(l[t]!=-1){
val[l[t]] = val[t];
}
if(r[t]!=-1){
val[r[t]] = val[t];
}
}
int merge(int le, int ri){
if(le==-1 && ri==-1){
return -1;
}
if(le==-1){
return ri;
}
if(ri==-1){
return le;
}
push(le);
push(ri);
if(pri[le]>pri[ri]){
r[le] = merge(r[le],ri);
pull(le);
return le;
}
else{
l[ri] = merge(le,l[ri]);
pull(ri);
return ri;
}
}
pair<int, int> split(int t, int k){
if(t==-1){
return make_pair(-1,-1);
}
push(t);
int lsum = 0;
if(l[t]!=-1){
lsum = sub[l[t]];
}
if(lsum>=k){
pair<int, int> ret = split(l[t],k);
l[t] = ret.second;
ret.second = t;
pull(t);
// pull(l[t]);
// pull(r[t]);
return ret;
}
else{
pair<int, int> ret = split(r[t],k-lsum-1);
r[t] = ret.first;
ret.first = t;
pull(t);
return ret;
}
}
pair<int, int> dfs(int now, int from){
int ret = 0;
if(from==-1 && l[now]!=-1){
ret += sub[l[now]];
}
if(from!=-1 && r[now]==from){
ret ++;
if(l[now]!=-1){
ret += sub[l[now]];
}
}
if(p[now]==-1){
return make_pair(now,ret);
}
pair<int, int> get = dfs(p[now],now);
get.second += ret;
return get;
}
int first(int now){
if(l[now]==-1){
return now;
}
return first(l[now]);
}
void dfs2(int now){
cout << "NOW " << now << endl;
if(l[now]!=-1){
cout << "LEFT " << now << " -> " << l[now] << endl;
dfs2(l[now]);
}
if(r[now]!=-1){
cout << "RIGHT " << now << " -> " << r[now] << endl;
dfs2(r[now]);
}
}
vector<pair<int, int> > compress(vector<pair<int, int> > a){
vector<int> all;
for(int i = 0; i<a.size(); i++){
all.push_back(a[i].first);
}
sort(all.begin(),all.end());
int p =1;
map<int, int> m;
for(int i = 0; i<a.size(); i++){
if(i==0 || all[i]!=all[i-1]){
m[all[i]] = p++;
}
}
for(int i = 0; i<a.size(); i++){
a[i].first = m[a[i].first];
}
return a;
}
class Fenwick{
public:
vector<ll> li;
int n;
Fenwick(int a){
n = a;
li.resize(n+3);
}
void add(int ind, ll val){
for(;ind<n; ind+=(ind&(-ind))){
li[ind] += val;
}
}
ll get(int ind){
ll ret = 0LL;
for(;ind>0; ind-=(ind&(-ind))){
ret += li[ind];
}
return ret;
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
srand(11);
int n;
cin >> n;
for(int i = 0; i<n; i++){
l[i] = -1;
r[i] = -1;
p[i] = -1;
sub[i] = 1;
pri[i] = rand();
cin >> val[i];
par[i] = -1;
}
for(int i = 0; i+1<n; i++){
int a, b;
cin >> a >> b;
a--;
b--;
vector<pair<int, int> > li;
//not including now
int now = a;
int set = val[b];
pair<int, int> got = dfs(now,-1);
int ind = got.second;
int root = got.first;
// cout << "PRE DFS " << endl;
// dfs2(root);
// cout << "POST DFS " << endl;
// cout << "BASE " << ind << " " << root << " " << first(root) << endl;
pair<int, int> both = split(root,ind+1);
p[both.first] = -1;
p[both.second] = -1;
got = dfs(now,-1);
ind = got.second;
root = got.first;
li.push_back(make_pair(val[got.first],1));
// cout << "BASE " << ind << " " << root << " " << first(root) << endl;
while(true){
got = dfs(now,-1);
ind = got.second;
root = got.first;
int f = first(root);
int all = val[root];
// cout << "! " << root << " " << ind << " " << all << " " << f << " " << par[f] << endl;
if(par[f]==-1){
//started from the bottom, now we here
li.push_back(make_pair(all,ind));
val[root] = set;
now = f;
break;
}
else{
li.push_back(make_pair(all,ind));
int nex = par[f];
pair<int, int> got2 = dfs(nex,-1);
int ind2 = got2.second;
int root2 = got2.first;
// cout << "!@# " << ind2 << " " << root2 << " " << first(root2) << " " << endl;
pair<int, int> both2 = split(root2,ind2+1);
p[both2.first] = -1;
p[both2.second] = -1;
now = f;
int nval = val[both2.first];
val[merge(both2.first,root)] = nval;
}
}
got = dfs(now,-1);
root = got.first;
par[b] = a;
val[merge(root,b)] = set;
// cout << "STAGE " << i << endl;
// for(int j = 0; j<li.size(); j++){
// cout << j << " " << li[j].first << " " << li[j].second << endl;
// }
li =compress(li);
Fenwick fen = Fenwick(li.size());
ll all = 0LL;
ll ans = 0LL;
for(int i = li.size()-1; i>=0; i--){
ans += (all-fen.get(li[i].first))*(ll)li[i].second;
fen.add(li[i].first,li[i].second);
all += li[i].second;
}
cout << ans << endl;
}
// for(int i = 1; i<n; i++){
// // cout << i << " " << n << endl;
// // cout << "! " << dfs(0,-1).first << endl;
// merge(dfs(0,-1).first,i);
// }
// pair<int, int> got = dfs(5,-1);
// dfs2(got.first);
// pair<int, int> gotten = split(got.first,4);
// p[gotten.first] = -1;
// p[gotten.second] = -1;
// dfs2(dfs(0,-1).first);
// dfs2(dfs(4,-1).first);
}
Compilation message
construction.cpp: In function 'std::vector<std::pair<int, int> > compress(std::vector<std::pair<int, int> >)':
construction.cpp:116:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<a.size(); i++){
~^~~~~~~~~
construction.cpp:122:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<a.size(); i++){
~^~~~~~~~~
construction.cpp:127:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i<a.size(); i++){
~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
3 ms |
644 KB |
Output is correct |
5 |
Correct |
4 ms |
644 KB |
Output is correct |
6 |
Correct |
4 ms |
700 KB |
Output is correct |
7 |
Correct |
4 ms |
728 KB |
Output is correct |
8 |
Correct |
3 ms |
740 KB |
Output is correct |
9 |
Correct |
4 ms |
740 KB |
Output is correct |
10 |
Correct |
3 ms |
740 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
892 KB |
Output is correct |
13 |
Correct |
3 ms |
892 KB |
Output is correct |
14 |
Correct |
3 ms |
892 KB |
Output is correct |
15 |
Correct |
4 ms |
892 KB |
Output is correct |
16 |
Correct |
6 ms |
908 KB |
Output is correct |
17 |
Correct |
7 ms |
908 KB |
Output is correct |
18 |
Correct |
5 ms |
920 KB |
Output is correct |
19 |
Correct |
5 ms |
944 KB |
Output is correct |
20 |
Correct |
4 ms |
944 KB |
Output is correct |
21 |
Correct |
5 ms |
960 KB |
Output is correct |
22 |
Correct |
5 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
980 KB |
Output is correct |
24 |
Correct |
5 ms |
988 KB |
Output is correct |
25 |
Correct |
4 ms |
996 KB |
Output is correct |
26 |
Correct |
4 ms |
1004 KB |
Output is correct |
27 |
Correct |
4 ms |
1012 KB |
Output is correct |
28 |
Correct |
3 ms |
1020 KB |
Output is correct |
29 |
Correct |
4 ms |
1028 KB |
Output is correct |
30 |
Correct |
4 ms |
1036 KB |
Output is correct |
31 |
Correct |
4 ms |
1044 KB |
Output is correct |
32 |
Correct |
4 ms |
1052 KB |
Output is correct |
33 |
Correct |
4 ms |
1060 KB |
Output is correct |
34 |
Correct |
4 ms |
1068 KB |
Output is correct |
35 |
Correct |
5 ms |
1076 KB |
Output is correct |
36 |
Correct |
4 ms |
1084 KB |
Output is correct |
37 |
Correct |
5 ms |
1092 KB |
Output is correct |
38 |
Correct |
5 ms |
1100 KB |
Output is correct |
39 |
Correct |
4 ms |
1108 KB |
Output is correct |
40 |
Correct |
4 ms |
1116 KB |
Output is correct |
41 |
Correct |
5 ms |
1124 KB |
Output is correct |
42 |
Correct |
5 ms |
1132 KB |
Output is correct |
43 |
Correct |
5 ms |
1140 KB |
Output is correct |
44 |
Correct |
4 ms |
1140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
3 ms |
644 KB |
Output is correct |
5 |
Correct |
4 ms |
644 KB |
Output is correct |
6 |
Correct |
4 ms |
700 KB |
Output is correct |
7 |
Correct |
4 ms |
728 KB |
Output is correct |
8 |
Correct |
3 ms |
740 KB |
Output is correct |
9 |
Correct |
4 ms |
740 KB |
Output is correct |
10 |
Correct |
3 ms |
740 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
892 KB |
Output is correct |
13 |
Correct |
3 ms |
892 KB |
Output is correct |
14 |
Correct |
3 ms |
892 KB |
Output is correct |
15 |
Correct |
4 ms |
892 KB |
Output is correct |
16 |
Correct |
6 ms |
908 KB |
Output is correct |
17 |
Correct |
7 ms |
908 KB |
Output is correct |
18 |
Correct |
5 ms |
920 KB |
Output is correct |
19 |
Correct |
5 ms |
944 KB |
Output is correct |
20 |
Correct |
4 ms |
944 KB |
Output is correct |
21 |
Correct |
5 ms |
960 KB |
Output is correct |
22 |
Correct |
5 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
980 KB |
Output is correct |
24 |
Correct |
5 ms |
988 KB |
Output is correct |
25 |
Correct |
4 ms |
996 KB |
Output is correct |
26 |
Correct |
4 ms |
1004 KB |
Output is correct |
27 |
Correct |
4 ms |
1012 KB |
Output is correct |
28 |
Correct |
3 ms |
1020 KB |
Output is correct |
29 |
Correct |
4 ms |
1028 KB |
Output is correct |
30 |
Correct |
4 ms |
1036 KB |
Output is correct |
31 |
Correct |
4 ms |
1044 KB |
Output is correct |
32 |
Correct |
4 ms |
1052 KB |
Output is correct |
33 |
Correct |
4 ms |
1060 KB |
Output is correct |
34 |
Correct |
4 ms |
1068 KB |
Output is correct |
35 |
Correct |
5 ms |
1076 KB |
Output is correct |
36 |
Correct |
4 ms |
1084 KB |
Output is correct |
37 |
Correct |
5 ms |
1092 KB |
Output is correct |
38 |
Correct |
5 ms |
1100 KB |
Output is correct |
39 |
Correct |
4 ms |
1108 KB |
Output is correct |
40 |
Correct |
4 ms |
1116 KB |
Output is correct |
41 |
Correct |
5 ms |
1124 KB |
Output is correct |
42 |
Correct |
5 ms |
1132 KB |
Output is correct |
43 |
Correct |
5 ms |
1140 KB |
Output is correct |
44 |
Correct |
4 ms |
1140 KB |
Output is correct |
45 |
Correct |
17 ms |
1156 KB |
Output is correct |
46 |
Correct |
28 ms |
1344 KB |
Output is correct |
47 |
Correct |
19 ms |
1380 KB |
Output is correct |
48 |
Correct |
19 ms |
1456 KB |
Output is correct |
49 |
Correct |
14 ms |
1532 KB |
Output is correct |
50 |
Correct |
20 ms |
1608 KB |
Output is correct |
51 |
Correct |
16 ms |
1684 KB |
Output is correct |
52 |
Correct |
20 ms |
1888 KB |
Output is correct |
53 |
Correct |
24 ms |
1944 KB |
Output is correct |
54 |
Correct |
21 ms |
2020 KB |
Output is correct |
55 |
Correct |
25 ms |
2096 KB |
Output is correct |
56 |
Correct |
25 ms |
2096 KB |
Output is correct |
57 |
Correct |
28 ms |
2120 KB |
Output is correct |
58 |
Correct |
24 ms |
2176 KB |
Output is correct |
59 |
Correct |
27 ms |
2252 KB |
Output is correct |
60 |
Correct |
24 ms |
2328 KB |
Output is correct |
61 |
Correct |
25 ms |
2404 KB |
Output is correct |
62 |
Correct |
25 ms |
2480 KB |
Output is correct |
63 |
Correct |
18 ms |
2700 KB |
Output is correct |
64 |
Correct |
22 ms |
2700 KB |
Output is correct |
65 |
Correct |
34 ms |
2724 KB |
Output is correct |
66 |
Correct |
29 ms |
2860 KB |
Output is correct |
67 |
Correct |
18 ms |
2860 KB |
Output is correct |
68 |
Correct |
13 ms |
2860 KB |
Output is correct |
69 |
Correct |
15 ms |
3016 KB |
Output is correct |
70 |
Correct |
19 ms |
3016 KB |
Output is correct |
71 |
Correct |
16 ms |
3016 KB |
Output is correct |
72 |
Correct |
21 ms |
3056 KB |
Output is correct |
73 |
Correct |
24 ms |
3112 KB |
Output is correct |
74 |
Correct |
16 ms |
3212 KB |
Output is correct |
75 |
Correct |
14 ms |
3300 KB |
Output is correct |
76 |
Correct |
14 ms |
3300 KB |
Output is correct |
77 |
Correct |
20 ms |
3336 KB |
Output is correct |
78 |
Correct |
14 ms |
3428 KB |
Output is correct |
79 |
Correct |
16 ms |
3516 KB |
Output is correct |
80 |
Correct |
20 ms |
3516 KB |
Output is correct |
81 |
Correct |
24 ms |
3576 KB |
Output is correct |
82 |
Correct |
18 ms |
3668 KB |
Output is correct |
83 |
Correct |
26 ms |
3688 KB |
Output is correct |
84 |
Correct |
21 ms |
3752 KB |
Output is correct |
85 |
Correct |
19 ms |
3800 KB |
Output is correct |
86 |
Correct |
17 ms |
3892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
544 KB |
Output is correct |
4 |
Correct |
3 ms |
644 KB |
Output is correct |
5 |
Correct |
4 ms |
644 KB |
Output is correct |
6 |
Correct |
4 ms |
700 KB |
Output is correct |
7 |
Correct |
4 ms |
728 KB |
Output is correct |
8 |
Correct |
3 ms |
740 KB |
Output is correct |
9 |
Correct |
4 ms |
740 KB |
Output is correct |
10 |
Correct |
3 ms |
740 KB |
Output is correct |
11 |
Correct |
3 ms |
748 KB |
Output is correct |
12 |
Correct |
3 ms |
892 KB |
Output is correct |
13 |
Correct |
3 ms |
892 KB |
Output is correct |
14 |
Correct |
3 ms |
892 KB |
Output is correct |
15 |
Correct |
4 ms |
892 KB |
Output is correct |
16 |
Correct |
6 ms |
908 KB |
Output is correct |
17 |
Correct |
7 ms |
908 KB |
Output is correct |
18 |
Correct |
5 ms |
920 KB |
Output is correct |
19 |
Correct |
5 ms |
944 KB |
Output is correct |
20 |
Correct |
4 ms |
944 KB |
Output is correct |
21 |
Correct |
5 ms |
960 KB |
Output is correct |
22 |
Correct |
5 ms |
972 KB |
Output is correct |
23 |
Correct |
5 ms |
980 KB |
Output is correct |
24 |
Correct |
5 ms |
988 KB |
Output is correct |
25 |
Correct |
4 ms |
996 KB |
Output is correct |
26 |
Correct |
4 ms |
1004 KB |
Output is correct |
27 |
Correct |
4 ms |
1012 KB |
Output is correct |
28 |
Correct |
3 ms |
1020 KB |
Output is correct |
29 |
Correct |
4 ms |
1028 KB |
Output is correct |
30 |
Correct |
4 ms |
1036 KB |
Output is correct |
31 |
Correct |
4 ms |
1044 KB |
Output is correct |
32 |
Correct |
4 ms |
1052 KB |
Output is correct |
33 |
Correct |
4 ms |
1060 KB |
Output is correct |
34 |
Correct |
4 ms |
1068 KB |
Output is correct |
35 |
Correct |
5 ms |
1076 KB |
Output is correct |
36 |
Correct |
4 ms |
1084 KB |
Output is correct |
37 |
Correct |
5 ms |
1092 KB |
Output is correct |
38 |
Correct |
5 ms |
1100 KB |
Output is correct |
39 |
Correct |
4 ms |
1108 KB |
Output is correct |
40 |
Correct |
4 ms |
1116 KB |
Output is correct |
41 |
Correct |
5 ms |
1124 KB |
Output is correct |
42 |
Correct |
5 ms |
1132 KB |
Output is correct |
43 |
Correct |
5 ms |
1140 KB |
Output is correct |
44 |
Correct |
4 ms |
1140 KB |
Output is correct |
45 |
Correct |
17 ms |
1156 KB |
Output is correct |
46 |
Correct |
28 ms |
1344 KB |
Output is correct |
47 |
Correct |
19 ms |
1380 KB |
Output is correct |
48 |
Correct |
19 ms |
1456 KB |
Output is correct |
49 |
Correct |
14 ms |
1532 KB |
Output is correct |
50 |
Correct |
20 ms |
1608 KB |
Output is correct |
51 |
Correct |
16 ms |
1684 KB |
Output is correct |
52 |
Correct |
20 ms |
1888 KB |
Output is correct |
53 |
Correct |
24 ms |
1944 KB |
Output is correct |
54 |
Correct |
21 ms |
2020 KB |
Output is correct |
55 |
Correct |
25 ms |
2096 KB |
Output is correct |
56 |
Correct |
25 ms |
2096 KB |
Output is correct |
57 |
Correct |
28 ms |
2120 KB |
Output is correct |
58 |
Correct |
24 ms |
2176 KB |
Output is correct |
59 |
Correct |
27 ms |
2252 KB |
Output is correct |
60 |
Correct |
24 ms |
2328 KB |
Output is correct |
61 |
Correct |
25 ms |
2404 KB |
Output is correct |
62 |
Correct |
25 ms |
2480 KB |
Output is correct |
63 |
Correct |
18 ms |
2700 KB |
Output is correct |
64 |
Correct |
22 ms |
2700 KB |
Output is correct |
65 |
Correct |
34 ms |
2724 KB |
Output is correct |
66 |
Correct |
29 ms |
2860 KB |
Output is correct |
67 |
Correct |
18 ms |
2860 KB |
Output is correct |
68 |
Correct |
13 ms |
2860 KB |
Output is correct |
69 |
Correct |
15 ms |
3016 KB |
Output is correct |
70 |
Correct |
19 ms |
3016 KB |
Output is correct |
71 |
Correct |
16 ms |
3016 KB |
Output is correct |
72 |
Correct |
21 ms |
3056 KB |
Output is correct |
73 |
Correct |
24 ms |
3112 KB |
Output is correct |
74 |
Correct |
16 ms |
3212 KB |
Output is correct |
75 |
Correct |
14 ms |
3300 KB |
Output is correct |
76 |
Correct |
14 ms |
3300 KB |
Output is correct |
77 |
Correct |
20 ms |
3336 KB |
Output is correct |
78 |
Correct |
14 ms |
3428 KB |
Output is correct |
79 |
Correct |
16 ms |
3516 KB |
Output is correct |
80 |
Correct |
20 ms |
3516 KB |
Output is correct |
81 |
Correct |
24 ms |
3576 KB |
Output is correct |
82 |
Correct |
18 ms |
3668 KB |
Output is correct |
83 |
Correct |
26 ms |
3688 KB |
Output is correct |
84 |
Correct |
21 ms |
3752 KB |
Output is correct |
85 |
Correct |
19 ms |
3800 KB |
Output is correct |
86 |
Correct |
17 ms |
3892 KB |
Output is correct |
87 |
Correct |
65 ms |
4280 KB |
Output is correct |
88 |
Correct |
194 ms |
5708 KB |
Output is correct |
89 |
Correct |
759 ms |
9828 KB |
Output is correct |
90 |
Correct |
642 ms |
12068 KB |
Output is correct |
91 |
Incorrect |
575 ms |
14044 KB |
Output isn't correct |
92 |
Halted |
0 ms |
0 KB |
- |