結果

問題 No.1435 Mmm......
ユーザー auauaauaua
提出日時 2021-03-19 23:30:46
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 788 ms / 2,000 ms
コード長 2,563 bytes
コンパイル時間 1,793 ms
コンパイル使用メモリ 175,452 KB
実行使用メモリ 17,104 KB
最終ジャッジ日時 2023-08-12 04:50:33
合計ジャッジ時間 15,921 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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,384 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 461 ms
16,524 KB
testcase_07 AC 583 ms
16,444 KB
testcase_08 AC 686 ms
17,020 KB
testcase_09 AC 609 ms
16,784 KB
testcase_10 AC 538 ms
16,448 KB
testcase_11 AC 526 ms
16,504 KB
testcase_12 AC 482 ms
16,572 KB
testcase_13 AC 476 ms
16,636 KB
testcase_14 AC 340 ms
10,052 KB
testcase_15 AC 713 ms
16,700 KB
testcase_16 AC 552 ms
16,444 KB
testcase_17 AC 393 ms
10,100 KB
testcase_18 AC 760 ms
16,860 KB
testcase_19 AC 513 ms
16,532 KB
testcase_20 AC 660 ms
16,868 KB
testcase_21 AC 173 ms
16,744 KB
testcase_22 AC 391 ms
17,104 KB
testcase_23 AC 747 ms
16,712 KB
testcase_24 AC 785 ms
17,052 KB
testcase_25 AC 771 ms
17,092 KB
testcase_26 AC 788 ms
16,708 KB
testcase_27 AC 757 ms
16,700 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
#define vec vector<ll>
#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=LONG_MAX;
const ll mod=MOD;
const ll MAX=20000010;

//SegmentTree
template< typename Monoid >
struct SegmentTree
{
  using F = function< Monoid(Monoid, Monoid) >;
 
  int sz;
  vector< Monoid > seg;
 
  const F f;
  const Monoid M1;
 
  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1)
  {
    sz = 1;
    while(sz < n) sz <<= 1;
    seg.assign(2 * sz, M1);
  }
 
  void set(int k, const Monoid &x)
  {
    seg[k + sz] = x;
  }
 
  void build()
  {
    for(int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
 
  void update(int k, const Monoid &x)
  {
    k += sz;
    seg[k] = x;
    while(k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }
 
  Monoid query(int a, int b)
  {
    Monoid L = M1, R = M1;
    for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if(a & 1) L = f(L, seg[a++]);
      if(b & 1) R = f(seg[--b], R);
    }
    return f(L, R);
  }
 
  Monoid operator[](const int &k) const
  {
    return seg[k + sz];
  }
};


signed main(){
    ll n;cin>>n;
    vector<ll>a(n);
    rep(i,n)cin>>a[i];
    SegmentTree<pll>small(n,[](pll a,pll b){
        pll c;
        if(a.fi<b.fi){
            c.fi=a.fi;
            c.se=min(b.fi,a.se);
        }else{
            c.fi=b.fi;
            c.se=min(a.fi,b.se);
        }
        return c;},mp(inf,inf));
    SegmentTree<ll>big(n,[](ll a,ll b){return max(a,b);},-1);
    rep(i,n){
        small.update(i,mp(a[i],inf));
        big.update(i,a[i]);
    }
    ll ans=0;
    REP(i,1,n){
        pll c=small.query(0,i+1);
        ll d=big.query(0,i+1);
        if(d<=c.fi+c.se){
            ans+=i;
            continue;
        }
        ll l=0,r=i-1;
        while(r-l>1){
            ll mid=(r+l)/2;
            pll cc=small.query(mid,i+1);
            ll dd=big.query(mid,i+1);
            if(dd<=cc.fi+cc.se){
                r=mid;
            }else{
                l=mid;
            }
        }
        ans+=i-r;
    }
    cout<<ans<<endl;
}
0