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 <iostream>
#include <vector>
#include <algorithm>
#define ll long long
#define pii pair<ll, int>
#define fst first
#define snd second
using namespace std;
int n, fenwick[300001] = {};
vector<pii> dt;
ll to = 0;
int query(int x)
{
int to = 0;
for (; x; x -= x & (-x)) {to += fenwick[x];}
return to;
}
void upd(int x)
{
for (; x <= n; x += x & (-x)) {fenwick[x]++;}
}
int main()
{
ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {ll x; cin >> x; dt.push_back({x, i + 1});}
sort(dt.begin(), dt.end());
int i = 0;
while (i < n)
{
to += (ll)query(dt[i].snd) * (i - query(dt[i].snd));
int j = i + 1;
for (; j < n && dt[j].fst == dt[j - 1].fst; j++) {to += (ll)query(dt[j].snd) * (i - query(dt[j].snd));}
for (; i < j; i++) {upd(dt[i].snd);}
}
cout << to << "\n";
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... |