結果
問題 |
No.1031 いたずら好きなお姉ちゃん
|
ユーザー |
|
提出日時 | 2020-04-18 10:25:04 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 57 ms / 3,500 ms |
コード長 | 1,578 bytes |
コンパイル時間 | 2,026 ms |
コンパイル使用メモリ | 182,316 KB |
実行使用メモリ | 8,052 KB |
最終ジャッジ日時 | 2024-10-03 23:06:22 |
合計ジャッジ時間 | 5,792 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<n;i++) using namespace std; typedef long long ll; typedef pair<int,int>P; template<class T> class Segtree{ int n; vector<T>dat; T INIT; T E; function<T(T,T)>F; public: Segtree(int n_,T INIT,T E,function<T(T,T)>F):INIT(INIT),E(E),F(F){ n=1;while(n<n_)n<<=1; dat=vector<T>(2*n,INIT); } void set(int k,T x){ k+=n; dat[k]=x; while(k>1){ k>>=1; dat[k]=F(dat[k<<1],dat[(k<<1)+1]); } } void update(int k,T x){ k+=n; dat[k]=F(dat[k],x); while(k>1){ k>>=1; dat[k]=F(dat[k<<1],dat[(k<<1)+1]); } } T query(int l,int r){ T resl=E,resr=E; for(l+=n,r+=n;l<r;l>>=1,r>>=1){ if(l&1)resl=F(resl,dat[l++]); if(r&1)resr=F(dat[--r],resr); } return F(resl,resr); } }; int n; int p[200000]; ll solve(){ vector<P>vmin,vmax; stack<P>st;st.push(P(-1,n)); for(int i=n-1;i>=0;i--){ while(st.top().first>p[i])st.pop(); vmin.push_back(P(i,st.top().second-1)); st.push(P(p[i],i)); } while(!st.empty())st.pop(); st.push(P(INT_MAX,-1)); rep(i,n){ while(st.top().first<p[i])st.pop(); vmax.push_back(P(st.top().second+1,i)); st.push((P(p[i],i))); } sort(vmin.begin(),vmin.end()); sort(vmax.begin(),vmax.end()); Segtree<int>seg(n,0,0,[](int a,int b){return a+b;}); ll ans=0; int g=0; for(auto p:vmin){ while(g<vmax.size()&&vmax[g].first<=p.first){ seg.update(vmax[g].second,1); g++; } ans+=seg.query(p.first+1,p.second+1); } return ans; } int main(){ cin>>n; rep(i,n)scanf("%d",&p[i]); ll ans=solve(); reverse(p,p+n); ans+=solve(); cout<<ans<<endl; }