标题效果:

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



解题思路:矩形面积并。



以下是代码:

#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. 【pyhton】短路逻辑

    编程语言常用的逻辑if a and b:#如果a是false,那么跳过b的判断,结果直接falseif a or b:#如果a为true,那么跳过b的判断,直接true

  2. Delphi中快捷键的使用

    CTRL+SHIFT+↑(↓) 在过程.函数.事件内部, 可跳跃到相应的过程.函数.事件的定义(在INTERFACE和IMPLEMENTATION之间来回切换)CTRL+J (弹出DELPHI语句提示 ...

  3. UFLDL教程(四)之Softmax回归

    关于Andrew Ng的machine learning课程中,有一章专门讲解逻辑回归(Logistic回归),具体课程笔记见另一篇文章. 下面,对Logistic回归做一个简单的小结: 给定一个待分 ...

  4. Debug和Release之本质区别

    转自Debug和Release之本质区别 Debug 和 Release 编译方式的本质区别 Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发 ...

  5. 减少 WAF 漏报的 8 种方法 !

    近十年来,WAF 已经逐渐发展成熟,被软件行业接受并成为无数企业保护应用的不二选择.很多大型企业甚至相继亲自设计或通过并购涉足其中,在这个硕大的市场里逐鹿竞争,同时也推动了应用层防火墙的技术演进. 与 ...

  6. 在DJANGO的类视图中实现登陆要求和权限保护

    以前接触的是基于函数的保护,网上材料比较多. 但基于类视图的很少. 补上! Decorating class-based views 装饰类视图 对于类视图的扩展并不局限于使用mixin.你也可以使用 ...

  7. Java接口修饰符详解

    接口就是提供一种统一的”协议”,而接口中的属性也属于“协议”中的成员.它们是公共的,静态的,最终的常量.相当于全局常量.抽象类是不“完全”的类,相当于是接口和具体类的一个中间层.即满足接口的抽象,也满 ...

  8. [LeetCode#201] Bitwise AND of Numbers Range

    Problem: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of al ...

  9. Y86处理器的各个阶段

    0x00e:30f480000000        |              irmovl $128,%esp  实现所有Y86指令所需要的计算分为6个基本阶段:取值.译码.执行.访存.写回和更新 ...

  10. ubuntu制作usb启动盘

    准备: u盘 iso镜像文件--ubuntu-12.04.2-desktop-amd64.iso 烧盘软件--unetbootin-linux-583 步骤: 格式化u盘 查看u盘信息 #mount/ ...