Color the ball
Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
total submission (s): 4529 accepted submission (s): 1114
Problem descriptionthere is infinite balls in a line (numbered 1 2 3 ...), and initially all of them is paint black. Now Jim use a brush paint the balls, every time give the integers a B and follow by a char ' w ' or ' B ', ' W ' denotes the Bal L from A to B is painted white, ' B ' denotes is painted black. You were ask to find the longest white ball sequence.
Inputfirst line was an integer N (<=2000), The Times Jim paint, next N line contain a B c, C can be ' W ' and ' B '.
There is multiple cases, process to the end of file.
Outputtwo integers the left end of the longest white ball sequence and the right end of the longest white ball sequence (If mo Re than one output the small number one). All the input is less than 2^31-1. If No such sequence exists, output "Oh, my God".
Sample Input31 4 W8 W3 5 B
Sample Output8 11
Authorzhou, Kai
Sourcezoj Monthly, February 2005
Same title: ZOJ 2301[data is strong]
Can't stand the disgusting discretization 、、、、、
Query is a query, violence just
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineN 20010structline{intL,r,c;} P[n];inttot;intX[n];intVis[n];intlazy[n<<2];voidPushdown (intRT) { if(lazy[rt]!=-1) {Lazy[rt<<1]=lazy[rt<<1|1]=Lazy[rt]; LAZY[RT]=-1; }}voidBuildintLintRintRT) {Lazy[rt]=-1; if(L==R)return; intM= (l+r) >>1; Build (L,m,rt<<1); Build (M+1,r,rt<<1|1);}voidUpdateintLintRintRtintLintRintc) { if(L==l && r==R) {Lazy[rt]=C; return; } pushdown (RT); intM= (l+r) >>1; if(r<=m) Update (l,m,rt<<1, L,r,c); Else if(l>m) Update (m+1,r,rt<<1|1, L,r,c); Else{update (L,m,rt<<1, L,m,c); Update (M+1,r,rt<<1|1, m+1, R,c); }}voidQueryintLintRintRT) { if(lazy[rt]==1) { for(inti=l;i<=r;i++) Vis[i]=1; return; } if(L>=R)return; Pushdown (RT); intM= (l+r) >>1; Query (L,m,rt<<1); Query (M+1,r,rt<<1|1);}intMain () {intI,j,n; while(SCANF ("%d", &n)! =EOF) { if(!N) {cout<<"Oh, my god\n"; Continue; } tot=0; memset (Vis,0,sizeof(VIS)); for(i=0; i<n;i++) { Charch; scanf ("%d%d%c",&p[i].l,&p[i].r,&ch); if(p[i].l>p[i].r) Swap (P[I].L,P[I].R); P[I].C=ch=='b'?0:1; X[tot++]=P[I].L; X[tot++]=P[I].R; } /*discretization of*/Sort (x,x+tot); Tot=unique (X,x+tot)-x; for(i=tot-1; i>=0; i--) { if(x[i]-x[i-1]>1) x[tot++]=x[i-1]+1; if(x[i]-x[i-1]>2) x[tot++]=x[i]-1; } sort (X,x+tot); Build (0, tot-1,1); for(i=0; i<n;i++) { intL=lower_bound (X,X+TOT,P[I].L)-x; intR=lower_bound (X,X+TOT,P[I].R)-x; Update (0, tot-1,1, L,R,P[I].C); } query (0, tot-1,1); intans=-1, s=-1, e=-1; for(i=0; i<tot;i++) { if(Vis[i]) {J=i; while(vis[j+1]) J + +; if(x[j]-x[i]+1>ans) {ans=x[j]-x[i]+1; S=X[i]; E=X[j]; } } } if(s!=-1) cout<<s<<' '<<e<<Endl; Elsecout<<"Oh, my god\n"; } return 0;}
Line segment tree [HDU 1199] Color the ball