結果

問題 No.1209 XOR Into You
ユーザー snow39snow39
提出日時 2020-08-30 14:34:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 2,000 ms
コード長 3,202 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 115,668 KB
実行使用メモリ 20,964 KB
最終ジャッジ日時 2023-08-09 12:32:20
合計ジャッジ時間 9,587 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 26 ms
4,604 KB
testcase_05 AC 25 ms
4,552 KB
testcase_06 AC 42 ms
7,720 KB
testcase_07 AC 190 ms
18,412 KB
testcase_08 AC 179 ms
16,616 KB
testcase_09 AC 188 ms
16,696 KB
testcase_10 AC 134 ms
13,824 KB
testcase_11 AC 176 ms
16,660 KB
testcase_12 AC 165 ms
15,568 KB
testcase_13 AC 193 ms
17,188 KB
testcase_14 AC 159 ms
15,224 KB
testcase_15 AC 175 ms
16,076 KB
testcase_16 AC 157 ms
15,708 KB
testcase_17 AC 148 ms
15,220 KB
testcase_18 AC 243 ms
20,168 KB
testcase_19 AC 155 ms
15,900 KB
testcase_20 AC 194 ms
17,344 KB
testcase_21 AC 134 ms
14,024 KB
testcase_22 AC 139 ms
20,784 KB
testcase_23 AC 142 ms
20,912 KB
testcase_24 AC 141 ms
20,832 KB
testcase_25 AC 256 ms
20,772 KB
testcase_26 AC 261 ms
20,768 KB
testcase_27 AC 253 ms
20,848 KB
testcase_28 AC 245 ms
20,784 KB
testcase_29 AC 259 ms
20,748 KB
testcase_30 AC 254 ms
20,784 KB
testcase_31 AC 109 ms
9,924 KB
testcase_32 AC 109 ms
10,080 KB
testcase_33 AC 108 ms
10,120 KB
testcase_34 AC 111 ms
10,076 KB
testcase_35 AC 110 ms
10,092 KB
testcase_36 AC 108 ms
9,944 KB
testcase_37 AC 74 ms
9,680 KB
testcase_38 AC 195 ms
18,080 KB
testcase_39 AC 170 ms
16,492 KB
testcase_40 AC 145 ms
20,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

#include <functional>
#include <climits>

//SegmentTree<int> seg(N,[](int a,int b){return min(a,b);},INT_MAX);
template< typename T>
class SegmentTree{
public:

  using F = function<T(T,T)>;

  int n;
  vector<T> tree;
  F operation;
  T def;

  SegmentTree(int size,F _operation,T _def):operation(_operation),def(_def){
    n=1;
    while(n<size) n*=2;
    tree.resize(2*n-1,def);
  }

  SegmentTree(){}

  void embody(int size,F _operation,T _def){
    operation=_operation;
    def=_def;
    n=1;
    while(n<size) n*=2;
    tree.resize(2*n-1,def);
  }

  void initialize(vector<T> v){
    int vSize=v.size();
    n=1;
    while(n<vSize) n*=2;
    tree.resize(2*n-1,def);

    for(int i=0;i<vSize;i++) tree[i+n-1]=v[i];
    for(int i=n-2;i>=0;i--) tree[i]=operation(tree[2*i+1],tree[2*i+2]);
  }

  void update(int index,T value){
    index+=n-1;

    tree[index]=value;
    while(index>0){
      index=(index-1)/2;
      tree[index]=operation(tree[2*index+1],tree[2*index+2]);
    }
  }

  T query(int a,int b,int k=0,int l=0,int r=-1){//[a,b),getMin(a,b,0,0,-1)
    if(r<0) r=n;

    if(r<=a||b<=l) return def;
    else if(a<=l&&r<=b) return tree[k];
    else{
      T lval=query(a,b,2*k+1,l,(l+r)/2);
      T rval=query(a,b,2*k+2,(l+r)/2,r);
      return operation(lval,rval);
    }
  }

  int find(int x,int k=0,int l=0,int r=-1){// a[0]+...+a[i]>=x (i:minimal)
    if(r<0) r=n;
    if(r-l==1) return k-(n-1);
    if(tree[2*k+1]>=x) return find(x,2*k+1,l,(l+r)/2);
    else return find(x-tree[2*k+1],2*k+2,(l+r)/2,r);
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin>>N;
  vector<ll> A(N),B(N);
  rep(i,N) cin>>A[i];
  rep(i,N) cin>>B[i];

  if(A[0]!=B[0]||A.back()!=B.back()){
    cout<<-1<<endl;
    return 0;
  }

  vector<ll> C(N-1,0),D(N-1,0);
  rep(i,N-1) C[i]=A[i]^A[i+1];
  rep(i,N-1) D[i]=B[i]^B[i+1];

  {
    vector<ll> a=C,b=D;
    sort(all(a));
    sort(all(b));
    if(a!=b){
      cout<<-1<<endl;
      return 0;
    }
  }
  vector<ll> v=C;
  sort(all(v));
  v.erase(unique(v.begin(),v.end()),v.end());
  int V=v.size();
  map<ll,int> mp;
  rep(i,V) mp[v[i]]=i;

  vector<int> X(N-1,0);
  vector<vector<int>> idxs(V);
  rep(i,N-1) idxs[mp[C[i]]].push_back(i);
  int cnt=0;
  rep(i,V) for(auto n:idxs[i]) X[n]=cnt++;
  vector<int> Y(N-1,0);
  idxs.clear();
  idxs.resize(V);
  rep(i,N-1) idxs[mp[D[i]]].push_back(i);
  cnt=0;
  rep(i,V) for(auto n:idxs[i]) Y[n]=cnt++;

  vector<int> trans(N-1,0);
  rep(i,N-1) trans[X[i]]=i;
  rep(i,N-1) Y[i]=trans[Y[i]];

  SegmentTree<int> seg(N-1,[](int a,int b){return a+b;},0);
  ll ans=0;
  for(int i=0;i<N-1;i++){
    ll val=seg.query(Y[i],N-1);
    ans+=val;
    seg.update(Y[i],1);
  }
  cout<<ans<<endl;

  return 0;
}
0