結果

問題 No.1300 Sum of Inversions
ユーザー queeequeee
提出日時 2020-11-27 22:18:10
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,147 bytes
コンパイル時間 1,271 ms
コンパイル使用メモリ 107,716 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-07-26 13:17:30
合計ジャッジ時間 8,858 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 2 WA * 32
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

//g++ t.cpp -o t && t < p.txt
//d>p.txt&&t<p.txt&&t2<p.txt
#include <iostream> //
#include <vector> //
#include <algorithm> //
#include <queue> //
#include <string> // to_string
#include <functional> //
#include <set> //
#include <map> // set
#include <random> //
#include <ctime> //
#include <bitset> // シャー
#include <cassert>
// #include <atcoder/all>
// using namespace atcoder; //
// g++ t.cpp -o t -I .
#define DB cerr<<"D"<<endl
using namespace std; using ll=long long; using ld=long double; const int INF=1e9; const ll LINF=1e18; const double dINF = 1e18; const ld ldINF = 1e18;
    const double EPS = 1e-6;
using P=pair<ll,ll>;
const ll M = 1000000007;
ll mod_pow(ll x, ll a) { ll an = 1; while(a > 0) { if (a&1) an = an * x % M; x = x * x % M; a >>= 1;} return an;}
ll mod_pow(ll x, ll a, ll m) { ll an = 1; while(a > 0) { if (a&1) an = an * x % m; x = x * x % m; a >>= 1;} return an;}
void add(ll& x, ll y) {x+=y; x%=M;}; void mul(ll& x, ll y) {x*=y; x%=M;}; template<typename T, typename U> void chmax(T& x, U y) {if (x<y) x=y;};
    template<typename T, typename U> void chmin(T& x, U y) {if (x>y) x=y;} bool vaild(int x, int y, int hh, int ww){return 0<=x&&x<hh&&0<=y&&y<ww;}
ll gcd(ll a, ll b) {if (b==0) return a; else return gcd(b, a%b);}
int keta(ll a) {int res=0; while(a>0) res+=a%10, a/=10; return res;}
const int up[]={1,-1,0,0}, lf[]={0,0,1,-1};
struct BIT { // 0-index
int n; vector<ll> v;
BIT(int m) : n(m) {v.resize(m);}
ll sum(int i) {
//cout<<"sum "<<i<<" "<<n<<" ";
if (i<0 || i>=n) return 0;
ll res=0; for(; i>=0; i=(i&(i+1))-1) res+=v[i]; res%=M; return res;
}
ll sum(int a, int b) {
return (sum(b) - sum(a-1) + M )% M ;
}
void add(int i, ll x) {for(;i<n;i|=i+1) v[i]+=x,v[i]%=M;}
void print() {
for(int i=0;i<n;i++) cout<<sum(i)-sum(i-1)<<" "; cout<<endl;
}
};
int main() {
int n; cin>>n;
ll a[n]; for(int i=0;i<n;i++) cin>>a[i];
ll b[n]; copy(a,a+n,b); sort(b,b+n);
BIT b1(n),b2(n),c1(n),c2(n);
for(int i=0;i<n;i++) {
int id = lower_bound(b,b+n,a[i])-b;
b2.add(id,1); c2.add(id,a[i]);
}
ll an=0;
for(int i=0;i<n;i++) {
int id = lower_bound(b,b+n,a[i])-b;
b1.add(id,1);
c1.add(id,a[i]);
b2.add(id,-1);
c2.add(id,-a[i]);
ll nu = b2.sum(0,id-1) * b1.sum(id+1,n-1) % M;
//cout << "@ id " << id << " a " << a[i] << " " << nu << " " << c2.sum(0,id-1) << " " << c1.sum(id+1,n-1) << endl;
//b1.print(); b2.print(); c1.print(); c2.print();
an += a[i] * nu % M;
an += (b1.sum(id+1,n-1) * c2.sum(0,id-1) + b2.sum(0,id-1) * c1.sum(id+1,n-1)) % M;
an %= M;
}
cout<<an<<endl;
}
// ok priority_queuegreater debug
// 4……WJ……1/10……2/10……
// Thank you for making problems and running the contest
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0