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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cstring>
#define init_io(pname) ifstream cin((string)pname+".in"); ofstream cout((string)pname+".out"); ios_base::sync_with_stdio(false); cin.tie(NULL)
#define FOR(i, a, b, in) for (int i = (a); i < (b); i += in)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b, in) for (int i = (a) - 1; i >= (b); i -= in)
#define RREP(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define trav(a, x) for (auto& a : x)
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
#define mem(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef string str;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii; typedef pair<ll, ll> pl; typedef pair<ld, ld> pd;
typedef vector<int> vi; typedef vector<ll> vl; typedef vector<ld> vd;
typedef vector<ii> vii; typedef vector<pl> vpl; typedef vector<pd> vpd;
constexpr auto INF = (int)1e9;
constexpr auto LINF = (ll)1e18;
template<typename T> class fenwick_tree {
private: vector<T> FT;
public:
fenwick_tree(int N) { FT.assign(N + 1, 0); }
void clear() { FT.assign(FT.size(), 0); }
void update(int x, T val) { if (x > 0) for (; x < FT.size(); x += x & -x) FT[x] += val; }
T query(int x) { T ret = 0; if (x > 0) for (; x > 0; x -= x & -x) ret += FT[x]; return ret; }
T query(int x, int y) { return query(y) - (x == 1 ? 0 : query(x - 1)); }
};
template< typename T > class seg_tree {
private:
int N;
vector<T> seg, tmp;
public:
seg_tree(int size) {
N = size;
seg.resize(4 * N, 0);
tmp.resize(4 * N, 0);
}
seg_tree(int size, T A[]) {
N = size;
seg.resize(4 * N);
tmp.resize(4 * N);
build(A);
}
seg_tree(vector<T>& A) {
N = A.size();
seg.resize(4 * N);
tmp.resize(4 * N);
build(A);
}
void clear() { seg.assign(4 * N, 0); }
void pull(int n) { seg[n] = max(seg[n << 1], seg[n << 1 | 1]); }
void push(int l, int r, int n) {
seg[n] += tmp[n];
if (l != r) tmp[n << 1] += tmp[n], tmp[n << 1 | 1] += tmp[n];
tmp[n] = 0;
}
void build(T A[], int l = 0, int r = -1, int n = 1) {
if (r == -1) r = N - 1;
if (l == r) seg[n] = A[l];
else {
int m = (l + r) >> 1;
build(A, l, m, n << 1), build(A, m + 1, r, n << 1 | 1);
pull(n);
}
}
void build(vector<T>& A, int l = 0, int r = -1, int n = 1) {
if (r == -1) r = N - 1;
if (l == r) seg[n] = A[l];
else {
int m = (l + r) >> 1;
build(A, l, m, n << 1);
build(A, m + 1, r, n << 1 | 1);
pull(n);
}
}
void update(int x, T v, int l = 0, int r = -1, int n = 1) {
if (r == -1) r = N - 1;
if (l == r) seg[n] += v;
else {
int m = (l + r) >> 1;
x <= m ? update(x, v, l, m, n << 1) : update(x, v, m + 1, r, n << 1 | 1);
pull(n);
}
}
void update_range(int a, int b, T v, int l = 0, int r = -1, int n = 1) {
if (r == -1) r = N - 1;
push(n, l, r);
if (l > b || r < a) return;
if (l >= a && r <= b) {
tmp[n] = v;
push(n, l, r);
}
else {
int m = (l + r) >> 1;
update_range(a, b, v, l, m, n << 1), update_range(a, b, v, m + 1, r, n << 1 | 1);
pull(n);
}
}
T query(int a, int b, int l = 0, int r = -1, int n = 1) {
if (r == -1) r = N - 1;
if (l > b || r < a) return 0;
push(n, l, r);
if (l >= a && r <= b) return seg[n];
int m = (l + r) >> 1;
return max(query(a, b, l, m, n << 1), query(a, b, m + 1, r, n << 1 | 1));
}
};
int A[100005], C[100005] = { 0 };
int main() {
init_io("sort");
int N;
cin >> N;
for (int i = 0; i < N; i++) cin >> A[i];
vector<ii> tmp;
for (int i = 0; i < N; i++) tmp.emplace_back(A[i], i);
sort(tmp.begin(), tmp.end());
for (int i = 0; i < N; i++) A[tmp[i].second] = i;
ll ans = 0;
fenwick_tree<int> FT(N);
seg_tree<int> seg(N);
for (int i = 0; i < N; i++) {
ans += FT.query(A[i], N - 1);
FT.update(A[i], 1);
}
for (int i = 0; i < N; i++) seg.update(A[i], i);
for (int i = 0; i < N; i++) {
int q = seg.query(0, A[i]);
if (i != q) C[i]++, C[q]--, ans++;
}
FT.clear();
for (int i = N - 1; i >= 0; i--) {
ans += (ll)C[i] * FT.query(A[i], N - 1);
FT.update(A[i], 1);
}
cout << (ans ? ans : N) << endl;
}
|