結果
問題 |
No.1209 XOR Into You
|
ユーザー |
![]() |
提出日時 | 2020-08-30 22:21:40 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 519 ms / 2,000 ms |
コード長 | 3,135 bytes |
コンパイル時間 | 3,182 ms |
コンパイル使用メモリ | 213,144 KB |
最終ジャッジ日時 | 2025-01-14 02:11:24 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 37 |
ソースコード
#include<bits/stdc++.h> using namespace std; #define ALL(x) x.begin(),x.end() #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; #define mod 1000000007 using ll=long long; const int INF=1000000000; const ll LINF=1001002003004005006ll; int dx[]={1,0,-1,0},dy[]={0,1,0,-1}; // ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;} template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;} struct IOSetup{ IOSetup(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(12); } } iosetup; template<typename T1,typename T2> ostream &operator<<(ostream &os,const pair<T1,T2>&p){ os<<p.first<<" "<<p.second; return os; } template<typename T> ostream &operator<<(ostream &os,const vector<T>&v){ for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" "); return os; } template<typename T1,typename T2> istream &operator>>(istream &is,pair<T1,T2>&p){ is>>p.first>>p.second; return is; } template<typename T> istream &operator>>(istream &is,vector<T>&v){ for(T &x:v)is>>x; return is; } template<typename T> struct BinaryIndexedTree{ vector<T> data; BinaryIndexedTree(int sz){ data.assign(sz+1, 0); } // 区間[0,k]の合計 sum(v[0]~v[k]) T sum(int k){ T ret=0; for(++k;k>0;k-=(k&-k)) ret+=data[k]; return ret; } // v[a]+=x void add(int k,T x){ for(++k;k<data.size();k+=(k&-k)) data[k]+=x; } // wを超える最小のv[i]のiを返す(0indexedのを返す)(data上のindexを返したいならreturn x+1) int lower_bound(T w){//T? if(w<=0) return 0; //左右どっちに行くか考える int x=0,r=1; while(r<data.size()) r<<=1; for(int k=r;k>0;k>>=1){ if(x+k<=data.size()&&data[x+k]<w){ //左部分を引いて右へ,再起っぽい w-=data[x+k]; x+=k; } } // return x+1; return x; } }; //転倒数を求める ll inversion_number(vector<int> v){ int n=v.size(); if(n<=1){ return 0; } int vmax=*max_element(ALL(v)); BinaryIndexedTree<ll> bit(vmax+1); ll ret=0; rep(i,n){ ret+=bit.sum(vmax)-bit.sum(v[i]); bit.add(v[i],1); } return ret; } signed main(){ int n;cin>>n; vector<ll> a(n),b(n); cin>>a>>b; if(a[0]!=b[0] or a.back()!=b.back()){ cout<<-1<<endl; return 0; } vector<ll> u(n-1),v(n-1); rep(i,n-1){ u[i]=(a[i]^a[i+1]); v[i]=(b[i]^b[i+1]); } n--; map<ll,int> uc,vc; rep(i,n)uc[u[i]]++; rep(i,n)vc[v[i]]++; for(auto p:uc){ if(p.second!=vc[p.first]){ cout<<-1<<endl; return 0; } } map<ll,queue<int>> id; rep(i,n)id[u[i]].push(i); vector<int> is(n); rep(i,n){ is[i]=id[v[i]].front(); id[v[i]].pop(); } cout<<inversion_number(is)<<endl; return 0; }