Codeforces 161C(分治、性质)

要点

  • 因为当前最大字符只有一个且两边是回文的,所以如果答案包含最大字符则一定是重合部分。
  • 若不包含,则用此字符将两个区间分别断为两部分,则共有四种组合,答案一定为其中之一。
#include <cstdio>
#include <algorithm>
using namespace std;

int l1, l2, r1, r2;

int Divide(int a, int b, int c, int d, int depth) {
	if (a > b || c > d)	return 0;
	int res = max(0, min(b, d) - max(a, c) + 1);
	if ((a <= c && b >= d) || (c <= a && d >= b))	return res;

	int mid = 1 << depth;
	int x[2], y[2], s[2], t[2];
	x[0] = min(a, mid), y[0] = min(b, mid - 1);
	x[1] = max(a, mid + 1) - mid, y[1] = max(b, mid) - mid;
	s[0] = min(c, mid), t[0] = min(d, mid - 1);
	s[1] = max(c, mid + 1) - mid, t[1] = max(d, mid) - mid;

	res = max(res, Divide(x[0], y[0], s[0], t[0], depth - 1));//ll
	res = max(res, Divide(x[0], y[0], s[1], t[1], depth - 1));//lr
	res = max(res, Divide(x[1], y[1], s[0], t[0], depth - 1));//rl
	res = max(res, Divide(x[1], y[1], s[1], t[1], depth - 1));//rr
	return res;
}

int main() {
	scanf("%d %d %d %d", &l1, &r1, &l2, &r2);
	return !printf("%d\n", Divide(l1, r1, l2, r2, 30));
}
posted @ 2019-05-12 22:18  AlphaWA  阅读(264)  评论(0编辑  收藏  举报