ural1019 Find the longest all-white line segment

【Title description】

        First, white is drawn between 0 and 10^9 (10 to the 9th power) on the number line. Then, some parts of this interval are painted black again. Then some parts are painted white again, and so on. Please find the longest white interval after M (1 <= M <= 5000) recolorings.

【Input format】

        The first line is N, and the following N lines are recoloring information. The format of each line is as follows: ai bi ci where ai and bi are integers, and ci is the character 'b' or 'w', separated by spaces. Description of these three parameters: from ai to bi, color ci, ('w' means white, 'b' means black), it can be considered that 0 < ai <= bi < 10^9 

【Output format】

        The output x,y (x < y) is separated by a space, representing the longest white interval. If there are multiple answers, output the one with the smallest x. 

Sample Input
4
1 999999997 b
40 300 w
300 634 w

43 47 b


Sample Output

47 634

Solution

Very classic discretized segment tree. List a few pits:

①To build a tree with segments, pay attention to the opening and closing of the interval

②Don't forget the two endpoints 0 and 1000000000 

③ The maximum length is defined by the length before discretization

Code

#include <cstdio>
#include <algorithm>
#include <map>
#define N 5010
#define M 10010
using namespace std;
map<int, int>mp;
struct query {int l, r, c;}q[N];
int n, y[N<<1], m;
struct seg {int l, r, lazy, mx, ls, rs;}t[M<<2];
inline void update(int p) {
	if(t[p].l == t[p].r) return ;
	if(t[p<<1].lazy == t[p<<1|1].lazy) t[p].lazy = t[p<<1].lazy;
	t[p].mx = max(t[p<<1].mx, max(t[p<<1|1].mx, t[p<<1].rs + t[p<<1|1].ls));
	t[p].ls = (t[p<<1].lazy == 1)?(t[p<<1].ls + t[p<<1|1].ls):t[p<<1].ls;
	t[p].rs = (t[p<<1|1].lazy == 1)?(t[p<<1].rs + t[p<<1|1].rs):t[p<<1|1].rs;
}
inline void pushdown(int p) {
	if(t[p].l == t[p].r) return ;
	if(t[p].lazy) {
		t[p<<1].lazy = t[p<<1|1].lazy = t[p].lazy;
		if(t[p].lazy == 1) {
			t[p<<1].ls = t[p<<1].mx = t[p<<1].rs = y[t[p<<1].r + 1] - y[t[p<<1].l];
			t[p<<1|1].ls = t[p<<1|1].mx = t[p<<1|1].rs = y[t[p<<1|1].r + 1] - y[t[p<<1|1].l];
		}else {
			t[p<<1].ls = t[p<<1].mx = t[p<<1].rs = 0;
			t[p<<1|1].ls = t[p<<1|1].mx = t[p<<1|1].rs = 0;
		}
		t[p].lazy = 0;
	}
}
void build(int p, int l, int r) {
	t[p].l = l; t[p].r = r;
	t[p].lazy = 1; t[p].mx = t[p].ls = t[p].rs = y[r + 1] - y[l];
	if(l == r) return ;
	int mid = (l + r)>>1;
	build(p<<1, l, mid); build(p<<1|1, mid + 1, r);
}
void ins(int p, int l, int r, int color) {
	if(l > r) return ;
	if(l <= t[p].l && t[p].r <= r) {
		t[p].lazy = color;
		if(color == 1) {t[p].mx = t[p].ls = t[p].rs = y[t[p].r + 1] - y[t[p].l];}
		else {t[p].mx = t[p].ls = t[p].rs = 0;}
		return ;
	}
	int mid = (t[p].l + t[p].r)>>1;
	pushdown(p);
	if(l <= mid) ins(p<<1, l, r, color);
	if(mid +1 <= r) ins(p<<1|1, l, r, color);
	update(p);
}
int final[M], cor[M], Q[M];
void dfs(int p) {
	if(t[p].l == t[p].r) {
		final[t[p].l] = (t[p].lazy == 1)?(y[t[p].l + 1] - y[t[p].l]):0;
		cor[t[p].l] = (t[p].lazy == 1)?1:0;
		return ;
	}
	pushdown(p);
	dfs(p<<1); dfs(p<<1|1);
}
int main() {
	scanf("%d", &n); char opt[5];
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d%s", &q[i].l, &q[i].r, opt);
		if(opt[0] == 'w') q[i].c = 1; else q[i].c = 2;
		y[(i<<1) - 1] = q[i].l; y[i<<1] = q[i].r;
	}
	y[n<<1|1] = 0; y[(n<<1|1) + 1] = 1000000000;
	sort(y+1, y+2*n+3);
	m = unique(y+1, y+(n<<1|1)+2) - y - 1;
	for(int i = 1; i <= m; ++i) mp[y[i]] = i;
	for(int i = 1; i <= n; ++i) {q[i].l = mp[q[i].l]; q[i].r = mp[q[i].r];}
	build(1, 1, m - 1);
	for(int i = 1; i <= n; ++i) ins(1, q[i].l, q[i].r - 1, q[i].c);
	dfs(1);
	for(int i = 2; i <= m - 1; ++i) final[i]+= final[i - 1];
	int cont = 0;
	for(int i = 1; i <= m - 1; ++i) if(!cor[i]) Q[++cont] = i;
	Q[0] = 0; Q[++cont] = m;
	for(int i = 1; i <= cont; ++i) {
		int last = Q[i - 1] + 1, now = Q[i] - 1;
		if(last > now) continue;
		if(final[now] - final[last - 1] == t[1].mx) {
			printf("%d %d", y[last], y[now + 1]);
			break;
		}
	}
	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325835159&siteId=291194637