結果

問題 No.1788 Same Set
ユーザー Arnold ParkArnold Park
提出日時 2022-08-03 07:37:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 430 ms / 4,000 ms
コード長 1,680 bytes
コンパイル時間 2,047 ms
コンパイル使用メモリ 178,628 KB
実行使用メモリ 17,400 KB
最終ジャッジ日時 2023-10-09 21:36:23
合計ジャッジ時間 12,538 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,708 KB
testcase_01 AC 3 ms
9,668 KB
testcase_02 AC 3 ms
9,724 KB
testcase_03 AC 3 ms
9,612 KB
testcase_04 AC 3 ms
9,708 KB
testcase_05 AC 3 ms
9,696 KB
testcase_06 AC 3 ms
9,764 KB
testcase_07 AC 3 ms
9,692 KB
testcase_08 AC 3 ms
9,764 KB
testcase_09 AC 3 ms
9,624 KB
testcase_10 AC 3 ms
9,944 KB
testcase_11 AC 48 ms
11,364 KB
testcase_12 AC 124 ms
17,400 KB
testcase_13 AC 154 ms
17,048 KB
testcase_14 AC 216 ms
17,204 KB
testcase_15 AC 288 ms
17,140 KB
testcase_16 AC 350 ms
17,064 KB
testcase_17 AC 304 ms
17,148 KB
testcase_18 AC 370 ms
17,400 KB
testcase_19 AC 233 ms
17,220 KB
testcase_20 AC 94 ms
14,268 KB
testcase_21 AC 301 ms
17,216 KB
testcase_22 AC 347 ms
17,200 KB
testcase_23 AC 273 ms
16,888 KB
testcase_24 AC 314 ms
16,936 KB
testcase_25 AC 407 ms
17,100 KB
testcase_26 AC 408 ms
17,200 KB
testcase_27 AC 137 ms
11,368 KB
testcase_28 AC 430 ms
17,188 KB
testcase_29 AC 395 ms
17,204 KB
testcase_30 AC 139 ms
11,936 KB
testcase_31 AC 408 ms
17,204 KB
testcase_32 AC 354 ms
17,176 KB
testcase_33 AC 369 ms
17,000 KB
testcase_34 AC 388 ms
17,024 KB
testcase_35 AC 391 ms
17,060 KB
testcase_36 AC 390 ms
17,092 KB
testcase_37 AC 385 ms
17,100 KB
testcase_38 AC 384 ms
17,100 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];
vector<int> comp;

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

	void update(int node, int tl, int tr, int l, int r, int k)
	{
		if(l>r) return;
		if(r<tl || tr<l) return;
		if(l<=tl && tr<=r)
		{
			tree[node].first+=k;
			if(tree[node].first) tree[node].second=tr-tl+1;
			else if(tl==tr) tree[node].second=0;
			else tree[node].second=tree[node*2].second+tree[node*2+1].second;
			return;
		}
		int mid=tl+tr>>1;
		update(node*2, tl, mid, l, r, k);
		update(node*2+1, mid+1, tr, l, r, k);
		if(tree[node].first) tree[node].second=tr-tl+1;
		else tree[node].second=tree[node*2].second+tree[node*2+1].second;
	}
}seg;

int main()
{
	scanf("%d", &N);
	for(int i=1; i<=N; i++) scanf("%d", &A[i]), comp.push_back(A[i]);
	for(int i=1; i<=N; i++) scanf("%d", &B[i]), comp.push_back(B[i]);
	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());
	for(int i=1; i<=N; i++) A[i]=lower_bound(comp.begin(), comp.end(), A[i])-comp.begin()+1;
	for(int i=1; i<=N; i++) B[i]=lower_bound(comp.begin(), comp.end(), B[i])-comp.begin()+1;

	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);
		}

		ans+=i-seg.tree[1].second;
	}
	printf("%lld\n", ans);
}
0