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 ...
随机推荐
- jquery对javascript事件的封装一览
描述 jquery javascript 鼠标点击某个对象 click() onclick 鼠标双击某个对象 dblclick() ondblclick 元素获得焦点 focus() onfocus ...
- 地理数据可视化:Simple,Not Easy
如果要给2015年的地理信息行业打一个标签,地理大数据一定是其中之一.在信息技术飞速发展的今天,“大数据”作为一种潮流铺天盖地的席卷了各行各业,从央视的春运迁徙图到旅游热点预测,从大数据工程师奇货可居 ...
- Android WebView 获取网页的标题
final TextView txtTitle = (TextView) findViewById(R.id.txtTitle); final WebView webView = (WebView)f ...
- 本地预览图片html和js例子
本地预览图片html和js例子,直接上代码吧. <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" ...
- JAVA基础学习day22--IO流四-对象序列化、管道流、RandomAccessFile、DataStream、ByteArrayStream、转换流的字符编码
一.对象序列化 1.1.对象序列化 被操作的对象需要实现Serializable接口 1.2.对象序列化流ObjectOutputStream与ObjectInputStream ObjectInpu ...
- JAVA基础学习day18--常用工具类
一.System 1.1.概述 System 类包含一些有用的类字段和方法.它不能被实例化. 在 System 类提供的设施中,有标准输入.标准输出和错误输出流:对外部定义的属性和环境变量的访问:加载 ...
- 基础学习day11--多线程一线程的创建,运行,同步和锁
一.线程基本概述 1.1.进程和线程 进程:一个应用程序一般都是一个进程,正在进行的程序 每一个进程最少都有一个线程,都有一个执行顺序,该顺序是一个执行路径或者一个控制单元 线程:进程中一个独立的控制 ...
- Objective-C 代码规范(Code Style)
我们写出来的代码会给很多人看,为了使代码清晰简洁,方便阅读理解,都会统一遵从一定的代码规范,Objective-C同样如此. 主要参考规范: 1.Google Objective-C Style Gu ...
- Java 参数传递都是值传递
Java 参数传递都是值传递,验证代码如下 public class ParamTransferTest { public static void swap(int a, int b) { int t ...
- Bootstrap Table使用分享
版权声明:本文为博主原创文章,未经博主允许不得转载. 最近客户提出需求,想将原有的管理系统,做下优化,通过手机也能很好展现,想到2个方案: a方案:保留原有的页面,新设计一套适合手机的页面,当手机访问 ...