結果

問題 No.1300 Sum of Inversions
ユーザー iiljj
提出日時 2020-11-27 23:36:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 306 ms / 2,000 ms
コード長 16,625 bytes
コンパイル時間 2,152 ms
コンパイル使用メモリ 216,920 KB
最終ジャッジ日時 2025-01-16 08:37:00
ジャッジサーバーID
(参考情報)
judge4 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

/* #region Head */
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pll = pair<ll, ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vc<vc<T>>;
using vll = vc<ll>;
using vvll = vvc<ll>;
using vld = vc<ld>;
using vvld = vvc<ld>;
using vs = vc<string>;
using vvs = vvc<string>;
template <class T, class U> using um = unordered_map<T, U>;
template <class T> using pq = priority_queue<T>;
template <class T> using pqa = priority_queue<T, vc<T>, greater<T>>;
template <class T> using us = unordered_set<T>;
#define REP(i, m, n) for (ll i = (m), i##_len = (ll)(n); i < i##_len; ++(i))
#define REPM(i, m, n) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; ++(i))
#define REPR(i, m, n) for (ll i = (m), i##_min = (ll)(n); i >= i##_min; --(i))
#define REPD(i, m, n, d) for (ll i = (m), i##_len = (ll)(n); i < i##_len; i += (d))
#define REPMD(i, m, n, d) for (ll i = (m), i##_max = (ll)(n); i <= i##_max; i += (d))
#define REPI(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++)
#define ALL(x) begin(x), end(x)
#define SIZE(x) ((ll)(x).size())
#define PERM(c) \
sort(ALL(c)); \
for (bool c##p = 1; c##p; c##p = next_permutation(ALL(c)))
#define UNIQ(v) v.erase(unique(ALL(v)), v.end());
#define CEIL(a, b) (((a) + (b)-1) / (b))
#define endl '\n'
#define sqrt sqrtl
#define floor floorl
#define log2 log2l
constexpr ll INF = 1'010'000'000'000'000'017LL;
constexpr int IINF = 1'000'000'007LL;
// constexpr ll MOD = 1'000'000'007LL; // 1e9 + 7
constexpr ll MOD = 998244353;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.14159265358979323846;
template <typename T> istream &operator>>(istream &is, vc<T> &vec) { // vector
for (T &x : vec) is >> x;
return is;
}
template <typename T> ostream &operator<<(ostream &os, vc<T> &vec) { // vector (for dump)
os << "{";
REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "" : ", ");
os << "}";
return os;
}
template <typename T> ostream &operator>>(ostream &os, vc<T> &vec) { // vector (inline)
REP(i, 0, SIZE(vec)) os << vec[i] << (i == i_len - 1 ? "\n" : " ");
return os;
}
template <typename T, typename U> istream &operator>>(istream &is, pair<T, U> &pair_var) { // pair
is >> pair_var.first >> pair_var.second;
return is;
}
template <typename T, typename U> ostream &operator<<(ostream &os, pair<T, U> &pair_var) { // pair
os << "(" << pair_var.first << ", " << pair_var.second << ")";
return os;
}
// map, um, set, us
template <class T> ostream &out_iter(ostream &os, T &map_var) {
os << "{";
REPI(itr, map_var) {
os << *itr;
auto itrcp = itr;
if (++itrcp != map_var.end()) os << ", ";
}
return os << "}";
}
template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { return out_iter(os, map_var); }
template <typename T, typename U> ostream &operator<<(ostream &os, um<T, U> &map_var) {
os << "{";
REPI(itr, map_var) {
auto [key, value] = *itr;
os << "(" << key << ", " << value << ")";
auto itrcp = itr;
if (++itrcp != map_var.end()) os << ", ";
}
os << "}";
return os;
}
template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { return out_iter(os, set_var); }
template <typename T> ostream &operator<<(ostream &os, us<T> &set_var) { return out_iter(os, set_var); }
template <typename T> ostream &operator<<(ostream &os, pq<T> &pq_var) {
pq<T> pq_cp(pq_var);
os << "{";
if (!pq_cp.empty()) {
os << pq_cp.top(), pq_cp.pop();
while (!pq_cp.empty()) os << ", " << pq_cp.top(), pq_cp.pop();
}
return os << "}";
}
void pprint() { cout << endl; }
template <class Head, class... Tail> void pprint(Head &&head, Tail &&... tail) {
cout << head;
if (sizeof...(Tail) > 0) cout << ' ';
pprint(move(tail)...);
}
// dump
#define DUMPOUT cerr
void dump_func() { DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head &&head, Tail &&... tail) {
DUMPOUT << head;
if (sizeof...(Tail) > 0) DUMPOUT << ", ";
dump_func(move(tail)...);
}
// chmax ()
template <typename T, typename U, typename Comp = less<>> bool chmax(T &xmax, const U &x, Comp comp = {}) {
if (comp(xmax, x)) {
xmax = x;
return true;
}
return false;
}
// chmin ()
template <typename T, typename U, typename Comp = less<>> bool chmin(T &xmin, const U &x, Comp comp = {}) {
if (comp(x, xmin)) {
xmin = x;
return true;
}
return false;
}
//
#ifndef ONLINE_JUDGE
#define DEBUG_
#endif
#ifdef DEBUG_
#define DEB
#define dump(...) \
DUMPOUT << " " << string(#__VA_ARGS__) << ": " \
<< "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" << endl \
<< " ", \
dump_func(__VA_ARGS__)
#else
#define DEB if (false)
#define dump(...)
#endif
#define VAR(type, ...) \
type __VA_ARGS__; \
cin >> __VA_ARGS__;
template <typename T> istream &operator,(istream &is, T &rhs) { return is >> rhs; }
template <typename T> ostream &operator,(ostream &os, const T &rhs) { return os << ' ' << rhs; }
struct AtCoderInitialize {
static constexpr int IOS_PREC = 15;
static constexpr bool AUTOFLUSH = false;
AtCoderInitialize() {
ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cout << fixed << setprecision(IOS_PREC);
if (AUTOFLUSH) cout << unitbuf;
}
} ATCODER_INITIALIZE;
void Yn(bool p) { cout << (p ? "Yes" : "No") << endl; }
void YN(bool p) { cout << (p ? "YES" : "NO") << endl; }
/* #endregion */
// #include <atcoder/all>
// using namespace atcoder;
/* #region mint */
// MOD
struct mint {
ll x;
mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
mint &operator+=(const mint a) {
if ((x += a.x) >= MOD) x -= MOD;
return *this;
}
mint &operator-=(const mint a) {
if ((x += MOD - a.x) >= MOD) x -= MOD;
return *this;
}
mint &operator*=(const mint a) {
(x *= a.x) %= MOD;
return *this;
}
mint operator+(const mint a) const {
mint res(*this);
return res += a;
}
mint operator-(const mint a) const {
mint res(*this);
return res -= a;
}
mint operator*(const mint a) const {
mint res(*this);
return res *= a;
}
// O(log(t))
mint pow_rec(ll t) const {
if (!t) return 1;
mint a = pow(t >> 1); // ⌊t/2⌋
a *= a; // ⌊t/2⌋*2
if (t & 1) // ⌊t/2⌋*2 == t-1
a *= *this; // ⌊t/2⌋*2+1 => t
return a;
}
mint pow(ll t) const {
mint a(*this);
mint res = 1;
while (t) {
if (t & 1) res *= a;
t >>= 1, a *= a;
}
return res;
}
// for prime mod
mint inv_prime() const {
return pow(MOD - 2); // x^(-1) ≡ x^(p-2)
}
mint inv() const {
ll a = this->x, b = MOD, u = 1, v = 0, t;
mint res;
while (b) {
t = a / b;
a -= t * b;
swap(a, b);
u -= t * v;
swap(u, v);
}
if (u < 0) u += MOD;
res = u;
return res;
}
mint &operator/=(const mint a) { return (*this) *= a.inv(); }
mint operator/(const mint a) const {
mint res(*this);
return res /= a;
}
bool operator==(const mint a) const { return this->x == a.x; }
bool operator==(const ll a) const { return this->x == a; }
// mint
friend istream &operator>>(istream &is, mint &x) {
is >> x.x;
return is;
}
// mint
friend ostream &operator<<(ostream &os, mint x) {
os << x.x;
return os;
}
};
/* #endregion */
/* #region CoordCompress1D */
// 1
class CoordCompress1D {
um<ll, ll> coord2zipped; //
vll zipped2coord; //
ll sz;
public:
CoordCompress1D() {}
CoordCompress1D(vll coords) {
sort(ALL(coords));
UNIQ(coords);
sz = SIZE(coords);
zipped2coord = coords;
REP(i, 0, sz) coord2zipped[coords[i]] = i;
}
//
ll zip(ll coord) { return coord2zipped[coord]; }
ll unzip(ll zipped) { return zipped2coord[zipped]; }
// coord
ll coord_geq(ll coord) {
auto it = lower_bound(ALL(zipped2coord), coord);
if (it != zipped2coord.end()) return *it;
return INF;
}
// coord
ll coord_gt(ll coord) {
auto it = upper_bound(ALL(zipped2coord), coord);
if (it != zipped2coord.end()) return *it;
return INF;
}
// coord
ll coord_leq(ll coord) {
auto rit = lower_bound(zipped2coord.rbegin(), zipped2coord.rend(), coord,
[](ll const lhs, ll const rhs) { return lhs > rhs; });
if (rit != zipped2coord.rend()) return *rit;
return -INF;
}
// coord
ll coord_lt(ll coord) {
auto rit = upper_bound(zipped2coord.rbegin(), zipped2coord.rend(), coord,
[](ll const lhs, ll const rhs) { return lhs > rhs; });
if (rit != zipped2coord.rend()) return *rit;
return -INF;
}
ll size() { return sz; }
};
/* #endregion */
/* #region SegTree */
template <typename T> // T:
struct SegmentTree {
using F = function<T(T, T)>; // max
ll n; //
ll nn; //
F f; // 使 max
T ti; // f 0. (a>0 max(a,0)=max(0,a)=a)
vc<T> dat; // 1-indexed (index 1 | 2 3 | 4 5 6 7 | 8 9 10 11 12 13 14 15 | ...)
//
SegmentTree() {}
//
SegmentTree(F f, T ti) : f(f), ti(ti) {}
//
void init(ll n_) {
nn = n_;
n = 1;
while (n < n_) n <<= 1;
dat.assign(n << 1, ti);
}
//
void build(const vc<T> &v) {
ll n_ = v.size();
init(n_);
REP(i, 0, n_) dat[n + i] = v[i];
REPR(i, n - 1, 1) dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
}
// k x
void set_val(ll k, T x) {
dat[k += n] = x;
while (k >>= 1) dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]); //
}
// k
T get_val(ll k) { return dat[k + n]; }
// [a, b)
T query(ll a, ll b) {
if (a >= b) return ti;
// assert(a<b)
T vl = ti, vr = ti;
for (ll l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
if (l & 1) vl = f(vl, dat[l++]);
if (r & 1) vr = f(dat[--r], vr);
}
return f(vl, vr);
}
//
template <typename C> int find(ll st, C &check, T &acc, ll k, ll l, ll r) {
if (l + 1 == r) {
acc = f(acc, dat[k]);
return check(acc) ? k - n : -1;
}
ll m = (l + r) >> 1;
if (m <= st) return find(st, check, acc, (k << 1) | 1, m, r);
if (st <= l && !check(f(acc, dat[k]))) {
acc = f(acc, dat[k]);
return -1;
}
ll vl = find(st, check, acc, (k << 1) | 0, l, m);
if (~vl) return vl;
return find(st, check, acc, (k << 1) | 1, m, r);
}
// check(query(st, idx)) idx
template <typename C> int find(ll st, C &check) {
T acc = ti;
return find(st, check, acc, 1, 0, n);
}
//
// @param l
// @param check
// @return check(query(l,r)) r
int max_right(int l, const function<bool(T)> &check) {
assert(0 <= l && l <= nn);
assert(check(ti));
if (l == nn) return nn;
l += n;
T sm = ti;
do {
while (l % 2 == 0) l >>= 1;
if (!check(f(sm, dat[l]))) {
while (l < n) {
l = (2 * l);
if (check(f(sm, dat[l]))) {
sm = f(sm, dat[l]);
l++;
}
}
return l - n;
}
sm = f(sm, dat[l]);
l++;
} while ((l & -l) != l);
return nn;
}
//
// @param r
// @param check
// @return check(query(l,r)) l
int min_left(int r, const function<bool(T)> &check) {
assert(0 <= r && r <= nn);
assert(check(ti));
if (r == 0) return 0;
r += n;
T sm = ti;
do {
r--;
while (r > 1 && (r % 2)) r >>= 1;
if (!check(f(dat[r], sm))) {
while (r < n) {
r = (2 * r + 1);
if (check(f(dat[r], sm))) {
sm = f(dat[r], sm);
r--;
}
}
return r + 1 - n;
}
sm = f(dat[r], sm);
} while ((r & -r) != r);
return 0;
}
//
void _dump() {
REP(k, 0, nn) {
T val = dat[k + n];
cout << val << (k == nn - 1 ? '\n' : ' ');
}
}
};
/* #endregion */
// Problem
void solve() {
VAR(ll, n); //
vll a(n);
cin >> a;
CoordCompress1D cc(a);
using T = pair<mint, mint>; // {sum, cnt}
auto f = [](T a, T b) -> T {
auto [a0, a1] = a;
auto [b0, b1] = b;
return {a0 + b0, a1 + b1};
};
SegmentTree<T> seg0(f, {0, 0});
SegmentTree<T> seg1(f, {0, 0});
seg0.init(cc.size());
seg1.init(cc.size());
mint ans = 0;
REPR(i, n - 1, 0) {
ll zipped = cc.zip(a[i]); // [0, sz-1]
T cur0 = seg0.get_val(zipped);
cur0.first += a[i];
cur0.second += 1;
seg0.set_val(zipped, cur0); //
// seg0._dump();
T cur1_0 = seg0.query(0, zipped); //
cur1_0.first += mint(a[i]) * cur1_0.second;
T cur1_1 = seg1.get_val(zipped);
T cur1 = f(cur1_0, cur1_1);
seg1.set_val(zipped, cur1); //
// seg1._dump();
T cur2 = seg1.query(0, zipped); //
// dump(cur2);
cur2.first += mint(a[i]) * cur2.second;
ans += cur2.first;
// dump(i, cur2, ans);
}
pprint(ans);
}
// entry point
int main() {
solve();
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0