結果

問題 No.1300 Sum of Inversions
ユーザー queeequeee
提出日時 2020-11-27 22:36:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 3,168 bytes
コンパイル時間 1,720 ms
コンパイル使用メモリ 105,308 KB
実行使用メモリ 12,584 KB
最終ジャッジ日時 2023-10-09 20:39:21
合計ジャッジ時間 8,989 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 192 ms
10,196 KB
testcase_04 AC 192 ms
10,088 KB
testcase_05 AC 151 ms
9,000 KB
testcase_06 AC 229 ms
11,264 KB
testcase_07 AC 221 ms
10,676 KB
testcase_08 AC 254 ms
11,512 KB
testcase_09 AC 241 ms
11,516 KB
testcase_10 AC 120 ms
7,712 KB
testcase_11 AC 122 ms
8,028 KB
testcase_12 AC 193 ms
10,120 KB
testcase_13 AC 186 ms
9,884 KB
testcase_14 AC 273 ms
12,236 KB
testcase_15 AC 249 ms
11,544 KB
testcase_16 AC 205 ms
10,388 KB
testcase_17 AC 116 ms
7,772 KB
testcase_18 AC 135 ms
8,340 KB
testcase_19 AC 164 ms
9,404 KB
testcase_20 AC 171 ms
9,420 KB
testcase_21 AC 174 ms
9,352 KB
testcase_22 AC 149 ms
8,980 KB
testcase_23 AC 231 ms
11,152 KB
testcase_24 AC 155 ms
9,088 KB
testcase_25 AC 130 ms
8,384 KB
testcase_26 AC 130 ms
8,096 KB
testcase_27 AC 144 ms
8,988 KB
testcase_28 AC 259 ms
11,684 KB
testcase_29 AC 172 ms
9,380 KB
testcase_30 AC 243 ms
11,464 KB
testcase_31 AC 150 ms
9,020 KB
testcase_32 AC 157 ms
9,156 KB
testcase_33 AC 123 ms
12,584 KB
testcase_34 AC 176 ms
12,204 KB
testcase_35 AC 141 ms
12,272 KB
testcase_36 AC 162 ms
12,216 KB
権限があれば一括ダウンロードができます

ソースコード

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 = 998244353;
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 %= M;
    an += (b1.sum(id+1,n-1) * c2.sum(0,id-1) % M  + b2.sum(0,id-1) * c1.sum(id+1,n-1) % M) % M;
    an %= M;
  }
  cout<<an<<endl;
}

// ・配列の大きさok? ・priority_queueはgreater? ・debug消した?
// ・落ち着いて。提出まで4分待ってね……WJ……1/10……2/10……

// Thank you for making problems and running the contest
0