Xtu data structure H. City horizon

Source: Internet
Author: User
H. City horizontime limit: 2000 msmemory limit: 65536kb64-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 silhouettes formed by the rectangular buildings.

The entire horizon is represented by a number lineN(1 ≤N≤ 40,000) buildings. BuildingI'S silhouette has a base that spans locationsAIThroughBiAlong the horizon (1 ≤AI<Bi≤ 1,000,000,000) and has heightHi(1 ≤Hi≤ 1,000,000,000). determine the area, in square units, of the aggregate silhouette formed by allNBuildings.

Inputline 1: A single INTEGER: N
Lines 2 .. N+ 1: input line I+ 1 describes Building IWith three space-separated integers: AI, Bi, And HiOutputline 1: The total area, in square units, of the silhouettes formed by all NBuildings sample input
42 5 19 10 46 8 24 6 3
Sample output
16

Problem solving: reading the report on the question is still learning about the gray machine of discretization. However, this part of the line segment tree is actually very easy, but with the discretization operation, eggache!


1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <cstdlib> 5 # include <vector> 6 # include <climits> 7 # include <algorithm> 8 # include <cmath> 9 # define ll long long10 # define INF 0x3f3f11 using namespace STD; 12 const int maxn = 40010; 13 struct building {14 LL a, B, H; 15} BB [maxn]; 16 struct node {17 int lt, RT, h; 18} tree [maxn <3]; 19 ll p [maxn <1], n, ans; 20 int CNT; 21 bool CMP (Co NST Building & X, const Building & Y) {22 return X. h <Y. h; 23} 24 void build (INT lt, int RT, int v) {25 tree [v]. lt = lt; 26 tree [v]. RT = RT; 27 tree [v]. H = 0; 28 If (RT-Lt = 1) return; 29 int mid = (LT + RT)> 1; 30 build (LT, mid, v <1); 31 build (MID, RT, v <1 | 1); 32} 33 void Update (INT lt, int RT, int Val, int V) {34 if (tree [v]. lt = lT & tree [v]. RT = RT) {35 Tree [v]. H = val; 36 return; 37} 38 If (tree [v]. h> 0) {39 tree [v <<1]. H = tree [v <1 | 1]. H = tree [v]. h; 40 tree [v]. H = 0; 41} 42 int mid = (tree [v]. lt + tree [v]. RT)> 1; 43 If (RT <= mid) Update (LT, RT, Val, v <1); 44 else if (LT> = mid) update (LT, RT, Val, v <1 | 1); 45 else {46 Update (LT, mid, Val, v <1); 47 Update (MID, RT, Val, v <1 | 1); 48} 49} 50 void query (INT v) {51 if (tree [v]. h> 0) {52 ans + = (LL) tree [v]. H * (p [tree [v]. RT-1]-P [tree [v]. lt-1]); 53 return; 54} 55 if (tree [v]. RT-tree [v]. lt = 1) return; 56 query (v <1); 57 query (v <1 | 1); 58} 59 int bsearch (INT lt, int RT, int Val) {60 while (LT <= RT) {61 int mid = (LT + RT)> 1; 62 if (P [Mid] = Val) 63 return mid + 1; 64 else if (Val <p [Mid]) 65 RT = mid-1; 66 else lT = Mid + 1; 67} 68 return 0; 69} 70 int main () {71 int I, j, R; 72 scanf ("% LLD", & N); 73 for (I = 0; I <N; I ++) {74 scanf ("% d", & BB [I]. a, & BB [I]. b, & BB [I]. h); 75 p [CNT ++] = BB [I]. a; 76 P [CNT ++] = B B [I]. B; 77} 78 sort (p, p + CNT); // The sorting is to use binary search. 79 sort (BB, BB + N, CMP); 80 build (1, n <1, 1); 81 r = (n <1)-1; 82 for (I = 0; I <n; I ++) {83 int lT = bsearch (0, cnt-1, BB [I]. a); 84 int RT = bsearch (0, cnt-1, BB [I]. b); 85 Update (LT, RT, BB [I]. h, 1); 86} 87 ans = 0; 88 query (1); 89 printf ("% LLD", ANS); 90 return 0; 91}
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.