結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー HIR180HIR180
提出日時 2020-04-17 21:59:41
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 3,500 ms
コード長 2,554 bytes
コンパイル時間 3,235 ms
コンパイル使用メモリ 228,656 KB
実行使用メモリ 12,588 KB
最終ジャッジ日時 2024-04-14 14:09:49
合計ジャッジ時間 8,051 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,880 KB
testcase_01 AC 4 ms
7,088 KB
testcase_02 AC 3 ms
7,040 KB
testcase_03 AC 6 ms
7,268 KB
testcase_04 AC 6 ms
7,264 KB
testcase_05 AC 6 ms
7,064 KB
testcase_06 AC 7 ms
7,168 KB
testcase_07 AC 6 ms
7,168 KB
testcase_08 AC 6 ms
7,060 KB
testcase_09 AC 6 ms
7,100 KB
testcase_10 AC 7 ms
7,164 KB
testcase_11 AC 60 ms
10,008 KB
testcase_12 AC 59 ms
9,980 KB
testcase_13 AC 72 ms
10,688 KB
testcase_14 AC 62 ms
10,144 KB
testcase_15 AC 67 ms
10,428 KB
testcase_16 AC 59 ms
10,076 KB
testcase_17 AC 69 ms
10,456 KB
testcase_18 AC 65 ms
10,376 KB
testcase_19 AC 69 ms
10,596 KB
testcase_20 AC 71 ms
10,696 KB
testcase_21 AC 66 ms
10,516 KB
testcase_22 AC 58 ms
9,948 KB
testcase_23 AC 60 ms
10,232 KB
testcase_24 AC 69 ms
10,480 KB
testcase_25 AC 72 ms
10,680 KB
testcase_26 AC 66 ms
10,408 KB
testcase_27 AC 62 ms
10,156 KB
testcase_28 AC 73 ms
10,824 KB
testcase_29 AC 72 ms
10,696 KB
testcase_30 AC 72 ms
10,808 KB
testcase_31 AC 72 ms
10,704 KB
testcase_32 AC 72 ms
10,700 KB
testcase_33 AC 73 ms
10,768 KB
testcase_34 AC 61 ms
12,588 KB
testcase_35 AC 65 ms
11,944 KB
testcase_36 AC 64 ms
12,028 KB
testcase_37 AC 66 ms
12,332 KB
testcase_38 AC 63 ms
11,224 KB
testcase_39 AC 62 ms
11,256 KB
testcase_40 AC 63 ms
11,372 KB
testcase_41 AC 63 ms
11,144 KB
testcase_42 AC 60 ms
10,952 KB
testcase_43 AC 63 ms
11,064 KB
testcase_44 AC 62 ms
11,116 KB
testcase_45 AC 60 ms
10,936 KB
testcase_46 AC 62 ms
11,436 KB
testcase_47 AC 61 ms
10,740 KB
testcase_48 AC 62 ms
10,852 KB
testcase_49 AC 64 ms
11,152 KB
testcase_50 AC 62 ms
10,688 KB
testcase_51 AC 66 ms
10,420 KB
testcase_52 AC 65 ms
10,424 KB
testcase_53 AC 65 ms
10,424 KB
testcase_54 AC 4 ms
6,944 KB
testcase_55 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const int mod = 1000000007;
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}


struct RMQ{
	#define s (1<<18)
	int seg[s];
	void init(){
		memset(seg, 0, sizeof(seg));
	}
	void update(int k,int a){
		k+=s/2-1; seg[k]=a;
		while(k>0){
			k=(k-1)/2;
			seg[k]=(seg[k*2+1]+seg[k*2+2]);
		}
	}
	int query(int a,int b,int k,int l,int r){
		if(r<a || b<l) return 0;
		if(a<=l && r<=b) return seg[k];
		else{
			int vl=query(a,b,k*2+1,l,(l+r)/2);
			int vr=query(a,b,k*2+2,(l+r)/2+1,r);
			return (vl+vr);
		}
	}
}rmq;
ll ans;
vector<int>vec[100005];
void solve(vector<int>vi){
	int n = vi.size();
	vector<int>mx(n), mn(n);
	stack<P>st;
	rep(i, n){
		while(st.size() && st.top().fi < vi[i]){
			mx[st.top().sc] = i-1;
			st.pop();
		}
		st.push(mp(vi[i],i));
	}
	while(st.size()) { mx[st.top().sc] = n-1; st.pop(); }
	for(int i=n-1;i>=0;i--){
		while(st.size() && st.top().fi > vi[i]){
			mn[st.top().sc] = i+1;
			st.pop();
		}
		st.push(mp(vi[i],i));
	}
	while(st.size()) { mn[st.top().sc] = 0; st.pop(); }
	
	rep(i, n){
		vec[i].clear();
	}
	rep(i, n) vec[mn[i]].pb(i);
	rmq.init();
	for(int i=n-1;i>=0;i--){
		for(auto at:vec[i+1]) rmq.update(at, 0);
		int L = i, R = mx[i];
		ans += rmq.query(L, R, 0, 0, s/2-1);
		rmq.update(i, 1);
	}
}
int main(){
	int n;
	scanf("%d",&n);
	vector<int>vi(n);
	rep(i, n) scanf("%d", &vi[i]);
	solve(vi);
	reverse(all(vi));
	solve(vi);
	o(ans);
}
0