結果

問題 No.1788 Same Set
ユーザー Arnold ParkArnold Park
提出日時 2022-08-03 07:32:09
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 457 ms / 4,000 ms
コード長 1,346 bytes
コンパイル時間 9,312 ms
コンパイル使用メモリ 518,620 KB
実行使用メモリ 22,116 KB
最終ジャッジ日時 2023-10-09 21:29:03
合計ジャッジ時間 21,027 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
19,172 KB
testcase_01 AC 6 ms
18,408 KB
testcase_02 AC 6 ms
19,328 KB
testcase_03 AC 6 ms
18,232 KB
testcase_04 AC 6 ms
19,052 KB
testcase_05 AC 5 ms
19,280 KB
testcase_06 AC 7 ms
18,880 KB
testcase_07 AC 5 ms
16,408 KB
testcase_08 AC 6 ms
16,488 KB
testcase_09 AC 6 ms
16,368 KB
testcase_10 AC 6 ms
19,100 KB
testcase_11 AC 45 ms
19,932 KB
testcase_12 AC 145 ms
20,160 KB
testcase_13 AC 179 ms
20,008 KB
testcase_14 AC 251 ms
19,596 KB
testcase_15 AC 326 ms
20,376 KB
testcase_16 AC 390 ms
19,584 KB
testcase_17 AC 265 ms
21,656 KB
testcase_18 AC 341 ms
21,028 KB
testcase_19 AC 159 ms
21,184 KB
testcase_20 AC 57 ms
19,960 KB
testcase_21 AC 339 ms
20,120 KB
testcase_22 AC 372 ms
21,420 KB
testcase_23 AC 277 ms
21,628 KB
testcase_24 AC 319 ms
21,664 KB
testcase_25 AC 442 ms
22,072 KB
testcase_26 AC 426 ms
21,884 KB
testcase_27 AC 65 ms
21,632 KB
testcase_28 AC 457 ms
22,116 KB
testcase_29 AC 398 ms
21,836 KB
testcase_30 AC 63 ms
22,104 KB
testcase_31 AC 426 ms
21,616 KB
testcase_32 AC 341 ms
21,804 KB
testcase_33 AC 368 ms
21,640 KB
testcase_34 AC 408 ms
22,020 KB
testcase_35 AC 402 ms
22,024 KB
testcase_36 AC 392 ms
21,744 KB
testcase_37 AC 394 ms
21,972 KB
testcase_38 AC 383 ms
21,952 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:29:13: warning: operator '>>' has lower precedence than '+'; '+' will be evaluated first [-Wshift-op-parentheses]
                int mid=tl+tr>>1;
                        ~~^~~~~
main.cpp:29:13: note: place parentheses around the '+' expression to silence this warning
                int mid=tl+tr>>1;
                          ^
                        (    )
1 warning generated.

ソースコード

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];

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

	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