結果

問題 No.3812 Max Min Sigma Problem
コンテスト
ユーザー Kude
提出日時 2026-11-20 23:47:36
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 511 ms / 2,000 ms
+ 875µs
コード長 2,252 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,966 ms
コンパイル使用メモリ 368,292 KB
実行使用メモリ 33,524 KB
最終ジャッジ日時 2026-11-20 23:47:53
合計ジャッジ時間 9,246 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge5_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using mint = modint998244353;

struct S {
  mint c, x, y, xy;
};
S op(S x, S y) {
  return {x.c + y.c, x.x + y.x, x.y + y.y, x.xy + y.xy};
}
S e() { return {}; }
struct F {
  int x, y;
};
F id() { return {-1, -1}; }
F composition(F f, F g) {
  F h;
  h.x = f.x == -1 ? g.x : f.x;
  h.y = f.y == -1 ? g.y : f.y;
  return h;
}
S mapping(F f, S x) {
  if (f.x != -1) {
    mint v = f.x;
    x.x = v * x.c;
    x.xy = v * x.y;
  }
  if (f.y != -1) {
    mint v = f.y;
    x.y = v * x.c;
    x.xy = v * x.x;
  }
  return x;
}

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  VI a(n);
  rep(i, n) cin >> a[i];
  lazy_segtree<S, op, e, F, mapping, composition, id> seg(n);
  VI stmx{-1}, stmn{-1};
  mint x2, y2, ans;
  rep(i, n) {
    seg.set(i, S{1, 0, 0, 0});
    while (stmx.back() != -1 && a[stmx.back()] < a[i]) {
      int p = stmx.back(); stmx.pop_back();
      int q = stmx.back();
      x2 -= mint(a[p]).pow(2) * (p - q);
    }
    {
      int p = stmx.back();
      x2 += mint(a[i]).pow(2) * (i - p);
      seg.apply(p + 1, i + 1, F{a[i], -1});
    }
    stmx.emplace_back(i);
    while (stmn.back() != -1 && a[stmn.back()] > a[i]) {
      int p = stmn.back(); stmn.pop_back();
      int q = stmn.back();
      y2 -= mint(a[p]).pow(2) * (p - q);
    }
    {
      int p = stmn.back();
      y2 += mint(a[i]).pow(2) * (i - p);
      seg.apply(p + 1, i + 1, F{-1, a[i]});
    }
    stmn.emplace_back(i);
    ans += x2 - 2 * seg.all_prod().xy + y2;
  }
  cout << ans.val() << '\n';
}
0