제출 #1349398

#제출 시각아이디문제언어결과실행 시간메모리
1349398TobČVENK (COI15_cvenk)C++20
100 / 100
32 ms1212 KiB
#include <bits/stdc++.h>

#define F first
#define S second
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <ll> vl;

const int N = 1e5 + 7;

int n;
int a[N], b[N];

inline int lg(int x) {
	return !x ? 0 : 32-__builtin_clz(x);
}
inline int f(int x, int y) {
	return (x|y)&~((1<<min(lg(x),lg(y)))-1);
}

int main () {
	ios_base::sync_with_stdio(false); cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
	ll res = 0;
	for (int j = 29; j >= 0; j--) {
		int c1 = 0, c2 = 0;
		for (int i = 0; i < n; i++) {
			if ((a[i]&(1<<j)) && !(b[i]&(1<<j))) c1++;
			if (!(a[i]&(1<<j)) && (b[i]&(1<<j))) c2++;
		}
		if (c1 < c2) {
			for (int i = 0; i < n; i++) swap(a[i], b[i]);
			swap(c1, c2);
		}
		if (c1 > n/2) {
			res += c2*(1LL<<j);
			for (int i = 0; i < n; i++) {
				ll sum = a[i]+b[i];
				if (!(a[i]&(1<<j)) && (b[i]&(1<<j))) res += sum;
				if (!(a[i]&(1<<j)) && !(b[i]&(1<<j))) {
					if (a[i] < b[i]) res += sum+(1LL<<j);
					else res += sum+(1LL<<j)-2*f(a[i], b[i]);
				}
				if (!(a[i]&(1<<j))) a[i] = (1 << j), b[i] = 0;
			}
			for (int i = 0; i < n; i++) a[i] ^= (1 << j);
		}
		else {
			for (int i = 0; i < n; i++) {
				if ((a[i]&(1<<j)) || (b[i]&(1<<j))) {
					res += a[i]+b[i]-(1LL<<j)+1;
					if (a[i]&(1<<j)) a[i] = (1<<j)-1, b[i] = 0;
					else b[i] = (1<<j)-1, a[i] = 0;
				}
			}
		}
	}
	cout << res;

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...