Codeforces35E(扫描线)
E. Parade
No Great Victory anniversary in Berland has ever passed without the war parade. This year is not an exception. That’s why the preparations are on in full strength. Tanks are building a line, artillery mounts are ready to fire, soldiers are marching on the main square... And the air forces general Mr. Generalov is in trouble again. This year a lot of sky-scrapers have been built which makes it difficult for the airplanes to fly above the city. It was decided that the planes should fly strictly from south to north. Moreover, there must be no sky scraper on a plane’s route, otherwise the anniversary will become a tragedy. The Ministry of Building gave the data on n sky scrapers (the rest of the buildings are rather small and will not be a problem to the planes). When looking at the city from south to north as a geometrical plane, the i-th building is a rectangle of height hi. Its westernmost point has the x-coordinate of li and the easternmost — of ri. The terrain of the area is plain so that all the buildings stand on one level. Your task as the Ministry of Defence’s head programmer is to find an envelopingpolyline using the data on the sky-scrapers. The polyline’s properties are as follows:
- If you look at the city from south to north as a plane, then any part of any building will be inside or on the boarder of the area that the polyline encloses together with the land surface.
- The polyline starts and ends on the land level, i.e. at the height equal to 0.
- The segments of the polyline are parallel to the coordinate axes, i.e. they can only be vertical or horizontal.
- The polyline’s vertices should have integer coordinates.
- If you look at the city from south to north the polyline (together with the land surface) must enclose the minimum possible area.
- The polyline must have the smallest length among all the polylines, enclosing the minimum possible area with the land.
- The consecutive segments of the polyline must be perpendicular.
Input
The first input line contains integer n (1 ≤ n ≤ 100000). Then follow n lines, each containing three integers hi, li, ri(1 ≤ hi ≤ 109, - 109 ≤ li < ri ≤ 109).
Output
In the first line output integer m — amount of vertices of the enveloping polyline. The next m lines should contain 2 integers each — the position and the height of the polyline’s vertex. Output the coordinates of each vertex in the order of traversing the polyline from west to east. Remember that the first and the last vertices of the polyline should have the height of 0.
Examples
input
2
3 0 2
4 1 3
output
6
0 0
0 3
1 3
1 4
3 4
3 0
input
5
3 -3 0
2 -1 1
4 2 4
2 3 7
3 6 8
output
14
-3 0
-3 3
0 3
0 2
1 2
1 0
2 0
2 4
4 4
4 2
6 2
6 3
8 3
8 0
1 //2017-08-11 2 #include <iostream> 3 #include <cstdio> 4 #include <algorithm> 5 #include <cstring> 6 #include <set> 7 #include <vector> 8 9 using namespace std; 10 11 int n; 12 multiset<int> ms; 13 vector< pair<int, int> > line, ans; 14 15 void init() 16 { 17 ms.clear(); 18 line.clear(); 19 ans.clear(); 20 } 21 22 int main() 23 { 24 freopen("input.txt", "r", stdin); 25 freopen("output.txt", "w", stdout); 26 while(scanf("%d", &n)!=EOF) 27 { 28 init(); 29 int l, r, h; 30 for(int i = 0; i < n; i++){ 31 scanf("%d%d%d", &h, &l, &r); 32 line.push_back(make_pair(l, h)); 33 line.push_back(make_pair(r, -h)); 34 } 35 sort(line.begin(), line.end()); 36 vector< pair<int, int> >::const_iterator it, iter; 37 it = line.begin(); 38 int height = 0; 39 ms.insert(height); 40 while(it != line.end()){ 41 iter = it; 42 do{ 43 if(iter->second > 0) 44 ms.insert(iter->second); 45 else 46 ms.erase(ms.find(-iter->second)); 47 iter++; 48 }while(iter != line.end() && iter->first == it->first); 49 if(*ms.rbegin() != height){ 50 ans.push_back(make_pair(it->first, height)); 51 ans.push_back(make_pair(it->first, height = *ms.rbegin())); 52 } 53 it = iter; 54 } 55 printf("%d\n", (int)ans.size()); 56 for(it = ans.begin(); it != ans.end(); it++){ 57 printf("%d %d\n", it->first, it->second); 58 } 59 } 60 }