結果
問題 | No.1300 Sum of Inversions |
ユーザー | momohara |
提出日時 | 2020-11-27 22:34:25 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 329 ms / 2,000 ms |
コード長 | 5,990 bytes |
コンパイル時間 | 2,519 ms |
コンパイル使用メモリ | 221,780 KB |
実行使用メモリ | 25,488 KB |
最終ジャッジ日時 | 2024-07-26 18:54:11 |
合計ジャッジ時間 | 12,039 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,816 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 242 ms
22,264 KB |
testcase_04 | AC | 230 ms
21,952 KB |
testcase_05 | AC | 171 ms
16,000 KB |
testcase_06 | AC | 278 ms
23,620 KB |
testcase_07 | AC | 262 ms
22,984 KB |
testcase_08 | AC | 299 ms
24,388 KB |
testcase_09 | AC | 290 ms
24,028 KB |
testcase_10 | AC | 137 ms
14,592 KB |
testcase_11 | AC | 138 ms
14,848 KB |
testcase_12 | AC | 227 ms
22,072 KB |
testcase_13 | AC | 217 ms
21,776 KB |
testcase_14 | AC | 329 ms
25,200 KB |
testcase_15 | AC | 291 ms
24,108 KB |
testcase_16 | AC | 238 ms
22,476 KB |
testcase_17 | AC | 133 ms
14,336 KB |
testcase_18 | AC | 166 ms
15,232 KB |
testcase_19 | AC | 208 ms
20,736 KB |
testcase_20 | AC | 206 ms
21,052 KB |
testcase_21 | AC | 203 ms
20,860 KB |
testcase_22 | AC | 178 ms
16,128 KB |
testcase_23 | AC | 286 ms
23,524 KB |
testcase_24 | AC | 185 ms
16,384 KB |
testcase_25 | AC | 148 ms
14,976 KB |
testcase_26 | AC | 148 ms
14,976 KB |
testcase_27 | AC | 163 ms
15,872 KB |
testcase_28 | AC | 308 ms
24,396 KB |
testcase_29 | AC | 202 ms
20,884 KB |
testcase_30 | AC | 302 ms
24,128 KB |
testcase_31 | AC | 185 ms
16,128 KB |
testcase_32 | AC | 185 ms
16,384 KB |
testcase_33 | AC | 19 ms
6,944 KB |
testcase_34 | AC | 31 ms
6,944 KB |
testcase_35 | AC | 166 ms
25,488 KB |
testcase_36 | AC | 173 ms
25,464 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; /* #include <atcoder/all> using namespace atcoder; */ #define all(hoge) (hoge).begin(), (hoge).end() #define en '\n' using ll = long long; using ull = unsigned long long; #define rep(i, m, n) for(ll i = (ll)(m); i < (ll)(n); ++i) #define rep2(i, m, n) for(ll i = (ll)(n)-1; i >= (ll)(m); --i) #define REP(i, n) rep(i, 0, n) #define REP2(i, n) rep2(i, 0, n) template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; typedef pair<ll, ll> P; using tp = tuple<ll, ll, ll>; constexpr long long INF = 1LL << 60; constexpr int INF_INT = 1 << 25; //constexpr long long MOD = (ll)1e9 + 7; constexpr long long MOD = 998244353LL; using ld = long double; static const ld pi = 3.141592653589793L; using Array = vector<ll>; using Matrix = vector<Array>; /* #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") */ template <class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } //グラフ関連 struct Edge { ll to, cap, rev; Edge(ll _to, ll _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; typedef vector<Edge> Edges; typedef vector<Edges> Graph; void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if(revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } template <typename T, typename F> class SegmentTree { private: T e; F op; ll n; vector<T> dat; public: SegmentTree(F f, T _e, vector<T> v) : op(f), e(_e) { int _n = v.size(); n = 1; while(n < _n) n *= 2; dat.resize(2 * n - 1, e); REP(i, _n) dat[n + i - 1] = v[i]; for(int i = n - 2; i >= 0; i--) dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T _e, int _n) : op(f), e(_e) { n = 1; while(n < _n) n *= 2; dat.resize(2 * n - 1, e); for(int i = n - 2; i >= 0; i--) dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]); } void update(int i, T x) { i += n - 1; dat[i] = x; while(i > 0) { i = (i - 1) / 2; dat[i] = op(dat[i * 2 + 1], dat[i * 2 + 2]); } } T get(int i) { i += n - 1; return dat[i]; } T query(int l, int r) { //[l,r)でのクエリ処理 T left = e, right = e; l += n - 1; r += n - 1; while(l < r) { if((l & 1) == 0) left = op(left, dat[l]); if((r & 1) == 0) right = op(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return op(left, right); } }; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while(b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while(n > 0) { if(n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using mint = ModInt<MOD>; void solve() { ll n; cin >> n; vec<ll> a(n); map<ll, ll> mp; REP(i, n) { cin >> a[i]; mp[a[i]]++; } mp[INF]++; int id = 0; for(auto &[a, b] : mp) { b = id++; } using pm = pair<mint, mint>; auto op = [](pm a, pm b) { return make_pair(a.first + b.first, a.second + b.second); }; //個数,合計 SegmentTree<pm, decltype(op)> seg1(op, make_pair(mint(0), mint(0)), id), seg2(op, make_pair(mint(0), mint(0)), id); mint ans = 0; REP(i, n) { int v = mp[a[i]]; seg1.update(v, op(seg1.get(v), make_pair(1, mint(a[i])))); { //ai>aj auto [num, sum] = seg1.query(v + 1, id); seg2.update(v, op(seg2.get(v), make_pair(num, sum + mint(a[i]) * num))); } { //aj>ak auto [num, sum] = seg2.query(v + 1, id); ans += sum + mint(a[i]) * num; } } cout << ans << en; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); /* ll t; cin >> t; REP(i, t - 1) { solve(); }*/ solve(); return 0; }