結果
| 問題 |
No.1435 Mmm......
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2021-03-19 22:38:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
RE
|
| 実行時間 | - |
| コード長 | 3,266 bytes |
| コンパイル時間 | 1,110 ms |
| コンパイル使用メモリ | 101,860 KB |
| 最終ジャッジ日時 | 2025-01-19 18:56:55 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 2 RE * 22 |
ソースコード
#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};
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;
add(n1.m1);
add(n1.m2);
add(n1.M);
add(n2.m1);
add(n2.m2);
add(n2.M);
sort(v.begin(), v.end());
int m = v.size();
return node{v[0], v[1], v[m-1]};
}
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;
if(n > 100000) return -1;
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;
}
milanis48663220