結果

問題 No.1435 Mmm......
ユーザー siro53siro53
提出日時 2023-02-13 15:21:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 60 ms / 2,000 ms
コード長 6,408 bytes
コンパイル時間 2,208 ms
コンパイル使用メモリ 205,976 KB
実行使用メモリ 11,824 KB
最終ジャッジ日時 2023-09-23 11:04:12
合計ジャッジ時間 6,378 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 32 ms
10,776 KB
testcase_07 AC 37 ms
11,152 KB
testcase_08 AC 43 ms
11,368 KB
testcase_09 AC 40 ms
11,304 KB
testcase_10 AC 35 ms
11,032 KB
testcase_11 AC 34 ms
10,780 KB
testcase_12 AC 33 ms
10,832 KB
testcase_13 AC 32 ms
10,988 KB
testcase_14 AC 24 ms
7,268 KB
testcase_15 AC 44 ms
11,556 KB
testcase_16 AC 37 ms
11,096 KB
testcase_17 AC 26 ms
7,524 KB
testcase_18 AC 45 ms
11,620 KB
testcase_19 AC 34 ms
10,768 KB
testcase_20 AC 41 ms
11,420 KB
testcase_21 AC 60 ms
11,500 KB
testcase_22 AC 59 ms
11,604 KB
testcase_23 AC 53 ms
11,684 KB
testcase_24 AC 52 ms
11,672 KB
testcase_25 AC 53 ms
11,572 KB
testcase_26 AC 52 ms
11,628 KB
testcase_27 AC 53 ms
11,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 1 "yuki1435.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1435"
#line 1 "/Users/cbiolab/kyo-pro/compro_library/template/template.cpp"
#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
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;
}
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

struct Setup {
    Setup() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(15);
    }
} __Setup;

using ll = long long;
#define OVERLOAD3(_1, _2, _3, name, ...) name
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
#define REP1(i, n) for(int i = 0; i < int(n); i++)
#define REP2(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(...) OVERLOAD3(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define REVERSE(v) reverse(ALL(v))
#define SZ(v) ((int)(v).size())
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
constexpr int MOD2 = 998244353;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

void Case(int i) { cout << "Case #" << i << ": "; }
int popcount(int x) { return __builtin_popcount(x); }
ll popcount(ll x) { return __builtin_popcountll(x); }
#pragma endregion Macros
#line 2 "/Users/cbiolab/kyo-pro/compro_library/data-structure/segtree/segtree.hpp"

#line 5 "/Users/cbiolab/kyo-pro/compro_library/data-structure/segtree/segtree.hpp"

/*
    テンプレート引数にモノイドを受け取る
    モノイドには静的メンバ関数 op(), e() が実装されている必要がある
*/
template <class Monoid> class Segtree {
  public:
    using T = typename Monoid::value_type;

    Segtree() : Segtree(0) {}
    explicit Segtree(int n) : Segtree(std::vector<T>(n, Monoid::e())) {}
    explicit Segtree(const std::vector<T> &v) : N((int)v.size()), sz(1) {
        while(sz < N) sz <<= 1;
        node.resize(sz * 2, Monoid::e());
        for(int i = 0; i < N; i++) node[i + sz] = v[i];
        for(int i = sz - 1; i >= 1; i--) {
            node[i] = Monoid::op(node[i << 1], node[i << 1 | 1]);
        }
    }
    void set(int pos, T val) {
        assert(0 <= pos && pos < N);
        pos += sz;
        node[pos] = val;
        while(pos > 1) {
            pos >>= 1;
            node[pos] = Monoid::op(node[pos << 1], node[pos << 1 | 1]);
        }
    }
    T get(int pos) const {
        assert(0 <= pos && pos < N);
        return node[pos + sz];
    }
    T prod(int l, int r) const {
        assert(0 <= l && l <= r && r <= N);
        T value_l = Monoid::e(), value_r = Monoid::e();
        l += sz;
        r += sz;
        while(l < r) {
            if(l & 1) value_l = Monoid::op(value_l, node[l++]);
            if(r & 1) value_r = Monoid::op(node[--r], value_r);
            l >>= 1;
            r >>= 1;
        }
        return Monoid::op(value_l, value_r);
    }
    T all_prod() const { return node[1]; }
    template <class F> int max_right(int l, F f) const {
        assert(0 <= l && l <= N);
        assert(f(Monoid::e()));
        if(l == N) return N;
        l += sz;
        T value_now = Monoid::e();
        do {
            while((l & 1) == 0) l >>= 1;
            if(!f(Monoid::op(value_now, node[l]))) {
                while(l < sz) {
                    l = 2 * l;
                    if(f(Monoid::op(value_now, node[l]))) {
                        value_now = Monoid::op(value_now, node[l]);
                        l++;
                    }
                }
                return (l - sz);
            }
            value_now = Monoid::op(value_now, node[l]);
            l++;
        } while((l & -l) != l);
        return N;
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= N);
        assert(f(Monoid::e()));
        if(r == 0) return 0;
        r += sz;
        T value_now = Monoid::e();
        do {
            r--;
            while(r > 1 && (r & 1)) r >>= 1;
            if(!f(Monoid::op(node[r], value_now))) {
                while(r < sz) {
                    r = 2 * r + 1;
                    if(f(Monoid::op(node[r], value_now))) {
                        value_now = Monoid::op(node[r], value_now);
                        r--;
                    }
                }
                return ((r + 1) - sz);
            }
            value_now = Monoid::op(node[r], value_now);
        } while((r & -r) != r);
        return 0;
    }

  private:
    int N, sz;
    std::vector<T> node;
};
#line 4 "yuki1435.test.cpp"

// seg.max_right() の verify

struct S {
    int mn1, mn2, mx;
    S(int mn1, int mn2, int mx): mn1(mn1), mn2(mn2), mx(mx) {} 
};

constexpr const int inf = 1000000000 + 5;

struct Monoid {
    using value_type = S;
    static S op(const S& l, const S& r) {
        int mn1 = min(l.mn1, r.mn1);
        int mn2 = min({max(l.mn1, r.mn1), l.mn2, r.mn2});
        int mx = max(l.mx, r.mx);
        return S(mn1, mn2, mx);
    }
    static S e() { return S(inf, inf, -1); }
};

int main() {
    int N;
    cin >> N;
    Segtree<Monoid> seg(N);
    REP(i, N) {
        int a;
        cin >> a;
        seg.set(i, S(a, inf, a));
    }
    ll ans = 0;
    REP(l, N) {
        auto check = [&](const S& x) {
            return (x.mn1 + x.mn2 >= x.mx);
        };
        int r = seg.max_right<decltype(check)>(l, check);
        ans += max(0, r - l - 1);
    }
    cout << ans << endl;
}
0