Submission #1248516

#TimeUsernameProblemLanguageResultExecution timeMemory
1248516kaiboyBoard (CEOI13_board)C++20
0 / 100
45 ms1132 KiB
#include <algorithm>
#include <iostream>
#include <cstring>

using namespace std;

const int   N = 100000;
const int   M = 100000;
const int  N_ = 1 << 17;
const int INF = 0x3f3f3f3f;

char aa[N + 1], bb[M + 1];
bool st0[N_ << 1], st1[N_ << 1], lz[N_];
int h_, n_;

void put(int i) {
	swap(st0[i], st1[i]);
	if (i < n_)
		lz[i] = !lz[i];
}

void pus(int i) {
	if (lz[i]) {
		int l = i << 1, r = l ^ 1;
		put(l), put(r), lz[i] = false;
	}
}

void pul(int i) {
	if (!lz[i]) {
		int l = i << 1, r = l ^ 1;
		st0[i] = st0[l] || st0[r];
		st1[i] = st1[l] || st1[r];
	}
}

void push(int i) {
	for (int h = h_; h; h--)
		pus(i >> h);
}

void pull(int i) {
	while (i >>= 1)
		pul(i);
}

void build(int n) {
	h_ = 0, n_ = 1;
	while (n_ <= n)
		h_++, n_ <<= 1;
	for (int i = 0; i < n_; i++)
		st0[i + n_] = true, st1[i + n_] = lz[i] = false;
	for (int i = n_ - 1; i; i--)
		pul(i);
}

void update(int l, int r) {
	int l_ = l += n_, r_ = r += n_;
	push(l_), push(r_);
	for ( ; l <= r; l >>= 1, r >>= 1) {
		if (l & 1)
			put(l++);
		if (!(r & 1))
			put(r--);
	}
	pull(l_), pull(r_);
}

int search0(int r) {
	int l = 0;
	push(l += n_), push(r += n_);
	for ( ; l <= r; l >>= 1, r >>= 1)
		if (!(r & 1)) {
			if (st0[r]) {
				while (r < n_) {
					r <<= 1;
					if (st0[r ^ 1])
						r ^= 1;
				}
				return r - n_;
			}
			r--;
		}
	return -1;
}

int search1(int r) {
	int l = 0;
	push(l += n_), push(r += n_);
	for ( ; l <= r; l >>= 1, r >>= 1)
		if (!(r & 1)) {
			if (st1[r]) {
				while (r < n_) {
					r <<= 1;
					if (st1[r ^ 1])
						r ^= 1;
				}
				return r - n_;
			}
			r--;
		}
	return -1;
}

int query(int i) {
	push(i += n_);
	return st1[i];
}

int transform(char *aa, int n) {
	int m = 0;
	build(n);
	for (int i = 0; i < n; i++) {
		char a = aa[i];
		if (a == '1')
			m++;
		else if (a == '2')
			update(m, m), m++;
		else if (a == 'U') {
			if (query(m - 1))
				update(m - 1, m - 1);
			m--;
		} else if (a == 'L')
			update(search1(m - 1), m - 1);
		else
			update(search0(m - 1), m - 1);
	}
	for (int h = 0; h < m; h++)
		aa[h] = query(h);
	return m;
}

int main() {
	cin >> aa >> bb;
	int n = strlen(aa), m = strlen(bb);
	n = transform(aa, n);
	m = transform(bb, m);
	if (n > m)
		swap(n, m), swap(aa, bb);
	int ans = INF, i_ = 0;
	while (i_ < n && aa[i_] == bb[i_])
		i_++;
	for (int d = 0, i = i_; i <= n && i < i_ + 19; i++) {
		ans = min(ans, n - i + abs(d) + m - i);
		if (i < n)
			d = d * 2 + aa[i] - bb[i];
	}
	cout << ans << '\n';
	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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...