結果

問題 No.1300 Sum of Inversions
ユーザー beetbeet
提出日時 2020-11-27 22:02:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,028 bytes
コンパイル時間 5,646 ms
コンパイル使用メモリ 247,276 KB
実行使用メモリ 14,664 KB
最終ジャッジ日時 2023-10-01 05:58:44
合計ジャッジ時間 43,740 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1,139 ms
11,968 KB
testcase_04 AC 1,118 ms
11,712 KB
testcase_05 AC 832 ms
10,412 KB
testcase_06 AC 1,341 ms
13,224 KB
testcase_07 AC 1,260 ms
12,872 KB
testcase_08 AC 1,453 ms
13,592 KB
testcase_09 AC 1,442 ms
13,580 KB
testcase_10 AC 644 ms
9,104 KB
testcase_11 AC 649 ms
9,056 KB
testcase_12 AC 1,208 ms
11,708 KB
testcase_13 AC 1,116 ms
11,736 KB
testcase_14 AC 1,591 ms
14,664 KB
testcase_15 AC 1,447 ms
13,588 KB
testcase_16 AC 1,181 ms
12,340 KB
testcase_17 AC 634 ms
8,800 KB
testcase_18 AC 774 ms
9,636 KB
testcase_19 AC 960 ms
10,952 KB
testcase_20 AC 1,033 ms
10,956 KB
testcase_21 AC 1,015 ms
10,976 KB
testcase_22 AC 834 ms
10,392 KB
testcase_23 AC 1,394 ms
13,132 KB
testcase_24 AC 883 ms
10,688 KB
testcase_25 AC 707 ms
9,624 KB
testcase_26 AC 694 ms
9,340 KB
testcase_27 AC 801 ms
10,160 KB
testcase_28 AC 1,508 ms
13,876 KB
testcase_29 AC 943 ms
10,916 KB
testcase_30 AC 1,436 ms
13,608 KB
testcase_31 AC 827 ms
10,448 KB
testcase_32 AC 869 ms
10,688 KB
testcase_33 AC 1,021 ms
7,704 KB
testcase_34 AC 1,038 ms
7,580 KB
testcase_35 AC 1,004 ms
14,600 KB
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx2") // or sse4
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

using Int = long long;
const char newl = '\n';

template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}


template<typename T, T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T, T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T, T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}


template<typename T>
class BIT{
private:
  // \sum_{j < i}  v[j]
  T sum(int i){
    T s(0);
    for(int x=i;x>0;x-=(x&-x))
      s+=bit[x];
    return s;
  }
public:
  int n;
  vector<T> bit;
  BIT(int n_):n(n_+1),bit(n+1,0){}

  // v[i] += a
  void add(int i,T a){
    for(int x=++i;x<=n;x+=(x&-x))
      bit[x]+=a;
  }
  // \sum_{l <= i < r} v[i]
  T query(int l,int r){return sum(r)-sum(l);}

  // min({x | sum(x) >= w})
  int lower_bound(const T w){
    if(w<=0) return 0;
    T r=w;
    int x=0,p=1;
    while(p<n) p<<=1;
    for(int k=p;k>0;k>>=1){
      if(x+k<=n and bit[x+k]<r){
        r-=bit[x+k];
        x+=k;
      }
    }
    x++;
    assert(sum(x-1)<w and sum(x)>=w);
    return x;
  }

  // min({x | sum(x) > w})
  int upper_bound(T w){return lower_bound(w+1);}
};


template<typename F>
struct FixPoint : F{
  FixPoint(F&& f):F(forward<F>(f)){}
  template<typename... Args>
  decltype(auto) operator()(Args&&... args) const{
    return F::operator()(*this,forward<Args>(args)...);
  }
};
template<typename F>
inline decltype(auto) MFP(F&& f){
  return FixPoint<F>{forward<F>(f)};
}


template<typename V>
V compress(V vs){
  sort(vs.begin(),vs.end());
  vs.erase(unique(vs.begin(),vs.end()),vs.end());
  return vs;
}
template<typename T>
map<T, int> dict(const vector<T> &vs){
  map<T, int> res;
  for(int i=0;i<(int)vs.size();i++)
    res[vs[i]]=i;
  return res;
}
map<char, int> dict(const string &s){
  return dict(vector<char>(s.begin(),s.end()));
}


//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin>>n;
  auto as=read(n);
  auto bs(as);
  {
    auto cs=compress(as);
    auto dc=dict(cs);
    for(int &a:as) a=dc[a];
  }

  using M = Mint<int, 998244353>;
  M ans{0};


  BIT<M> cnt(n),val(n),num(n),sum(n);
  auto clear=[&](int a){
    cnt.add(a,-cnt.query(a,a+1));
    val.add(a,-val.query(a,a+1));
    num.add(a,-num.query(a,a+1));
    sum.add(a,-sum.query(a,a+1));
  };

  MFP([&](auto dfs,int l,int r)->void{
    if(r-l<=16){
      for(int i=l;i<r;i++){
        for(int j=i+1;j<r;j++){
          if(!(as[i]>as[j])) continue;
          for(int k=j+1;k<r;k++)
            if(as[j]>as[k]) ans+=M(bs[i])+M(bs[j])+M(bs[k]);
        }
      }
      return;
    }

    int m=(l+r)>>1;
    dfs(l,m);
    dfs(m,r);

    {
      for(int i=l;i<m;i++){
        num.add(as[i],cnt.query(as[i]+1,n));
        sum.add(as[i],cnt.query(as[i]+1,n)*M(bs[i])+val.query(as[i]+1,n));
        cnt.add(as[i],1);
        val.add(as[i],bs[i]);
      }

      for(int i=m;i<r;i++)
        ans+=num.query(as[i]+1,n)*M(bs[i])+sum.query(as[i]+1,n);

      for(int i=l;i<m;i++) clear(as[i]);
    }
    {
      for(int i=r-1;i>=m;i--){
        num.add(as[i],cnt.query(0,as[i]));
        sum.add(as[i],cnt.query(0,as[i])*M(bs[i])+val.query(0,as[i]));
        cnt.add(as[i],1);
        val.add(as[i],bs[i]);
      }

      for(int i=m-1;i>=l;i--)
        ans+=num.query(0,as[i])*M(bs[i])+sum.query(0,as[i]);

      for(int i=m;i<r;i++) clear(as[i]);
    }
  })(0,n);

  cout<<ans<<newl;
  return 0;
}
0