結果
| 問題 |
No.1435 Mmm......
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-02-13 15:21:17 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 56 ms / 2,000 ms |
| コード長 | 6,408 bytes |
| コンパイル時間 | 1,607 ms |
| コンパイル使用メモリ | 198,540 KB |
| 最終ジャッジ日時 | 2025-02-10 14:53:12 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 24 |
ソースコード
#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;
}