結果

問題 No.1209 XOR Into You
ユーザー furafura
提出日時 2020-10-16 14:53:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 248 ms / 2,000 ms
コード長 1,987 bytes
コンパイル時間 2,534 ms
コンパイル使用メモリ 220,072 KB
実行使用メモリ 16,484 KB
最終ジャッジ日時 2023-09-28 02:00:16
合計ジャッジ時間 12,250 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 39 ms
5,456 KB
testcase_05 AC 40 ms
5,428 KB
testcase_06 AC 40 ms
5,448 KB
testcase_07 AC 191 ms
14,248 KB
testcase_08 AC 161 ms
13,076 KB
testcase_09 AC 166 ms
13,124 KB
testcase_10 AC 129 ms
11,304 KB
testcase_11 AC 165 ms
12,796 KB
testcase_12 AC 153 ms
12,276 KB
testcase_13 AC 180 ms
13,320 KB
testcase_14 AC 144 ms
11,848 KB
testcase_15 AC 157 ms
12,412 KB
testcase_16 AC 158 ms
12,116 KB
testcase_17 AC 148 ms
11,832 KB
testcase_18 AC 230 ms
15,760 KB
testcase_19 AC 155 ms
12,312 KB
testcase_20 AC 192 ms
13,688 KB
testcase_21 AC 134 ms
11,112 KB
testcase_22 AC 143 ms
16,368 KB
testcase_23 AC 143 ms
16,452 KB
testcase_24 AC 139 ms
16,412 KB
testcase_25 AC 229 ms
16,368 KB
testcase_26 AC 227 ms
16,412 KB
testcase_27 AC 229 ms
16,364 KB
testcase_28 AC 226 ms
16,484 KB
testcase_29 AC 248 ms
16,372 KB
testcase_30 AC 229 ms
16,368 KB
testcase_31 AC 144 ms
11,564 KB
testcase_32 AC 145 ms
11,700 KB
testcase_33 AC 146 ms
11,612 KB
testcase_34 AC 146 ms
11,656 KB
testcase_35 AC 146 ms
11,664 KB
testcase_36 AC 143 ms
11,616 KB
testcase_37 AC 81 ms
11,736 KB
testcase_38 AC 185 ms
13,932 KB
testcase_39 AC 159 ms
12,712 KB
testcase_40 AC 138 ms
16,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

template<class G>
class Fenwick_tree{
	vector<G> a;
public:
	Fenwick_tree(){}
	Fenwick_tree(int n){ build(n); }
	Fenwick_tree(const vector<G>& a){ build(a); }
	void build(int n){
		a.assign(n,G{});
	}
	void build(const vector<G>& a){
		this->a=a;
		for(int i=1;i<a.size();i++) if(i+(i&-i)-1<a.size()) (this->a)[i+(i&-i)-1]+=(this->a)[i-1];
	}
	void add(int i,const G& val){
		for(i++;i<=a.size();i+=i&-i) a[i-1]+=val;
	}
	G sum(int l,int r)const{
		if(l==0){
			G res{};
			for(;r>0;r-=r&-r) res+=a[r-1];
			return res;
		}
		return sum(0,r)-sum(0,l);
	}
	int lower_bound(G val)const{
		if(val<=G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<val) val-=a[x+k-1], x+=k;
		return x;
	}
	int upper_bound(G val)const{
		if(val<G{}) return 0;
		int x=0,k;
		for(k=1;k<=a.size();k<<=1);
		for(k>>=1;k>0;k>>=1) if(x+k<=a.size() && a[x+k-1]<=val) val-=a[x+k-1], x+=k;
		return x;
	}
};

template<class T>
long long inversion_number(const vector<T>& a){
	auto X=a;
	sort(X.begin(),X.end());
	X.erase(unique(X.begin(),X.end()),X.end());

	long long res=0;
	Fenwick_tree<int> F(X.size());
	rep(i,a.size()){
		int x=lower_bound(X.begin(),X.end(),a[i])-X.begin();
		res+=F.sum(x+1,X.size());
		F.add(x,1);
	}
	return res;
}

int main(){
	int n; scanf("%d",&n);
	vector<int> a(n),b(n);
	rep(i,n) scanf("%d",&a[i]);
	rep(i,n) scanf("%d",&b[i]);

	vector<int> a2(n),b2(n);
	a2[0]=a[0];
	b2[0]=b[0];
	rep(i,n-1){
		a2[i+1]=a[i]^a[i+1];
		b2[i+1]=b[i]^b[i+1];
	}

	{
		auto a3=a2;
		auto b3=b2;
		sort(a3.begin(),a3.end());
		sort(b3.begin(),b3.end());
		if(a3!=b3){
			puts("-1");
			return 0;
		}
	}

	map<int,int> f;
	map<pair<int,int>,int> g;
	rep(i,n){
		int cnt=f[a2[i]];
		g[{a2[i],cnt}]=i;
		++f[a2[i]];
	}
	f.clear();
	rep(i,n){
		int cnt=f[b2[i]];
		++f[b2[i]];
		b2[i]=g[{b2[i],cnt}];
	}

	printf("%lld\n",inversion_number(b2));

	return 0;
}
0