結果

問題 No.1209 XOR Into You
ユーザー theory_and_metheory_and_me
提出日時 2020-09-04 02:25:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 127 ms / 2,000 ms
コード長 4,744 bytes
コンパイル時間 2,413 ms
コンパイル使用メモリ 211,088 KB
実行使用メモリ 17,900 KB
最終ジャッジ日時 2023-08-16 02:56:11
合計ジャッジ時間 10,485 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 25 ms
4,480 KB
testcase_05 AC 25 ms
4,488 KB
testcase_06 AC 75 ms
17,724 KB
testcase_07 AC 98 ms
15,404 KB
testcase_08 AC 87 ms
14,120 KB
testcase_09 AC 84 ms
14,076 KB
testcase_10 AC 70 ms
12,228 KB
testcase_11 AC 87 ms
14,076 KB
testcase_12 AC 71 ms
13,232 KB
testcase_13 AC 95 ms
14,656 KB
testcase_14 AC 75 ms
12,976 KB
testcase_15 AC 77 ms
13,492 KB
testcase_16 AC 79 ms
13,288 KB
testcase_17 AC 73 ms
12,864 KB
testcase_18 AC 127 ms
17,204 KB
testcase_19 AC 81 ms
13,188 KB
testcase_20 AC 87 ms
14,520 KB
testcase_21 AC 67 ms
11,904 KB
testcase_22 AC 71 ms
17,688 KB
testcase_23 AC 69 ms
17,684 KB
testcase_24 AC 71 ms
17,776 KB
testcase_25 AC 114 ms
17,688 KB
testcase_26 AC 119 ms
17,852 KB
testcase_27 AC 114 ms
17,692 KB
testcase_28 AC 118 ms
17,772 KB
testcase_29 AC 114 ms
17,724 KB
testcase_30 AC 119 ms
17,900 KB
testcase_31 AC 46 ms
7,832 KB
testcase_32 AC 45 ms
7,744 KB
testcase_33 AC 45 ms
7,808 KB
testcase_34 AC 49 ms
7,696 KB
testcase_35 AC 47 ms
7,820 KB
testcase_36 AC 46 ms
7,640 KB
testcase_37 AC 30 ms
7,716 KB
testcase_38 AC 91 ms
15,316 KB
testcase_39 AC 79 ms
13,716 KB
testcase_40 AC 67 ms
17,720 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "line " << __LINE__ << ": "<<  #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long, long>;
using pdd = pair<ld, ld>;

template<typename T> struct V : vector<T> { using vector<T>::vector; };
V() -> V<ll>;
V(size_t) -> V<ll>;
template<typename T> V(size_t, T) -> V<T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<endl;}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<endl;};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

template< typename T >
struct BIT {
  vector< T > data;

  BIT(int sz) {
    data.assign(++sz, 0);
  }

  // 閉区間 [0, k] の和を求める
  // k は(指定したsz)-1 までしか値を取れない
  T sum(int k) {
    T ret = 0;
    for(++k; k > 0; k -= k & -k) ret += data[k];
    return (ret);
  }

  // 閉区間 [k, l] の和を求める
  T segment_sum(int k, int l){
      T ret = 0;
      ret += sum(l);
      ret -= sum(k-1);
      return (ret);
  }

  // 要素 k に x を加える
  void add(int k, T x) {
    for(++k; k < (int)data.size(); k += k & -k) data[k] += x;
  }

  // 要素 k を x の変更
  void update(int k, T x){
      T tmp = segment_sum(k, k);
      add(k, x-tmp);
  }

  // 二分探索 [0, k] の和が w 以上になるような最小の k を求める
  // 値が全て0以上じゃないと無理
  // 存在しなければ sz-1 を返す
  // w<=0 のときは -1 を返す
  int lower_bound(T w){
      if(w<=0) return -1;

      int sz = data.size();
      int x = 0, r = 1;
      while(r < sz-1) r = (r << 1);
      for(int len = r; len > 0; len = (len >> 1)){
          if(x + len <= sz-1 and data[x + len] < w){
              w -= data[x + len];
              x += len;
          }
      }
      return x;
  }

  // 全ての要素をprint
  void print(){
      cout << "Line" <<  __LINE__  << ": BIT = ";
      for(int i=0;i<(int)data.size()-1;i++) cout << segment_sum(i, i) << " ";
      cout << "\n";
  }
};

template<class T>
ll inversion_count_between(vector<T> &x, vector<T> &y){
    // x を y にするために必要な最小 swap 回数(転倒数)を計算
    // 同じ値が含まれても良い(最小のswap回数になる)
    // 座標圧縮も勝手にやってくれる
    // x を y にできない場合 -1 を返す
    // ちょっと遅い

    int sz = x.size();

    assert(sz = (int)y.size());

    BIT<ll> bit(sz);

    map<T, vector<int>> loc;
    for(int i=sz-1;i>=0;i--){
        loc[x[i]].push_back(i);
    }

    ll res = 0;
    for(int i=0;i<sz;i++){
        vector<int> &v = loc[y[i]];
        if(v.empty()) return -1;
        int id = v.back(); v.pop_back();
        res += id - bit.sum(id-1);
        bit.add(id, 1);
    }

    return res;
}

int main(){

    ll N;
    cin >> N;
    V<ll> A(N), B(N);
    REP(i, N) cin >> A[i];
    REP(i, N) cin >> B[i];

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

    V<ll> a(N-1), b(N-1);
    REP(i, N-1) a[i] = A[i]^A[i+1];
    REP(i, N-1) b[i] = B[i]^B[i+1];

    cout << inversion_count_between(a, b) << endl;    

    return 0;
}
0