模板,线段树,矩形轮廓周长
2014-04-16 11:17
281 查看
// poj 1177 矩形轮廓周长 // by utoppia #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define lson (u<<1) #define rson ((u<<1)+1) #define nMax 40010 int len[nMax<<2],cov[nMax << 2],cnt[nMax << 2],lcov[nMax<<2],rcov[nMax<<2]; struct Node{ int l,r,m; void build(int a,int b){ l=a,r=b; m = (l+r) >> 1; } } node[nMax << 2]; void setlen(int u) { if(cov[u]>0) { len[u] = node[u].r - node[u].l; lcov[u] = rcov[u] = 1; cnt[u] = 1; return ; } if(node[u].r == node[u].l+1) { len[u]=0; lcov[u]=rcov[u]=cnt[u]=0; }else { len[u] = len[lson] + len[rson]; lcov[u] = lcov[lson]; rcov[u] = rcov[rson]; cnt[u] = cnt[lson]+cnt[rson]-rcov[lson]*lcov[rson]; } } void build(int u,int l,int r){ node[u].build(l,r); cov[u] = len[u] = cnt[u] = lcov[u] = rcov[u] = 0; if(l+1 == r) return ; int m = (l+r) >> 1; build(lson,l,m); build(rson,m,r); } void ins(int u,int l,int r,int k) { if(node[u].l==l && node[u].r==r) { cov[u]+=k; setlen(u); return ; } int m = node[u].m; if(r<=m) ins(lson,l,r,k); else if(l>=m) ins(rson,l,r,k); else { ins(lson,l,m,k); ins(rson,m,r,k); } setlen(u); } struct Seg{ int x[2],y,flag; void set(int x1,int x2,int _y,int k) { x[0]=x1;x[1]=x2;y=_y;flag=k; } bool operator < (const Seg& a) const { return y < a.y;} } seg[nMax]; int const inf = 10010; int inline max(int x,int y){ return x > y ? x : y; } int inline abs(int x) { if(x<0) x=-x;return x;} int n,xl,xr,yl,yr; int main() { while(~scanf("%d",&n)){ int m = 0; int L = 0; for(int i=0;i<n;i++) { scanf("%d%d%d%d",&xl,&yl,&xr,&yr); xl += inf; xr += inf; L = max(L,xl);L = max(L,xr); seg[m++].set(xl,xr,yl,1); seg[m++].set(xl,xr,yr,-1); } build(1,0,L); sort(seg,seg+m); int now = 0,ans=0; for(int i=0;i<m-1;i++) { ins(1,seg[i].x[0],seg[i].x[1],seg[i].flag); ans += abs(len[1]-now); ans += 2*(seg[i+1].y-seg[i].y)*cnt[1]; now = len[1]; } ins(1,seg[m-1].x[0],seg[m-1].x[1],seg[m-1].flag); ans += abs(len[1]-now); printf("%d\n",ans); } return 0; }
原理和面积并是一样的,注意有了两边的长要计算;
POJ 1177 Picture
相关文章推荐
- JavaScript在IE下设置innerHTML时出现"未知的运行时错误"
- mybatis一对多用collection多对一用assocation
- 在一个字符串中找到第一个只出现一次的字符(java实现)
- RDLC报表带搜索与传参数功能演示(ASP.NET MVC)
- 最新公布的十中抗癌食谱,
- C#4.0中有一个新特性:协变与逆变
- javascript打开word文档的方法
- cocos2d-x 3.0rc开发指南:Windows下Android环境搭建
- Ubuntu下完美安装Sublime
- Altium Designer输出pcb丝印图
- Linux防火墙
- leetcode:reverseWords
- 做完心脏支架手术后一年内反反复复 胸闷失眠、心绞痛老是得不到改善
- 宏和const、typedef的区别
- UIWebView加载HTML,如何使网页中的图片可点击?
- android4.4 的单双卡判断
- 会话跟踪技术
- c++ 弹性数组
- c#异步下载
- iOS的主要框架介绍