結果

問題 No.1788 Same Set
ユーザー Arnold ParkArnold Park
提出日時 2022-08-03 07:04:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 574 ms / 4,000 ms
コード長 2,121 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 176,732 KB
実行使用メモリ 24,120 KB
最終ジャッジ日時 2023-10-10 21:52:58
合計ジャッジ時間 15,205 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
21,980 KB
testcase_01 AC 7 ms
22,100 KB
testcase_02 AC 7 ms
22,024 KB
testcase_03 AC 7 ms
22,036 KB
testcase_04 AC 6 ms
22,076 KB
testcase_05 AC 7 ms
21,944 KB
testcase_06 AC 7 ms
21,892 KB
testcase_07 AC 7 ms
22,008 KB
testcase_08 AC 7 ms
22,040 KB
testcase_09 AC 7 ms
21,968 KB
testcase_10 AC 7 ms
21,896 KB
testcase_11 AC 62 ms
21,896 KB
testcase_12 AC 172 ms
22,132 KB
testcase_13 AC 214 ms
22,256 KB
testcase_14 AC 310 ms
22,372 KB
testcase_15 AC 411 ms
22,108 KB
testcase_16 AC 490 ms
22,344 KB
testcase_17 AC 310 ms
23,836 KB
testcase_18 AC 426 ms
22,908 KB
testcase_19 AC 191 ms
23,012 KB
testcase_20 AC 78 ms
22,380 KB
testcase_21 AC 422 ms
22,636 KB
testcase_22 AC 458 ms
23,680 KB
testcase_23 AC 355 ms
23,816 KB
testcase_24 AC 408 ms
24,012 KB
testcase_25 AC 574 ms
23,952 KB
testcase_26 AC 535 ms
23,872 KB
testcase_27 AC 90 ms
23,640 KB
testcase_28 AC 573 ms
23,928 KB
testcase_29 AC 500 ms
23,960 KB
testcase_30 AC 92 ms
23,624 KB
testcase_31 AC 558 ms
23,928 KB
testcase_32 AC 421 ms
23,880 KB
testcase_33 AC 455 ms
23,952 KB
testcase_34 AC 493 ms
23,896 KB
testcase_35 AC 488 ms
23,844 KB
testcase_36 AC 493 ms
23,912 KB
testcase_37 AC 491 ms
23,872 KB
testcase_38 AC 483 ms
24,120 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;
		l=PA[A[i]]; r=PB[A[i]]; if(l>r) swap(l, r);
		seg.update(1, 1, N, l+1, r, -1);

		if(A[i]!=B[i])
		{
			l=PA[B[i]]; r=PB[B[i]]; if(l>r) swap(l, r);
			seg.update(1, 1, N, l+1, r, -1);
		}

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

		l=PA[A[i]]; r=PB[A[i]]; if(l>r) swap(l, r);
		seg.update(1, 1, N, l+1, r, 1);

		if(A[i]!=B[i])
		{
			l=PA[B[i]]; r=PB[B[i]]; 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