SGU 319. Kalevich Strikes Back (线段树)

Time limit per test: 0.5 second(s)
Memory limit: 65536 kilobytes

input: standard
output: standard

And yet again the Berland community can see that talent is always multi-sided. The talent is always seeking new ways of self-expression. This time genius Kalevich amazed everybody with his new painting “Rectangular Frames”. The masterpiece is full of impenetrable meaning and hidden themes.

Narrow black rectangular frames are drawn on a white rectangular canvas. No two frames have a common point. Sides of each frame are parallel to the sides of the canvas.

The painting is very big and the reduced replica cannot pass the idea of the original drawing. That’s why Kalevich requested that the simplified versions of the masterpiece should be used as replicas. The simplified version is the sequence of the areas of all facets of the original. A facet is a connected enclosed white area within the painting. The areas in the sequence should be written in the non-decreasing order.

Input

The first line of the input contains an integer N(1 ≤ N≤ 60000) — the number of the frames on the drawing. The second line contains integer numbers W_and _H(1 ≤ W, H≤ 108 ).

Let’s introduce the Cartesian coordinate system in such a way that the left bottom corner of the canvas has (0, 0) coordinate and the right top corner has the (W, H) coordinate. The sides of the canvas are parallel to the axes.

The following N_lines contain the description of the frames. Each description is composed of the coordinates of the two opposite corners of the corresponding frame _x_1 , _y_1 , _x_2 , _y_2 (1 ≤ _x_1 , _x_2 < _W; 1 ≤ y_1 , _y_2 < _H; _x_1 != _x_2 , _y_1 != _y_2 ). All coordinates are integers. No two frames have a common point.

Output

Write the desired sequence to the output.

Example(s)

sample input
sample output
1
3 3
2 1 1 2
1 8

矩形不相交,可能会内含。

使用线段树的扫描线,搞出保含关系,然后dfs一遍。

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2014/5/1 16:10:22
  4 File Name     :E:\2014ACM\专题学习\数据结构\线段树\SGU319.cpp
  5 ************************************************ */
  6
  7 #include 
  8 #include <string.h>
  9 #include 
 10 #include 
 11 #include 
 12 #include 
 13 #include <set>
 14 #include 
 15 #include <string>
 16 #include 
 17 #include 
 18 #include 
 19 using namespace std;
 20 const int MAXN = 120010;
 21 struct Node
 22 {
 23     int l,r;
 24     int ll,rr;//实际的左右区间
 25     int c;//覆盖标记,懒惰标记
 26 }segTree[MAXN*3];
 27 int x[MAXN];
 28 struct Line
 29 {
 30     int x1,x2,y;
 31     int id;
 32     Line(){}
 33     Line(int _x1,int _x2,int _y,int _id)
 34     {
 35         x1 = _x1;
 36         x2 = _x2;
 37         y = _y;
 38         id = _id;
 39     }
 40 }line[MAXN];
 41 bool cmp(Line a,Line b)
 42 {
 43     return a.y < b.y;
 44 }
 45 void push_down(int r)
 46 {
 47     if(segTree[r].l + 1 == segTree[r].r)return;
 48     if(segTree[r].c != -1)
 49     {
 50         segTree[r<<1].c = segTree[(r<<1)|1].c = segTree[r].c;
 51         segTree[r].c = -1;
 52     }
 53 }
 54 void build(int i,int l,int r)
 55 {
 56     segTree[i].l = l;
 57     segTree[i].r = r;
 58     segTree[i].ll = x[l];
 59     segTree[i].rr = x[r];
 60     segTree[i].c = 0;
 61     if(l+1 == r)return;
 62     int mid = (l+r)/2;
 63     build(i<<1,l,mid);
 64     build((i<<1)|1,mid,r);
 65 }
 66 int pre[MAXN];
 67 void Update(int i,Line e)
 68 {
 69     if(segTree[i].ll == e.x1 && segTree[i].rr == e.x2)
 70     {
 71         if(e.id > 0)
 72             segTree[i].c = e.id;
 73         else segTree[i].c = pre[-e.id];
 74         return;
 75     }
 76     push_down(i);
 77     if(e.x2 1].rr)Update(i<<1,e);
 78     else if(e.x1 >= segTree[(i<<1)|1].ll)Update((i<<1)|1,e);
 79     else
 80     {
 81         Line tmp = e;
 82         tmp.x2 = segTree[i<<1].rr;
 83         Update(i<<1,tmp);
 84         tmp = e;
 85         tmp.x1 = segTree[(i<<1)|1].ll;
 86         Update((i<<1)|1,tmp);
 87     }
 88 }
 89 int query(int i,Line e)
 90 {
 91     if(segTree[i].c != -1)
 92         return segTree[i].c;
 93     if(e.x2 1].rr)return query(i<<1,e);
 94     else if(e.x1 >= segTree[(i<<1)|1].ll)return query((i<<1)|1,e);
 95     else
 96     {
 97         e.x2 = segTree[i<<1].rr;
 98         return query(i<<1,e);
 99     }
100 }
101 long long area[MAXN];
102 vector<int>vec[MAXN];
103 void dfs(int u)
104 {
105     int sz = vec[u].size();
106     for(int i = 0;i < sz;i++)
107     {
108         int v = vec[u][i];
109         area[u] -= area[v];
110         dfs(v);
111     }
112 }
113
114 int main()
115 {
116     //freopen("in.txt","r",stdin);
117     //freopen("out.txt","w",stdout);
118     int n;
119     int w,h;
120     while(scanf("%d",&n) == 1)
121     {
122         scanf("%d%d",&w,&h);
123         area[0] = (long long)w*h;
124         int x1,x2,y1,y2;
125         int tot = 0;
126         for(int i = 1;i )
127         {
128             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
129             if(x1 > x2)swap(x1,x2);
130             if(y1 > y2)swap(y1,y2);
131             area[i] = (long long)(x2-x1)*(y2-y1);
132             line[tot] = Line(x1,x2,y1,i);
133             x[tot++] = x1;
134             line[tot] = Line(x1,x2,y2,-i);
135             x[tot++] = x2;
136         }
137         sort(x,x+tot);
138         tot = unique(x,x+tot) - x;
139         build(1,0,tot-1);
140         sort(line,line+2*n,cmp);
141         for(int i = 0;i )
142             vec[i].clear();
143         for(int i = 0;i < 2*n;i++)
144         {
145             if(line[i].id > 0)
146             {
147                 pre[line[i].id] = query(1,line[i]);
148                 vec[pre[line[i].id]].push_back(line[i].id);
149             }
150             Update(1,line[i]);
151         }
152         dfs(0);
153         sort(area,area+n+1);
154         for(int i = 0;i )
155         {
156             printf("%I64d",area[i]);
157             if(i < n)printf(" ");
158             else printf("\n");
159         }
160     }
161     return 0;
162 }

Original: https://www.cnblogs.com/kuangbin/p/3703850.html
Author: kuangbin
Title: SGU 319. Kalevich Strikes Back (线段树)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/546653/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 分布式Session如何存储

    使用Tomcat+Redis 我们可以在Tomcat的配置文件中进行设置,设置成功之后,Tomcat会将Session存储到Redis中,这样我们访问不通的Tomcat时,可以保证…

    技术杂谈 2023年6月21日
    081
  • SpringBoot中通过AOP整合日志文件

    1.SpringBoot中通过AOP整合日志文件 1. 导入相关的依赖 org.springframework.boot spring-boot-starter org.sprin…

    技术杂谈 2023年6月21日
    071
  • commit –amend

    为了节省时间,这个教程使用现有的历史记录作为本地数据库。 从这里下载 我们将修改最近一次的提交。 首先进入stepup-tutorial/tutorial1目录。本地端的历史记录状…

    技术杂谈 2023年5月31日
    087
  • Salt (cryptography) 密码加盐

    引子 最近有个虚拟练习项目,涉及到系统安全保障的设计,于是对安全保障这块做了一些更深入的了解。发现了很多有趣的东西,开阔了眼界。中间查了一些资料,于是我打算重新整理,用更加循序渐进…

    技术杂谈 2023年5月31日
    0108
  • 离散化

    3 -1 2 -2 这个数列有 5个逆序对 4 2 3 1 也是五个 我们把最小的-2视作1 第二的-1看做2 … 法一(推荐): 结构体保存数组num 和它在原数组里…

    技术杂谈 2023年6月21日
    089
  • 八、变量与常量

    一、变量 1.1、变量的基本概念 Java是一种强类型语言,每个变量都必须声明其类型。Java变量是程序中最基本的存储单元,其要素包括变量名、变量类型和作用域。 type varN…

    技术杂谈 2023年6月21日
    085
  • MySQL 常用命令手册 增删改查大法

    一、数据库操作 创建数据库 语法: CREATE DATABASE database_name; 删除数据库 删除数据库务必谨慎!因为执行删除命令后,所有数据将消失。 语法: DR…

    技术杂谈 2023年7月25日
    060
  • INNO安装卸载自动结束进程插件使用

    [Code] //安装前判断是否有进程正在运行,istask.dll文件与打包的exe文件一起function RunTask(FileName: string; bFullpat…

    技术杂谈 2023年5月31日
    0103
  • ArcGIS Pro SDK二次开发打开属性表

    using System; using System.Collections.Generic; using System.Linq; using System.Text; usin…

    技术杂谈 2023年5月30日
    084
  • 多级缓存-Centos安装OpenResty

    1)安装开发库首先要安装OpenResty的依赖开发库,执行命令: 2)安装OpenResty仓库你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未…

    技术杂谈 2023年5月31日
    054
  • Jira Database schema

    https://developer.atlassian.com/server/jira/platform/database-schema/ https://developer.at…

    技术杂谈 2023年5月30日
    085
  • Game Engine Architecture 1

    【 Game Engine Architecture 1】 1、This book is really just the beginning of a fascinating an…

    技术杂谈 2023年5月31日
    081
  • 堆栈

    目录: 9、【剑指Offer学习】【面试题09:用两个栈实现队列】 30、【剑指Offer学习】【面试题30:包含min函数的栈】 31、【剑指Offer学习】【面试题31:栈的压…

    技术杂谈 2023年6月21日
    076
  • RocketMQ系列二:RocketMQ监控/告警一站式搭建应用

    ​实验简介研究RocketMQ的同学都知道,RocketMQ的生态目前并不是很完善,包括官方的文档资料也有限,官方的Console存在一些Bug,页面的样式有的也有问题,但是正是由…

    技术杂谈 2023年7月11日
    059
  • 只有程序员才能读懂的西游记

    一、我佛造经传极乐 话说我佛如来为度化天下苍生,有三藏真经,可劝人为善。 就如图中所示,真经所藏之处,在于云端。佛祖所管辖之下,有四个区域Region,称为四大部洲, 一是东胜神洲…

    技术杂谈 2023年6月22日
    061
  • QT Creator项目路径设置

    为了编译时输出的文件不那么混乱,需要对QT Creator的项目文件做一些设置,这里记录一下pro文件里面各个参数的用法 1、一些中间文件的生成路径的设置 MOC_DIR = te…

    技术杂谈 2023年7月11日
    0101
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球