結果
問題 | No.1209 XOR Into You |
ユーザー |
![]() |
提出日時 | 2020-10-08 10:11:51 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 105 ms / 2,000 ms |
コード長 | 1,300 bytes |
コンパイル時間 | 465 ms |
コンパイル使用メモリ | 53,504 KB |
実行使用メモリ | 9,984 KB |
最終ジャッジ日時 | 2024-07-20 05:48:33 |
合計ジャッジ時間 | 5,775 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 37 |
ソースコード
#include <stdio.h> #include <utility> #include <string.h> #include <set> using namespace std; #define F first #define S second typedef long long int ll; constexpr int kN = int(1E5 + 10); int ABS(int n) {return n >= 0 ? n : -n;} struct BIT { int val[kN]; void init() {memset(val, 0, sizeof(val));} void add(int pos) { while (pos < kN) { val[pos]++; pos += pos & -pos; } return ; } int ask(int pos) { int ans = 0; while (pos) { ans += val[pos]; pos ^= pos & -pos; } return ans; } }; int a[kN], b[kN], difa[kN], difb[kN]; set<pair<int, int>> se; BIT bit; int main() { int n; ll ans = 0; set<pair<int, int>>::iterator u; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); if (a[1] != b[1] || a[n] != b[n]) { printf("-1\n"); return 0; } for (int i = 2; i <= n; i++) difa[i] = a[i] ^ a[i - 1]; for (int i = 2; i <= n; i++) difb[i] = b[i] ^ b[i - 1]; for (int i = 2; i <= n; i++) se.insert({difa[i], i}); bit.init(); for (int i = 2; i <= n; i++) { u = se.lower_bound({difb[i], 0}); if (u == se.end() || u -> F != difb[i]) { printf("-1\n"); return 0; } else { ans += ABS(u -> S - bit.ask(u -> S) - 2); bit.add(u -> S); se.erase(u); } } printf("%lld\n", ans); }