ural 1019 Line Painting 线段树 区间染色
2013-03-06 21:33
225 查看
做法:先统计,后映射到数组上.每一次然a,b-1的区域即可。
#include<cstdio> #include<cstring> #include<algorithm> #define left l,m,x<<1 #define right m+1,r,x<<1|1 const int LMT=35005; const int big=1000000000; using namespace std; int cov[LMT<<2],myx[LMT],cnt; bool seg[LMT]; struct line { int x1,x2; char c; }e[LMT]; void init(void) { memset(cov,-1,sizeof(cov)); } void cut(int x) { if(cov[x]!=-1) { cov[x<<1]=cov[x<<1|1]=cov[x]; cov[x]=-1; } } void update(int op,int L,int R,int l,int r,int x) { if(L<=l&&r<=R) { cov[x]=op; return; } cut(x); int m=(l+r)>>1; if(L<=m)update(op,L,R,left); if(R>m)update(op,L,R,right); } void query(int l,int r,int x) { if(cov[x]!=-1) { int i=l; for(;i<=r;i++)seg[i]=cov[x]; return; } if(l==r)return; cut(x); int m=(l+r)>>1; query(left); query(right); } int myfind(int x) { int l=0,r=cnt-1,m; while(l<=r) { m=(l+r)>>1; if(myx[m]==x)return m; if(myx[m]>x)r=m-1; else l=m+1; } return -1; } int main(void) { int n,i,t,j,leng=0,ansl,ansr,k; scanf("%d",&n); myx[cnt++]=0; myx[cnt++]=big; for(i=0;i<n;i++) { scanf("%d%d %c",&e[i].x1,&e[i].x2,&e[i].c);getchar(); myx[cnt++]=e[i].x1; myx[cnt++]=e[i].x2; } t=1; for(i=1;i<cnt;i++) if(myx[i]!=myx[i-1]) myx[t++]=myx[i]; cnt=t; sort(myx,myx+cnt); t=cnt; for(i=1;i<t;i++) if(myx[i]-myx[i-1]>1) myx[cnt++]=myx[i-1]+1; sort(myx,myx+cnt); for(i=0;i<n;i++) { e[i].x1=myfind(e[i].x1); e[i].x2=myfind(e[i].x2); update(e[i].c=='b',e[i].x1,e[i].x2-1,0,cnt-1,1); } query(0,cnt-1,1); for( i=0;i<cnt;) { j=i; while(j<cnt&&seg[j]==seg[i])j++; if(j==cnt)k=big; else k=myx[j]; if(0==seg[i]&&leng<k-myx[i]) { leng=k-myx[i]; ansl=myx[i];ansr=k; } i=j; } printf("%d %d\n",ansl,ansr); return 0; }
相关文章推荐
- ural 1019 Line Painting (线段树)
- URAL 1019. Line Painting (线段树)
- Ural 1019 A Line painting(线段树,成段更新离散化)
- Ural_1019. Line Painting(线段树)
- ural 1019 Line Painting(线段树)
- poj2528&&zoj1610 线段树区间染色问题
- poj2528(线段树区间染色)
- 【codevs1191】数轴染色 线段树 区间修改+固定区间查询
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- poj 2777(线段树+区间染色)
- POJ训练计划2777_Count Color(线段树/成段更新/区间染色)
- 【线段树区间更新 && 染色】ZOJ - 1610 Count the Colors
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- Ural 1019 Line Painting
- poj2777-----线段树------位运算模拟线段染色------求区间颜色种数
- URAL 1855 区间更新线段树
- POJ 1436 Horizontally Visible Segments(线段树区间染色问题)
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- BZOJ 2243 [SDOI2011]染色 (树链剖分)(线段树区间修改)
- POJ 2528-posters(线段树区间染色+离散化)