結果

問題 No.1435 Mmm......
ユーザー yuto1115yuto1115
提出日時 2020-10-21 21:59:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 366 ms / 2,000 ms
コード長 4,173 bytes
コンパイル時間 2,229 ms
コンパイル使用メモリ 208,596 KB
実行使用メモリ 14,848 KB
最終ジャッジ日時 2023-09-28 14:40:18
合計ジャッジ時間 8,962 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 173 ms
12,940 KB
testcase_07 AC 215 ms
13,788 KB
testcase_08 AC 255 ms
14,164 KB
testcase_09 AC 233 ms
14,048 KB
testcase_10 AC 199 ms
13,524 KB
testcase_11 AC 194 ms
13,128 KB
testcase_12 AC 180 ms
12,884 KB
testcase_13 AC 175 ms
12,912 KB
testcase_14 AC 126 ms
8,704 KB
testcase_15 AC 266 ms
14,608 KB
testcase_16 AC 219 ms
13,724 KB
testcase_17 AC 145 ms
9,380 KB
testcase_18 AC 268 ms
14,476 KB
testcase_19 AC 192 ms
13,180 KB
testcase_20 AC 242 ms
14,256 KB
testcase_21 AC 366 ms
14,724 KB
testcase_22 AC 354 ms
14,724 KB
testcase_23 AC 280 ms
14,848 KB
testcase_24 AC 283 ms
14,792 KB
testcase_25 AC 280 ms
14,796 KB
testcase_26 AC 278 ms
14,724 KB
testcase_27 AC 278 ms
14,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//@formatter:off
#include<bits/stdc++.h>
#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 rep2(i,s,n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class S,class T> istream& operator>>(istream &is,pair<S,T> &p) { return is >> p.first >> p.second; }
template<class S,class T> ostream& operator<<(ostream &os,const pair<S,T> &p) { return os<<'{'<<p.first<<","<<p.second<<'}'; }
template<class T> istream& operator>>(istream &is,vector<T> &v) { for(T &t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) { os<<'[';rep(i,v.size())os<<v[i]<<(i==int(v.size()-1)?"":","); return os<<']'; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\n'; }
template<class T> bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

template<class Monoid>
class segtree {
    using T = typename Monoid::value_type;
    
    int n;
    vector <T> val;

public:
    constexpr segtree(int _n) {
        n = 1;
        while (n < _n) n *= 2;
        val = vector<T>(2 * n, Monoid::identity);
    }
    
    constexpr segtree(vector <T> init) {
        int _n = init.size();
        n = 1;
        while (n < _n) n *= 2;
        val = vector<T>(2 * n, Monoid::identity);
        rep(i, _n) val[i + n] = init[i];
        rrep(i, n) val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]);
    }
    
    // segment [l,r)
    T fold(int l, int r) {
        l += n, r += n;
        T fold_l = Monoid::identity;
        T fold_r = Monoid::identity;
        while (l < r) {
            if (l & 1) fold_l = Monoid::operate(fold_l, val[l++]);
            if (r & 1) fold_r = Monoid::operate(val[--r], fold_r);
            l >>= 1, r >>= 1;
        }
        return Monoid::operate(fold_l, fold_r);
    }
    
    template<class F>
    void update(int i, const F &f) {
        i += n;
        val[i] = f(val[i]);
        while (i > 1) {
            i >>= 1;
            val[i] = Monoid::operate(val[i * 2], val[i * 2 + 1]);
        }
    }
};

class Node {
public:
    // min, second_min, max
    using value_type = tuple<int, int, int>;
    
    value_type value;
    
    Node(value_type value) : value(value) {}
    
    static constexpr value_type identity = {inf, inf, 0};
    
    static constexpr value_type operate(const value_type &l, const value_type &r) {
        auto[lm, ls, lM] = l;
        auto[rm, rs, rM] = r;
        int m = 0, s = 0, M = 0;
        if (lm > rm) {
            m = rm;
            s = min(lm, rs);
            M = max(lM, rM);
        } else {
            m = lm;
            s = min(ls, rm);
            M = max(lM, rM);
        }
        return make_tuple(m, s, M);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    vi p(n);
    cin >> p;
    vector <tuple<int, int, int>> init(n);
    rep(i, n) init[i] = make_tuple(p[i], inf, p[i]);
    segtree<Node> st(init);
    ll ans = 0;
    rep(l, n - 1) {
        int ok = l + 1, ng = n;
        auto f = [&](int x) -> bool {
            auto t = st.fold(l, x + 1);
            auto[m, s, M] = t;
            return M <= m + s;
        };
        while (abs(ok - ng) > 1) {
            int mid = (ng + ok) / 2;
            if (f(mid)) ok = mid;
            else ng = mid;
        }
        ans += ok - l;
    }
    cout << ans << endl;
}
0