結果

問題 No.1209 XOR Into You
ユーザー risujirohrisujiroh
提出日時 2020-08-30 13:40:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 1,510 bytes
コンパイル時間 3,612 ms
コンパイル使用メモリ 275,044 KB
実行使用メモリ 7,424 KB
最終ジャッジ日時 2024-04-27 07:02:34
合計ジャッジ時間 7,279 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 41 ms
6,944 KB
testcase_05 AC 41 ms
6,944 KB
testcase_06 AC 42 ms
6,940 KB
testcase_07 AC 58 ms
6,940 KB
testcase_08 AC 52 ms
6,940 KB
testcase_09 AC 52 ms
6,944 KB
testcase_10 AC 43 ms
6,944 KB
testcase_11 AC 51 ms
6,944 KB
testcase_12 AC 47 ms
6,940 KB
testcase_13 AC 55 ms
6,944 KB
testcase_14 AC 46 ms
6,944 KB
testcase_15 AC 49 ms
6,944 KB
testcase_16 AC 49 ms
6,940 KB
testcase_17 AC 45 ms
6,944 KB
testcase_18 AC 67 ms
7,168 KB
testcase_19 AC 49 ms
6,940 KB
testcase_20 AC 55 ms
6,944 KB
testcase_21 AC 42 ms
6,944 KB
testcase_22 AC 35 ms
7,296 KB
testcase_23 AC 36 ms
7,424 KB
testcase_24 AC 35 ms
7,296 KB
testcase_25 AC 69 ms
7,168 KB
testcase_26 AC 69 ms
7,296 KB
testcase_27 AC 69 ms
7,296 KB
testcase_28 AC 68 ms
7,296 KB
testcase_29 AC 69 ms
7,296 KB
testcase_30 AC 69 ms
7,296 KB
testcase_31 AC 61 ms
7,296 KB
testcase_32 AC 61 ms
7,296 KB
testcase_33 AC 61 ms
7,296 KB
testcase_34 AC 62 ms
7,168 KB
testcase_35 AC 61 ms
7,296 KB
testcase_36 AC 61 ms
7,296 KB
testcase_37 AC 38 ms
7,296 KB
testcase_38 AC 57 ms
6,940 KB
testcase_39 AC 50 ms
6,940 KB
testcase_40 AC 36 ms
7,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/extc++.h>

#ifndef DUMP
#define DUMP(...) void(0)
#endif

using namespace std;

template <class T>
struct fenwick {
  int n;
  vector<T> t;
  fenwick(int _n = 0) : n(_n), t(n) {}
  void add(int i, T a) {
    for (++i; i <= n; i += i & -i) t[i - 1] += a;
  }
  T sum(int i) const {
    T s = 0;
    for (; i; i -= i & -i) s += t[i - 1];
    return s;
  }
};

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  for (auto&& e : a) cin >> e;
  for (auto&& e : b) cin >> e;

  adjacent_difference(begin(a), end(a), begin(a), bit_xor<>());
  adjacent_difference(begin(b), end(b), begin(b), bit_xor<>());
  auto sa = a, sb = b;
  sort(begin(sa), end(sa));
  sort(begin(sb), end(sb));
  if (a[0] != b[0] or sa != sb) {
    cout << "-1\n";
    exit(0);
  }
  DUMP(a);
  DUMP(b);

  vector<int> ord_a(n), ord_b(n);
  iota(begin(ord_a), end(ord_a), 0);
  iota(begin(ord_b), end(ord_b), 0);
  stable_sort(begin(ord_a), end(ord_a),
              [&](int i, int j) { return a[i] < a[j]; });
  stable_sort(begin(ord_b), end(ord_b),
              [&](int i, int j) { return b[i] < b[j]; });
  vector<int> rnk_a(n), rnk_b(n);
  for (int k = 0; k < n; ++k) rnk_a[ord_a[k]] = k;
  vector<int> p(n);
  for (int i = 0; i < n; ++i) p[i] = ord_b[rnk_a[i]];

  int64_t res = 0;
  fenwick<int> ft(n);
  for (int i = n; i--;) {
    res += ft.sum(p[i]);
    ft.add(p[i], 1);
  }
  cout << res << '\n';
}
0