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. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  2. Table嵌套去掉子table的外边框

    Table表格去掉子表格的边框 1. 父表格 <table align="center" style="border:none;cell-padding:0; ce ...

  3. 在MVC或WEBAPI中记录每个Action的执行时间和记录下层方法调用时间

    刚才在博客园看了篇文章,http://www.cnblogs.com/cmt/p/csharp_regex_timeout.html  突然联想到以前遇到的问题,w3wp进程吃光CPU都挂起IIS进程 ...

  4. phpStorm 使用技巧大集合

    之前整理了一部分使用技巧了,但是在实际操作中发现phpstorm的技巧实在是太多了,所以大部分都统一整理到这篇文字中 ,备用 插件1 1:phpstrom的插件Provides live edit H ...

  5. django celery redis简单测试

    希望在下一版中,能用这个小芹菜,来实现异步的多任务并行哈. 安装REDIS之类的不表,只说在DJANGO当中要注意配置的事项. 0,安装插件 yum install redis-server pip ...

  6. 【HDU 4276】The Ghost Blows Light(树形DP,依赖背包)

    The Ghost Blows Light Problem Description My name is Hu Bayi, robing an ancient tomb in Tibet. The t ...

  7. Eclipse怎样导入github上的项目

    国外有些比较好的源代码网站,经常需要从这些网站上导入项目到eclipse中,我最近就发现github.com这个网站上有很多开源项目.这里就以从github网站上导入项目到eclipse中为例来详细的 ...

  8. 怎么样删除eclipse已经记录svn的地址

    eclipse-->window-->show view-->svn选项卡中选中要删除的svn链接,点击右键废弃即可. 1.

  9. Linux中的随机数文件 /dev/random /dev/urandom

    Linux中的随机数可以从两个特殊的文件中产生,一个是/dev/urandom.另外一个是/dev/random.他们产生随机数的原理是利用当前系统的熵池来计算出固定一定数量的随机比特,然后将这些比特 ...

  10. android 世界各国英文简写代码 资源文件

    今日又用到这段代码,忽然感觉到如果是第一次用的人肯定也会很麻烦.故在此上传一份.后人再用就不必重复做此工作.跟体育老师学过语文,见谅. 提供下载地址 http://download.csdn.net/ ...