結果

問題 No.1788 Same Set
ユーザー kmjp
提出日時 2021-12-18 00:39:11
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 554 ms / 4,000 ms
コード長 2,629 bytes
コンパイル時間 2,340 ms
コンパイル使用メモリ 208,728 KB
最終ジャッジ日時 2025-01-27 02:53:33
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------


int N;
int A[202020];
int B[202020];

int PA[404040];
int PB[404040];

static ll const def=0;
template<class V,int NV> class SegTree_3 {
public:
	vector<V> val;
	vector<pair<V,int>> ma;
	SegTree_3(){
		int i;
		val.resize(NV*2,0); ma.resize(NV*2);
		FOR(i,NV) ma[i+NV]={0,1};
		for(i=NV-1;i>=1;i--) ma[i]={0,ma[2*i].second+ma[2*i+1].second};
	};
	
	pair<V,int> getval(int x,int y,int l=0,int r=NV,int k=1) {
		if(r<=x || y<=l || y<=x) return {1<<20,0};
		if(x<=l && r<=y) return ma[k];
		auto a=getval(x,y,l,(l+r)/2,k*2);
		auto b=getval(x,y,(l+r)/2,r,k*2+1);
		if(a.first>b.first) swap(a,b);
		if(a.first==b.first) a.second+=b.second;
		a.first+=val[k];
		return a;
	}
	
	pair<V,int> update(int x,int y, V v,int l=0,int r=NV,int k=1) {
		if(l>=r||y<=x) return {1<<20,0};
		if(x<=l && r<=y) {
			val[k]+=v;
			ma[k].first+=v;
		}
		else if(l < y && x < r) {
			auto a=update(x,y,v,l,(l+r)/2,k*2);
			auto b=update(x,y,v,(l+r)/2,r,k*2+1);
			if(a.first>b.first) swap(a,b);
			if(a.first==b.first) a.second+=b.second;
			a.first+=val[k];
			ma[k]=a;
		}
		return ma[k];
	}
};
SegTree_3<int,1<<20> st;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	
	cin>>N;
	FOR(i,N) cin>>A[i+1];
	FOR(i,N) cin>>B[i+1];
	
	ll ret=0;
	for(i=1;i<=N;i++) {
		if(A[i]==B[i]) {
			x=min(PA[A[i]],PB[B[i]]);
			y=max(PA[A[i]],PB[B[i]]);
			st.update(x,y,-1);
			PA[A[i]]=i;
			PB[B[i]]=i;
		}
		else {
			x=min(PA[A[i]],PB[A[i]]);
			y=max(PA[A[i]],PB[A[i]]);
			st.update(x,y,-1);
			x=min(PA[B[i]],PB[B[i]]);
			y=max(PA[B[i]],PB[B[i]]);
			st.update(x,y,-1);
			PA[A[i]]=i;
			PB[B[i]]=i;
			x=min(PA[A[i]],PB[A[i]]);
			y=max(PA[A[i]],PB[A[i]]);
			st.update(x,y,1);
			x=min(PA[B[i]],PB[B[i]]);
			y=max(PA[B[i]],PB[B[i]]);
			st.update(x,y,1);
		}
		
		auto a=st.getval(0,i);
		if(a.first==0) ret+=a.second;
		
		
	}
	
	
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0