HDU 1199 Color the Ball
来源:互联网 发布:淘宝侵权怎么处理 编辑:程序博客网 时间:2024/06/06 01:55
Color the Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5631 Accepted Submission(s): 1395
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.
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
31 4 w8 11 w3 5 b
Sample Output
8 11
线段树区间覆盖的问题,问题是数字范围太大,所以要离散化。当然也可以不用离散化作,即动态开点。
这道题目的测试数据比较弱,所以我用了动态开点,要是数据强一点,可能会内存超限,
#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <algorithm>#include <math.h>using namespace std;typedef long long int LL;const int maxn=2*1e3;LL mmax[maxn*35];LL lmax[maxn*35];LL rmax[maxn*35];int c[maxn*35];int l[maxn*35];int r[maxn*35];LL ll[maxn*35];LL rr[maxn*35];int n;int p;int newnode(){ l[p]=r[p]=-1; lmax[p]=rmax[p]=mmax[p]=0; ll[p]=rr[p]=-1; return p++;}void pushdown(int node,LL L,LL R){ if(!(L==R)) { if(l[node]==-1) l[node]=newnode(); if(r[node]==-1) r[node]=newnode(); } if(c[node]!=-1) { if(c[node]) { lmax[node]=rmax[node]=mmax[node]=R-L+1; ll[node]=L,rr[node]=R; } else { lmax[node]=rmax[node]=mmax[node]=0; ll[node]=rr[node]=-1; } c[l[node]]=c[r[node]]=c[node]; c[node]=-1; }}void pushup(int node,LL L,LL R){ LL mid=(L+R)>>1; pushdown(l[node],L,mid); pushdown(r[node],mid+1,R); if(lmax[l[node]]==(mid-L+1)) lmax[node]=lmax[l[node]]+lmax[r[node]]; else lmax[node]=lmax[l[node]]; if(rmax[r[node]]==(R-mid)) rmax[node]=rmax[r[node]]+rmax[l[node]]; else rmax[node]=rmax[r[node]]; mmax[node]=max(lmax[r[node]]+rmax[l[node]],max(mmax[l[node]],mmax[r[node]])); if(mmax[node]==mmax[l[node]]) ll[node]=ll[l[node]],rr[node]=rr[l[node]]; else if(mmax[node]==lmax[r[node]]+rmax[l[node]]) ll[node]=mid-rmax[l[node]]+1,rr[node]=mid+lmax[r[node]]; else ll[node]=ll[r[node]],rr[node]=rr[r[node]];}void update(int node,LL begin,LL end,LL L,LL R,int tag){ if(L<=begin&&end<=R) { c[node]=tag; if(l[node]==-1) l[node]=newnode(); if(r[node]==-1) r[node]=newnode(); pushdown(node,begin,end); return; } if(l[node]==-1) l[node]=newnode(); if(r[node]==-1) r[node]=newnode(); pushdown(node,begin,end); int mid=(begin+end)>>1; if(L<=mid) update(l[node],begin,mid,L,R,tag); if(R>mid) update(r[node],mid+1,end,L,R,tag); pushup(node,begin,end);}int main(){ while(scanf("%d",&n)!=EOF) { p=0; int root=newnode(); LL a,b; char cc; LL len=(1<<31)-1; int tag; update(root,1,len,1,len,0); memset(c,-1,sizeof(c)); for(int i=1;i<=n;i++) { scanf("%lld%lld %c",&a,&b,&cc); if(cc=='w') tag=1; else tag=0; update(root,1,len,a,b,tag); } if(ll[root]==-1) printf("Oh, my god\n"); else printf("%lld %lld\n",ll[root],rr[root]); } return 0;}
0 0
- hdu 1199 Color the Ball
- HDU 1199-Color the Ball
- HDU-1199 color the ball
- hdu 1199 Color the Ball
- HDU 1199 Color the Ball
- HDU 1199 Color the Ball
- Color the ball HDU
- Color the ball HDU
- Color the Ball HDU
- zoj 2301 || hdu 1199 Color the Ball
- HDU 1199 Color the Ball (线段树)
- hdu 1199 Color the Ball vector使用
- hdu 1556 color the ball
- hdu 1556Color the ball
- HDU 1556 - Color the ball
- Hdu 1556 Color the ball
- hdu 1556 Color the ball
- hdu 1556 Color the ball
- 王朝 鸡和兔
- 契约——MTConnect:基本通讯模型
- 有向图中寻找强连通分量(环)和拓扑排序——Kosaraju、Trajan、Gabow算法
- 第八周实践年龄几何
- -映射组成关系
- HDU 1199 Color the Ball
- java的嵌套内部类
- 输入半径,求圆的面积和周长
- Java的String
- 三天上手PHP之10:循环(while、do...while、for、foreac)
- Java 时间和日期类型的 Hibernate 映射及二进制映射及大文本的映射
- 输入三条边,判断是否可构成三角形
- linux多线程设计
- 三个数的最大值