結果
問題 | No.2950 Max Min Product |
ユーザー | shobonvip |
提出日時 | 2024-10-25 23:12:08 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 899 ms / 3,000 ms |
コード長 | 3,333 bytes |
コンパイル時間 | 5,420 ms |
コンパイル使用メモリ | 280,440 KB |
実行使用メモリ | 18,308 KB |
最終ジャッジ日時 | 2024-10-25 23:31:45 |
合計ジャッジ時間 | 28,460 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,816 KB |
testcase_03 | AC | 397 ms
17,052 KB |
testcase_04 | AC | 434 ms
17,156 KB |
testcase_05 | AC | 413 ms
17,088 KB |
testcase_06 | AC | 574 ms
18,072 KB |
testcase_07 | AC | 575 ms
18,216 KB |
testcase_08 | AC | 575 ms
18,156 KB |
testcase_09 | AC | 282 ms
10,704 KB |
testcase_10 | AC | 508 ms
17,592 KB |
testcase_11 | AC | 430 ms
17,152 KB |
testcase_12 | AC | 577 ms
18,164 KB |
testcase_13 | AC | 576 ms
18,244 KB |
testcase_14 | AC | 575 ms
18,168 KB |
testcase_15 | AC | 595 ms
17,932 KB |
testcase_16 | AC | 445 ms
16,780 KB |
testcase_17 | AC | 487 ms
16,932 KB |
testcase_18 | AC | 622 ms
18,144 KB |
testcase_19 | AC | 629 ms
18,204 KB |
testcase_20 | AC | 626 ms
18,176 KB |
testcase_21 | AC | 378 ms
11,132 KB |
testcase_22 | AC | 509 ms
16,988 KB |
testcase_23 | AC | 334 ms
10,652 KB |
testcase_24 | AC | 661 ms
18,096 KB |
testcase_25 | AC | 665 ms
18,252 KB |
testcase_26 | AC | 669 ms
18,004 KB |
testcase_27 | AC | 597 ms
16,592 KB |
testcase_28 | AC | 872 ms
18,024 KB |
testcase_29 | AC | 641 ms
16,800 KB |
testcase_30 | AC | 899 ms
18,004 KB |
testcase_31 | AC | 833 ms
18,132 KB |
testcase_32 | AC | 884 ms
18,140 KB |
testcase_33 | AC | 629 ms
16,892 KB |
testcase_34 | AC | 410 ms
10,844 KB |
testcase_35 | AC | 617 ms
16,792 KB |
testcase_36 | AC | 860 ms
18,224 KB |
testcase_37 | AC | 847 ms
18,128 KB |
testcase_38 | AC | 854 ms
18,308 KB |
testcase_39 | AC | 2 ms
6,824 KB |
ソースコード
/** author: shobonvip created: 2024.10.25 22:03:11 **/ #include<bits/stdc++.h> using namespace std; //* ATCODER #include<atcoder/all> using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include<boost/multiprecision/cpp_int.hpp> using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) template <typename T> bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template <typename T> bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template <typename T> T max(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template <typename T> T min(vector<T> &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template <typename T> T sum(vector<T> &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } struct S{ mint val; ll len; }; struct F{ mint b; mint c; }; S op(S x, S y){ return {x.val + y.val, x.len + y.len}; } S e(){ return {0, 0}; } S mapping(F f, S x){ return {f.b * x.val + f.c * x.len, x.len}; } F composition(F g, F f){ return {f.b * g.b, f.c * g.b + g.c}; } F id(){ return {1, 0}; } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<ll> a(n); rep(i,0,n){ cin >> a[i]; } vector<int> max_st = {(int)1e9+20}; vector<int> max_ind = {0}; vector<int> min_st = {-1}; vector<int> min_ind = {0}; multiset<pair<int,int>> bad_points; lazy_segtree<S,op,e,F,mapping,composition,id> seg(vector<S>(n,S{1,1})); mint ans = 0; rep(i,0,n){ { int now = i; while(a[i] >= max_st.back()) { int val = max_st.back(); max_ind.pop_back(); int tar = max_ind.back(); if (val == 998244353){ bad_points.erase(bad_points.find(pair(tar, now))); }else{ seg.apply(tar, now, F{mint(val).inv(), 0}); } max_st.pop_back(); now = tar; } if (a[i] == 998244353){ bad_points.insert(pair(now, i+1)); }else{ seg.apply(now, i+1, F{a[i], 0}); } max_st.push_back(a[i]); max_ind.push_back(i+1); } { int now = i; while(a[i] <= min_st.back()) { int val = min_st.back(); min_ind.pop_back(); int tar = min_ind.back(); if (val == 998244353){ bad_points.erase(bad_points.find(pair(tar, now))); }else{ seg.apply(tar, now, F{mint(val).inv(), 0}); } min_st.pop_back(); now = tar; } if (a[i] == 998244353){ bad_points.insert(pair(now, i+1)); }else{ seg.apply(now, i+1, F{a[i], 0}); } min_st.push_back(a[i]); min_ind.push_back(i+1); } vector<int> v = {0, i+1}; for(auto [l,r]: bad_points){ v.push_back(l); v.push_back(r); } sort(v.begin(), v.end()); vector<int> imos((int)v.size()); for(auto [l,r]: bad_points){ imos[lower_bound(v.begin(), v.end(), l) - v.begin()] += 1; imos[lower_bound(v.begin(), v.end(), r) - v.begin()] -= 1; } rep(j,0,(int)v.size()-1){ imos[j+1] += imos[j]; } rep(j,0,(int)v.size()-1){ if (imos[j] == 0){ ans += seg.prod(v[j], v[j+1]).val; } } } cout << ans.val() << '\n'; }