結果

問題 No.1788 Same Set
ユーザー Arnold ParkArnold Park
提出日時 2022-08-03 07:24:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 547 ms / 4,000 ms
コード長 2,029 bytes
コンパイル時間 1,901 ms
コンパイル使用メモリ 176,648 KB
実行使用メモリ 23,896 KB
最終ジャッジ日時 2023-10-09 21:20:16
合計ジャッジ時間 14,551 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
21,808 KB
testcase_01 AC 7 ms
21,756 KB
testcase_02 AC 7 ms
21,860 KB
testcase_03 AC 7 ms
21,740 KB
testcase_04 AC 7 ms
21,872 KB
testcase_05 AC 7 ms
21,852 KB
testcase_06 AC 7 ms
21,808 KB
testcase_07 AC 7 ms
21,736 KB
testcase_08 AC 7 ms
21,856 KB
testcase_09 AC 7 ms
21,868 KB
testcase_10 AC 7 ms
21,744 KB
testcase_11 AC 71 ms
21,892 KB
testcase_12 AC 172 ms
22,080 KB
testcase_13 AC 212 ms
22,084 KB
testcase_14 AC 293 ms
22,104 KB
testcase_15 AC 381 ms
22,096 KB
testcase_16 AC 448 ms
22,184 KB
testcase_17 AC 299 ms
23,668 KB
testcase_18 AC 401 ms
22,692 KB
testcase_19 AC 191 ms
22,872 KB
testcase_20 AC 86 ms
22,312 KB
testcase_21 AC 395 ms
22,340 KB
testcase_22 AC 430 ms
23,584 KB
testcase_23 AC 331 ms
23,696 KB
testcase_24 AC 392 ms
23,684 KB
testcase_25 AC 532 ms
23,800 KB
testcase_26 AC 503 ms
23,896 KB
testcase_27 AC 102 ms
23,560 KB
testcase_28 AC 547 ms
23,896 KB
testcase_29 AC 463 ms
23,820 KB
testcase_30 AC 101 ms
23,668 KB
testcase_31 AC 504 ms
23,804 KB
testcase_32 AC 391 ms
23,800 KB
testcase_33 AC 427 ms
23,892 KB
testcase_34 AC 457 ms
23,796 KB
testcase_35 AC 456 ms
23,800 KB
testcase_36 AC 444 ms
23,780 KB
testcase_37 AC 448 ms
23,880 KB
testcase_38 AC 437 ms
23,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int MAXN = 4e5;

int N, A[MAXN+10], B[MAXN+10];
int PA[MAXN+10], PB[MAXN+10];

pii operator + (const pii &p, const pii &q)
{
	if(p.first==q.first) return {p.first, p.second+q.second};
	return min(p, q);
}

struct SEG
{
	pii tree[MAXN*4+10];
	int lazy[MAXN*4+10];

	void busy(int node, int tl, int tr)
	{
		if(!lazy[node]) return;
		tree[node].first+=lazy[node];
		if(tl!=tr)
		{
			lazy[node*2]+=lazy[node];
			lazy[node*2+1]+=lazy[node];
		}
		lazy[node]=0;
	}

	void init(int node, int tl, int tr)
	{
		if(tl==tr)
		{
			tree[node]=pii(0, 1);
			return;
		}
		int mid=tl+tr>>1;
		init(node*2, tl, mid);
		init(node*2+1, mid+1, tr);
		tree[node]=tree[node*2]+tree[node*2+1];
	}

	void update(int node, int tl, int tr, int l, int r, int k)
	{
		if(l>r) return;
		busy(node, tl, tr);
		if(r<tl || tr<l) return;
		if(l<=tl && tr<=r)
		{
			lazy[node]+=k;
			busy(node, tl, tr);
			return;
		}
		int mid=tl+tr>>1;
		update(node*2, tl, mid, l, r, k);
		update(node*2+1, mid+1, tr, l, r, k);
		tree[node]=tree[node*2]+tree[node*2+1];
	}

	pii query(int node, int tl, int tr, int l, int r)
	{
		busy(node, tl, tr);
		if(l<=tl && tr<=r) return tree[node];
		if(r<tl || tr<l) return pii(987654321, 0);
		int mid=tl+tr>>1;
		return query(node*2, tl, mid, l, r)+query(node*2+1, mid+1, tr, l, r);
	}
}seg;

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++) scanf("%d", &A[i]);
	for(int i=1; i<=N; i++) scanf("%d", &B[i]);

	seg.init(1, 1, N);

	ll ans=0;
	for(int i=1; i<=N; i++)
	{
		int l, r;
		vector<int> V;
		V.push_back(A[i]);
		if(A[i]!=B[i]) V.push_back(B[i]);

		for(auto it : V)
		{
			l=PA[it]; r=PB[it]; if(l>r) swap(l, r);
			seg.update(1, 1, N, l+1, r, -1);
		}

		PA[A[i]]=i; PB[B[i]]=i;

		for(auto it : V)
		{
			l=PA[it]; r=PB[it]; if(l>r) swap(l, r);
			seg.update(1, 1, N, l+1, r, 1);
		}

		pii t=seg.query(1, 1, N, 1, i);
		if(t.first==0) ans+=t.second;
	}
	printf("%lld\n", ans);
}
0