forked from Jonathan-Uy/CSES-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSorting Methods.cpp
68 lines (56 loc) · 1.26 KB
/
Sorting Methods.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxN = 2e5+5;
int N, x[maxN], ds[maxN];
ll bit[maxN], ans[4];
set<int> S;
set<int>::iterator it;
int find(int u){
if(ds[u] < 0) return u;
ds[u] = find(ds[u]);
return ds[u];
}
bool merge(int u, int v){
u = find(u); v = find(v);
if(u == v) return false;
if(ds[u] < ds[v]) swap(u, v);
ds[v] += ds[u];
ds[u] = v;
return true;
}
void update(int idx, int val){
for(int i = idx; i < maxN; i += -i&i)
bit[i] += val;
}
ll query(int idx){
int sum = 0;
for(int i = idx; i > 0; i -= -i&i)
sum += bit[i];
return sum;
}
int main(){
scanf("%d", &N);
for(int i = 1; i <= N; i++)
scanf("%d", &x[i]);
int K = N;
ans[3] = N;
fill(ds+1, ds+N+1, -1);
for(int i = 1; i <= N; i++){
ans[0] += (i-query(x[i])-1);
update(x[i], 1);
if(!merge(i, x[i]))
ans[1] += (-ds[find(i)]-1);
S.insert(x[i]);
it = S.lower_bound(x[i]);
if(++it != S.end())
S.erase(it);
if(x[N-i+1] == K){
K--;
ans[3]--;
}
}
ans[2] = N - (int) S.size();
for(int i = 0; i < 4; i++)
printf("%lld%c", ans[i], (" \n")[i==3]);
}