# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1095357 | kh0i | Mountains (NOI20_mountains) | C++17 | 140 ms | 19460 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>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r){
assert(l <= r);
return uniform_int_distribution<ll>(l, r)(rng);
}
const int N = 3e5 + 3;
struct Fenwick{
#define gb(x) (x) & -(x)
int n;
vector<ll> bit;
Fenwick(int n) : n(n), bit(n + 2) {}
void add(int pos, ll x){
for(; pos <= n; pos += gb(pos))
bit[pos] += x;
}
ll get(int l, int r){
ll res = 0;
--l;
for(; r > 0; r -= gb(r)) res += bit[r];
for(; l > 0; l -= gb(l)) res -= bit[l];
return res;
}
};
int n;
ll a[N], r[N];
vector<ll> xx;
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
xx.push_back(a[i]);
}
sort(all(xx));
xx.resize(unique(all(xx)) - xx.begin());
for(int i = 1; i <= n; ++i)
a[i] = lower_bound(all(xx), a[i]) - xx.begin() + 1;
Fenwick fi(n);
for(int i = n; i >= 1; --i){
r[i] = fi.get(1, a[i] - 1);
fi.add(a[i], 1);
}
Fenwick se(n);
ll res = 0;
for(int i = 1; i <= n; ++i){
res += se.get(1, a[i] - 1) * r[i];
se.add(a[i], 1);
}
cout << res;
}
signed main(){
cin.tie(nullptr)->sync_with_stdio(false);
#define task "troll"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
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... |