SGU 319. Kalevich Strikes Back (线段树)
319. Kalevich Strikes Back
Memory limit: 65536 kilobytes
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.
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 x1, y1, x2, y2(1 ≤ x1, x2 < W; 1 ≤ y1, y2 < H; x1 != x2, y1 != y2). All coordinates are integers. No two frames have a common point.
Write the desired sequence to the output.
sample input |
sample output |
1 |
1 8 |
矩形不相交,可能会内含。
使用线段树的扫描线,搞出保含关系,然后dfs一遍。
/* ***********************************************
Author :kuangbin
Created Time :2014/5/1 16:10:22
File Name :E:\2014ACM\专题学习\数据结构\线段树\SGU319.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
struct Node
{
int l,r;
int ll,rr;//实际的左右区间
int c;//覆盖标记,懒惰标记
}segTree[MAXN*];
int x[MAXN];
struct Line
{
int x1,x2,y;
int id;
Line(){}
Line(int _x1,int _x2,int _y,int _id)
{
x1 = _x1;
x2 = _x2;
y = _y;
id = _id;
}
}line[MAXN];
bool cmp(Line a,Line b)
{
return a.y < b.y;
}
void push_down(int r)
{
if(segTree[r].l + == segTree[r].r)return;
if(segTree[r].c != -)
{
segTree[r<<].c = segTree[(r<<)|].c = segTree[r].c;
segTree[r].c = -;
}
}
void build(int i,int l,int r)
{
segTree[i].l = l;
segTree[i].r = r;
segTree[i].ll = x[l];
segTree[i].rr = x[r];
segTree[i].c = ;
if(l+ == r)return;
int mid = (l+r)/;
build(i<<,l,mid);
build((i<<)|,mid,r);
}
int pre[MAXN];
void Update(int i,Line e)
{
if(segTree[i].ll == e.x1 && segTree[i].rr == e.x2)
{
if(e.id > )
segTree[i].c = e.id;
else segTree[i].c = pre[-e.id];
return;
}
push_down(i);
if(e.x2 <= segTree[i<<].rr)Update(i<<,e);
else if(e.x1 >= segTree[(i<<)|].ll)Update((i<<)|,e);
else
{
Line tmp = e;
tmp.x2 = segTree[i<<].rr;
Update(i<<,tmp);
tmp = e;
tmp.x1 = segTree[(i<<)|].ll;
Update((i<<)|,tmp);
}
}
int query(int i,Line e)
{
if(segTree[i].c != -)
return segTree[i].c;
if(e.x2 <= segTree[i<<].rr)return query(i<<,e);
else if(e.x1 >= segTree[(i<<)|].ll)return query((i<<)|,e);
else
{
e.x2 = segTree[i<<].rr;
return query(i<<,e);
}
}
long long area[MAXN];
vector<int>vec[MAXN];
void dfs(int u)
{
int sz = vec[u].size();
for(int i = ;i < sz;i++)
{
int v = vec[u][i];
area[u] -= area[v];
dfs(v);
}
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
int w,h;
while(scanf("%d",&n) == )
{
scanf("%d%d",&w,&h);
area[] = (long long)w*h;
int x1,x2,y1,y2;
int tot = ;
for(int i = ;i <= n;i++)
{
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
if(x1 > x2)swap(x1,x2);
if(y1 > y2)swap(y1,y2);
area[i] = (long long)(x2-x1)*(y2-y1);
line[tot] = Line(x1,x2,y1,i);
x[tot++] = x1;
line[tot] = Line(x1,x2,y2,-i);
x[tot++] = x2;
}
sort(x,x+tot);
tot = unique(x,x+tot) - x;
build(,,tot-);
sort(line,line+*n,cmp);
for(int i = ;i <= n;i++)
vec[i].clear();
for(int i = ;i < *n;i++)
{
if(line[i].id > )
{
pre[line[i].id] = query(,line[i]);
vec[pre[line[i].id]].push_back(line[i].id);
}
Update(,line[i]);
}
dfs();
sort(area,area+n+);
for(int i = ;i <= n;i++)
{
printf("%I64d",area[i]);
if(i < n)printf(" ");
else printf("\n");
}
}
return ;
}
SGU 319. Kalevich Strikes Back (线段树)的更多相关文章
- SGU 319 Kalevich Strikes Back(线段树扫描线)
题目大意: n个矩形,将一个大矩形分成 n+1 块.矩形之间不重合,可是包括.求这n+1个矩形的面积 思路分析: 用线段树记录他们之间的父子关系.然后dfs 计算面积. 当给出的矩形上边的时候,就要记 ...
- SGU 531. Bonnie and Clyde 线段树
531. Bonnie and Clyde 题目连接: http://acm.sgu.ru/problem.php?contest=0&problem=531 Description Bonn ...
- SGU 311. Ice-cream Tycoon(线段树)
311. Ice-cream Tycoon Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: standar ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- SGU - 311 Ice-cream Tycoon(线段树)
Description You've recently started an ice-cream business in a local school. During a day you have m ...
- BestCoder Round #56 1002 Clarke and problem 1003 Clarke and puzzle (dp,二维bit或线段树)
今天第二次做BC,不习惯hdu的oj,CE过2次... 1002 Clarke and problem 和Codeforces Round #319 (Div. 2) B Modulo Sum思路差不 ...
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
随机推荐
- JS获取屏幕,浏览器,网页高度宽度
网页可见区域宽:document.body.clientWidth 网页可见区域高:document.body.clientHeight 网页可见区域宽:document.body.offsetW ...
- C语言编程心得
记录这些是为了日后自己想查阅以前经验的方便,同时若能给其他网友带来一些帮助,就更好了~ C语言,自己经常遇到的问题: 1.段错误 段错误一般是由于访问了不存在的地址造成的,具体的原因有文件路径不存在, ...
- WTL编程小技巧汇编
1.设置窗体生成大小并中央显示窗口 2.设置窗体最大/小尺寸 3.动态设置窗体标题 4.设置对话框的字体和背景颜色 5.设置窗体控件默认字体 以下技巧可应用于SDI和MDI程序: 1.设置窗体生成大小 ...
- SpringMVC学习系列(2) 之 经典的HelloWorld实现
前一篇简单介绍了Spring MVC的一些知识,下面就要开始学习如何把Spring MVC运用到具体的项目中去. 首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubunt ...
- 将本地项目上传到git总结
一.总结:
- oracle控制文件丢失恢复
在学习群里有个同学误删除了控制文件,于是我也把自己数据库的控制文件删除了,看看能不能进行恢复,以下是整个实验的过程~~在做之前,先看看控制文件的备份方式:1.生成可以重建控制文件的脚本.2.备份二进制 ...
- eclipse 3.7 配置svn
1.首先需要安装SVN客户端TortoisSVN.地址:http://tortoisesvn.net/ 2.eclipse低版本的需要安装Subversive Revision Graph,地址:ht ...
- IIS7 IIS7.5 IIS8.5 HTTP 错误 500.19 – Internal Server Error解决方案小记
今天配置IIS(win8.1 IIS8.5)的web.config出现如下错误: HTTP 错误 500.19 – Internal Server Error无法访问请求的页面,因为该页的相关配置数据 ...
- 关于javascript tween的学后小感想
今天决定了解一下tween算法,首先得下载个tween.js看看吧,好吧,有点被惊艳到了. 也让我想起了之前上数学课时,听到过的一句话:“数学世界是神秘.纯洁.有魅力的”,一直 记得这句话,期待有朝一 ...
- Mantis 1.2.19 on Windows Server 2012 r2 datacenter 安装及配置随笔
一.前言 新的小团队需要搭建一个缺陷管理的工具,之前用过bugfree,感觉比较适合,但是 禅道不太适合,放弃之,于是又百度推荐的: .JTrac13.BugNet14.BugOnline15.eTr ...