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)

大家都在看

  • Spark学习(3)SparkSQL

    什么事sparkSQL Spark SQL是Spark用来处理结构化数据的一个模块,它提供了一个编程抽象叫做DataFrame并且作为分布式SQL查询引擎的作用, 它是将Spark…

    技术杂谈 2023年7月24日
    078
  • 经济

    《哈佛笔记》曼昆的《宏观经济学》《预测》 洪灏的微博 微信 公众号。 Original: https://www.cnblogs.com/rgqancy/p/15877178.ht…

    技术杂谈 2023年6月1日
    097
  • Codeforces1573B

    问题描述 给你两个数组,a数组里面是1 – 2n中的奇数任意顺序排列组成,b数组里面是1 – 2n中的奇数任意顺序排列组成。 问你最少需要多少次操作能让a的…

    技术杂谈 2023年7月24日
    059
  • CSS 埋点统计

    原文地址: https://my.oschina.net/u/1778933/blog/1608904 CSS 埋点统计 当一个网站或者 App 的规模达到一定程度,需要分析用户在…

    技术杂谈 2023年5月31日
    086
  • 稀疏数组详细讲解

    稀疏数组的应用场景 稀疏sparsearray数组 稀疏:从字面意思理解就是为了压缩重复冗余的数据 基本介绍: 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组…

    技术杂谈 2023年6月21日
    081
  • 上周热点回顾(7.4-7.10)

    热点随笔: · 一大波开源小抄来袭 (削微寒)· 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇] (Artech)· [开源精品] .NET Redis Cli…

    技术杂谈 2023年5月31日
    082
  • Win10系统的SurfacePro4如何重装系统-4 如何再次备份和还原系统

    还是进入到PE 环境,直接用GHOST ,Local-Partition-ToImage 即可创建C 盘新的备份 选择目标硬盘 选择要备份的分区 选择保存文件的路径(如果没有接键盘…

    技术杂谈 2023年5月31日
    0189
  • 05 Java中的输入、输出流

    输入输出流 内容概括: 存在java.io包中 所有输入流都是抽象类InputStream(字节输入流)和抽象类Reader(字符输入流)的子类。 所有输出流都是抽象类Output…

    技术杂谈 2023年6月21日
    081
  • 为在线数据库构建基于Kudu的实时数据同步

    Kudu 是 Cloudera 开源的新型列式存储系统,是 Apache Hadoop 生态圈的成员之一。它专门为了对快速变化的数据进行快速的分析,填补了以往Hadoop 存储层的…

    技术杂谈 2023年7月23日
    064
  • How To Write A Business Plan 如何撰写商业计划书 笔记

    一、计划书的结构 ■概述 ■简介 ■业务背景 ■产品 ■市场 ■运营 ■管理 ■提案■财务背景 ——目前的交易情况 ——财务预测 ■风险 ■结论 ■附录 二、概述 尽管概述出现在计…

    技术杂谈 2023年5月31日
    073
  • 计算机系统大作业:Hello的一生

    计算机系统 大作业 题 目 程序人生-Hello’s P2P 专 业 计算机科学与技术 学 号 班 级 学 生 江水为竭 指导教师 刘宏伟 计算机科学与技术学院 202…

    技术杂谈 2023年7月11日
    067
  • find -exec 与多个命令

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    技术杂谈 2023年5月31日
    084
  • dremio InfoSchemaScanCreator 参考调用链

    Press Q or Ctrl+C to abort. Affect(class count: 1 , method count: 2) cost in 260 ms, liste…

    技术杂谈 2023年5月30日
    090
  • Linux快速安装流量监控工具(实用版)

    前言: Linux流量监控工具,在此我推荐两种分别为: 1、nload(推荐)因为个人看着舒服点😂 2、iftop 以上两种任选其一即可,在此对两种都有介绍和安装教程,我写了,大家…

    技术杂谈 2023年6月21日
    0106
  • [转]Confluence Jira Issues Macro(Jira Issues宏)

    截图:带有Jira Issue宏的项目状态页面,显示必须在发布前解决的Issue。 连接Confluence和Jira 在您可以使用此宏之前,您的Confluence和Jira系统…

    技术杂谈 2023年5月30日
    0130
  • 部署-docker资源踩坑

    docker资源踩坑 博主在自己的电脑上,使用docker运行gitlab镜像的时候,发现docker命令失去了响应。但是根据网上的资料显示,gitlab最低配置只需要2核,4GB…

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