結果
| 問題 |
No.1435 Mmm......
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-10-22 18:01:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 416 ms / 2,000 ms |
| コード長 | 4,173 bytes |
| コンパイル時間 | 2,247 ms |
| コンパイル使用メモリ | 203,088 KB |
| 最終ジャッジ日時 | 2025-01-15 12:28:13 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 24 |
ソースコード
//@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;
}