結果

問題 No.1209 XOR Into You
ユーザー 沙耶花沙耶花
提出日時 2020-08-30 14:12:44
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 2,008 bytes
コンパイル時間 2,736 ms
コンパイル使用メモリ 214,756 KB
実行使用メモリ 10,516 KB
最終ジャッジ日時 2023-08-09 12:19:21
合計ジャッジ時間 9,218 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 27 ms
4,384 KB
testcase_05 AC 27 ms
4,380 KB
testcase_06 AC 43 ms
5,484 KB
testcase_07 AC 149 ms
8,852 KB
testcase_08 AC 136 ms
8,420 KB
testcase_09 AC 135 ms
8,388 KB
testcase_10 AC 104 ms
7,368 KB
testcase_11 AC 129 ms
8,364 KB
testcase_12 AC 120 ms
7,840 KB
testcase_13 AC 137 ms
8,764 KB
testcase_14 AC 114 ms
7,904 KB
testcase_15 AC 127 ms
8,156 KB
testcase_16 AC 124 ms
8,152 KB
testcase_17 AC 115 ms
7,840 KB
testcase_18 AC 178 ms
9,812 KB
testcase_19 AC 125 ms
7,800 KB
testcase_20 AC 148 ms
8,648 KB
testcase_21 AC 104 ms
7,264 KB
testcase_22 AC 115 ms
10,324 KB
testcase_23 AC 115 ms
10,328 KB
testcase_24 AC 115 ms
10,488 KB
testcase_25 AC 187 ms
10,216 KB
testcase_26 AC 194 ms
10,272 KB
testcase_27 AC 191 ms
10,516 KB
testcase_28 AC 192 ms
10,484 KB
testcase_29 AC 188 ms
10,264 KB
testcase_30 AC 191 ms
10,328 KB
testcase_31 AC 98 ms
6,328 KB
testcase_32 AC 100 ms
6,452 KB
testcase_33 AC 99 ms
6,092 KB
testcase_34 AC 102 ms
6,260 KB
testcase_35 AC 101 ms
6,280 KB
testcase_36 AC 101 ms
6,448 KB
testcase_37 AC 38 ms
6,244 KB
testcase_38 AC 143 ms
8,944 KB
testcase_39 AC 125 ms
8,492 KB
testcase_40 AC 113 ms
10,172 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