hdu 1199 Color the Ball(离散化线段树)
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
There are multiple cases, process to the end of file.
1 4 w
8 11 w
3 5 b
//好事恶习啊,我都快要吐了,第一次做离散化的线段树
#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(离散化线段树)的更多相关文章
- ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...
- 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]之间的气球涂一次色,统计每个气球涂色的次数. 思路: 这道题目用树状数组和 ...
- 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 ...
- hdu 1556 Color the ball(非线段树做法)
#include<stdio.h> #include<string.h> ]; int main() { int n,i; int a,b; while(scanf(" ...
随机推荐
- python学习之--内置函数:
Python内置函数: Python内置了很多有用的函数,我们可以直接调用.要调用一个函数,需要知道函数的名称和参数,比如求绝对值的函数abs,只有一个参数. 1. 内置函数调用之--abs()函数: ...
- 基于opencv的小波变换
基于opencv的小波变换 提供函数DWT()和IDWT(),前者完成任意层次的小波变换,后者完成任意层次的小波逆变换.输入图像要求必须是单通道浮点图像,对图像大小也有要求(1层变换:w,h必须是2的 ...
- 【技术贴】note8 N5100刷机 双清 落雨
双清模式:开机键 + [音量+] + HOME键 刷机模式:开机键 + [音量- ]+ HOME键 1.双清步骤: 关机时.长按音量上键+home键+开机键,直到进入recovery模式,然后选择wi ...
- Qt无边框MainWindow如何拖动四周改变大小
原来还有winEvent(), x11Event() and macEvent() 这些东西...不过貌似还需要找更好的办法,否则就无法跨平台了. 你需要重新处理部分窗体事件,以下代码适用于Windo ...
- Yii modules中layout文件的调用
在YII中,如果我们使用了modules区分了前后台,那么在不同的modules中需要使用各自的layout文件,在使用中发现经常会调用不到modules中的layout,下面介绍一下如何才能正确的调 ...
- WC2015 滚粗记
Day 0 和南师附中诸人去杭州,想到这是第三次去杭州有点感动 想到noi还要在杭州,简直…… 火车站接送好评如潮 ym大学军,到学军领资料然后到浙大宿舍安顿,noi的书包还是挺不错的 看起来宿舍还可 ...
- Apache Struts 远程代码执行漏洞(CVE-2013-4316)
漏洞版本: Apache Group Struts < 2.3.15.2 漏洞描述: BUGTRAQ ID: 62587 CVE(CAN) ID: CVE-2013-4316 Struts2 是 ...
- BZOJ3132: 上帝造题的七分钟
3132: 上帝造题的七分钟 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 483 Solved: 222[Submit][Status] Desc ...
- BZOJ3210: 花神的浇花集会
3210: 花神的浇花集会 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 238 Solved: 119[Submit][Status] Descri ...
- [pod install] error: cannot open .git/FETCH_HEAD: Permission denied
pod installAnalyzing dependencies[!] Pod::Executable pull error: cannot open .git/FETCH_HEAD: Permis ...