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;
#define futaba ios_base::sync_with_stdio(false); cin.tie(NULL);
#define rio return 0;
#define ll long long
const ll mod = 1e9 + 7;
// Fun things are fun. //
ll a[300005], b[300005], fac[300005], seg[1200005];
ll merge(ll a, ll b) {
return a + b;
}
void build(int l, int r, int v) {
if(l == r) seg[v] = 1;
else {
int mid = l + (r - l) / 2;
build(l, mid, 2 * v);
build(mid + 1, r, (2 * v) + 1);
seg[v] = merge(seg[2 * v], seg[(2 * v) + 1]);
}
}
void update(int l, int r, int v, int pos) {
if(l == r) seg[v] = 0;
else {
int mid = l + (r - l) / 2;
if(pos <= mid) update(l, mid, 2 * v, pos);
else update(mid + 1, r, (2 * v) + 1, pos);
seg[v]--;
}
}
ll query(int l, int r, int v, int lq, int rq) {
if(lq <= l and r <= rq) return seg[v];
if(lq > r or rq < l) return 0;
int mid = l + (r - l) / 2;
return merge(query(l, mid, 2 * v, lq, rq), query(mid + 1, r, (2 * v) + 1, lq, rq));
}
int main() {
/* freopen(".txt", "r", stdin);
freopen(".txt", "w", stdout); */
futaba
int n;
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
b[i] = a[i];
}
sort(b + 1, b + n + 1);
map<ll, int> mp;
for(int i = 1; i <= n; i++) mp[b[i]] = i;
vector<ll> v(n + 1);
for(int i = 1; i <= n; i++) v[i] = mp[a[i]];
fac[0] = 1;
for(int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % mod;
build(1, n, 1);
ll ans = 1;
for(int i = 1; i <= n; i++) {
ans += fac[n - i] * query(1, n, 1, 1, v[i] - 1);
ans %= mod;
update(1, n, 1, v[i]);
}
if(ans < 0) ans += mod;
cout << ans << '\n';
rio
}
# | 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... |