結果
問題 | No.1209 XOR Into You |
ユーザー |
![]() |
提出日時 | 2020-08-30 13:27:27 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 616 ms / 2,000 ms |
コード長 | 2,072 bytes |
コンパイル時間 | 3,826 ms |
コンパイル使用メモリ | 212,540 KB |
最終ジャッジ日時 | 2025-01-13 21:11:23 |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 37 |
ソースコード
#include <bits/stdc++.h> using namespace std; using Int = long long; const char newl = '\n'; template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;} template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;} template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);} template<typename T=Int> vector<T> read(size_t n){ vector<T> ts(n); for(size_t i=0;i<n;i++) cin>>ts[i]; return ts; } template<typename T> struct BIT{ Int n; vector<T> bit; // 1-indexed BIT(Int n_):n(n_+1),bit(n+1,0){} T sum(Int i){ T s(0); for(Int x=i;x>0;x-=(x&-x)) s+=bit[x]; return s; } void add(Int i,T a){ if(i==0) return; for(Int x=i;x<=n;x+=(x&-x)) bit[x]+=a; } // [l, r) T query(Int l,Int r){ return sum(r-1)-sum(l-1); } Int lower_bound(Int w){ if(w<=0) return 0; Int x=0,r=1; while(r<n) r<<=1; for(Int k=r;k>0;k>>=1){ if(x+k<=n&&bit[x+k]<w){ w-=bit[x+k]; x+=k; } } return x+1; } // 0-indexed T sum0(Int i){return sum(i+1);} void add0(Int i,T a){add(i+1,a);} T query0(Int l,Int r){return sum(r)-sum(l);} }; //INSERT ABOVE HERE Int calc(vector<Int> vs){ Int n=vs.size(); BIT<Int> bit(n+10); Int res=0; for(Int i=0;i<n;i++){ res+=i-bit.sum0(vs[i]); bit.add0(vs[i],1); } return res; } signed main(){ cin.tie(0); ios::sync_with_stdio(0); Int n; cin>>n; auto as=read(n); auto bs=read(n); if(as[0]!=bs[0]) drop(-1); vector<Int> xs(n-1),ys(n-1); for(Int i=0;i+1<n;i++) xs[i]=as[i]^as[i+1]; for(Int i=0;i+1<n;i++) ys[i]=bs[i]^bs[i+1]; { auto us=xs; auto vs=ys; sort(us.begin(),us.end()); sort(vs.begin(),vs.end()); if(us!=vs) drop(-1); } using P = pair<Int, Int>; map<Int, Int> app; map<P, Int> idx; for(Int i=0;i+1<n;i++){ idx[P(xs[i],app[xs[i]])]=i; app[xs[i]]++; } app.clear(); vector<Int> ps(n-1); for(Int i=0;i+1<n;i++){ ps[i]=idx[P(ys[i],app[ys[i]])]; app[ys[i]]++; } cout<<calc(ps)<<newl; return 0; }