結果
問題 | No.2250 Split Permutation |
ユーザー |
![]() |
提出日時 | 2024-07-04 18:18:02 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 61 ms / 3,000 ms |
コード長 | 2,688 bytes |
コンパイル時間 | 547 ms |
コンパイル使用メモリ | 61,788 KB |
最終ジャッジ日時 | 2025-02-22 01:56:45 |
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 35 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:111:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 111 | scanf("%d", &n); | ~~~~~^~~~~~~~~~ main.cpp:112:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 112 | for (int i = 0; i < n; i++) scanf("%d", ps + i); | ~~~~~^~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-** 2250.cc: No.2250 Split Permutation - yukicoder*/#include<cstdio>#include<vector>#include<algorithm>using namespace std;/* constant */const int MAX_N = 200000;const int MOD = 998244353;/* typedef */template<const int MOD>struct MI {int v;MI(): v() {}MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; }MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; }explicit operator int() const { return v; }MI operator+(const MI m) const { return MI(v + m.v); }MI operator-(const MI m) const { return MI(v + MOD - m.v); }MI operator*(const MI m) const { return MI((long long)v * m.v); }MI &operator+=(const MI m) { return (*this = *this + m); }MI &operator-=(const MI m) { return (*this = *this - m); }MI &operator*=(const MI m) { return (*this = *this * m); }bool operator==(const MI m) const { return v == m.v; }bool operator!=(const MI m) const { return v != m.v; }MI pow(int n) const { // a^n % MODMI pm = 1, a = *this;while (n > 0) {if (n & 1) pm *= a;a *= a;n >>= 1;}return pm;}MI inv() const { return pow(MOD - 2); }MI operator/(const MI m) const { return *this * m.inv(); }MI &operator/=(const MI m) { return (*this = *this / m); }};using mi = MI<MOD>;template <typename T>struct BIT {int n;vector<T> bits;BIT() {}BIT(int _n) { init(_n); }void init(int _n) {n = _n;bits.assign(n + 1, 0);}T sum(int x) {x = min(x, n);T s = 0;while (x > 0) {s += bits[x];x -= (x & -x);}return s;}void add(int x, T v) {if (x <= 0) return;while (x <= n) {bits[x] += v;x += (x & -x);}}int lower_bound(T v) {int k = 1;while ((k << 1) <= n) k <<= 1;int x = 0;for (; k > 0; k >>= 1)if (x + k <= n && bits[x + k] < v) {x += k;v -= bits[x];}return x + 1;}};/* global variables */int ps[MAX_N];mi es[MAX_N * 2 + 1], inves[MAX_N * 2 + 1];/* subroutines *//* main */int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) scanf("%d", ps + i);int n2 = n * 2;es[0] = inves[0] = 1;mi inv2 = mi(2).inv();for (int i = 0; i < n2; i++) {es[i + 1] = es[i] * 2;inves[i + 1] = inves[i] * inv2;}BIT<int> bit0;BIT<mi> bit1;bit0.init(n); bit1.init(n);mi sum = 0;for (int i = 0; i < n; i++) {int c = bit0.sum(n) - bit0.sum(ps[i]);mi s = bit1.sum(n) - bit1.sum(ps[i]);sum += es[n - 1] * c - s * inves[i];bit0.add(ps[i], 1);bit1.add(ps[i], es[n - 1 + i]);}printf("%d\n", (int)sum);return 0;}