标题效果:

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



解题思路:矩形面积并。



以下是代码:

#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. WebApi学习总结系列第四篇(路由系统)

    由于工作的原因,断断续续终于看完了<ASP.NET Web API 2 框架揭秘>第二章关于WebApi的路由系统的知识. 路由系统是请求消息进入Asp.net WebApi的第一道屏障, ...

  2. python相关的工具

    在使用python的时候,发现Adaconda工具包真的很不错,里面集合了很多的工具,并且,自带了很多的python常用模块 另外,PyCharm编辑器也是不错的,界面清晰,可以实现数据的可视化

  3. 最常用的动态sql语句梳理——分享给使用Mybatis的小伙伴们!

    公司项目中一直使用Mybatis作为持久层框架,自然,动态sql写得也比较多了,最常见的莫过于在查询语句中使用if标签来动态地改变过滤条件了.Mybatis的强大特性之一便是它的动态sql,免除了拼接 ...

  4. bzoj 3545&&3551: [ONTAK2010]Peaks &&加强版 平衡树&&并查集合并树&&主席树

    3545: [ONTAK2010]Peaks Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 635  Solved: 177[Submit][Stat ...

  5. 提高matlab运行速度和节省空间的心得

    提高matlab运行速度和节省空间的心得 首先推荐使用matlab 2006a版本,该版本优点很多(不过有一个小bug,就是通过GUI自动生成的m文件居然一大堆warning,希望在已经发布了的200 ...

  6. js 作用域,变量提升

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

  7. 有关AVR的介绍

    Atmel扩展AVR MCU系列 优化内存.连接性.集成性和超低功耗 http://avr.eefocus.com/article/12-03/833141332293957.html   AVR单片 ...

  8. SparkSQL配置和使用初探

    1.环境 OS:Red Hat Enterprise Linux Server release 6.4 (Santiago) Hadoop:Hadoop 2.4.1 Hive:0.11.0 JDK:1 ...

  9. java多线程 ReentrantLock

    本章对ReentrantLock包进行基本介绍,这一章主要对ReentrantLock进行概括性的介绍,内容包括:ReentrantLock介绍ReentrantLock函数列表ReentrantLo ...

  10. ftp 匿名访问设置

    为了让ftp可以匿名访问,需要设置/etc/vsftp.conf 的 anonymous_enable=YES. 当然仅仅是这样,还是不可以的,会出现错误: vsftpd: refusing to r ...