題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1199
這個題目開始一看就知道用線段樹加離散化來解,可是好長時間沒寫線段樹了,一時間不知道離散化用線段樹怎麼搞定
於是就用一個網上稱是切割線段的類似類比方法,感覺這個方法比較容易想,但是寫的過程中每一步到底是加一還是減
一都要想好,表示很煩人,開始寫的時候其實也挺矛盾,分析下複雜度如果專門挑那些奇怪的測試資料可能會逾時,還
是寫出來了,果然不出所料wa了幾次,但是後來46ms通過可能是資料的問題吧,至於離散化思想我倒是覺得沒怎麼體
現,反正寫出來的代碼感覺很噁心了!
#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define maxn 2500struct point{ int l; int r;}po[maxn];int pos;int n;int black(int a,int b){ int p=pos; for(int i=0;i<p;i++) { if(a>po[i].r || b<po[i].l) continue; if(a<=po[i].l && b>=po[i].r) po[i].l=0,po[i].r=-1; else if(a>po[i].l && b<po[i].r) po[pos].l=b+1,po[pos].r=po[i].r,po[i].r=a-1,pos++; else if(a<=po[i].l && b<po[i].r) po[i].l=b+1; else if(a>po[i].l && b>=po[i].r) po[i].r=a-1; } return 0;}bool cmp(const point &a,const point &b){ if(a.l==b.l) return a.r < b.r; return a.l < b.l;}int main(){ int i,j,k,a,b; char ch; int ans,left,now,my_pos,re_pos; while(scanf("%d",&n)!=EOF) { ans=0; pos=0; left=0; for(i=0;i<n;i++) { scanf("%d%d",&a,&b); getchar(); scanf("%c",&ch); if(ch=='w') { po[pos].l=a; po[pos].r=b; pos++; } else { black(a,b); } } sort(po,po+pos,cmp); //for(i=0;i<pos;i++) //printf("%d %d \n",po[i].l,po[i].r); if(pos==0) { printf("Oh, my god\n"); continue; } i=0; while(po[i].r==-1) i++; if(i<pos) { now=po[i].r-po[i].l+1; left=po[i].r; my_pos=left; } else ans=0,now=0; for(i;i<pos;i++) { if(po[i].l <= left+1) { if(po[i].r <= left) continue; else now+=po[i].r-left,my_pos=po[i].r,left=po[i].r; } else { if(ans < now) ans=now,re_pos=my_pos; now=po[i].r-po[i].l+1; my_pos=po[i].r,left=po[i].r; } } if(ans < now) ans=now,re_pos=my_pos; if(ans==0) { printf("Oh, my god\n"); continue; } printf("%d %d\n",re_pos-ans+1,re_pos); } return 0;}