結果

問題 No.1209 XOR Into You
ユーザー 👑 jupirojupiro
提出日時 2020-10-20 09:42:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 2,840 bytes
コンパイル時間 1,618 ms
コンパイル使用メモリ 146,088 KB
実行使用メモリ 16,436 KB
最終ジャッジ日時 2023-09-28 13:36:33
合計ジャッジ時間 10,797 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 24 ms
4,384 KB
testcase_05 AC 24 ms
4,380 KB
testcase_06 AC 41 ms
5,420 KB
testcase_07 AC 146 ms
14,192 KB
testcase_08 AC 122 ms
12,992 KB
testcase_09 AC 124 ms
12,984 KB
testcase_10 AC 88 ms
11,356 KB
testcase_11 AC 116 ms
12,712 KB
testcase_12 AC 110 ms
12,212 KB
testcase_13 AC 129 ms
13,460 KB
testcase_14 AC 102 ms
11,964 KB
testcase_15 AC 116 ms
12,556 KB
testcase_16 AC 123 ms
12,144 KB
testcase_17 AC 104 ms
11,864 KB
testcase_18 AC 184 ms
15,768 KB
testcase_19 AC 108 ms
12,120 KB
testcase_20 AC 133 ms
13,604 KB
testcase_21 AC 95 ms
11,004 KB
testcase_22 AC 94 ms
16,300 KB
testcase_23 AC 92 ms
16,436 KB
testcase_24 AC 92 ms
16,304 KB
testcase_25 AC 184 ms
16,376 KB
testcase_26 AC 174 ms
16,372 KB
testcase_27 AC 177 ms
16,420 KB
testcase_28 AC 168 ms
16,300 KB
testcase_29 AC 172 ms
16,300 KB
testcase_30 AC 176 ms
16,424 KB
testcase_31 AC 65 ms
6,024 KB
testcase_32 AC 64 ms
6,080 KB
testcase_33 AC 65 ms
6,072 KB
testcase_34 AC 66 ms
6,004 KB
testcase_35 AC 64 ms
6,124 KB
testcase_36 AC 64 ms
6,092 KB
testcase_37 AC 34 ms
6,344 KB
testcase_38 AC 133 ms
14,168 KB
testcase_39 AC 119 ms
12,692 KB
testcase_40 AC 92 ms
16,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
//mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr int MAX = 1100000;

template<typename T>
struct BIT {
	int n;
	vector<T> bit;
	BIT() :n(0) {}
	BIT(int _n) :n(_n) { bit = vector<T>(n + 1); }
	void add1(int idx, T val) {
		for (int i = idx; i <= n; i += i & -i) bit[i] += val;
	}
	T sum1(int idx) {
		T res = 0;
		for (int i = idx; i > 0; i -= i & -i) res += bit[i];
		return res;
	}
	//0-indexed
	void add(int idx, T val) { add1(idx + 1, val); }
	//0-indexed [left, right)
	T sum(int left, int right) { return sum1(right) - sum1(left); }

	int lower_bound(T x) {
		int res = 0;
		int k = 1;
		while (2 * k <= n) k <<= 1;
		for (; k > 0; k >>= 1) {
			if (res + k <= n and bit[res + k] < x) {
				x -= bit[res + k];
				res += k;
			}
		}
		return res;
	}
};

void fail() {
	cout << -1 << '\n';
	exit(0);
}

bool check(vector<int> a, vector<int> b) {
	sort(a.begin(), a.end());
	sort(b.begin(), b.end());
	return a == b;
}
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	int n; cin >> n;
	vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
	vector<int> b(n); for (int i = 0; i < n; i++) cin >> b[i];
	if (a[0] != b[0] or a.back() != b.back()) fail();
	vector<int> c(n - 1); for (int i = 0; i + 1 < n; i++) c[i] = a[i] ^ a[i + 1];
	vector<int> d(n - 1); for (int i = 0; i + 1 < n; i++) d[i] = b[i] ^ b[i + 1];
	if (!check(c, d)) fail();
	map<int, vector<int>> mp;
	for (int i = 0; i < d.size(); i++) {
		mp[d[i]].emplace_back(i);
	}
	for (auto& p : mp) {
		reverse(p.second.begin(), p.second.end());
	}
	for (int i = 0; i < c.size(); i++) {
		int t = mp[c[i]].back();
		mp[c[i]].pop_back();
		c[i] = t;
	}
	BIT<ll> bit(n);
	ll res = 0;
	for (int i = 0; i < c.size(); i++) {
		res += bit.sum(c[i], n);
		bit.add(c[i], 1);
	}
	cout << res << endl;
}
0