結果
| 問題 |
No.1209 XOR Into You
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-20 20:59:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 146 ms / 2,000 ms |
| コード長 | 1,566 bytes |
| コンパイル時間 | 758 ms |
| コンパイル使用メモリ | 76,928 KB |
| 実行使用メモリ | 7,888 KB |
| 最終ジャッジ日時 | 2025-03-20 21:00:31 |
| 合計ジャッジ時間 | 6,836 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 37 |
ソースコード
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5 + 5;
int n;
long long a[N] = {0}, b[N] = {0};
int m;
int t[N] = {0};
int lowbit(int x) {
return x & -x;
}
void mdf(int x, int v) {
while (x <= n)
t[x] += v, x += lowbit(x);
}
int qry(int x) {
int ans = 0;
while (x > 0)
ans += t[x], x -= lowbit(x);
return ans;
}
long long ta[N] = {0}, tb[N] = {0};
int cnt[N] = {0};
int pos[N] = {0};
bool init() {
sort(ta + 1, ta + n + 1);
sort(tb + 1, tb + n + 1);
for (int i = 1; i <= n; i++)
if (ta[i] != tb[i])
return false;
for (int i = 1; i <= n; i++) {
int x = lower_bound(ta + 1, ta + n + 1, a[i]) - ta;
a[i] = x + cnt[x]++;
// cout << a[i] << " " << x << " " << cnt[x] << endl;
pos[a[i]] = i;
}
/// cout << endl;
for (int i = 1; i <= n; i++)
cnt[i] = 0;
for (int i = 1; i <= n; i++) {
int x = lower_bound(ta + 1, ta + n + 1, b[i]) - ta;
b[i] = x + cnt[x]++;
// cout << b[i] << " ";
b[i] = pos[b[i]];
}
// cout << endl;
return true;
}
int main() {
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
if (a[1] != b[1]) {
printf("-1\n");
return 0;
}
for (int i = 1; i < n; i++)
a[i] ^= a[i + 1], b[i] ^= b[i + 1], swap(a[i], b[i]);
n--;
for (int i = 1; i <= n; i++)
ta[i] = a[i], tb[i] = b[i];
if (!init()) {
printf("-1\n");
return 0;
}
long long ans = 0ll;
for (int i = 1; i <= n; i++) {
ans += (i - 1) - qry(b[i] - 1);
mdf(b[i], 1);
}
cout << ans << endl;
return 0;
}
lam6er