結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー autumn-eelautumn-eel
提出日時 2020-04-18 10:25:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 3,500 ms
コード長 1,578 bytes
コンパイル時間 2,136 ms
コンパイル使用メモリ 177,896 KB
実行使用メモリ 8,172 KB
最終ジャッジ日時 2024-04-14 20:31:09
合計ジャッジ時間 6,545 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 4 ms
6,940 KB
testcase_05 AC 4 ms
6,940 KB
testcase_06 AC 3 ms
6,940 KB
testcase_07 AC 3 ms
6,944 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 4 ms
6,940 KB
testcase_11 AC 45 ms
7,176 KB
testcase_12 AC 45 ms
7,052 KB
testcase_13 AC 54 ms
7,508 KB
testcase_14 AC 48 ms
7,068 KB
testcase_15 AC 51 ms
7,220 KB
testcase_16 AC 44 ms
6,944 KB
testcase_17 AC 54 ms
7,224 KB
testcase_18 AC 50 ms
7,212 KB
testcase_19 AC 52 ms
7,228 KB
testcase_20 AC 55 ms
7,504 KB
testcase_21 AC 50 ms
7,212 KB
testcase_22 AC 45 ms
6,944 KB
testcase_23 AC 46 ms
7,056 KB
testcase_24 AC 52 ms
7,228 KB
testcase_25 AC 54 ms
7,372 KB
testcase_26 AC 51 ms
7,216 KB
testcase_27 AC 48 ms
7,192 KB
testcase_28 AC 54 ms
7,504 KB
testcase_29 AC 54 ms
7,380 KB
testcase_30 AC 54 ms
7,376 KB
testcase_31 AC 55 ms
7,372 KB
testcase_32 AC 54 ms
7,376 KB
testcase_33 AC 55 ms
7,380 KB
testcase_34 AC 57 ms
7,888 KB
testcase_35 AC 60 ms
7,888 KB
testcase_36 AC 62 ms
7,892 KB
testcase_37 AC 64 ms
7,888 KB
testcase_38 AC 62 ms
7,492 KB
testcase_39 AC 57 ms
7,812 KB
testcase_40 AC 59 ms
7,364 KB
testcase_41 AC 64 ms
7,820 KB
testcase_42 AC 63 ms
7,960 KB
testcase_43 AC 63 ms
7,908 KB
testcase_44 AC 64 ms
7,912 KB
testcase_45 AC 62 ms
7,764 KB
testcase_46 AC 66 ms
8,000 KB
testcase_47 AC 57 ms
7,772 KB
testcase_48 AC 61 ms
8,152 KB
testcase_49 AC 66 ms
7,944 KB
testcase_50 AC 65 ms
7,796 KB
testcase_51 AC 59 ms
8,048 KB
testcase_52 AC 59 ms
8,052 KB
testcase_53 AC 59 ms
8,172 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0