結果

問題 No.1209 XOR Into You
ユーザー ianCK
提出日時 2020-09-23 17:39:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 1,300 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 44,288 KB
実行使用メモリ 9,856 KB
最終ジャッジ日時 2024-06-27 22:50:40
合計ジャッジ時間 4,829 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:44:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   44 |         scanf("%d", &n);
      |         ~~~~~^~~~~~~~~~
main.cpp:45:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |         for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
      |                                      ~~~~~^~~~~~~~~~~~~
main.cpp:46:43: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   46 |         for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
      |                                      ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

#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);
}
0