結果

問題 No.1788 Same Set
ユーザー shobonvip
提出日時 2024-09-21 15:28:17
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 878 ms / 4,000 ms
コード長 2,746 bytes
コンパイル時間 6,437 ms
コンパイル使用メモリ 317,024 KB
実行使用メモリ 49,544 KB
最終ジャッジ日時 2024-09-21 15:28:46
合計ジャッジ時間 28,179 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

struct S {
	ll val, sum;
};

S op(S a, S b){
	if (a.val < b.val) return a;
	if (a.val > b.val) return b;
	return S{a.val, a.sum + b.sum};
}

S e(){
	return S{(ll)1e18, 0};
}

S mapping(ll f, S x) {
	return S{x.val + f, x.sum};
}

ll composition(ll g, ll f){
	return g + f;
}

ll id(){
	return 0;
}

// importbisect
template <typename T>
int bisect_left(vector<T> &X, T v){
	return lower_bound(X.begin(), X.end(), v) - X.begin();
}

template <typename T>
int bisect_right(vector<T> &X, T v){
	return upper_bound(X.begin(), X.end(), v) - X.begin();
}
// -----


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int n; cin >> n;
	vector<int> a(n), b(n);
	rep(i,0,n){
		cin >> a[i];
	}
	rep(i,0,n){
		cin >> b[i];
	}

	const int mx = 400050;

	lazy_segtree<S,op,e,ll,mapping,composition,id> seg(vector<S>(n, S{0,1}));

	vector bucket_a(mx, vector<int>(0));
	vector bucket_b(mx, vector<int>(0));
	rep(i,0,n){
		bucket_a[a[i]].push_back(i);
		bucket_b[b[i]].push_back(i);
	}

	vector<pair<int,int>> now_intervals(mx, pair(0,0));

	auto update = [&](int i, int l) -> void {
		int x = bisect_left(bucket_a[i], l);
		int y = bisect_left(bucket_b[i], l);
		int f = n;
		int g = n;
		if (x < (int)bucket_a[i].size()) {
			f = bucket_a[i][x];
		}
		if (y < (int)bucket_b[i].size()) {
			g = bucket_b[i][y];
		}
		if (f > g) swap(f, g);
		seg.apply(now_intervals[i].first, now_intervals[i].second, -1);
		now_intervals[i] = pair(f, g);
		seg.apply(now_intervals[i].first, now_intervals[i].second, +1);
	};

	rep(i,0,mx){
		update(i, 0);
	}

	ll ans = 0;
	rep(i,0,n){
		S g = seg.prod(i, n);
		if (g.val == 0) {
			ans += g.sum;
		}
		update(a[i], i+1);
		update(b[i], i+1);
	}
	cout << ans << '\n';


}

0