結果

問題 No.1300 Sum of Inversions
ユーザー suta28407928suta28407928
提出日時 2020-11-27 22:30:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 5,057 bytes
コンパイル時間 2,499 ms
コンパイル使用メモリ 191,900 KB
実行使用メモリ 15,656 KB
最終ジャッジ日時 2023-10-09 20:32:52
合計ジャッジ時間 8,799 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 139 ms
12,828 KB
testcase_04 AC 134 ms
12,524 KB
testcase_05 AC 110 ms
10,856 KB
testcase_06 AC 165 ms
13,828 KB
testcase_07 AC 152 ms
13,488 KB
testcase_08 AC 175 ms
14,516 KB
testcase_09 AC 188 ms
14,484 KB
testcase_10 AC 88 ms
9,636 KB
testcase_11 AC 94 ms
9,552 KB
testcase_12 AC 143 ms
12,584 KB
testcase_13 AC 137 ms
12,200 KB
testcase_14 AC 205 ms
15,544 KB
testcase_15 AC 178 ms
14,416 KB
testcase_16 AC 141 ms
13,012 KB
testcase_17 AC 85 ms
9,320 KB
testcase_18 AC 95 ms
10,284 KB
testcase_19 AC 123 ms
11,504 KB
testcase_20 AC 128 ms
11,860 KB
testcase_21 AC 120 ms
11,684 KB
testcase_22 AC 102 ms
10,884 KB
testcase_23 AC 159 ms
13,904 KB
testcase_24 AC 110 ms
11,120 KB
testcase_25 AC 98 ms
10,412 KB
testcase_26 AC 93 ms
10,148 KB
testcase_27 AC 103 ms
10,696 KB
testcase_28 AC 172 ms
14,892 KB
testcase_29 AC 126 ms
11,720 KB
testcase_30 AC 189 ms
14,476 KB
testcase_31 AC 114 ms
10,988 KB
testcase_32 AC 125 ms
11,116 KB
testcase_33 AC 93 ms
15,656 KB
testcase_34 AC 146 ms
15,620 KB
testcase_35 AC 101 ms
15,576 KB
testcase_36 AC 125 ms
15,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
int dy[8] = {0,-1,0,1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//const int mod = 1e9 + 7;
const int mod = 998244353;

template <std::uint_fast64_t Modulus> class modint {
  using u64 = std::uint_fast64_t;

public:
  u64 a;

  constexpr modint(const u64 x = 0) noexcept : a(x % Modulus) {}
  constexpr u64 &value() noexcept { return a; }
  constexpr const u64 &value() const noexcept { return a; }
  constexpr modint operator+(const modint rhs) const noexcept {
	return modint(*this) += rhs;
  }
  constexpr modint operator-(const modint rhs) const noexcept {
	return modint(*this) -= rhs;
  }
  constexpr modint operator*(const modint rhs) const noexcept {
	return modint(*this) *= rhs;
  }
  constexpr modint operator/(const modint rhs) const noexcept {
	return modint(*this) /= rhs;
  }
  constexpr modint &operator+=(const modint rhs) noexcept {
	a += rhs.a;
	if (a >= Modulus) {
	  a -= Modulus;
	}
	return *this;
  }
  constexpr modint &operator-=(const modint rhs) noexcept {
	if (a < rhs.a) {
	  a += Modulus;
	}
	a -= rhs.a;
	return *this;
  }
  constexpr modint &operator*=(const modint rhs) noexcept {
	a = a * rhs.a % Modulus;
	return *this;
  }
  constexpr modint &operator/=(modint rhs) noexcept {
	u64 exp = Modulus - 2;
	while (exp) {
	  if (exp % 2) {
		*this *= rhs;
	  }
	  rhs *= rhs;
	  exp /= 2;
	}
	return *this;
  }
};

using mint = modint<mod>;

template<typename T> struct BIT{
	vector<T> dat;
	ll sz;
	//all 1-indexed
	BIT(ll sz) : sz(sz){
		dat.assign(++sz, 0);
	}

	T sum(ll k){
		T ret = 0;
		for(++k; k > 0; k -= k & -k) ret += dat[k];
		return (ret);
	}

	void add(ll k, T x){
		for(++k; k < dat.size(); k += k & -k) dat[k] += x;
	}
	
	ll get(T k){
		if(k <= 0) return 0; 
		ll ret = 0;
		int n = 1; while(n < sz) n *= 2;
		for(int i=n/2; i>0; i/=2){
			if(ret+i < sz && dat[ret+i] < k){
				k -= dat[ret+i];
				ret += i;
			}
		}
		return ret;
	}
};

template <typename T> struct Compress{
	vector<T> comp;

	Compress(vector<T> v) : comp(v){
		sort(comp.begin(), comp.end());
		comp.erase(unique(comp.begin(), comp.end()), comp.end());
	}

	ll get(T x){
		return lower_bound(comp.begin(), comp.end(), x) - comp.begin();
	}

	T operator[](int id){
		return comp[id];
	}

	ll siz(){
		return comp.size();
	}
};

int main(){
	ll n; cin >> n;
	vl c;
	vl a(n); rep(i,n) cin >> a[i], c.push_back(a[i]);
	BIT<mint> bit1(n), bit2(n), cnt1(n), cnt2(n);
	Compress<ll> comp(c);
	mint ans = 0;
	for(int i=n-1; i>=0; i--){
		ll pos = comp.get(a[i]);
		mint c1 = cnt1.sum(pos-1);
		mint s1 = bit1.sum(pos-1) + c1 * a[i];
		ans += bit2.sum(pos-1) + cnt2.sum(pos-1) * a[i];
		bit1.add(pos,a[i]);
		bit2.add(pos,s1);
		cnt1.add(pos,1);
		cnt2.add(pos,c1);
	}
	cout << ans.value() << "\n";
}
0