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] .根 ...
随机推荐
- 快速开发CSS的利器-LESS
快速开发CSS的利器-LESS? 天下功夫,唯快不破!效率,在项目开发上,这是极其重要的.要做到快.精.准,在人任何时候都不是一件轻松容易的事.但是如果借助一些相应的工具,那就另当别论了!那么要想快速 ...
- 建立一个简单的SpringMVC程序
首先,所建立的程序是一个web程序,所以在web.xml文件中进行如下的配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
- Amazon Web Services
- Unity2D多分辨率屏幕适配方案(转载)
一下内容转自:http://imgtec.eetrend.com/forum/3992 此文将阐述一种简单有效的Unity2D多分辨率屏幕适配方案,该方案适用于基于原生开发的Unity2D游戏,即没有 ...
- ZooKeeper程序员指南(转)
译自http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html 1 简介 本文是为想要创建使用ZooKeeper协调服务优势的分布式 ...
- mysql高效分页方案及原理
很久以前的一次面试中,被面试官问到这个问题,由于平时用到的分页方法不多,只从索引.分表.使用子查询精准定位偏移以外,没有使用到其它方法. 后来在看其它博客看到了一些不同的方案,也一直没有整理.今天有时 ...
- CoreData 添加新字段
给CoreData添加新属性,就是给数据库加新字段,那么必须要进行数据库版本升级及CoreData数据迁移: 具体操作是 1.选择DemoCoreData.xcdatamodeld 文件,Editor ...
- 全面了解 Linux 服务器 - 4. 查看 Linux 系统的平均负载
可使用 uptime.top.w 命令来查看. 以 uptime 命令为例: liuqian@ubuntu:~$ uptime 17:31:26 up 7:27, 2 users, load aver ...
- INI 文件的读写操作
在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...
- Consuming a Web Service in AX 2012
Consuming a Web Service in AX 2012 在AX2012版本中如果想调用外部的Web Service变得非常容易. 第一步,在VS中创建一个Web Service并发布 第 ...