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 enveloping polyline using the data on the sky-scrapers. The polyline’s properties are as follows:
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).
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.
2 3 0 2 4 1 3
6 0 0 0 3 1 3 1 4 3 4 3 0
5 3 -3 0 2 -1 1 4 2 4 2 3 7 3 6 8
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
题意:给出几个矩形的h,l,r这几个矩形重叠之后,有转折点,要求所有转折点
思路:由于数据比较大,要用到离散化,一开始疯狂RE到我要吐血,结果后来才知道必须加上文件输入输出才行。。。。
<pre name="code" class="cpp">#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue> #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm> using namespace std; #define ls 2*i #define rs 2*i+1 #define up(i,x,y) for(i=x;i<=y;i++) #define down(i,x,y) for(i=x;i>=y;i--) #define mem(a,x) memset(a,x,sizeof(a)) #define w(a) while(a) #define LL long long const double pi = acos(-1.0); #define N 100005 #define mod 19999997 const int INF = 0x3f3f3f3f; #define exp 1e-8 int n; vector<int> X; map<int,int> H; struct kode { int h,l,r; } s[N]; int MID(int a,int b) { return (a+b)/2; } struct node { int l,r,maxn; void fun(int h) { maxn = max(maxn,h); } } a[N*8]; int push_down(int i) { if(a[i].maxn) { a[ls].fun(a[i].maxn); a[rs].fun(a[i].maxn); a[i].maxn = 0; } } void init(int l,int r,int i) { a[i].l = l; a[i].r = r; a[i].maxn = 0; if(l+1!=r) { int mid = MID(l,r); init(l,mid,ls); init(mid,r,rs); } } void insert(int l,int r,int i,int h) { int ll = a[i].l,rr = a[i].r; if(l<=ll && rr<=r) a[i].fun(h); else { int mid = MID(ll,rr); push_down(i); if(l<mid) insert(l,r,ls,h); if(r>mid) insert(l,r,rs,h); } } int query(int l,int r,int i) { if(a[i].l+1 == a[i].r) return a[i].maxn; else { push_down(i); int mid = MID(a[i].l,a[i].r),maxn = 0;; if(l<mid) maxn = query(l,r,ls); else maxn = query(l,r,rs); return maxn; } } int main() { int i,j,k,h,l,r; freopen ( "input.txt" , "r" , stdin ) ; freopen ( "output.txt" , "w" , stdout ) ; scanf("%d",&n); H.clear(); X.clear(); up(i,0,n-1) { scanf("%d%d%d",&s[i].h,&s[i].l,&s[i].r); X.push_back(s[i].l); X.push_back(s[i].r); } sort(X.begin(),X.end()); X.erase(unique(X.begin(),X.end()),X.end());//去重 int len = (int)X.size(); up(i,0,len-1)//离散化 H[X[i]] = i; init(0,len-1,1); up(i,0,n-1) insert(H[s[i].l],H[s[i].r],1,s[i].h); int y = 0; vector<pair<int,int> > ans; up(i,0,len-2) { int tem = query(i,i+1,1); if(tem==y) continue; ans.push_back(make_pair(X[i],y)); ans.push_back(make_pair(X[i],tem)); y = tem; } if(y!=0) { ans.push_back(make_pair(X[len-1],y)); ans.push_back(make_pair(X[len-1],0)); } printf("%d\n",ans.size()); up(i,0,ans.size()-1) { printf("%d %d\n",ans[i].first,ans[i].second); } return 0; }