题意:给你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 ;
}

ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和的更多相关文章

  1. hdu 1199 Color the Ball(离散化线段树)

    Color the Ball Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  2. hdu 1199 Color the Ball 离散线段树

    C - Color the Ball Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  3. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  4. hdu 1556 Color the ball (技巧 || 线段树)

    Color the ballTime Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. HDU 1556 Color the ball(线段树:区间更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...

  6. hdoj 1556 Color the ball【线段树区间更新】

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  7. HDU 1199 - Color the Ball 离散化

    [题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后 ...

  8. ZOJ 2301 Color the Ball (离散化+线段树)

    题意:有从 1 开始递增依次编号的很多球,开始他们都是黑色的,现在依次给出 n 个操作(ai,bi,ci),每个操作都是把编号 ai 到 bi 区间内 的-所有球涂成 ci 表示的颜色(黑 or 白) ...

  9. hdu 1556 Color the ball (线段树做法)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...

随机推荐

  1. 跨平台的 SQL 客户端

    The major update to SQL client was to move to the .NET Core networking libraries instead of the nati ...

  2. Afinal

    1.注解功能 1)继承:FinalActivity ( 需要复制 afinal_0.5.1_bin.jar到lib下) 2)@ViewInject() public class AfinalActiv ...

  3. 为什么不推崇复杂的ORM

    上一篇文章写完,回复的人很多,有的说的很中肯,有的貌似只是看到文章的标题就进来写评论的!还有人问为什么我要屏蔽掉[反对]按钮,因为谁写文章都是为了分享,都在说出自己的心得体会.不过由于大家遇到的项目, ...

  4. 一个页面从输入 URL 到页面加载完的过程中都发生了什么事情?

    过程概述 浏览器查找域名对应的 IP 地址: 浏览器根据 IP 地址与服务器建立 socket 连接: 浏览器与服务器通信: 浏览器请求,服务器处理请求: 浏览器与服务器断开连接. 以下为详细解析: ...

  5. chenxi的html学习笔记

    0.本文主体源自:http://www.cnblogs.com/coco1s/p/4034937.html,有兴趣的可以直接去那里看,也可以看看我整理加拓展的.1.浏览器内核: 1.ie:triden ...

  6. Win7 64位下sql server链接oracle的方法

    继上一次mysql同步sql server后,这一次需要将Oracle同步到sql server上来,方案相似,只是在sql server链接oracle的时候费了很多时间. 一.测试环境 本方案实现 ...

  7. 扩展SharePoint链接字段

    默认SharePoint中的链接字段有很多限制,例如 输入文字的时候只能录入255个字符 链接显示的是文字 点击链接后只能在当前页面打开链接 - - - - - - -- - - - - - -   ...

  8. Windows环境下利用github快速配置git环境

    在windows环境下利用github客户端我们可以直接拥有可视化的界面来管理工程,当然你也可以选择你喜欢的命令行工具来做.今天我分享一个比较快速的方式来配置git环境. 先去下载github的win ...

  9. BI笔记-SSAS部署的几种方式及部署后的SSAS刷新

    SSAS的部署方式在哥本哈士奇的博客:BI笔记之--- SSAS部署的几种方式已经介绍了四种方式,在这里再介绍一种比较常用的快速部署方式. 环境约定:SQL Server 2008 R2 示例库:Ad ...

  10. Android软键盘与输入框的设置

    大家开发Android或者用app的时候会发现转到输入框就会自动弹出软键盘,切换别的页面就会自动的隐藏,下面几行代码用的熟练了就行了: 1.方法一(如果输入法在窗口上已经显示,则隐藏,反之则显示) I ...