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 ...
随机推荐
- java事务理解
还在学Hibernate,后续一大堆概念刚接触需要理解.觉得-——事务——这个概念不是很好理解,所以发上来记录一下. 首先说点千篇一律的东西.概念和特性都是随处可见的,无论哪里都很容易找到,关键是你如 ...
- 阿里前CEO卫哲用自己10余年经历,倾诉B2B的三差、四率、两大坑
今日(12 月 28 日),嘉御基金创始人.阿里巴巴(B2B)前 CEO 卫哲在第三届中国 B2B 电子商务大会上进行了"B2B 冬天里的春天"的主题分享.他提出中国 B2B 行业 ...
- H5前端面试题及答案(1)
前几天去面试了一家公司,整下改公司的面试题. 1.新的 HTML5 文档类型和字符集是? HTML5 文档类型很简单: <!doctype html> HTML5 使用 UTF-8 编码示 ...
- js中创建对象的几种方式
创建对象指创建一个object并给这个对象添加属性和方法,有以下几个方式: 最基本的: var Person={}; Person.name='tom'; Person.age='20'; Perso ...
- 关于原生的Javascript
JQuery是个好工具,它做了太多的事. 以至于让人渐渐忘记原生的JS该怎么写了,导致连为了用个DOM选择器或者Ajax就直接加个JQuery,确实,JQuery太方便了. 坏处: 由于JQuery的 ...
- 使用WCF对外提供接口
本篇将通过WCF以webservices的方式对外提供接口.同时使用NUnit对webservices中的方法进行单元测试. 开发契约 contract Contract项目为类库项目,该项目下会包含 ...
- ASP.NET版CKEditor与CKFinder的配置使用
ASP.NET版 CKEditor与CKFinder的配置使用 将CKEditor 与 CKFinder 的包含在项目中,并添加程序集的引用 从http://cksource.com网站上下载CKEd ...
- iOS didReceiveMemoryWarning 的处理
当iOS触发didReceiveMemoryWarning这个方法的时候,我们一般会做一些手动处理,强制清理掉一些目前不用的数据.但是这个方法并不只是单纯的通知开发者你的内存已经吃紧了,系统通知你的同 ...
- Java使用正则表达式获取文本的章节名称
获取文本的章节,首先要确定章节的开始标准,一般中文的章节都是以“第”开头,第一章.第二章等.所以使用“^”字符来确定首位,但是很多时候章节前面会有空白字符,所有以“第”作为章节的开始,进行以下的匹配 ...
- Struts2(十七)验证框架二
一.实现注册验证 package com.pb.entity; import java.util.Date; /** * 用户实体类 * */ public class User { /** * 住址 ...