提出 #72233857


ソースコード 拡げる

#include <iostream>
#include <vector>
#include <functional>
#define fastio cin.tie(0)->sync_with_stdio(0)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define sz(x) (int)(x).size()
using namespace std;

template<typename Node>
struct SegTree {
    int n, lg, size;
    Node e; // 항등원
    vector<Node> tree;
    function<Node(Node, Node)> func;
    int log2(int n) {
        int res = 0;
        while (n > (1 << res)) res++;
        return res;
    }
    SegTree(int n, const Node& e, auto func) : n(n), lg(log2(n)), size(1<<lg), e(e), tree(size<<1, e), func(func) {}
    SegTree(const vector<Node>& v, const Node& e, auto func) : n(sz(v)), lg(log2(n)), size(1<<lg), e(e), tree(size<<1, e), func(func) {
        for (int i = 0; i < n; i++) {
            tree[i+size] = v[i];
        }
        for (int i = size-1; i > 0; i--) {
            tree[i] = func(tree[i<<1], tree[i<<1 | 1]);
        }
    }
    void add(int i, const Node& val) {
        tree[--i |= size] += val;
        while (i >>= 1) {
            tree[i] = func(tree[i<<1], tree[i<<1 | 1]);
        }
    }
    void update(int i, const Node& val) {
        tree[--i |= size] = val;
        while (i >>= 1) {
            tree[i] = func(tree[i<<1], tree[i<<1 | 1]);
        }
    }
    Node query(int i) {
        return tree[--i | size];
    }
    Node query(int l, int r) {
        Node L = e, R = e;
        for (--l |= size, --r |= size; l <= r; l >>= 1, r >>= 1) {
            if (l & 1) L = func(L, tree[l++]);
            if (~r & 1) R = func(tree[r--], R);
        }
        return func(L, R);
    }
    int find_kth(Node k) {
        int node = 1, st = 1, en = size;
        while (st != en) {
            int mid = (st + en) / 2;
            node <<= 1;
            if (tree[node] >= k) {
                en = mid;
            }
            else {
                k -= tree[node];
                node |= 1; st = mid+1;
            }
        }
        return st;
    }
};

typedef long long ll;
const ll MOD = 998244353;

int main() {
    fastio; int N; cin >> N;
    vector<int> v(N);
    for (auto& i : v) cin >> i;
    vector<int> L(N), R(N);
    SegTree<int> sg(N+1, 0, [](int a, int b) { return a+b; });
    for (int i = 0; i < N; i++) {
        L[i] = sg.query(1, v[i]-1);
        R[i] = v[i]-1-L[i];
        sg.add(v[i], 1);
    }
    ll c = 1, t = 0;
    for (int i = 1; i < N; i++) {
        t += (c * (ll)R[i]); t %= MOD;
        c = (c*2) % MOD;
    }
    ll ans = 0;
    for (int i = 0; i < N-1; i++) {
        ans += (ll)L[i]*R[i]; ans %= MOD;
        ans += L[i]*t; ans %= MOD;
        t = (t - R[i+1] + MOD) % MOD;
        t *= (MOD+1)/2; t %= MOD;
    }
    cout << ans << "\n";
    return 0;
}

提出情報

提出日時
問題 F - Beautiful Kadomatsu
ユーザ Lov34ever
言語 C++23 (GCC 15.2.0)
得点 525
コード長 2885 Byte
結果 AC
実行時間 55 ms
メモリ 11024 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 525 / 525
結果
AC × 3
AC × 24
セット名 テストケース
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt
ケース名 結果 実行時間 メモリ
sample_01.txt AC 1 ms 3588 KiB
sample_02.txt AC 1 ms 3600 KiB
sample_03.txt AC 1 ms 3456 KiB
test_01.txt AC 1 ms 3448 KiB
test_02.txt AC 23 ms 7056 KiB
test_03.txt AC 31 ms 7696 KiB
test_04.txt AC 12 ms 5304 KiB
test_05.txt AC 39 ms 8072 KiB
test_06.txt AC 27 ms 7352 KiB
test_07.txt AC 42 ms 8080 KiB
test_08.txt AC 21 ms 5776 KiB
test_09.txt AC 33 ms 7568 KiB
test_10.txt AC 24 ms 6932 KiB
test_11.txt AC 16 ms 5396 KiB
test_12.txt AC 45 ms 10896 KiB
test_13.txt AC 45 ms 11024 KiB
test_14.txt AC 55 ms 10956 KiB
test_15.txt AC 54 ms 10964 KiB
test_16.txt AC 54 ms 10896 KiB
test_17.txt AC 55 ms 10944 KiB
test_18.txt AC 54 ms 10964 KiB
test_19.txt AC 54 ms 10896 KiB
test_20.txt AC 54 ms 10896 KiB
test_21.txt AC 54 ms 10944 KiB