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;
constexpr int mod = 1e9 + 7;
template <typename T>
struct fenwick_tree {
int n;
vector<T> bit;
fenwick_tree(int _n) : n(_n), bit(n + 1) {
}
void apply(int p, T v) {
for (int i = p + 1; i <= n; i += i & -i) {
bit[i] += v;
}
}
T query(int p) {
T ans{};
for (int i = p; i > 0; i -= i & -i) {
ans += bit[i];
}
return ans;
}
T range_query(int l, int r) {
return query(r) - query(l);
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto b = a;
sort(b.begin(), b.end());
for (int i = 0; i < n; i++) {
a[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
}
vector<int> fac(n + 1);
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = 1LL * fac[i - 1] * i % mod;
}
int ans = 1;
fenwick_tree<int> fw(n);
for (int i = 0; i < n; i++) {
ans = (ans + 1LL * (a[i] + fw.range_query(a[i] + 1, n) - i) * fac[n - i - 1] % mod) % mod;
fw.apply(a[i], 1);
}
cout << ans << "\n";
return 6/22;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |