标题效果:

每间房子的长度给出阴影(在间隔代表)而高度,求阴影总面积。



解题思路:矩形面积并。



以下是代码:

#include <set>
#include <map>
#include <queue>
//#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> #define eps 1e-8
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define iabs(x) ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define min( x, y ) ( ((x) < (y)) ? (x) : (y) ) using namespace std; struct node2
{
int num;
long long y,l,r;
} edge[80005]; long long tempx[80005],binx[80005];
long long x1,x2,y1,y2,ans;
int cntx,n; bool cmp(node2 a,node2 b)
{
return a.y<b.y;
} int binnum(long long num)
{
int ll=0,m,rr=cntx-1;
while(rr>ll)
{
m=(ll+rr)>>1;
if(binx[m]==num)return m;
else if(binx[m]<num)ll=m+1;
else rr=m-1;
}
return ll;
} struct node1
{
long long disnow;
int cnt;
} node[80005<<2]; inline void PushUp(int l,int r,int tr)
{
if(node[tr].cnt)node[tr].disnow=binx[r+1]-binx[l];
else if(l==r)node[tr].disnow=0;
else node[tr].disnow=node[tr<<1].disnow+node[tr<<1|1].disnow;
} void update(int L,int R,int num,int l,int r,int tr)
{
if(L<=l&&r<=R)
{
node[tr].cnt+=num;
PushUp(l,r,tr);
return ;
}
int m=(l+r)>>1;
if(L<=m)update(L,R,num,l,m,tr<<1);
if(m<R)update(L,R,num,m+1,r,tr<<1|1);
PushUp(l,r,tr);
}
int main()
{
int case1=1;
while(scanf("%d",&n)!=EOF)
{
for(int i=0; i<n; i++)
{
scanf("%d%d%d",&x1,&x2,&y2);
tempx[2*i]=x1;
tempx[2*i+1]=x2;
edge[2*i].l=x1;
edge[2*i].r=x2;
edge[2*i+1]=edge[2*i];
edge[2*i].y=0;
edge[2*i+1].y=y2;
edge[2*i].num=1;
edge[2*i+1].num=-1;
}
sort(tempx,tempx+2*n);
binx[0]=tempx[0];
cntx=1;
for(int i=1; i<2*n; i++)
{
if(tempx[i]!=binx[cntx-1])
{
binx[cntx++]=tempx[i];
}
}
sort(edge,edge+2*n,cmp);
clearall(node,0);
ans=0;
x1=binnum(edge[0].l);
x2=binnum(edge[0].r);
x2--;
update(x1,x2,edge[0].num,0,cntx-2,1);
y1=edge[0].y;
for(int i=1; i<2*n; i++)
{
ans+=node[1].disnow*(edge[i].y-y1);
y1=edge[i].y;
x1=binnum(edge[i].l);
x2=binnum(edge[i].r);
x2--;
update(x1,x2,edge[i].num,0,cntx-2,1);
}
printf("%lld\n",ans);
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

POJ 3277 City Horizon的更多相关文章

  1. 离散化+线段树 POJ 3277 City Horizon

    POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...

  2. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  3. POJ 3277 City Horizon(扫描线+线段树)

    题目链接 类似求面积并..2Y.. #include <cstdio> #include <cstring> #include <string> #include ...

  4. [POJ] 3277 .City Horizon(离散+线段树)

    来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...

  5. POJ 3277 City Horizon(叶子节点为[a,a+1)的线段树+离散化)

    网上还有用unique函数和lowerbound函数离散的方法,可以百度搜下题解就有. 这里给出介绍unique函数的链接:http://www.cnblogs.com/zhangshu/archiv ...

  6. poj City Horizon (线段树+二分离散)

    http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  7. 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543

    学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...

  8. [POJ3277]City Horizon

    [POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...

  9. 1645: [Usaco2007 Open]City Horizon 城市地平线

    1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 315  Solved: ...

随机推荐

  1. php引用(&)详解及注意事项

    php的引用(就是在变量或者函数.对象等前面加上&符号) 在PHP 中引用的意思是:不同的名字访问同一个变量内容. 与C语言中的指针是有差别的.C语言中的指针里面存储的是变量的内容,在内存中存 ...

  2. Maven镜像配置

    镜像是为了提供更快的服务 如图:X就认为是Y的一个镜像. 编辑settings.xml配置中央仓库镜像: <settings> ... <mirrors> <mirror ...

  3. mysql 中创建存储过程

    mysql中创建存储过程和存储函数虽相对其他的sql语言相对复杂,但却功能强大,存储过程和存储函数更像是一种sql语句中特定功能的一种封装,这种封装可以大大简化外围调用语句的复杂程度. 首先以表emp ...

  4. js 作用域,变量提升

    先看下面一段代码: 代码执行的结果是: 1st alert : a = 0 2nd alert : a = undefined 5th alert : a = 0 3rd alert : a = 3 ...

  5. Struts2 全局拦截器、result 的实现

    定义一个可以继承的包,在这个包里面写入自己常用的拦截器,于是就实现了全局拦截器的实现. 现在,我们定义一个专门用来继承的包: <!--专门提供前台继承的包--> <package n ...

  6. c++ RTTI(runtime type info)

    RTTI(Run-Time Type Information,通过运行时类型信息)程序能够使用基类的指针或引用来检查这些指针或引用所指的对象的实际派生类型. RTTI提供了以下两个非常有用的操作符: ...

  7. android 通过eclipse混淆代码 打包 + proguard 总结

    android应用程序的混淆打包 1 . 在工程文件project.properties中加入下proguard.config=proguard.cfg , 如下所示: target=android- ...

  8. Nginx windows下搭建过程

    内容列表: 简要介绍 下载安装 配置测试 一.简要介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP ...

  9. eclipse中 com.sun.image.codec.jpeg.JPEGCodec 无法编译通过问题

    在Eclipse中处理图片,需要引入两个包:import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEG ...

  10. 【转】单独编译android framework模块出现的问题

    原文网址:http://blog.csdn.net/leonan/article/details/8629561 全编andorid后,单独修改编译一个framwork模块,make snod会有如下 ...