結果

問題 No.1435 Mmm......
ユーザー mugen_1337mugen_1337
提出日時 2021-10-26 10:41:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,845 ms / 2,000 ms
コード長 4,410 bytes
コンパイル時間 3,557 ms
コンパイル使用メモリ 220,916 KB
実行使用メモリ 10,236 KB
最終ジャッジ日時 2024-04-15 08:19:34
合計ジャッジ時間 32,998 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 989 ms
9,852 KB
testcase_07 AC 1,222 ms
10,008 KB
testcase_08 AC 1,436 ms
9,956 KB
testcase_09 AC 1,312 ms
10,188 KB
testcase_10 AC 1,124 ms
10,136 KB
testcase_11 AC 1,102 ms
9,968 KB
testcase_12 AC 1,020 ms
10,068 KB
testcase_13 AC 989 ms
9,924 KB
testcase_14 AC 697 ms
6,944 KB
testcase_15 AC 1,498 ms
10,236 KB
testcase_16 AC 1,209 ms
9,932 KB
testcase_17 AC 799 ms
6,944 KB
testcase_18 AC 1,517 ms
10,104 KB
testcase_19 AC 1,076 ms
9,976 KB
testcase_20 AC 1,364 ms
10,080 KB
testcase_21 AC 1,832 ms
10,164 KB
testcase_22 AC 1,845 ms
10,116 KB
testcase_23 AC 1,539 ms
10,152 KB
testcase_24 AC 1,533 ms
10,032 KB
testcase_25 AC 1,545 ms
10,112 KB
testcase_26 AC 1,547 ms
10,112 KB
testcase_27 AC 1,537 ms
10,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 998244353
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}

template<typename Monoid>
struct SegmentTree{
    using F=function<Monoid(Monoid,Monoid)>;

    private:
    int sz;
    vector<Monoid> seg;

    Monoid query(int a,int b,int k,int l,int r){
        if(a<=l and r<=b)   return seg[k];
        if(b<=l or r<=a)    return M0;
        Monoid L=query(a,b,2*k,l,(l+r)/2);
        Monoid R=query(a,b,2*k+1,(l+r)/2,r);
        return f(L,R);
    }
    template<typename C>
    int find_first(int a,const C &check,Monoid &acc,int k,int l,int r){
        if(k>=sz){
            acc=f(acc,seg[k]);
            if(check(acc))  return -1;
            else            return k-sz;
        }
        int m=(l+r)/2;
        if(m<=a) return find_first(a,check,acc,2*k+1,m,r);
        if(a<=l and check(f(acc,seg[k]))){
            acc=f(acc,seg[k]);
            return -1;
        }
        int x=find_first(a,check,acc,2*k+0,l,m);
        if(x>=0) return x;
        return find_first(a,check,acc,2*k+1,m,r);
    }
    template<typename C>
    int find_last(int b,const C &check,Monoid &acc,int k,int l,int r){
        if(k>=sz){
            acc=f(acc,seg[k]);
            if(check(acc))  return -1;
            else            return k-sz+1;//ここはfalse, +1した位置はtrue
        }
        int m=(l+r)/2;
        if(b<=m) return find_last(b,check,acc,2*k,l,m);
        if(r<=b and check(f(acc,seg[k]))){
            acc=f(acc,seg[k]);
            return -1;
        }
        int x=find_last(b,check,acc,2*k+1,m,r);
        if(x>=0) return x;
        return find_last(b,check,acc,2*k,l,m);
    }

    public:

    F f;
    Monoid M0;// モノイドの元
    SegmentTree(int n,F f,Monoid M0):f(f),M0(M0){
        sz=1;
        while(sz<n)sz<<=1;
        seg.assign(2*sz,M0);
    }
    void set(int k,Monoid x){
        seg[k+sz]=x;
    }
    void build(){
        for(int k=sz-1;k>0;k--) seg[k]=f(seg[2*k],seg[2*k+1]);
    }
    void update(int k,Monoid x){
        k+=sz;
        seg[k]=x;
        k>>=1;
        for(;k;k>>=1) seg[k]=f(seg[2*k],seg[2*k+1]);
    }
    Monoid query(int a,int b){
        return query(a,b,1,0,sz);
    }
    Monoid operator[](const int &k)const{
        return seg[k+sz];
    }

    // http://codeforces.com/contest/914/submission/107505449
    // max x, check(query(a, x)) = true 
    template<typename C>
    int find_first(int a,const C &check){
        Monoid val=M0;
        return find_first(a,check,val,1,0,sz);
    }
    // http://codeforces.com/contest/914/submission/107505582
    // min x, check(query(x, b)) = true
    template<typename C>
    int find_last(int b,C &check){
        Monoid val=M0;
        return find_last(b,check,val,1,0,sz);
    }
};

using P=pair<int,int>;

P segf(P a,P b){
    auto [x,y]=minmax(a.first,b.first);
    return P(x,min({y,a.second,b.second}));
}



signed main(){
    int N;cin>>N;
    vector<int> A(N);
    cin>>A;

    SegmentTree<P> mi(N,segf,P(INF,INF));
    SegmentTree<int> mx(N,[&](int a,int b){return a>b?a:b;},-INF);
    rep(i,N){
        mi.set(i,P(A[i],INF));
        mx.set(i,A[i]);
    }
    mi.build();
    mx.build();

    ll res=0;
    rep(i,N-1){
        int l=i+2,r=N,t=i+2;
        while(l<=r){
            int m=(l+r)/2;
            auto u=mi.query(i,m);
            int v=mx.query(i,m);

            int S=(u.first+u.second-v);
            if(S>=0) t=m,l=m+1;
            else     r=m-1;
        }
        res+=t-i-1;
    }
    cout<<res<<endl;
    return 0;
}
0