Codeforces35E:Parade(区间更新)

E. Parade
time limit per test
4 seconds
memory limit per test
64 megabytes
input
input.txt
output
output.txt

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:

  • 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.
Codeforces35E:Parade(区间更新)_第1张图片 Picture to the second sample test (the enveloping polyline is marked on the right).
Input

The first input line contains integer n (1 ≤ n ≤ 100000). Then follow n lines, each containing three integers hiliri (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.

Sample test(s)
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
 
    
题意:给出几个矩形的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;
}


 
   

你可能感兴趣的:(CF)