Submission #1114810

#TimeUsernameProblemLanguageResultExecution timeMemory
1114810ljtunasMountains (NOI20_mountains)C++14
100 / 100
179 ms29028 KiB
// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v)    memset(h, v, sizeof h)
#define Forv(i, a)     for(auto& i : a)
#define For(i, a, b)   for(int i = a; i <= b; ++i)
#define Ford(i, a, b)  for(int i = a; i >= b; --i)
#define c_bit(i)     __builtin_popcountll(i)
#define Bit(mask, i)    ((mask >> i) & 1LL)
#define onbit(mask, i)  ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
namespace std {
    template <typename T, int D>
    struct _vector : public vector <_vector <T, D - 1>> {
        static_assert(D >= 1, "Dimension must be positive!");
        template <typename... Args>
        _vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
    };
    template <typename T> struct _vector <T, 1> : public vector <T> {
        _vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
    };
    template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
    template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e6 + 5;
const  ll  mod = 998244353;
const  ll   oo = 1e18;

//#define int long long

int n; ll h[MAXN], suff[MAXN], pref[MAXN], bit[MAXN];

void update(int u, int val){
    while(u <= 1e6) bit[u] += val, u += u &- u;
}

int get(int u){
    int res = 0;
    while(u) res += bit[u], u -= u &- u;
    return res;
}

void Solve() {
    cin >> n;
    _vector<ll, 1> compress;
    For(i, 1, n) cin >> h[i], compress.push_back(h[i]);
    sort(all(compress)), compress.resize(unique(all(compress)) - compress.begin());
    For(i, 1, n) h[i] = lower_bound(all(compress), h[i]) - compress.begin() + 1;
    reset(bit, 0);
    For(i, 1, n) {
        pref[i] = get(h[i] - 1);
        update(h[i], 1);
    }
    reset(bit, 0);
    Ford(i, n, 1) {
        suff[i] = get(h[i] - 1);
        update(h[i], 1);
    }
    ll ans = 0;
    For(i, 1, n) ans += pref[i] * suff[i];
    cout << ans << '\n';
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
     // if(fopen("a.inp", "r")) {
     //     freopen("a.inp", "r", stdin);
     //     freopen("a.out", "w", stdout);
     // }

    int t = 1;
//    cin >> t;
    while(t --) Solve();

    return 0;
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...