bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)

P2061 [USACO07OPEN]城市的地平线City Horizon

扫描线

扫描线简化版

流程(本题为例):

把一个矩形用两条线段(底端点的坐标,向上长度,添加$or$删除)表示,按横坐标排序

$upd:$本题的底端点坐标简化为$(x,0)$

蓝后对纵坐标建一棵线段树(本题需要对高度进行离散化)。

每次对线段树进行覆盖$or$删除区间操作,顺便统计一下$k=$有多少点被覆盖到

而两次(线段)操作之间的长度为$r=x_{i}-x_{i-1}$

于是两条线段之间被覆盖的面积即为$k*r$

(某退役选手又一次省出了宝贵的1.5h)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 #define N 40010
 7 struct line{
 8     int l,h,f;// l:横坐标 h:向上高度 f:添加/删除
 9     line(){}
10     line(int A,int B,int C):
11         l(A),h(B),f(C){}
12     bool operator < (const line &tmp) const{
13         return l<tmp.l;
14     }
15 }a[N<<1];
16 int x[N],n,cnt,tn,sum[N<<2],tag[N<<2],res;
17 long long ans;
18 #define lc o<<1
19 #define rc o<<1|1
20 #define mid l+((r-l)>>1)
21 void upd(int o,int l,int r,line e){
22     if(l>=r) return; //左端点在本题中简化成0,下同
23     if(x[r]<=e.h) tag[o]+=e.f;//覆盖层数增加/减少
24     else{
25         upd(lc,l,mid,e);
26         if(e.h>x[mid]) upd(rc,mid,r,e);//注意mid~mid+1的区间不可被忽略
27     }sum[o]= tag[o]? x[r]-x[l]:sum[lc]+sum[rc];//是否被完全覆盖
28 }
29 int main(){
30     scanf("%d",&n); int q1,q2,q3;
31     for(int i=1;i<=n;++i){
32         scanf("%d%d%d",&q1,&q2,&q3);
33         a[++cnt]=line(q1,q3,1);
34         a[++cnt]=line(q2,q3,-1);//一个矩形用两条线段表示
35         x[i+1]=q3;//存横坐标用于离散化
36     }sort(a+1,a+cnt+1);//(线段)操作按横坐标排序
37     sort(x+1,x+n+2);x[0]=-1;//注意要加上坐标(0,0)
38     for(int i=1;i<=n+1;++i)
39         if(x[i]!=x[i-1]) x[++tn]=x[i];//离散化
40     upd(1,1,tn,a[1]);
41     for(int i=2;i<=cnt;++i){
42         ans+=1ll*sum[1]*(a[i].l-a[i-1].l);//累计两条线段间的面积
43         upd(1,1,tn,a[i]);
44     }printf("%lld",ans);
45     return 0;
46 }

原文地址:https://www.cnblogs.com/kafuuchino/p/10023529.html

时间: 2024-08-27 07:57:11

bzoj1645 / P2061 [USACO07OPEN]城市的地平线City Horizon(扫描线)的相关文章

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

1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 315  Solved: 157[Submit][Status] Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe t

POJ 3277 City Horizon

City Horizon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18555   Accepted: 5114 Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouette

xtu数据结构 H. City Horizon

H. City Horizon Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java class name: Main Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silh

POJ 3277 City Horizon(线段树+扫描线+离散化)

题目地址:POJ 3277 水题..稍微处理一下然后用求面积并的方法求即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <queue> #include <

离散化+线段树 POJ 3277 City Horizon

POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 5077 Description Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silho

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

题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不会出错, 所有的区间都能列出来,只是在查找的时候费点事. 给的矩形相当于在同一水平线上的,也就是y1坐标相当于为0,其他的就和 poj 1151 Atlantis 差不多了. 我写的思路是按照矩形面积并的思路写的: 但是还有另一种方法也是挺简单的,就是把给的矩形按照高从小到大排序,然后依次插入线段树

[POJ3277]City Horizon

试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings. The entire horizon is represented by a number line with N (1 ≤ N

BZOJ1645 [Usaco2007 Open]City Horizon 城市地平线

本来想打线段树的说... 就是把坐标离散化了,然后区间最大求和即可... 后来觉得有点烦的说(silver题就要线段树...),于是看了下usaco的题解,发现了个高端的东西:善用STL里的容器和迭代器就可以了. 以下就是高端程序: 1 /************************************************************** 2 Problem: 1645 3 User: rausen 4 Language: C++ 5 Result: Accepted 6

BZOJ_1654_[Usaco2007 Open]City Horizon 城市地平线_扫描线

Description N个矩形块,交求面积并. Input * Line 1: A single integer: N * Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: A_i, B_i, and H_i Output * Line 1: The total area, in square units, of the silhouettes formed by all