本文共 3344 字,大约阅读时间需要 11 分钟。
Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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 silhouettes formed by the rectangular buildings.
The entire horizon is represented by a number line with N (1 ≤ N ≤ 40,000) buildings. Building i's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
Input
Output
Sample Input
42 5 19 10 46 8 24 6 3
Sample Output
16
Hint
Source
解题思路
线段树+离散化 因为它们的宽度太大,无法存储所以先离散,然后再根据按从低到高的顺序更新线段树。
AC代码1:
#include#include #include #include using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'#define lson root<<1#define rson root<<1|1typedef long long ll;const int MAXN = 4e4+5;ll sum[MAXN<<3];int add[MAXN<<3],sl[MAXN<<3],sr[MAXN<<3];vector v,vec;struct Node{ int a,b,h;};Node node[MAXN];bool compare(Node a,Node b){ return a.h >1; build(lson,l,mid); build(rson,mid,r);}void update(int root,int l,int r,int L,int R,int num){ if(l>=L && r<=R) { sum[root]=(ll)num*(sr[root]-sl[root]); add[root]=num; return; } if(l+1==r) return; push_down(root); int mid=(l+r)>>1; if(L<=mid) update(lson,l,mid,L,R,num); if(R>=mid) update(rson,mid,r,L,R,num); push_up(root);}int main(){ SIS; int n; cin >> n; for(int i=0;i > node[i].a >> node[i].b >> node[i].h; v.push_back(node[i].a); v.push_back(node[i].b); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); int len=v.size(); build(1,1,len); sort(node,node+n,compare); for(int i=0;i
AC代码2:
#include#include #include #include using namespace std;#define SIS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)#define endl '\n'#define lson root<<1#define rson root<<1|1typedef long long ll;const int MAXN = 4e4+5;ll sum[MAXN<<3];int add[MAXN<<3],sl[MAXN<<3],sr[MAXN<<3];vector v,vec;struct Node{ int a,b,h;};Node node[MAXN];bool compare(Node a,Node b){ return a.h >1; build(lson,l,mid); build(rson,mid,r);}void update(int root,int l,int r,int L,int R,int num){ if(L<=sl[root] && R>=sr[root]) { sum[root]=(ll)num*(sr[root]-sl[root]); add[root]=num; return; } if(l+1==r) return; push_down(root); int mid=(l+r)>>1; if(L<=sr[lson]) update(lson,l,mid,L,R,num); if(R>=sl[rson]) update(rson,mid,r,L,R,num); push_up(root);}int main(){ SIS; int n; cin >> n; for(int i=0;i > node[i].a >> node[i].b >> node[i].h; v.push_back(node[i].a); v.push_back(node[i].b); } sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end()); int len=v.size(); build(1,1,len); sort(node,node+n,compare); for(int i=0;i
转载地址:http://qiyof.baihongyu.com/