結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー 沙耶花沙耶花
提出日時 2020-04-17 23:26:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 337 ms / 3,500 ms
コード長 2,616 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 205,408 KB
実行使用メモリ 13,824 KB
最終ジャッジ日時 2024-04-14 15:39:19
合計ジャッジ時間 16,947 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 9 ms
6,944 KB
testcase_04 AC 9 ms
6,940 KB
testcase_05 AC 9 ms
6,940 KB
testcase_06 AC 9 ms
6,944 KB
testcase_07 AC 9 ms
6,944 KB
testcase_08 AC 8 ms
6,940 KB
testcase_09 AC 8 ms
6,944 KB
testcase_10 AC 9 ms
6,940 KB
testcase_11 AC 262 ms
12,032 KB
testcase_12 AC 258 ms
11,912 KB
testcase_13 AC 332 ms
13,696 KB
testcase_14 AC 273 ms
12,288 KB
testcase_15 AC 304 ms
13,056 KB
testcase_16 AC 250 ms
11,904 KB
testcase_17 AC 311 ms
13,108 KB
testcase_18 AC 296 ms
12,800 KB
testcase_19 AC 313 ms
13,312 KB
testcase_20 AC 333 ms
13,824 KB
testcase_21 AC 297 ms
12,928 KB
testcase_22 AC 257 ms
11,780 KB
testcase_23 AC 268 ms
12,288 KB
testcase_24 AC 314 ms
13,312 KB
testcase_25 AC 330 ms
13,568 KB
testcase_26 AC 305 ms
12,928 KB
testcase_27 AC 275 ms
12,316 KB
testcase_28 AC 332 ms
13,696 KB
testcase_29 AC 331 ms
13,696 KB
testcase_30 AC 333 ms
13,824 KB
testcase_31 AC 331 ms
13,696 KB
testcase_32 AC 337 ms
13,696 KB
testcase_33 AC 330 ms
13,696 KB
testcase_34 AC 285 ms
13,696 KB
testcase_35 AC 275 ms
13,824 KB
testcase_36 AC 275 ms
13,696 KB
testcase_37 AC 277 ms
13,696 KB
testcase_38 AC 270 ms
13,696 KB
testcase_39 AC 260 ms
13,312 KB
testcase_40 AC 265 ms
13,440 KB
testcase_41 AC 269 ms
13,568 KB
testcase_42 AC 266 ms
13,440 KB
testcase_43 AC 265 ms
13,568 KB
testcase_44 AC 263 ms
13,440 KB
testcase_45 AC 256 ms
13,184 KB
testcase_46 AC 271 ms
13,440 KB
testcase_47 AC 265 ms
13,440 KB
testcase_48 AC 277 ms
13,696 KB
testcase_49 AC 275 ms
13,696 KB
testcase_50 AC 273 ms
13,568 KB
testcase_51 AC 295 ms
13,824 KB
testcase_52 AC 294 ms
13,696 KB
testcase_53 AC 295 ms
13,696 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x)+modulo)%modulo)
#define Inf 100000000



template <typename T,typename F>
struct segtree{
	//元データx[i]はv[n+i]
	//v[i]の親はv[i/2],子はv[i*2]とv[i*2+1]
	F func;
	vector<T> v;
	int n;
	
	const T init_value = Inf;
	
	segtree(int sz,F f):func(f){
		n=1;
		while(true){
			if(n>=sz)break;
			n*=2;
		}
		v.resize(2*n,init_value);

		for(int i=n-1;i>=0;i--){
			v[i]=func(v[i<<1],v[(i<<1)+1]);
		}
	}
	
	segtree(vector<T> &x,F f):func(f){
		n=1;
		while(true){
			if(n>=x.size())break;
			n*=2;
		}
		v.resize(2*n,init_value);
		
		for(int i=0;i<x.size();i++){
			v[n+i]=x[i];
		}
		for(int i=n-1;i>=0;i--){
			v[i]=func(v[i<<1],v[(i<<1)+1]);
		}
	}

	void update(int x,T val){
		x+=n;
		v[x]=val;
		while(x>0){
			x>>=1;
			v[x]=func(v[x<<1],v[(x<<1)+1]);
		}
	}
	
	//区間[l,r)におけるクエリ処理
	T query(int l,int r){
		if(l>=r)return init_value;
		l+=n;
		r+=n;
		T res1 = init_value;
		T res2 = init_value;
		while(true){
			if(l&1){
				res1=func(res1,v[l++]);
			}
			if(r&1){
				res2=func(v[--r],res2);
			}
			if(l>=r)break;
			l>>=1;r>>=1;
		}
		return func(res1,res2);
	}
	
	void show(){
		int n = 1;
		for(int i=1;i<v.size();i++){
			for(int j=0;j<n;j++){
				if(j!=0)cout<<' ';
				cout<<v[i+j];
			}
			cout<<endl;
			i+=n-1;
			n*=2;
		}
	}
	
};

long long get(vector<int> P){
	int N = P.size();
	
	vector<int> V(N);
	for(int i=0;i<N;i++)V[P[i]] = i;
	
	auto f = [](int a,int b){
		return min(a,b);
	};
	
	segtree<int,decltype(f)> seg(N,f);
	
	vector<vector<int>> next(20,vector<int>(N,Inf));
	
	for(int i=N-1;i>=0;i--){
		int ind = V[i];
		next[0][V[i]] = seg.query(ind,N);
		seg.update(ind,ind);
	}
	
	for(int i=1;i<20;i++){
		for(int j=0;j<N;j++){
			if(next[i-1][j]!=Inf){
				next[i][j] = next[i-1][next[i-1][j]];
			}
		}
	}
	//cout<<next[0][1]<<endl;
	for(int i=0;i<N;i++)seg.update(V[i],i);
	//seg.show();
	long long ans = 0;
	for(int i=0;i<N;i++){
		int ok = V[i]+1;
		int ng = N+1;
		while(ng-ok>1){
			int mid = (ok+ng)/2;
			if(seg.query(V[i],mid)==i)ok = mid;
			else ng = mid;
			//cout<<ok<<','<<ng<<endl;
		}
		long long add = 0;
		int pos = V[i];
		for(int j=19;j>=0;j--){
			if(next[j][pos] < ok){
				pos = next[j][pos];
				add |= (1<<j);
			}
			else{
			}
		}
		//cout<<add<<endl;
		ans += add;
	}
	return ans;
}


int main() {
	
	int N;
	cin>>N;
	
	vector<int> P(N);
	for(int i=0;i<N;i++){
		cin>>P[i];
		P[i]--;
	}
	
	long long ans = get(P);
	//cout<<ans<<endl;
	reverse(P.begin(),P.end());
	ans += get(P);
	
	cout<<ans<<endl;
	
    return 0;
}
0