結果
問題 | No.1300 Sum of Inversions |
ユーザー |
![]() |
提出日時 | 2020-11-23 23:02:50 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 162 ms / 2,000 ms |
コード長 | 4,360 bytes |
コンパイル時間 | 3,506 ms |
コンパイル使用メモリ | 204,496 KB |
最終ジャッジ日時 | 2025-01-16 05:17:45 |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 34 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:176:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 176 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:179:31: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 179 | for(auto &v : a) scanf("%d", &v); | ~~~~~^~~~~~~~~~
ソースコード
#include <bits/stdc++.h>using namespace std;template <const int &mod>struct ModInt {private:int x;public:ModInt() : x(0) {}ModInt(long long x_) {if((x = x_ % mod + mod) >= mod) x -= mod;}int val() const { return x; }static int get_mod() { return mod; }constexpr ModInt &operator+=(ModInt rhs) {if((x += rhs.x) >= mod) x -= mod;return *this;}constexpr ModInt &operator-=(ModInt rhs) {if((x -= rhs.x) < 0) x += mod;return *this;}constexpr ModInt &operator*=(ModInt rhs) {x = (unsigned long long)x * rhs.x % mod;return *this;}constexpr ModInt &operator/=(ModInt rhs) {x = (unsigned long long)x * rhs.inv().x % mod;return *this;}constexpr ModInt operator-() const noexcept { return -x < 0 ? mod - x : -x; }constexpr ModInt operator+(ModInt rhs) const noexcept { return ModInt(*this) += rhs; }constexpr ModInt operator-(ModInt rhs) const noexcept { return ModInt(*this) -= rhs; }constexpr ModInt operator*(ModInt rhs) const noexcept { return ModInt(*this) *= rhs; }constexpr ModInt operator/(ModInt rhs) const noexcept { return ModInt(*this) /= rhs; }constexpr ModInt &operator++() {*this += 1;return *this;}constexpr ModInt operator++(int) {*this += 1;return *this - 1;}constexpr ModInt &operator--() {*this -= 1;return *this;}constexpr ModInt operator--(int) {*this -= 1;return *this + 1;}bool operator==(ModInt rhs) const { return x == rhs.x; }bool operator!=(ModInt rhs) const { return x != rhs.x; }bool operator<=(ModInt rhs) const { return x <= rhs.x; }bool operator>=(ModInt rhs) const { return x >= rhs.x; }bool operator<(ModInt rhs) const { return x < rhs.x; }bool operator>(ModInt rhs) const { return x > rhs.x; }ModInt inv() {int a = x, b = mod, u = 1, v = 0, t;while(b > 0) {t = a / b;std::swap(a -= t * b, b);std::swap(u -= t * v, v);}return ModInt(u);}ModInt pow(long long n) const {ModInt ret(1), mul(x);while(n > 0) {if(n & 1) ret *= mul;mul *= mul;n >>= 1;}return ret;}ModInt sqrt() const {if(x <= 1) return x;int v = (mod - 1) / 2;if(pow(v) != 1) return -1;int q = mod - 1, m = 0;while(~q & 1) q >>= 1, m++;std::mt19937 mt;ModInt z = mt();while(z.pow(v) != mod - 1) z = mt();ModInt c = z.pow(q), t = pow(q), r = pow((q + 1) / 2);for(; m > 1; m--) {ModInt tmp = t.pow(1 << (m - 2));if(tmp != 1) r = r * c, t = t * c * c;c = c * c;}return std::min(r.x, mod - r.x);}friend std::ostream &operator<<(std::ostream &s, ModInt<mod> a) {s << a.x;return s;}friend std::istream &operator>>(std::istream &s, ModInt<mod> &a) {s >> a.x;return s;}};constexpr static int MOD = 998244353;using mint = ModInt<MOD>;template <typename T>struct BIT {private:int n, p;std::vector<T> d;public:BIT() = default;BIT(int n) : n(n), d(n + 1) {p = 1;while(p < n) p *= 2;}void add(int i, T x = 1) {for(i++; i <= n; i += i & -i) d[i] += x;}//return sum[0,i)T sum(int i) {T res = 0;for(; i; i -= i & -i) res += d[i];return res;}//return sum[l,r)T sum(int l, int r) { return sum(r) - sum(l); }};template <class T>std::vector<T> press(std::vector<T> &x) {auto res = x;std::sort(res.begin(), res.end());res.erase(std::unique(res.begin(), res.end()), res.end());for(int i = 0; i < (int)x.size(); i++)x[i] = std::lower_bound(res.begin(), res.end(), x[i]) - res.begin();return res;}mint res;int main() {//入力受け取りint n;scanf("%d", &n);vector a(n, 0);for(auto &v : a) scanf("%d", &v);//座標圧縮auto rec = press(a);int m = rec.size();//以下は前準備vector<mint> x(n), y(n);vector<int> l(n), r(n);{BIT<mint> BT(m);BIT<int> kosuu(m);for(int i = 0; i < n; i++) {BT.add(a[i], rec[a[i]]);kosuu.add(a[i], 1);x[i] = BT.sum(a[i] + 1, m);l[i] = kosuu.sum(a[i] + 1, m);}}{BIT<mint> BT(m);BIT<int> kosuu(m);for(int i = n - 1; i >= 0; i--) {BT.add(a[i], rec[a[i]]);kosuu.add(a[i], 1);y[i] = BT.sum(0, a[i]);r[i] = kosuu.sum(0, a[i]);}}//答えの計算for(int j = 0; j < n; j++) {if(!l[j] * r[j]) continue;mint tmp = rec[a[j]];res += x[j] * r[j] + y[j] * l[j] + tmp * l[j] * r[j];}cout << res << endl;return 0;}