ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和

时间:2023-03-09 15:47:05
ZOJ 2301 / HDU 1199  Color the Ball  离散化+线段树区间连续最大和

题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑。问最后最长的白色区间的起点和终点的位置。

解法:先离散化,为了防止离散后错误,不仅将L,R离散,还要加入L+1,L-1,R+1,R-1一起离散,这样就绝不会有问题了。然后建线段树,线段树维护四个值:

1.col  区间颜色  0 表示黑  1 表示白  -1表示无标记

2.maxi 区间内最大白区间的长度,由于白色用1表示,所以最大白区间的长度即为区间最大连续和

3.lmax 从区间左端开始的最大白区间长度

4.rmax 从区间右端开始的最大白区间长度

然后更新,查询,就跟普通求区间连续最大和无异了

代码:

#include <iostream>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <map>
using namespace std;
#define N 14007 struct node
{
int lmax,rmax,maxi,col;
}tree[*N];
int num[N],x[N];
int L[],R[];
char ss[][];
map<int,int> mp; void pushup(int l,int r,int rt)
{
tree[rt].lmax = tree[*rt].lmax;
tree[rt].rmax = tree[*rt+].rmax;
tree[rt].maxi = max(max(tree[*rt].maxi,tree[*rt+].maxi),tree[*rt].rmax+tree[*rt+].lmax);
int mid = (l+r)/;
int L = x[mid]-x[l-]; //真实的长度
int R = x[r]-x[mid];
if(tree[*rt].lmax == L)
tree[rt].lmax += tree[*rt+].lmax;
if(tree[*rt+].rmax == R)
tree[rt].rmax += tree[*rt].rmax;
} void pushdown(int l,int r,int rt)
{
if(tree[rt].col != -)
{
tree[*rt].col = tree[*rt+].col = tree[rt].col;
int mid = (l+r)/;
int L = x[mid]-x[l-];
int R = x[r]-x[mid];
tree[*rt].maxi = tree[*rt].lmax = tree[*rt].rmax = L*tree[rt].col;
tree[*rt+].maxi = tree[*rt+].lmax = tree[*rt+].rmax = R*tree[rt].col;
tree[rt].col = -;
}
}
void build(int l,int r,int rt)
{
tree[rt].maxi = tree[rt].lmax = tree[rt].rmax = ;
tree[rt].col = -;
if(l == r) return;
int mid = (l+r)/;
build(l,mid,*rt);
build(mid+,r,*rt+);
} void update(int l,int r,int aa,int bb,int col,int rt)
{
if(aa <= l && bb >= r)
{
tree[rt].col = col;
tree[rt].maxi = tree[rt].lmax = tree[rt].rmax = col*(x[r]-x[l-]);
return;
}
pushdown(l,r,rt);
int mid = (l+r)/;
if(aa <= mid)
update(l,mid,aa,bb,col,*rt);
if(bb > mid)
update(mid+,r,aa,bb,col,*rt+);
pushup(l,r,rt);
} int query(int l,int r,int rt)
{
if(l == r) return x[l];
int mid = (l+r)/;
pushdown(l,r,rt);
if(tree[*rt].maxi == tree[].maxi) //tree[1] not tree[rt]
return query(l,mid,*rt);
else if(tree[*rt].rmax+tree[*rt+].lmax == tree[].maxi)
return x[mid]-tree[*rt].rmax+;
else
return query(mid+,r,*rt+);
} int main()
{
int n,i,j,k;
while(scanf("%d",&n)!=EOF)
{
mp.clear();
for(i=;i<=n;i++)
{
scanf("%d%d%s",&L[i],&R[i],ss[i]);
num[*i-] = L[i]-;
num[*i-] = L[i];
num[*i-] = L[i]+;
num[*i-] = R[i]-;
num[*i-] = R[i];
num[*i] = R[i]+;
}
sort(num+,num+*n+);
int ind = unique(num+,num+*n+)-num-;
int now = ;
x[] = ;
for(i=;i<=ind;i++)
{
x[++now] = num[i];
mp[num[i]] = now;
}
build(,now,);
for(i=;i<=n;i++)
{
int ka = mp[L[i]];
int kb = mp[R[i]];
if(ss[i][] == 'w')
update(,now,ka,kb,,);
else
update(,now,ka,kb,,);
}
if(tree[].maxi <= )
puts("Oh, my god");
else
{
int left = query(,now,);
int right = left+tree[].maxi-;
printf("%d %d\n",left,right);
}
}
return ;
}