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>
typedef long long ll;
#define pb push_back
#define MS(x,y) memset((x),(y),sizeof((x)))
const ll MN = 1000000007;
using namespace std;
ll n;
ll tree[300001];
ll niza[300001];
ll query(ll pos) {
ll ret = 0;
while (pos > 0) {
ret += tree[pos];
pos -= pos&(-pos);
}
return ret;
}
void update(ll pos, ll val) {
while (pos <= n) {
tree[pos] += val;
pos += pos&(-pos);
}
}
ll presmetaj (ll pos) {
ll levo = query(pos-1);
ll site = query(n);
return levo*(site-levo-1);
}
int main()
{
#if LOCAL_DEBUG
fstream cin("in.txt");
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
MS(tree,0);
cin>>n;
for (ll i = 1; i<=n; i++) cin>>niza[i];
vector<pair<ll,ll>> sorti(n);
for (ll i = 1; i<=n; i++) sorti[i] = {niza[i],i};
sort(sorti.begin(),sorti.end());
vector<ll> moment;
ll sea = -1, res = 0;
for (ll i = 1; i<=n; i++) {
ll a = sorti[i].first;
if (a != sea) {
for (auto it:moment) res += presmetaj(it);
vector<ll> nov;
swap(moment,nov);
}
sea = a;
update(sorti[i].second,1);
moment.pb(sorti[i].second);
}
for (auto it:moment) res += presmetaj(it);
cout<<res<<endl;
return 0;
}
# | 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... |