Color the Ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3529    Accepted Submission(s): 874

Problem Description
There are infinite balls in a line (numbered 1 2 3 ....), and initially all of them are paint black. Now Jim use a brush paint the balls, every time give two integers a b and follow by a char 'w' or 'b', 'w' denotes the ball from a to b are painted white, 'b' denotes that be painted black. You are ask to find the longest white ball sequence.

 
Input
First line is an integer N (<=2000), the times Jim paint, next N line contain a b c, c can be 'w' and 'b'.

There are multiple cases, process to the end of file.

 
Output
Two integers the left end of the longest white ball sequence and the right end of longest white ball sequence (If more than one output the small number one). All the input are less than 2^31-1. If no such sequence exists, output "Oh, my god".

 
Sample Input
3
1 4 w
8 11 w
3 5 b
 
Sample Output
8 11
 
 
//好事恶习啊,我都快要吐了,第一次做离散化的线段树
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 4040
int a[N],r[N],p[N],c[N],f[N];//r[i]排名i的原位置,p[i]原位置i的排名
int cmp(int i,int j){
return a[i]<a[j];
}
struct node{
int l,r,lz;
void init(int _l,int _r){
l=_l;r=_r;lz=-1;
}
}t[4*N];
void build(int l,int r,int k){
if(l>=r)return;
t[k].init(l,r);
if(l+1>=r)return;
int md=(l+r)/2;
build(l,md,k*2);
build(md,r,k*2+1);//不是md+1(因为md被覆盖)
}
void down(int k){
if(t[k].l+1==t[k].r)return;
if(t[k].lz==-1)return;
t[k*2].lz=t[k*2+1].lz=t[k].lz;
t[k].lz=-1;
}
void DownAll(int k){
if(t[k].lz!=-1){
for(int i=t[k].l;i<t[k].r;i++)f[i]=t[k].lz;//右端点不标记(只有被覆盖时才标记)
return ;
}
DownAll(k*2);
DownAll(k*2+1);
}
void update(int l,int r,int cr,int k){
if(t[k].l>=t[k].r)return;
if(l==t[k].l&&r==t[k].r){
t[k].lz=cr;//标记
return;
}
down(k);
if(t[k].l+1>=t[k].r)return; int md=(t[k].l+t[k].r)/2;
if(md>=r)update(l,r,cr,k*2);
else if(l>md)update(l,r,cr,k*2+1);
else{
update(l,md,cr,k*2);
update(md,r,cr,k*2+1);
}
}
int main(){
int i,j,k,n;
int x,y;
char ch;
while(~scanf("%d",&n)){
for(i=j=0;i<n;i++){
scanf("%d%d %c",&x,&y,&ch);
c[i]=(ch=='w');
//l-1 或r+1 [)一闭一开离散化
r[j]=j;a[j]=x;++j;
r[j]=j;a[j]=y+1;++j;
} sort(r,r+j,cmp);
//去重
p[r[0]]=k=0;//第0位的a,排第0位
for(i=1;i<j;i++){
if(a[r[i]]!=a[r[i-1]])a[r[++k]]=a[r[i]];
p[r[i]]=k;
}
//for(i=0;i<j;i++)cout<<p[r[i]]<<" ";cout<<endl;
//初始化树
build(0,k,1);
memset(f,0,sizeof(f));//0黑色1白色
//update
for(i=0,n+=n;i<n;i+=2){
x=p[i];
y=p[i+1];
update(x,y,c[i/2],1);//区间更新
}
DownAll(1);
// for(i=0;i<=k;i++)cout<<f[i]<<" ";cout<<endl;
int tx,ty;
for(i=x=y=0;i<=k;i++){
if(f[i]==0)continue;
tx=a[r[i]];
while(f[i]==1)i++;
ty=a[r[i]];
if(ty-tx>y-x){x=tx;y=ty;}
}
if(x==y) puts("Oh, my god");
else printf("%d %d\n",x, y-1); }
return 0;
}

 

hdu 1199 Color the Ball(离散化线段树)的更多相关文章

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

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

  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. HDU 1199 - Color the Ball 离散化

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

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

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

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

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

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

    #include<stdio.h> #include<string.h> ]; int main() { int n,i; int a,b; while(scanf(" ...

随机推荐

  1. [转]如何根据cpu的processor数来确定程序的并发线程数量

    原文:http://blog.csdn.net/kirayuan/article/details/6321967 我们可以在cat 里面发现processor数量,这里的processor可以理解为逻 ...

  2. 哈希,哈希表,哈希Map

    数组: 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1):数组的特点是:寻址容易,插入和删除困难: 链表: 链表存储区间离散,占用内存比较宽松,故空间复杂 ...

  3. Python 全栈开发 -- 监控篇

    如果你已经玩转了 Python 编程语言语法,肯定想用这些知识,开发一款应用程序,它可以是在网上,可以炫耀或出售,那就需要全栈式开发 Python.具体如何创建,部署和运行生产 Python Web ...

  4. 好吧,如果一定要RESTFUL的DJANGO

    看看人家写的VIEWSET, 然后用REQUESTS测试一下. from rest_framework import viewsets from rest_framework.decorators i ...

  5. 批量建立EXCHANGE邮件帐号建立三部曲

    第一步:从AD里导出用户名(可以基于OU),将输出的CSV的DN列删除,并去除可能的测试及其它用途用户名. csvde -f users-gz.csv -d "ou=MKT gz,dc=xm ...

  6. FFMPEG之TimeBase成员理解

    http://blog.csdn.net/supermanwg/article/details/14521869

  7. 17.1.1.5 Creating a Data Snapshot Using mysqldump

    一种方式创建一个数据库的快照在一个存在的master 数据库是使用mysqldump 来创建你需要复制的所有数据库的dump. 一旦数据dump 是完成,你然后倒入数据到slave 在开始复制过程前 ...

  8. C++ Prime:函数

    局部静态对象: 某些时候,有必要令局部变量的生命周期贯穿函数调用及之后的时间.可以将局部变量定义成static类型从而获得这样的对象.局部静态对象在程序执行路径第一次经过对象定义语句时初始化,并且直到 ...

  9. mybatis源码分析(2)——事务概述

    这篇文章主要对mybatis中的事务做一简单的分析,帮助读者理清一些概念. 先来看看在JAVA事务的相关技术,在JAVA中有两类事务,JDBC事务和JTA事务,如果是JDBC类型的事务,则是由Conn ...

  10. Apache Struts 安全措施绕过漏洞(CVE-2013-4310)

    漏洞版本: Apache Group Struts < 2.3.15.2 漏洞描述: BUGTRAQ ID: 62584 CVE(CAN) ID: CVE-2013-4310 Struts2 是 ...