結果

問題 No.2950 Max Min Product
ユーザー hitonanodehitonanode
提出日時 2024-10-26 22:23:28
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 275 ms / 3,000 ms
コード長 1,799 bytes
コンパイル時間 4,769 ms
コンパイル使用メモリ 129,300 KB
実行使用メモリ 17,420 KB
最終ジャッジ日時 2024-10-26 22:23:43
合計ジャッジ時間 11,499 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <utility>
#include <vector>
using namespace std;

#include <atcoder/lazysegtree>
#include <atcoder/modint>
using mint = atcoder::modint998244353;
// range set range sum
// constexpr int md = 998244353;

using S = pair<mint, int>;  // (sum, size)
using F = int;
S op(S a, S b) { return {(a.first + b.first), a.second + b.second}; }
S e() { return {0, 0}; }
S mapping(F f, S x) { return f == 0 ? x : S(x.second * mint(f), x.second); }
F composition(F f, F g) { return f == 0 ? g : f; }
F id() { return 0; }
using LST = atcoder::lazy_segtree<S, op, e, F, mapping, composition, id>;


int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);

    vector<pair<int, int>> maxi, mini;
    maxi.emplace_back(1 << 30, -1);
    mini.emplace_back(0, -1);

    mint ret = 0;

    int N;
    cin >> N;

    vector<S> init(N, {0, 1});
    LST mintree(init), maxtree(init);

    mint ans = 0;
    for (int i = 0; i < N; ++i) {
        int a;
        cin >> a;

        ret += (mint)a * a;

        {
            int r = i;
            while (maxi.back().first <= a) {
                int l = maxi.back().second;
                ret += (mint)(a - maxi.back().first) * mintree.prod(l, r).first;
                r = l;
                maxi.pop_back();
            }
            maxi.emplace_back(a, r);
            maxtree.apply(r, i + 1, a);
        }

        {
            int r = i;
            while (mini.back().first >= a) {
                const int l = mini.back().second;
                ret += (mint)(a - mini.back().first) * maxtree.prod(l, r).first;
                r = l;
                mini.pop_back();
            }
            mini.emplace_back(a, r);
            mintree.apply(r, i + 1, a);
        }

        ans += ret;
    }

    cout << ans.val() << '\n';
}
0