• POJ1177Picture(线段树+Y方向离散化+读取perimeterのX方向的扫描)


    时间限制: 
    2000ms
     
    内存限制: 
    65536kB
    描述
    A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wall. Their sides are all vertical or horizontal. Each rectangle can be partially or totally covered by the others. The length of the boundary of the union of all rectangles is called the perimeter.

    Write a program to calculate the perimeter. An example with 7 rectangles is shown in Figure 1.

    The corresponding boundary is the whole set of line segments drawn in Figure 2.

    The vertices of all rectangles have integer coordinates.
    输入
    Your program is to read from standard input. The first line contains the number of rectangles pasted on the wall. In each of the subsequent lines, one can find the integer coordinates of the lower left vertex and the upper right vertex of each rectangle. The values of those coordinates are given as ordered pairs consisting of an x-coordinate followed by a y-coordinate.

    0 <= number of rectangles < 5000
    All coordinates are in the range [-10000,10000] and any existing rectangle has a positive area.
    输出
    Your program is to write to standard output. The output must contain a single line with a non-negative integer which corresponds to the perimeter for the input rectangles.
    样例输入
    7
    -15 0 5 10
    -5 8 20 25
    15 -4 24 14
    0 -6 16 4
    2 15 10 22
    30 10 36 20
    34 0 40 16
    样例输出
    228

    #include"iostream"
    #include"algorithm"
    using namespace std;

    struct scanLine{
    int x;
    int y1,y2;
    int flag;//若该扫描线属于矩形的左边的竖边,如AB,则叫做入边,值为1
    //若属于矩形的右边的竖边,如CD,则叫做出边,值为0
    };
    struct Ynode{
    int Yleft,Yright,m;//m: 测度,即覆盖的区间长度,如[2,8]就为6(Y方向)、
    //m在update_m中更新!
    int count;//节点被覆盖次数
    int line,lbd,rbd;//line: 所包含线段的区间数量,如三条[1,2],[2,3],[4,5]线段被覆盖,则line=2(Y方向)
    //lbd,rbd: 左右端点是否被覆盖,用来辅助对line的计算
    //这些变量都在update_line中更新!
    };
    scanLine scanline[10000];//2*rectangle 条扫描线
    Ynode ynode[40000];//4*rectangle 个节点
    int y[10000];//保存所有的纵向坐标

    bool cmp(struct scanLine a,struct scanLine b){
    if(a.x==b.x)
    return a.flag>b.flag;//按照先入边后出边排列
    else
    return a.x<b.x;//按照x从低到高排列
    }

    void build(int l,int r,int i){//建立线段树树(初始count值为0,后期通过insert和remove操作count有变化)
    //采用数组保存建立线段树(l,r为Y方向离散出来的点),也可以利用指针
    ynode[i].Yleft=l;
    ynode[i].Yright=r;
    ynode[i].count=0;
    if(r-l>1){//递归建立线段树
    int m=(l+r)/2;
    build(l,m,2*i+1);
    build(m,r,2*i+2);
    }
    }
    void update_m(int i){//更新测度m
    if(ynode[i].count>0)//node[i]被覆盖
    ynode[i].m=y[ynode[i].Yright]-y[ynode[i].Yleft];
    else if(ynode[i].Yright-ynode[i].Yleft==1)//node[i]未被覆盖,到达叶节点
    ynode[i].m=0;
    else//node[i]被覆盖,未到达叶节点
    ynode[i].m=ynode[2*i+1].m+ynode[2*i+2].m;
    }
    //更新line
    void update_line(int i){
    if(ynode[i].count>0){//node[i]被覆盖
    ynode[i].lbd=1;
    ynode[i].rbd=1;
    ynode[i].line=1;
    }
    else if(ynode[i].Yright-ynode[i].Yleft==1){//node[i]未被覆盖,到达叶节点
    ynode[i].lbd=0;
    ynode[i].rbd=0;
    ynode[i].line=0;
    }
    else{//node[i]被覆盖,未到达叶节点
    ynode[i].lbd=ynode[2*i+1].lbd;
    ynode[i].rbd=ynode[2*i+2].rbd;
    //当左右端点都被覆盖,line减去1
    ynode[i].line=ynode[2*i+1].line+ynode[2*i+2].line-ynode[2*i+1].rbd*ynode[2*i+2].lbd;
    }
    }
    //注意,count在insert和remove中的变化分析参照文章“【转】线段树(segment tree)”
    void insert(int l,int r,int i){//此处l,r为离散化前Y方向的值
    //在这里要利用y[node[i]]取出node[i]离散化之前的值跟l和r进行比较
    if(y[ynode[i].Yleft]>=l&&y[ynode[i].Yright]<=r)//node[i]被[l,r]覆盖
    (ynode[i].count)++;
    else if(ynode[i].Yright-ynode[i].Yleft==1)//node[i]未被[l,r]覆盖,到达叶节点
    return;
    else{//node[i]未被[l,r]覆盖,未到达叶节点
    int m=(ynode[i].Yleft+ynode[i].Yright)/2;
    if(r<=y[m])
    insert(l,r,2*i+1);
    else if(l>=y[m])
    insert(l,r,2*i+2);
    else{
    insert(l,y[m],2*i+1);
    insert(y[m],r,2*i+2);
    }
    }
    update_m(i);
    update_line(i);
    }
    void remove(int l,int r,int i){//此处l,r为离散化前Y方向的值
    //在这里要利用y[node[i]]取出node[i]离散化之前的值跟l和r进行比较
    if(y[ynode[i].Yleft]>=l&&y[ynode[i].Yright]<=r)
    (ynode[i].count)--;
    else if(ynode[i].Yright-ynode[i].Yleft==1)
    return;
    else{
    int m=(ynode[i].Yleft+ynode[i].Yright)/2;
    if(r<=y[m])
    remove(l,r,2*i+1);
    else if(l>=y[m])
    remove(l,r,2*i+2);
    else{
    remove(l,y[m],2*i+1);
    remove(y[m],r,2*i+2);
    }
    }
    update_m(i);
    update_line(i);
    }
    /*
    1.将排序后的scan数组依次输入,执行插入线段insert函数(为入边)或者remove函数(为出边),同时更新m和line
    2.每扫描一次,就要计算一次周长perimeter,这里我们以图中的例子来讲解过程:
    首先是AB,它被插入线段树,perimeter = perimeter + |AB|;
    然后是EG,它被插入线段树,此时线段树的root节点的测度为|EG|的值,但由于之前加过|AB|,因而应该减去|AB|,其实就是减去|KL|,然后再加上line*2*|AK|,这里的line的值是未插入EG时线段树的根节点的line值。
    */
    int main()
    {
    int n;
    cin>>n;
    int x1,y1,x2,y2;
    int number=0;
    int nY;
    while(n--){
    cin>>x1>>y1>>x2>>y2;
    scanline[number].x=x1;
    scanline[number].y1=y1;
    scanline[number].y2=y2;
    scanline[number].flag=1;
    y[number++]=y1;

    scanline[number].x=x2;
    scanline[number].y1=y1;
    scanline[number].y2=y2;
    scanline[number].flag=0;
    y[number++]=y2;
    }
    sort(scanline,scanline+number,cmp);
    /*
    for(int i=0;i<number;i++)
    cout<<scanline[i].x<<" ";
    cout<<endl;
    */
    sort(y,y+number);//x和y的数量都是一样的
    nY=unique(y,y+number)-y;//unique返回重排后形成的非重复数组の最后一个元素位置

    build(0,nY-1,0);//离散化,建立线段树

    int perimeter=0;int last_m=0;int last_line=0;
    for(int i=0;i<number;i++){
    if(scanline[i].flag==1)//入边
    insert(scanline[i].y1,scanline[i].y2,0);
    else
    remove(scanline[i].y1,scanline[i].y2,0);

    perimeter+=abs(ynode[0].m-last_m);
    //cout<<"perimeter1: "<<perimeter<<endl;
    last_m=ynode[0].m;

    if(number>0)
    perimeter+=2*last_line*(scanline[i].x-scanline[i-1].x);
    //cout<<"perimeter2: "<<perimeter<<endl;
    last_line=ynode[0].line;//根节点0的line值,line的作用具有滞后性,需要下一次循环才能体现出来
    }
    cout<<perimeter<<endl;
    //system("pause");
    }

  • 相关阅读:
    jq02--基础函数
    jq01--概述
    js06--函数库jq与prototype
    eclipse启动时 failed to create the java virtual machine 解决办法
    将博客搬至CSDN
    eclipse.ini 修改默认编码为 UTF-8
    Elicpse使用技巧-打开选中文件文件夹或者包的当前目录
    eclipse换了高版本的maven插件后报错:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project
    python进行数据清理之pandas中的drop用法
    如何用Python实现常见机器学习算法-4
  • 原文地址:https://www.cnblogs.com/lzhitian/p/2604301.html
Copyright © 2020-2023  润新知