Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int, int>
- #define pb(x) push_back(x)
- using namespace std;
- using ll = long long;
- using ld = long double;
- using db = double;
- void cv(vector <int> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvl(vector <ll> &v) {
- for (auto x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvv(vector <vector <int> > &v) {
- for (auto x : v) cv(x);
- cout << "\n";
- }
- void cvb(vector <bool> v) {
- for (bool x : v) cout << x << ' ';
- cout << "\n";
- }
- void cvs(vector <string> v) {
- for (auto a : v) {
- cout << a << "\n";
- }
- }
- void cvp(vector <pii> a) {
- for (auto p : a) {
- cout << p.first << ' ' << p.second << "\n";
- }
- cout << "\n";
- }
- ll ans = 0;
- bool sh = 0;
- vector <int> a;
- void mrg(int l, int m, int r) {
- if (sh) {
- cout << "l r = " << l << ' ' << r << "\n";
- }
- int k = m;
- for (int i = l; i <= m ; ++i) {
- if (a[m + 1] >= a[i]) continue;
- if (sh) {
- cout << "i = " << i << "\n";
- cout << "a\n"; cv(a);
- }
- int L = m + 1, R = r + 1, M;
- while (L + 1 < R) {
- M = (L + R) / 2;
- if (sh) {
- cout << "M = " << M << "\n";
- }
- if (a[M] >= a[i]) {
- R = M;
- } else {
- L = M;
- }
- }
- ans += (m - i + 1) * (L - k);
- if (sh) {
- cout << "ans += " << (m - i + 1) * (L - k) << "\n";
- }
- k = L;
- }
- vector <int> res;
- int A = l, B = m + 1;
- while (A <= m && B <= r) {
- if (a[A] < a[B]) {
- res.pb(a[A]);
- A++;
- } else {
- res.pb(a[B]);
- B++;
- }
- }
- while (A <= m) {
- res.pb(a[A]);
- A++;
- }
- while (B <= r) {
- res.pb(a[B]);
- B++;
- }
- for (int i = l; i <= r; ++i) {
- a[i] = res[i - l];
- }
- }
- void f(int l, int r) {
- if (l == r) {
- return;
- } else if (l + 1 == r) {
- if (a[l] > a[r]) {
- ans++;
- swap(a[l], a[r]);
- }
- return;
- }
- int m = (l + r) / 2;
- f(l, m);
- f(m + 1, r);
- mrg(l, m, r);
- }
- /*
- 5
- 179 4 3 2 1
- 9
- 179 4 3 2 1 11 100 0 2
- */
- int main() {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- int n; cin >> n;
- a.resize(n);
- for (int &i : a) cin >> i;
- f(0, n - 1);
- cout << ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement