标题效果:

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



解题思路:矩形面积并。



以下是代码:

#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. QQ登录接口(第三方登录接口)

    CI框架 QQ接口(第三方登录接口PHP版) 本帖内容较多,大部分都是源码,要修改的地方只有一个,其他只要复制过去,就可以完美运行.本帖主要针对CI框架,不用下载SDK,按我下面的步骤,建文件,复制代 ...

  2. #Leet Code# Divide Two Integers

    描述:不使用 * / % 完成除法操作.O(n)复杂度会超时,需要O(lg(n))复杂度. 代码: class Solution: # @return an integer def dividePos ...

  3. Start an installation from GRUB

    Start an installation from GRUB Description This tip will show you how to start an installation for ...

  4. js 中文排序

    /** * 比较函数 * @param {Object} param1 要比较的参数1 * @param {Object} param2 要比较的参数2 * @return {Number} 如果pa ...

  5. C# Dispose Finalize

    比较值得参考的文档:http://www.jb51.net/article/37214.htm. .NET 的内存管理过程: 托管堆假设内存无限大,线性连续分配内存: 实际内存不够使用时,遍历托管堆对 ...

  6. sqlplus 打印很乱,而且很短就换行

    set linesize 可以解决 设置行打印的字符长度,set linesize 400解决

  7. ios7新特性3-Map Kit新特性

    Map Kit 框架 (MapKit.framework) 包含了大量的改进以及为基于地图的程序提供了新特性.利用地图显示位置信息的应用现在可以使用Maps这个程序用到的3D地图,包括控制程序控制视线 ...

  8. 当今流行的 React.js 适用于怎样的 Web App?

    外村 和仁(株式会社 ピクセルグリッド)  React.js是什么? React.js是Facebook开发的框架. http://facebook.github.io/react/ 官网上的描述是「 ...

  9. Android 对话框弹出位置和透明度

    在Android中我们经常会用AlertDialog来显示对话框.通过这个对话框是显示在屏幕中心的.但在某些程序中,要求对话框可以显示在不同的位置.例如,屏幕的上方或下方.要实现这种效果.就需要获得对 ...

  10. 【HDOJ】1823 Luck and Love

    二维线段树.wa了几次,不存在输出-1,而不再是一位小数. #include <cstdio> #include <cstring> #define MAXN 105 #def ...