# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
499376 | sumit_kk10 | Mountains (NOI20_mountains) | C++17 | 123 ms | 32584 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
using namespace std;
const int N = 1e6 + 5;
int n, fenwick[N];
void upd(int x, int val){
for(int i = x; i < N; i += (i & -i))
fenwick[i] += val;
}
int get(int x){
int ans = 0;
for(int i = x; i >= 1; i -= (i & -i))
ans += fenwick[i];
return ans;
}
void solve(){
cin >> n;
vector<pair<long long, int> > v(n);
for(int i = 0; i < n; ++i){
cin >> v[i].first;
v[i].second = i;
}
vector<long long> a(n);
sort(v.begin(), v.end());
int ct = 1;
a[v[0].second] = ct;
for(int i = 1; i < n; ++i){
if(v[i].first != v[i - 1].first)
++ct;
a[v[i].second] = ct;
}
// for(auto k : a) cout << k << ' ';
// cout << endl;
long long pre[N], suf[N];
for(int i = 0; i < n; ++i){
int x = get(a[i] - 1);
upd(a[i], 1);
// cout << x << ' ';
pre[i] = x;
}
// cout << endl;
memset(fenwick, 0, sizeof fenwick);
for(int i = n - 1; i >= 0; --i){
int x = get(a[i] - 1);
upd(a[i], 1);
suf[i] = x;
}
long long ans = 0;
for(int i = 1; i < n - 1; ++i)
ans += (pre[i] * suf[i]);
cout << ans << '\n';
}
int main() {
fast;
int t = 1;
// cin >> t;
while(t--)
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |