結果

問題 No.1788 Same Set
ユーザー Arnold ParkArnold Park
提出日時 2022-08-03 07:34:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,363 bytes
コンパイル時間 2,258 ms
コンパイル使用メモリ 181,436 KB
実行使用メモリ 25,860 KB
最終ジャッジ日時 2023-10-09 21:32:51
合計ジャッジ時間 16,128 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
24,000 KB
testcase_01 AC 7 ms
24,064 KB
testcase_02 AC 7 ms
24,060 KB
testcase_03 AC 7 ms
23,920 KB
testcase_04 AC 7 ms
23,972 KB
testcase_05 AC 7 ms
24,068 KB
testcase_06 AC 8 ms
24,072 KB
testcase_07 AC 7 ms
24,068 KB
testcase_08 AC 7 ms
23,996 KB
testcase_09 AC 7 ms
24,064 KB
testcase_10 AC 7 ms
24,056 KB
testcase_11 AC 81 ms
25,588 KB
testcase_12 AC 187 ms
25,704 KB
testcase_13 AC 226 ms
25,504 KB
testcase_14 AC 316 ms
25,584 KB
testcase_15 AC 419 ms
25,644 KB
testcase_16 AC 496 ms
25,632 KB
testcase_17 AC 487 ms
25,636 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 121 ms
25,696 KB
testcase_21 AC 430 ms
25,708 KB
testcase_22 AC 477 ms
25,848 KB
testcase_23 AC 379 ms
25,564 KB
testcase_24 AC 438 ms
25,860 KB
testcase_25 AC 583 ms
25,560 KB
testcase_26 AC 551 ms
25,564 KB
testcase_27 AC 171 ms
25,576 KB
testcase_28 AC 605 ms
25,712 KB
testcase_29 AC 539 ms
25,816 KB
testcase_30 AC 170 ms
25,852 KB
testcase_31 AC 578 ms
25,708 KB
testcase_32 AC 472 ms
25,560 KB
testcase_33 AC 508 ms
25,520 KB
testcase_34 AC 546 ms
25,652 KB
testcase_35 AC 533 ms
25,692 KB
testcase_36 AC 527 ms
25,596 KB
testcase_37 AC 528 ms
25,600 KB
testcase_38 AC 517 ms
25,852 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;

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]), comp.push_back(A[i]);
	for(int i=1; i<=N; i++) scanf("%d", &B[i]), comp.push_back(A[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;

	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