This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 300050
#define mod 1000000007
vector<int> seg(maxn*4,0);
void build(int node,int start,int end){
if(start==end){
seg[node] = 1;
return;
}
int mid = (start+end)/2;
build(node*2+1,start,mid);
build(node*2+2,mid+1,end);
seg[node] = seg[node*2+1]+seg[node*2+2];
return;
}
void update(int node,int start,int end,int pos){
if(start==end&&start==pos){
seg[node]--;
return;
}
int mid = (start+end)/2;
if(pos<=mid){
update(node*2+1,start,mid,pos);
}
else{
update(node*2+2,mid+1,end,pos);
}
seg[node] = seg[node*2+1]+seg[node*2+2];
}
int query(int node,int start,int end,int rangemin,int rangemax){
if(start>rangemax||end<rangemin){
return 0;
}
if(start>=rangemin&&end<=rangemax){
return seg[node];
}
int mid = (start+end)/2;
return query(node*2+1,start,mid,rangemin,rangemax)+query(node*2+2,mid+1,end,rangemin,rangemax);
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int no_of_input;
vector<int> vect1;
vector<int> vect2;
int input;
cin >> no_of_input;
for(int i=0;i<no_of_input;i++){
cin >> input;
vect1.push_back(input);
vect2.push_back(input);
}
sort(vect2.begin(),vect2.end());
build(0,0,vect1.size()-1);
/*for(auto k:vect2){
cout << k << " ";
}
cout << "--\n";*/
for(int i=0;i<vect1.size();i++){
int temp = vect1[i];
vect1[i] = lower_bound(vect2.begin(),vect2.end(),temp)-vect2.begin();
}
vector<int> fact(maxn,0);
fact[1] = 1;
for(int i=2;i<fact.size();i++){
fact[i] = fact[i-1]*i;
fact[i] %= mod;
}
int sum = 0;
for(int i=0;i<vect1.size();i++){
int currsmaller = 0;
if(vect1[i]!=0){
currsmaller = query(0,0,vect1.size()-1,0,vect1[i]-1);
}
sum += fact[no_of_input-i-1]*currsmaller;
sum %= mod;
update(0,0,vect1.size()-1,vect1[i]);
}
sum++;
sum %= mod;
cout << sum;
}
Compilation message (stderr)
Crypto.cpp: In function 'int32_t main()':
Crypto.cpp:62:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i=0;i<vect1.size();i++){
| ~^~~~~~~~~~~~~
Crypto.cpp:68:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int i=2;i<fact.size();i++){
| ~^~~~~~~~~~~~
Crypto.cpp:73:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0;i<vect1.size();i++){
| ~^~~~~~~~~~~~~
# | 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... |