結果

問題 No.2950 Max Min Product
ユーザー Kude
提出日時 2024-10-25 21:58:44
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 328 ms / 3,000 ms
コード長 1,937 bytes
コンパイル時間 3,971 ms
コンパイル使用メモリ 285,916 KB
実行使用メモリ 17,916 KB
最終ジャッジ日時 2024-10-25 23:30:29
合計ジャッジ時間 14,693 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます

ソースコード

diff #

#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 cnt, smn, smx, s;
};

S op(S x, S y) { return {x.cnt + y.cnt, x.smn + y.smn, x.smx + y.smx, x.s + y.s}; }
S e() { return {}; }

struct F {
  int mn, mx;
};

F id() { return {-1, -1}; }
F composition(F f, F g) {
  if (f.mn == -1) f.mn = g.mn;
  if (f.mx == -1) f.mx = g.mx;
  return f;
}
S mapping(F f, S x) {
  if (f.mn != -1) {
    x.smn = f.mn * x.cnt;
    x.s = x.smx * f.mn;
  }
  if (f.mx != -1) {
    x.smx = f.mx * x.cnt;
    x.s = x.smn * f.mx;
  }
  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(vector<S>(n + 1, S{1, 0, 0, 0}));
  mint ans;
  constexpr int INF = 1001001001;
  vector<P> st_mx{{INF, -1}}, st_mn{{-INF, -1}};
  rep(i, n) {
    while (st_mx.back().first <= a[i]) st_mx.pop_back();
    while (st_mn.back().first >= a[i]) st_mn.pop_back();
    seg.apply(st_mx.back().second + 1, i + 1, {-1, a[i]});
    seg.apply(st_mn.back().second + 1, i + 1, {a[i], -1});
    ans += seg.prod(0, i + 1).s;
    st_mx.emplace_back(a[i], i);
    st_mn.emplace_back(a[i], i);
  }
  cout << ans.val() << '\n';
}
0