結果

問題 No.1300 Sum of Inversions
ユーザー snow39snow39
提出日時 2020-11-27 21:44:04
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 2,000 ms
コード長 4,147 bytes
コンパイル時間 1,483 ms
コンパイル使用メモリ 113,056 KB
実行使用メモリ 19,896 KB
最終ジャッジ日時 2023-10-01 05:12:43
合計ジャッジ時間 12,812 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 296 ms
18,060 KB
testcase_04 AC 289 ms
17,760 KB
testcase_05 AC 229 ms
12,500 KB
testcase_06 AC 355 ms
18,760 KB
testcase_07 AC 355 ms
18,376 KB
testcase_08 AC 391 ms
19,396 KB
testcase_09 AC 389 ms
19,012 KB
testcase_10 AC 202 ms
11,772 KB
testcase_11 AC 203 ms
11,756 KB
testcase_12 AC 307 ms
17,916 KB
testcase_13 AC 290 ms
17,632 KB
testcase_14 AC 389 ms
19,812 KB
testcase_15 AC 352 ms
19,016 KB
testcase_16 AC 304 ms
18,204 KB
testcase_17 AC 179 ms
11,468 KB
testcase_18 AC 204 ms
12,108 KB
testcase_19 AC 250 ms
17,036 KB
testcase_20 AC 265 ms
17,160 KB
testcase_21 AC 263 ms
17,044 KB
testcase_22 AC 229 ms
12,564 KB
testcase_23 AC 337 ms
18,812 KB
testcase_24 AC 238 ms
12,756 KB
testcase_25 AC 201 ms
11,896 KB
testcase_26 AC 200 ms
12,028 KB
testcase_27 AC 224 ms
12,440 KB
testcase_28 AC 369 ms
19,320 KB
testcase_29 AC 258 ms
17,080 KB
testcase_30 AC 370 ms
18,948 KB
testcase_31 AC 251 ms
12,664 KB
testcase_32 AC 264 ms
12,808 KB
testcase_33 AC 259 ms
19,860 KB
testcase_34 AC 273 ms
19,844 KB
testcase_35 AC 263 ms
19,752 KB
testcase_36 AC 268 ms
19,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=998244353;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

#include <functional>
#include <climits>

//SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX);
template< typename T>
class SegmentTree{
private:
  void resize(int size){
      n=1;
      while(n<size) n*=2;
      tree.resize(2*n-1,def);
  }
public:

  using F = function<T(T,T)>;

  int n;
  vector<T> tree;
  F operation;
  T def;

  SegmentTree(){}
  SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){
    resize(size);
  }

  void embody(int size,F _operation,T _def){
    operation=_operation;
    def=_def;
    resize(size);
  }

  void initialize(const vector<T> &v){
    int size=v.size();
    resize(size);

    for(int i=0;i<size;i++) tree[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]);
  }

  void update(int index,T value){
    index+=n-1;

    tree[index]=value;
    while(index>0){
      index=(index-1)/2;
      tree[index]=operation(tree[2*index+1],tree[2*index+2]);
    }
  }

  T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b)
    if(r<0) r=n;

    if(r<=a||b<=l) return def;
    else if(a<=l&&r<=b) return tree[k];
    else{
      T lval=query(a,b,2*k+1,l,(l+r)/2);
      T rval=query(a,b,2*k+2,(l+r)/2,r);
      return operation(lval,rval);
    }
  }

  T get(int index){
      return tree[index+n-1];
  }

  int find(int x,int k=0,int l=0,int r=-1){// a[0]+...+a[i]>=x (i:minimal)
    if(r<0) r=n;
    if(tree[k]<x) return -1;
    if(r-l==1) return k-(n-1);
    if(tree[2*k+1]>=x) return find(x,2*k+1,l,(l+r)/2);
    else return find(x-tree[2*k+1],2*k+2,(l+r)/2,r);
  }

  //right -> change vl to vr   (notice doesn't contain b. range [a,b))
  int find_left(int a,int b,T x,int k=0,int l=0,int r=-1){// max(a[a],...,a[i])>=x (i:minimal)
      if(r<0) r=n;
      if(tree[k]<x||r<=a||b<=l) return -1;
      if(r-l==1) return k-(n-1);
      int vl=find_left(a,b,x,2*k+1,l,(l+r)/2);
      if(vl>=0) return vl;
      return find_left(a,b,x,2*k+2,(l+r)/2,r);
  }
};

ll mod_pow(ll x,ll n){
  x%=MOD;
  ll res=1;
  while(n>0){
    if(n&1) res=res*x%MOD;
    x=x*x%MOD;
    n>>=1;
  }
  return res;
}

ll mod_inverse(ll x){
  return mod_pow(x,MOD-2);
}

void add(ll &a,ll b){
  a=(a+b)%MOD;
}

void mul(ll &a,ll b){
  a%=MOD;b%=MOD;
  a=a*b%MOD;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  vector<ll> A(N);
  rep(i,N) cin>>A[i];

  vector<ll> leftNum(N,0),rightNum(N,0);
  vector<ll> leftSum(N,0),rightSum(N,0);
  {
    SegmentTree<pair<ll,ll>> seg(N,[](pair<ll,ll> a,pair<ll,ll> b){
      return mkp(a.first+b.first,a.second+b.second);
    },mkp(0ll,0ll));
    vector<int> ord(N,0);
    rep(i,N) ord[i]=i;
    sort(all(ord),[&](int a,int b){
      if(A[a]!=A[b]) return A[a]>A[b];
      else return a>b;
    });

    for(auto i:ord){
      auto f=seg.query(0,i);
      leftNum[i]=f.second;
      leftSum[i]=f.first%MOD;
      seg.update(i,mkp(A[i],1));
    }
  }
  {
    SegmentTree<pair<ll,ll>> seg(N,[](pair<ll,ll> a,pair<ll,ll> b){
      return mkp(a.first+b.first,a.second+b.second);
    },mkp(0ll,0ll));
    vector<int> ord(N,0);
    rep(i,N) ord[i]=i;
    sort(all(ord),[&](int a,int b){
      if(A[a]!=A[b]) return A[a]<A[b];
      else return a<b;
    });

    for(auto i:ord){
      auto f=seg.query(i,N);
      rightNum[i]=f.second;
      rightSum[i]=f.first%MOD;
      seg.update(i,mkp(A[i],1));
    }
  }

  ll ans=0;
  for(int j=0;j<N;j++){
    if(leftNum[j]==0||rightNum[j]==0) continue;
    ll ares=leftSum[j]*rightNum[j]%MOD;
    ll bres=rightSum[j]*leftNum[j]%MOD;
    ll res=leftNum[j]*rightNum[j]%MOD*A[j]%MOD;
    add(res,ares);
    add(res,bres);
    add(ans,res);
  }
  cout<<ans<<endl;

  return 0;
}
0