結果
問題 | No.1435 Mmm...... |
ユーザー | snow39 |
提出日時 | 2021-03-19 22:57:40 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,194 ms / 2,000 ms |
コード長 | 4,804 bytes |
コンパイル時間 | 1,830 ms |
コンパイル使用メモリ | 133,732 KB |
実行使用メモリ | 104,368 KB |
最終ジャッジ日時 | 2024-11-19 00:31:11 |
合計ジャッジ時間 | 8,911 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 2 ms
6,820 KB |
testcase_03 | AC | 2 ms
6,820 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,816 KB |
testcase_06 | AC | 129 ms
12,948 KB |
testcase_07 | AC | 156 ms
13,324 KB |
testcase_08 | AC | 179 ms
13,564 KB |
testcase_09 | AC | 165 ms
13,660 KB |
testcase_10 | AC | 146 ms
13,232 KB |
testcase_11 | AC | 143 ms
13,324 KB |
testcase_12 | AC | 133 ms
13,092 KB |
testcase_13 | AC | 132 ms
13,136 KB |
testcase_14 | AC | 97 ms
8,436 KB |
testcase_15 | AC | 187 ms
13,852 KB |
testcase_16 | AC | 158 ms
13,412 KB |
testcase_17 | AC | 110 ms
8,612 KB |
testcase_18 | AC | 189 ms
13,820 KB |
testcase_19 | AC | 140 ms
13,096 KB |
testcase_20 | AC | 173 ms
13,612 KB |
testcase_21 | AC | 1,194 ms
104,368 KB |
testcase_22 | AC | 1,105 ms
77,508 KB |
testcase_23 | AC | 198 ms
13,708 KB |
testcase_24 | AC | 200 ms
13,648 KB |
testcase_25 | AC | 200 ms
13,768 KB |
testcase_26 | AC | 198 ms
13,796 KB |
testcase_27 | AC | 199 ms
13,864 KB |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} #include <functional> #include <climits> //SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX); template< typename T> class SegmentTree{ private: void resize(int size){ n=1; while(n<size) n*=2; tree.resize(2*n-1,def); } public: using F = function<T(T,T)>; int n; vector<T> tree; F operation; T def; SegmentTree(){} SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){ resize(size); } void embody(int size,F _operation,T _def){ operation=_operation; def=_def; resize(size); } void initialize(const vector<T> &v){ int size=v.size(); resize(size); for(int i=0;i<size;i++) tree[i+n-1]=v[i]; for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]); } void update(int index,T value){ index+=n-1; tree[index]=value; while(index>0){ index=(index-1)/2; tree[index]=operation(tree[2*index+1],tree[2*index+2]); } } T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b) if(r<0) r=n; if(r<=a||b<=l) return def; else if(a<=l&&r<=b) return tree[k]; else{ T lval=query(a,b,2*k+1,l,(l+r)/2); T rval=query(a,b,2*k+2,(l+r)/2,r); return operation(lval,rval); } } T get(int index){ return tree[index+n-1]; } int find(int x,int k=0,int l=0,int r=-1){// a[0]+...+a[i]>=x (i:minimal) if(r<0) r=n; if(tree[k]<x) return -1; if(r-l==1) return k-(n-1); if(tree[2*k+1]>=x) return find(x,2*k+1,l,(l+r)/2); else return find(x-tree[2*k+1],2*k+2,(l+r)/2,r); } //right -> change vl to vr (notice doesn't contain b. range [a,b)) int find_left(int a,int b,T x,int k=0,int l=0,int r=-1){// max(a[a],...,a[i])>=x (i:minimal) if(r<0) r=n; if(tree[k]<x||r<=a||b<=l) return -1; if(r-l==1) return k-(n-1); int vl=find_left(a,b,x,2*k+1,l,(l+r)/2); if(vl>=0) return vl; return find_left(a,b,x,2*k+2,(l+r)/2,r); } }; const int INF=1e9; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N; cin>>N; vector<int> A(N); rep(i,N) cin>>A[i]; typedef pair<int,int> Pli; SegmentTree<Pli> maseg(N,[](Pli a,Pli b){ if(a.first<=b.first) return b; return a; },mkp(-INF,N)); { vector<Pli> init(N); rep(i,N) init[i]=mkp(A[i],i); maseg.initialize(init); } typedef pair<int,int> Pll; SegmentTree<Pll> miseg(N,[](Pll a,Pll b){ int mi=min(min(a.first,a.second),min(b.first,b.second)); if(mi==a.first) return mkp(mi,min(a.second,min(b.first,b.second))); else if(mi==a.second) return mkp(mi,min(a.first,min(b.first,b.second))); else if(mi==b.first) return mkp(mi,min(b.second,min(a.first,a.second))); else return mkp(mi,min(b.first,min(a.first,a.second))); },mkp(INF,INF)); { vector<Pll> init(N); rep(i,N) init[i]=mkp(A[i],INF); miseg.initialize(init); } ll ans=0; auto solve = [&](auto &&solve,int l,int r)->void{//[l,r) if(r-l<=1) return; auto [ma,mat] = maseg.query(l,r); solve(solve,l,mat); solve(solve,mat+1,r); if(mat-l<r-mat){ int mi1=ma; int mi2=ma; int last=r; for(int i=mat;i>=l;i--){ if(mi1>A[i]){ mi2=mi1; mi1=A[i]; }else if(mi2>A[i]){ mi2=A[i]; } if(mi1+mi2<ma) break; int ok=mat,ng=last; while(abs(ok-ng)>1){ int mid=(ok+ng)/2; auto [va,vb]=miseg.query(mat,mid+1); vector<int> p={mi1,mi2,va,vb}; sort(all(p)); if(p[0]+p[1]>=ma) ok=mid; else ng=mid; } if(i==mat) ans--; ans+=ok-mat+1; last=ok+1; } }else{ int mi1=ma; int mi2=ma; int last=l-1; for(int i=mat;i<r;i++){ if(mi1>A[i]){ mi2=mi1; mi1=A[i]; }else if(mi2>A[i]){ mi2=A[i]; } if(mi1+mi2<ma) break; int ok=mat,ng=last; while(abs(ok-ng)>1){ int mid=(ok+ng)/2; auto [va,vb]=miseg.query(mid,mat+1); vector<int> p={mi1,mi2,va,vb}; sort(all(p)); if(p[0]+p[1]>=ma) ok=mid; else ng=mid; } if(i==mat) ans--; ans+=mat-ok+1; last=ok-1; } } }; solve(solve,0,N); cout<<ans<<endl; return 0; }