結果

問題 No.1209 XOR Into You
ユーザー 沙耶花沙耶花
提出日時 2020-08-30 14:12:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 2,008 bytes
コンパイル時間 2,008 ms
コンパイル使用メモリ 218,436 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-04-27 07:23:46
合計ジャッジ時間 7,411 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 24 ms
6,940 KB
testcase_05 AC 25 ms
6,944 KB
testcase_06 AC 39 ms
6,940 KB
testcase_07 AC 129 ms
9,344 KB
testcase_08 AC 113 ms
8,576 KB
testcase_09 AC 112 ms
8,576 KB
testcase_10 AC 88 ms
7,680 KB
testcase_11 AC 111 ms
8,576 KB
testcase_12 AC 101 ms
8,192 KB
testcase_13 AC 118 ms
8,832 KB
testcase_14 AC 100 ms
8,192 KB
testcase_15 AC 104 ms
8,320 KB
testcase_16 AC 105 ms
8,192 KB
testcase_17 AC 98 ms
8,064 KB
testcase_18 AC 146 ms
9,984 KB
testcase_19 AC 104 ms
8,192 KB
testcase_20 AC 119 ms
9,088 KB
testcase_21 AC 87 ms
7,552 KB
testcase_22 AC 100 ms
10,368 KB
testcase_23 AC 99 ms
10,368 KB
testcase_24 AC 99 ms
10,368 KB
testcase_25 AC 158 ms
10,368 KB
testcase_26 AC 155 ms
10,368 KB
testcase_27 AC 157 ms
10,368 KB
testcase_28 AC 157 ms
10,496 KB
testcase_29 AC 155 ms
10,240 KB
testcase_30 AC 158 ms
10,368 KB
testcase_31 AC 86 ms
6,944 KB
testcase_32 AC 86 ms
6,944 KB
testcase_33 AC 85 ms
6,940 KB
testcase_34 AC 90 ms
6,940 KB
testcase_35 AC 85 ms
6,940 KB
testcase_36 AC 85 ms
6,940 KB
testcase_37 AC 34 ms
6,940 KB
testcase_38 AC 127 ms
9,216 KB
testcase_39 AC 110 ms
8,448 KB
testcase_40 AC 103 ms
10,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 2000000000000000000

template <typename T>
struct BIT{
	vector<T> v;
	int n;
	
	T init_value = 0;
	
	BIT(int sz=0){
		n=sz+1;
		v.resize(n,init_value);
	}
	
	BIT(vector<T> &x){
		n=x.size()+1;
		v.resize(n,init_value);
		
		for(int i=0;i<x.size();i++){
			add(i,x[i]);
		}
		
	}

	void add(int x,T val){
		x++;
		while(x < n){
			v[x] = func(v[x],val);
			x += x & (-x);
		}
	}
	
	//区間[0,r)におけるクエリ処理
	T query(int r){
		T ret = init_value;
		
		while(r>0){
			ret = func(v[r],ret);
			r -= r & (-r);
		}
		
		return ret;
	}
	
	T query(int l,int r){
		return query(r) - query(l);
	}
	
	T func(T a,T b){
		return a+b;
	}
	
	int size(){
		return n-1;
	}
	
};

int main(){
	
	int N;
	cin>>N;
	
	vector<int> A(N),B(N);
	for(int i=0;i<N;i++)scanf("%d",&A[i]);
	for(int i=0;i<N;i++)scanf("%d",&B[i]);
	
	if(A[0]!=B[0]||A.back()!=B.back()){
		cout<<-1<<endl;
		return 0;
	}
	
	vector<int> C(N-1),D(N-1);
	for(int i=0;i<N-1;i++){
		C[i] = A[i]^A[i+1];
		D[i] = B[i]^B[i+1];
	}
	
	{
		auto x = C,y = D;
		sort(x.begin(),x.end());
		sort(y.begin(),y.end());
		if(x!=y){
			cout<<-1<<endl;
			return 0;
		}
		{
			map<int,int> cnt;
			for(int i=0;i<C.size();i++){
				int d = distance(x.begin(),lower_bound(x.begin(),x.end(),C[i]));
				cnt[C[i]]++;
				C[i] = d+cnt[C[i]]-1;
				
				
			}
		}
		
		{
			map<int,int> cnt;
			for(int i=0;i<D.size();i++){
				int d = distance(x.begin(),lower_bound(x.begin(),x.end(),D[i]));
				cnt[D[i]]++;
				D[i] = d+cnt[D[i]]-1;
				
			}
		}
		
	}
	
	vector<int> E(N-1);
	for(int i=0;i<N-1;i++){
		E[C[i]] = i;
	}
	
	for(int i=0;i<N-1;i++){
		D[i] = E[D[i]];
	}
	swap(E,D);
	
	vector<int> pos(N-1);
	for(int i=0;i<N-1;i++){
		pos[E[i]]=i;
	}
	
	BIT<long long> Bi(E.size());
	
	long long ans = 0LL;
	for(int i=0;i<N-1;i++){
		ans += Bi.query(pos[i],Bi.size());
		Bi.add(pos[i],1LL);
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0