POJ 3277 City Horizon
标题效果:
每间房子的长度给出阴影(在间隔代表)而高度,求阴影总面积。
解题思路:矩形面积并。
以下是代码:
#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的更多相关文章
- 离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- POJ 3277 City Horizon(扫描线+线段树)
题目链接 类似求面积并..2Y.. #include <cstdio> #include <cstring> #include <string> #include ...
- [POJ] 3277 .City Horizon(离散+线段树)
来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...
- POJ 3277 City Horizon(叶子节点为[a,a+1)的线段树+离散化)
网上还有用unique函数和lowerbound函数离散的方法,可以百度搜下题解就有. 这里给出介绍unique函数的链接:http://www.cnblogs.com/zhangshu/archiv ...
- poj City Horizon (线段树+二分离散)
http://poj.org/problem?id=3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543
学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...
- [POJ3277]City Horizon
[POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
随机推荐
- JavaScript学习心得(三)
一 变量 var:变量声明 变量名 =:赋值 简单值类型 全局变量:编程的一般规则——应用程序应该只完成必须的最少功能,如果一个变量不是绝对必需,就不该是全局:全局变量对维护性能不利,因为需要一直维护 ...
- 局部视图(partial)
局部视图(partial) 原文:Partial Views作者:Steve Smith翻译:张海龙(jiechen).刘怡(AlexLEWIS)校对:许登洋(Seay).何镇汐.魏美娟(初见) AS ...
- MVC OR API的接口
MVC OR WEBAPI的接口安全 当我们开发一款App的时候,App需要跟后台服务进行通信获取或者提交数据.如果我们没有完善的安全机制则很容易被别用心的人伪造请求而篡改数据.所以我们需要使用某种安 ...
- mmap内存映射复习
c语言初学时,比较常见的一个习题就是实现cp. 使用c库实现的cp就不赘述了. 最近工作用到内存映射,就拿来练下手,复习一下mmap的用法. 很简单,将目标文件和源文件映射到内存,然后使用memcpy ...
- 关于.NET的配置文件
无论是exe文件还是dll文件,都可以添加App.config文件,里面设置配置信息.比如<appSettings></appSettings>之间,可以加入Key-Value ...
- sqlserver 学习
http://www.cnblogs.com/CareySon/category/411344.html SQL Server查找的最小单位实际上是页.也就是说即使你只查找一行很小的数据,SQL Se ...
- NEURAL NETWORKS, PART 2: THE NEURON
NEURAL NETWORKS, PART 2: THE NEURON A neuron is a very basic classifier. It takes a number of input ...
- Features of Spring Web MVC
21.1.1 Features of Spring Web MVC Spring Web Flow Spring Web Flow (SWF) aims to be the best solution ...
- Storm学习笔记
1.如何让一个spout并行读取多个流? 方法:任何spout.bolts组件都可以访问TopologyContext.利用这个特性可以让Spouts的实例之间划分流. 示例:获取到storm集群sp ...
- poj2954
水题,先用叉积求三角形面积然后求三边上的点(由公约数上点)a然后用pick定理S=a+b/2-1就可以求出内部的点数了 var x,y,xx,yy,a1,a2,a3,x1,x2,x3,y1,y2,y3 ...