結果

問題 No.1435 Mmm......
ユーザー se1ka2se1ka2
提出日時 2021-03-19 22:43:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,497 bytes
コンパイル時間 822 ms
コンパイル使用メモリ 82,876 KB
実行使用メモリ 10,324 KB
最終ジャッジ日時 2023-08-12 03:30:28
合計ジャッジ時間 35,641 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1,106 ms
9,808 KB
testcase_07 AC 1,368 ms
9,780 KB
testcase_08 AC 1,623 ms
9,944 KB
testcase_09 AC 1,486 ms
9,876 KB
testcase_10 AC 1,264 ms
9,752 KB
testcase_11 AC 1,240 ms
9,924 KB
testcase_12 AC 1,140 ms
9,804 KB
testcase_13 AC 1,121 ms
9,932 KB
testcase_14 AC 795 ms
6,572 KB
testcase_15 AC 1,696 ms
9,800 KB
testcase_16 AC 1,379 ms
9,876 KB
testcase_17 AC 916 ms
6,416 KB
testcase_18 AC 1,725 ms
10,200 KB
testcase_19 AC 1,223 ms
9,884 KB
testcase_20 AC 1,550 ms
9,948 KB
testcase_21 TLE -
testcase_22 TLE -
testcase_23 AC 1,799 ms
9,948 KB
testcase_24 AC 1,798 ms
9,800 KB
testcase_25 AC 1,804 ms
9,804 KB
testcase_26 AC 1,813 ms
9,952 KB
testcase_27 AC 1,804 ms
10,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int INF = 2000000005;

template <typename T>
struct SegmentTree
{
    using F = function<T(T, T)>;
    
    const F f;
    const T e;
    
    int n;
    vector<T> seg;
    
    SegmentTree(int nn, const F f, const T e) : f(f), e(e){
        n = 1;
        while(n < nn) n <<= 1;
        seg.assign(n * 2, e);
    }
    
    void set(int i, T x){
        seg[i + n] = x;
    }
    
    void build(){
        for(int k = n - 1; k > 0; k--){
            seg[k] = f(seg[k * 2], seg[k * 2 + 1]);
        }
    }
    
    void update(int i, T x){
        int k = i + n;
        seg[k] = x;
        while(k >>= 1){
            seg[k] = f(seg[k * 2], seg[k * 2 + 1]);
        }
    }
    
    T query(int l, int r){
        l += n, r += n;
        T L = e, R = e;
        for(; l != r; l >>= 1, r >>= 1){
            if(l % 2) L = f(L, seg[l++]);
            if(r % 2) R = f(seg[--r], R);
        }
        return f(L, R);
    }
    
    T operator[](const int i)const {
        return seg[i + n];
    }
    
    template<typename C>
    int right_bound_sub(int k, const C &check, T x){
        while(k < n){
            if(check(f(x, seg[k * 2]))){
                k = k * 2;
            }
            else{
                x = f(x, seg[k * 2]);
                k = k * 2 + 1;
            }
        }
        return k - n;
    }
    
    template <typename C>
    int right_bound(int i, const C &check){
        T x = e;
        for(int l = i + n, r = n * 2; l != r; l >>= 1, r >>= 1){
            if(l % 2){
                if(check(f(x, seg[l]))){
                    return right_bound_sub(l, check, x);
                }
                x = f(x, seg[l]);
                l++;
            }
        }
        return -1;
    }
    
    template <typename C>
    int left_bound_sub(int k, const C &check, T x){
        while(k < n){
            if(check(f(seg[k * 2 + 1], x))){
                k = k * 2 + 1;
            }
            else{
                x = f(seg[k * 2 + 1], x);
                k = k * 2;
            }
        }
        return k - n;
    }
    
    template <typename C>
    int left_bound(int i, const C &check){
        T x = e;
        for(int l = n, r = i + n; l != r; l >>= 1, r >>= 1){
            if(r % 2){
                if(check(f(seg[--r], x))){
                    return left_bound_sub(r, check, x);
                }
                x = f(x, seg[r]);
            }
        }
        return -1;
    }
};

int main()
{
    int n;
    cin >> n;
    int a[200005];
    for(int i = 0; i < n; i++) cin >> a[i];
    SegmentTree<P> sml(n, [](P a, P b){return min(a, b);}, P(INF, INF));
    SegmentTree<int> lar(n, [](int a, int b){return max(a, b);}, 0);
    for(int i = 0; i < n; i++) sml.set(i, P(a[i], i));
    for(int i = 0; i < n; i++) lar.set(i, a[i]);
    sml.build();
    lar.build();
    ll ans = 0;
    for(int i = 0; i < n - 1; i++){
        int left = i + 1, right = n + 1;
        while(right - left > 1){
            int mid = (right + left) / 2;
            int M = lar.query(i, mid);
            P m1 = sml.query(i, mid);
            sml.update(m1.second, P(INF, INF));
            P m2 = sml.query(i, mid);
            sml.update(m1.second, P(m1.first, m1.second));
            if(M > m1.first + m2.first) right = mid;
            else left = mid;
        }
        ans += left - i - 1;
    }
    cout << ans << endl;
}
0