結果

問題 No.1209 XOR Into You
ユーザー beetbeet
提出日時 2020-08-30 13:25:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,042 bytes
コンパイル時間 2,403 ms
コンパイル使用メモリ 222,684 KB
実行使用メモリ 21,156 KB
最終ジャッジ日時 2024-04-27 06:53:04
合計ジャッジ時間 12,499 ms
ジャッジサーバーID
(参考情報)
judge1 / 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 2 ms
6,944 KB
testcase_04 AC 42 ms
8,192 KB
testcase_05 WA -
testcase_06 AC 42 ms
8,192 KB
testcase_07 AC 254 ms
18,192 KB
testcase_08 AC 207 ms
16,784 KB
testcase_09 AC 204 ms
16,784 KB
testcase_10 AC 149 ms
14,324 KB
testcase_11 AC 219 ms
16,624 KB
testcase_12 AC 188 ms
15,696 KB
testcase_13 AC 218 ms
17,280 KB
testcase_14 AC 174 ms
15,376 KB
testcase_15 AC 187 ms
16,012 KB
testcase_16 AC 181 ms
15,712 KB
testcase_17 AC 176 ms
15,236 KB
testcase_18 AC 297 ms
20,364 KB
testcase_19 AC 178 ms
15,576 KB
testcase_20 AC 234 ms
17,552 KB
testcase_21 AC 147 ms
14,012 KB
testcase_22 AC 171 ms
21,148 KB
testcase_23 AC 169 ms
21,024 KB
testcase_24 AC 166 ms
21,156 KB
testcase_25 AC 295 ms
21,148 KB
testcase_26 AC 302 ms
21,156 KB
testcase_27 AC 299 ms
21,152 KB
testcase_28 AC 298 ms
21,152 KB
testcase_29 AC 304 ms
21,152 KB
testcase_30 AC 296 ms
21,148 KB
testcase_31 AC 145 ms
15,008 KB
testcase_32 AC 141 ms
15,008 KB
testcase_33 AC 143 ms
15,008 KB
testcase_34 AC 143 ms
15,004 KB
testcase_35 AC 143 ms
15,012 KB
testcase_36 AC 141 ms
15,008 KB
testcase_37 AC 78 ms
15,012 KB
testcase_38 AC 224 ms
18,172 KB
testcase_39 AC 192 ms
16,480 KB
testcase_40 AC 171 ms
21,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);

  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;
}
0