Color the Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3081 Accepted Submission(s): 762
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
3
1 4 w
8 11 w
3 5 b
Sample Output
8
11
Author
ZHOU, Kai
Source
Recommend
Ignatius.L
开始求错了东西、好惨,Wa的惨不忍睹
离散化
线段树更新,最后更新到底 NlogN
然后对树底暴力进行统计 (N)
细节:
把每个点拆成2份
奇数代表区间 偶数代表点
#include <iostream> #include <map> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; #define lson l,m,k<<1 #define rson m+1,r,k<<1|1 #define N 4003 int st[N<<3]; struct record { int l,r; char c; }rc[N>>1]; int id[N<<1],ls[N],cnt; int bf(int n) { int l=1,r=cnt-1,m; while(l<=r) { m=(l+r)>>1; if(ls[m]>n) r=m-1; else if(ls[m]<n) l=m+1; else return m; } return 0; } void build(int l,int r,int k) { st[k]=0; if(l==r) { id[cnt++]=k; return; } int m=(l+r)>>1; build(lson); build(rson); } void down(int &k) { st[k<<1]=st[k]; st[k<<1|1]=st[k]; st[k]=-1; } int L,R,flag; void update(int l,int r,int k) { if(L<=l&&R>=r) { st[k]=flag; return ; } if(st[k]!=-1) down(k); int m=(l+r)>>1; if(L<=m) update(lson); if(R>m) update(rson); } void over(int l,int r,int k) { if(l==r) return; if(st[k]!=-1) down(k); int m=(l+r)>>1; over(lson); over(rson); } int main() { int n; int i,j,k; while(scanf("%d",&n)!=EOF) { for(k=1,i=0;i<n;i++) { scanf("%d %d %c",&rc[i].l,&rc[i].r,&rc[i].c); ls[k++]=rc[i].l; ls[k++]=rc[i].r; } sort(ls+1,ls+k); n=(n<<1); for(k=1,i=2;i<=n;i++) if(ls[i]!=ls[k]) ls[++k]=ls[i]; cnt=1; build(1,k<<1,1); n=(n>>1); for(i=0;i<n;i++) { L=bf(rc[i].l)<<1; R=bf(rc[i].r)<<1; flag=rc[i].c=='w'?1:0; update(1,k<<1,1); } over(1,k<<1,1); // for(i=1;i<cnt;i++) // printf("%d %d ->",i,st[id[i]]); int rcl=-1,rcr=-1,rclen=-1; for(i=1;i<cnt;i++) if(st[id[i]]) { int l,r; if(i%2) l=ls[i/2]+1;开始写成了-1,白白贡献了个 WA else l=ls[i/2]; //printf("%d %d\n",i,l); for(j=i+1;j<cnt;j++) if(st[id[j]]==0) { if(j%2) if(st[id[j+1]]&&ls[j/2+1]-ls[j/2]==1) continue; else { r=ls[j/2]; break; } else { r=ls[j/2]-1; break; } } if(j==cnt) r=ls[j/2]; if(r-l>rclen) { rclen=r-l; rcl=l; rcr=r; } i=j; } if(rcl==-1) printf("Oh,my god\n"); else printf("%d %d\n",rcl,rcr); } return 0; }