結果

問題 No.1435 Mmm......
ユーザー milanis48663220milanis48663220
提出日時 2021-03-19 22:45:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,314 ms / 2,000 ms
コード長 3,382 bytes
コンパイル時間 1,776 ms
コンパイル使用メモリ 118,236 KB
実行使用メモリ 9,352 KB
最終ジャッジ日時 2023-08-12 03:33:48
合計ジャッジ時間 26,574 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,384 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 838 ms
9,180 KB
testcase_07 AC 1,042 ms
9,204 KB
testcase_08 AC 1,226 ms
9,204 KB
testcase_09 AC 1,124 ms
9,200 KB
testcase_10 AC 958 ms
9,168 KB
testcase_11 AC 939 ms
9,208 KB
testcase_12 AC 873 ms
9,304 KB
testcase_13 AC 833 ms
9,224 KB
testcase_14 AC 585 ms
6,220 KB
testcase_15 AC 1,283 ms
9,184 KB
testcase_16 AC 1,038 ms
9,180 KB
testcase_17 AC 687 ms
6,060 KB
testcase_18 AC 1,299 ms
9,352 KB
testcase_19 AC 921 ms
9,172 KB
testcase_20 AC 1,166 ms
9,168 KB
testcase_21 AC 183 ms
9,180 KB
testcase_22 AC 1,207 ms
9,240 KB
testcase_23 AC 1,284 ms
9,208 KB
testcase_24 AC 1,314 ms
9,152 KB
testcase_25 AC 1,296 ms
9,148 KB
testcase_26 AC 1,298 ms
9,140 KB
testcase_27 AC 1,291 ms
9,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

template <typename T>
struct segtree{
    int n;
    T UNIT;
    vector<T> dat;
    T (*calc)(T, T);

    segtree(int n_, T unit, T (*_calc)(T, T)){
        UNIT = unit;
        calc = _calc;
        n = 1;
        while(n < n_) n *= 2;
        dat = vector<T>(2*n);
        for(int i = 0; i < 2*n; i++) dat[i] = UNIT;
    }

    void insert(int k, T a){
        dat[k+n-1] = a;
    }
    void update_all(){
        for(int i = n-2; i >= 0; i--){
            dat[i] = calc(dat[i*2+1], dat[i*2+2]);
        }
    }
    //k番目の値(0-indexed)をaに変更
    void update(int k, T a){
        k += n-1;
        dat[k] = a;
        while(k > 0){
            k = (k-1)/2;
            dat[k] = calc(dat[k*2+1], dat[k*2+2]);
        }
    }

    //[a, b)
    //区間[a, b]へのクエリに対してはquery(a, b+1)と呼ぶ
    T query(int a, int b, int k=0, int l=0, int r=-1){
        if(r < 0) r = n;
        if(r <= a || b <= l) return UNIT;
        if(a <= l && r <= b) return dat[k];
        else{
            T vl = query(a, b, k*2+1, l, (l+r)/2);
            T vr = query(a, b, k*2+2, (l+r)/2, r);
            return calc(vl, vr);
        }
    }
};

const int INVALID = -1;

struct node{
    int m1, m2, M;
};

node unit = node{INVALID, INVALID, INVALID};

inline bool is_unit(node n){
    return n.m1 == INVALID && n.m2 == INVALID && n.M == INVALID;
}

node calc(node n1, node n2){
    vector<int> v;
    auto add = [&](int x){
        if(x != INVALID) v.push_back(x);
    };
    if(is_unit(n1)) return n2;
    if(is_unit(n2)) return n1;
    int a = min(n1.m1, n2.m1);
    int b = max(n1.m1, n2.m1);
    int c = max({n1.m1, n2.m1, n1.m2, n2.m2, n1.M, n2.M});
    if(n2.m2 != INVALID) chmin(b, n2.m2);
    if(n1.m2 != INVALID) chmin(b, n1.m2);
    return node{a, b, c};
}

bool ok(node n){
    return n.m1+n.m2 >= n.M;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    segtree<node> sgt(n, unit, calc);
    for(int i = 0; i < n; i++){
        int a; cin >> a;
        sgt.update(i, node{a, INVALID, INVALID});
    }
    ll ans = 0;
    auto test = [&](int l, int r){
        auto nd = sgt.query(l, r+1);
        cout << nd.m1 << ' ' << nd.m2 << ' ' << nd.M << ' ';
        cout << (ok(nd) ? "OK" : "NG") << endl;
    };
    

    for(int i = 0; i < n-1; i++){
        if(!ok(sgt.query(i, i+2))) {
            continue;
        }
        if(ok(sgt.query(i, n))) {
            ans += n-i-1;
            continue;
        }
        int l = i+1, r = n-1;
        while(r-l > 1){
            int c = (l+r)/2;
            if(ok(sgt.query(i, c+1))) l = c;
            else r = c; 
        }
        ans += l-i;
    }
    cout << ans << endl;
}
0