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 ;
}
ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和的更多相关文章
- hdu 1199 Color the Ball(离散化线段树)
Color the Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1199 Color the Ball 离散线段树
C - Color the Ball Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556 Color the ball (技巧 || 线段树)
Color the ballTime Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1556 Color the ball(线段树:区间更新)
http://acm.hdu.edu.cn/showproblem.php?pid=1556 题意: N个气球,每次[a,b]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...
- hdoj 1556 Color the ball【线段树区间更新】
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1199 - Color the Ball 离散化
[题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后 ...
- ZOJ 2301 Color the Ball (离散化+线段树)
题意:有从 1 开始递增依次编号的很多球,开始他们都是黑色的,现在依次给出 n 个操作(ai,bi,ci),每个操作都是把编号 ai 到 bi 区间内 的-所有球涂成 ci 表示的颜色(黑 or 白) ...
- hdu 1556 Color the ball (线段树做法)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...
随机推荐
- Oracle锁表与解锁
查看锁表语句:方法1: select sess.sid, sess.serial#, lo.oracle_username, lo.o ...
- ASP.NET WebAPI 09 Controller的激活
在Controller之前我们先回顾一下HttpMessageHandler通道. 在这个图中我留一个HttpContollerDispatcher没有说明.因为这个类也是继承自HttpMessage ...
- spring mvc绑定复杂对象报错“Could not instantiate property type [com.ld.net.spider.pojo.WorkNode] to auto-grow nested property path: java.lang.InstantiationException: com.ld.net.spider.pojo.WorkNode”
解决方法之一: 1.确保所有的Pojo都包含了默认的构造器:
- 最熟悉的陌生人-------MVC
以前开发iOS程序的时候用的最多的是MVC的设计模式,这种软件架构的模式是由:模型(Model)[屏幕中展示的].视图(View)[如何展示的]和控制器(Controller)[程序的数据 ...
- jQuery waterbubble 水球图
在线实例 默认效果 显示文本 水球半径 文本颜色 边框宽度 设置字体 数据多少 是否显示波纹 水球颜色 是否显示动画 使用方法 <div class="wrap"> & ...
- FME2014汉化问题
问题:FME2014汉化包安装上不起作用,安装环境是是Win7 64位,FME是64位版本的,默认位置在Program Files\FME下,而汉化包安装默认位置在Program Files (x86 ...
- Xcode证书路径和缓存清理路径
1.Xcode证书路径: ~/Library/MobileDevice/Provisioning Profiles 2.Jenkines共享证书路径: /用户/共享/Jenkins/Library/M ...
- ReactiveCocoa入门教程:第二部分
翻译自:http://www.raywenderlich.com/62796/reactivecocoa-tutorial-pt2 原文链接: ReactiveCocoa 是一个框架,它允许你在你的i ...
- xib命名注意事项--防止被其他控制器意外地 当做默认的 view了
注意: 1.创建的xib如果不是想给指定的控制器做view的话,命名就要注意了! 2.最好是不要命名和控制器名字相关的xib. 如下举例说明一下: - (void)touchesBegan:(NSSe ...
- C语言-10-位域与共用体
位域 在某种特定情况下,一个结构体中的多个变量只使用各自存储空间的几位,而其他位从来不使用.这种情况下,可以使用位域来限定每个变量的用来存储数据的位宽. 作用 限定结构体中变量用来存放数据的位宽,即使 ...