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. phpStorm 使用技巧大集合

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

  2. WAF

    http://netsecurity.51cto.com/art/201010/231124.htm http://wenku.baidu.com/link?url=elrFtxPRcwJ5FjlXE ...

  3. EasyUI 树形菜单tree 定义图标

    { "id":1, "text":"Folder1", "iconCls":"icon-save", ...

  4. CF 33B String Problem

    对每个位置进行操作,求出最终变成相同字母的代价,然后把所有的位上代价加起来,使得总代价最小.res[i][j]表示将字母i+'a'和字母j+'a'变为相同的代价,设最终都变成字母k+'a',那么res ...

  5. mysqll 数据库相互堵塞问题

    192.168.11.186 远程访问192.168.11.185 数据库 186上看到: centos6.5:/root#mysql -uroot -p'kjk123123' -h192.168.1 ...

  6. 深入浅出 - Android系统移植与平台开发(十一)- Android系统的定制(瘋耔修改篇一)

    首先非常感谢原文作者为我们提供的知识库,因为有你们的贡献,我们的开发难度更显简单 原文 :   http://blog.csdn.net/mr_raptor/article/details/30113 ...

  7. Drainage Ditches(Dinic最大流)

    http://poj.org/problem?id=1273 用Dinic求最大流的模板题,注意会有重边. 邻接矩阵建图 #include<stdio.h> #include<str ...

  8. asp.net EasyUI DataGrid 实现增删改查

    转自:http://www.cnblogs.com/create/p/3410314.html 前台代码: <!DOCTYPE html> <html xmlns="htt ...

  9. (转载)常用JS加密编码算法 五:SHA1算法

    (转载)http://neil-yang.iteye.com/blog/703470 /* * A JavaScript implementation of the Secure Hash Algor ...

  10. poj magic number

    Problem H Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Sub ...