Submission #596572

#TimeUsernameProblemLanguageResultExecution timeMemory
596572AdamGSCryptography (NOI20_crypto)C++17
100 / 100
390 ms25316 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(a, b) for(int a = 0; a < (b); ++a) #define all(a) a.begin(), a.end() #define st first #define nd second #define pb push_back #define all(a) a.begin(), a.end() const ll MOD=1e9+7, LIM=3e5+7; int T[LIM], S[LIM], tr[4*LIM], N=1; ll sil[LIM]; map<int,int>mp; void upd(int v) { for(v+=N; v; v/=2) --tr[v]; } ll cnt(int l, int r) { l+=N; r+=N; ll ans=tr[l]; if(l!=r) ans+=tr[r]; while(l/2!=r/2) { if(l%2==0) ans+=tr[l+1]; if(r%2==1) ans+=tr[r-1]; l/=2; r/=2; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); sil[0]=1; for(ll i=1; i<LIM; ++i) sil[i]=(sil[i-1]*i)%MOD; int n; cin >> n; while(N<n) N*=2; rep(i, n) { cin >> T[i]; S[i]=T[i]; tr[N+i]=1; } for(int i=N-1; i; --i) tr[i]=tr[2*i]+tr[2*i+1]; sort(S, S+n); rep(i, n) mp[S[i]]=i; ll ans=1; rep(i, n) { ll x=cnt(0, mp[T[i]]); upd(mp[T[i]]); ans=(ans+sil[n-i-1]*(x-1))%MOD; } cout << ans << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...