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. delphi xe5 android sample

    安装xe5以后demo存放的路径在  C:\users\Public\Documents\RAD Studio\12.0\Samples 另外易博龙在sourceforget上也有 svn地址为:sv ...

  2. How to new a screen in linux

    screen -R -D: create a screen Ctrl + A & Ctrl + D: leave a screen

  3. Session里的对象是不可靠的!

    最近在做Database课程的final project,foodiePharos, 重新认识了JSP里容易出现的一些问题. 比如我们这个项目使用了JPA,就涉及到entity对象的状态问题,Enti ...

  4. BZOJ 3707: 圈地 计算几何

    Description 2维平面上有n个木桩,黄学长有一次圈地的机会并得到圈到的土地,为了体现他的高风亮节,他要使他圈到的土地面积尽量小.圈地需要圈一个至少3个点的多边形,多边形的顶点就是一个木桩,圈 ...

  5. [原博客] POI系列(3)

    正规.严谨.精妙. -POI BZOJ 1131 : [POI2008]Sta 树形dp吧,让求找一个点使以这个点深度和最小.首先可以随便整出来一棵树,对于每个节点记录down[i]以i为根下面的点的 ...

  6. Spring中的事务管理详解

    在这里主要介绍Spring对事务管理的一些理论知识,实战方面参考上一篇博文: http://www.cnblogs.com/longshiyVip/p/5061547.html 1. 事务简介: 事务 ...

  7. ANDROID_MARS学习笔记_S05_003_传感器采样率及属性

    1. 2. import android.app.Activity; import android.content.Context; import android.hardware.Sensor; i ...

  8. 新API:AttachThreadInput

    https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms681956(v=vs.85).aspx

  9. Service知识点总结

    转载请注明出处:http://blog.csdn.net/krislight/article Service可以看作一个后台服务,但并非是开启另外的线程,Service还是在主线程中运行.所以需避免耗 ...

  10. B*tree dump

    Oracle的索引是以平衡树的方式组织存储的:保存的是索引列的值,以及该行的rowid的一部分(文件号,块号,行号) 下面我们通过例子来了解一下: 1,create table test(id int ...