Color the Ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6689 Accepted Submission(s):
1655
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
题目大意:区间初始化颜色为黑色,给定N个区间颜色,求白色最长区间的L,R。
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn=2e5+5; int cnt=0; int l[maxn*2],node[maxn*4]; struct node { int l,r,color,lazy;//color=1白色 }init[maxn],tree[4*maxn],d[maxn]; void reset_node() { int i=1,j=1; node[1]=l[1]; for(i=2;i<=cnt;i++) { if(node[j]==l[i]) continue; if(node[j]+1<l[i]) { node[++j]=node[j-1]+1; } if(node[j]<l[i]-1) { node[++j]=l[i]-1; } node[++j]=l[i]; } cnt=j; } void btree(int l,int r,int k) { tree[k].color=0; tree[k].lazy=1; tree[k].l=l;tree[k].r=r; if(l==r) return ; int mid=(l+r)/2; btree(l,mid,k<<1); btree(mid+1,r,k<<1|1); } int ccnt=0; void date_tree(int l,int r,int color,int k) { //printf("%d ",ccnt++); int mid=(tree[k].l+tree[k].r)/2; if(tree[k].l>=l&&tree[k].r<=r) { tree[k].color=color; tree[k].lazy=1; return ; } if(tree[k].lazy&&tree[k].color==color) return ; if(tree[k].lazy) { tree[k<<1].color=tree[k<<1|1].color=tree[k].color; tree[k<<1].lazy=tree[k<<1|1].lazy=1; } if(l>mid) { date_tree(l,r,color,k<<1|1); } else if(r<=mid) { date_tree(l,r,color,k<<1); } else { date_tree(l,r,color,k<<1); date_tree(l,r,color,k<<1|1); } tree[k].lazy=0; } int in_node(int k) { int l=1,r=cnt,mid; while(l<=r) { mid=(l+r)/2; if(k==node[mid]) break; if(k>node[mid]) l=mid+1; if(k<node[mid]) r=mid-1; } return mid; } void push_down(int k,int l,int r) { int mid=(l+r)/2; if(tree[k].l==tree[k].r) return; if(tree[k].lazy) { tree[k<<1].color=tree[k<<1|1].color=tree[k].color; tree[k<<1].lazy=tree[k<<1|1].lazy=1; } push_down(k<<1,tree[k<<1].l,tree[k<<1].r); push_down(k<<1|1,tree[k<<1|1].l,tree[k<<1|1].r); } int dn=0; void get_node(int l,int r,int k) { if(tree[k].lazy) d[++dn]=tree[k]; if(l==r) return ; int mid=(l+r)/2; get_node(l,mid,k<<1); get_node(mid+1,r,k<<1|1); } int main() { int n; cnt=0; char s[5]; while(scanf("%d",&n)!=EOF) { //printf("1\n"); cnt=0; for(int i=1;i<=n;i++) { scanf("%d%d%s",&init[i].l,&init[i].r,s); l[++cnt]=init[i].l;l[++cnt]=init[i].r; if(s[0]=='w') { init[i].color=1; } else init[i].color=0; } sort(l+1,l+1+cnt); reset_node(); //printf("%d...\n",cnt); btree(1,cnt,1); //printf("2\n"); int ll,rr,cc; for(int i=1;i<=n;i++) { ll=in_node(init[i].l); rr=in_node(init[i].r); cc=init[i].color; date_tree(ll,rr,cc,1); } //printf("3\n"); push_down(1,1,cnt); //printf("4\n"); dn=0; get_node(1,cnt,1); int L,R; int len=-1; //printf("5\n"); for(int i=1;i<=dn;i++) { if(d[i].color) { int j=i; while(d[i+1].color!=0&&i+1<=dn) i++; if(i==dn&&node[d[i].r]-node[d[j].l]+1>len) { L=node[d[j].l]; R=node[d[i].r]; len=R-L+1; } if(i<dn&&node[d[i].l]-node[d[j].l]>len) { L=node[d[j].l]; R=node[d[i].l]; len=R-L; } } } //for(int i=1;i<=11;i++) //printf("%d\n",tree[i].color); //printf("6\n"); if(len!=-1) printf("%d %d\n",L,R); else printf("Oh, my god\n"); } return 0; }