結果
問題 | No.1209 XOR Into You |
ユーザー |
![]() |
提出日時 | 2020-08-30 13:40:32 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 81 ms / 2,000 ms |
コード長 | 1,510 bytes |
コンパイル時間 | 4,658 ms |
コンパイル使用メモリ | 262,776 KB |
最終ジャッジ日時 | 2025-01-13 21:18:43 |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 37 |
ソースコード
#include <bits/extc++.h>#ifndef DUMP#define DUMP(...) void(0)#endifusing 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';}