SGU 319 Kalevich Strikes Back(线段树扫描线)
题目大意:
n个矩形,将一个大矩形分成 n+1 块。矩形之间不重合,可是包括。求这n+1个矩形的面积
思路分析:
用线段树记录他们之间的父子关系。然后dfs 计算面积。
当给出的矩形上边的时候,就要记录到该矩形的父亲去。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#define lson num<<1,s,mid
#define rson num<<1|1,mid+1,e
#define maxn 70010 using namespace std;
typedef long long LL; int cov[maxn<<2];
LL area[maxn<<1];
int W,H; struct node
{
int s,e,h,type;
bool operator < (const node &cmp)const
{
return h<cmp.h;
}
}scline[maxn<<1]; struct foo
{
int s,e,h;
}sqr[maxn<<2]; int x[maxn<<1];
int pre[maxn<<1]; void pushdown(int num)
{
if(cov[num]!=-1){
cov[num<<1]=cov[num<<1|1]=cov[num];
cov[num]=-1;
}
}
void build(int num,int s,int e)
{
cov[num]=0;
if(s==e)return;
int mid=(s+e)>>1;
build(lson);
build(rson);
} void update(int num,int s,int e,int l,int r,int val)
{
if(l<=s && r>=e)
{
if(val<0)cov[num]=pre[abs(val)];
else cov[num]=val;
return;
}
pushdown(num);
int mid=(s+e)>>1;
if(l<=mid)update(lson,l,r,val);
if(r>mid)update(rson,l,r,val);
} int PRE;
int query(int num,int s,int e,int l,int r)
{ if(cov[num]!=-1)
{
return cov[num];
}
pushdown(num);
int mid=(s+e)>>1;
if(r<=mid)return query(lson,l,r);
else if(l>mid)return query(rson,l,r);
else return query(lson,l,mid);
} int head[maxn<<1];
int next[maxn<<1];
int to[maxn<<1];
int tot;
void add(int a,int b)
{
next[tot]=head[a];
head[a]=tot;
to[tot]=b;
tot++;
}
LL getarea(int index)
{
return (LL)sqr[index].h*(LL)(sqr[index].e-sqr[index].s);
}
void dfs(int x)
{
for(int s=head[x];s!=0;s=next[s])
{
area[x]-=getarea(to[s]);
dfs(to[s]);
}
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
tot=1; memset(pre,0,sizeof pre); scanf("%d%d",&W,&H);
area[0]=(LL)W*(LL)H;
sqr[0].s=0;sqr[0].e=W;sqr[0].h=H;
for(int i=1;i<=n;i++)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2); if(x1>x2)swap(x1,x2);
if(y1>y2)swap(y1,y2);
sqr[i].s=x1;sqr[i].e=x2;sqr[i].h=y2-y1; scline[2*i-1].s=x1;
scline[2*i-1].e=x2;
scline[2*i-1].h=y1;
scline[2*i-1].type=i;
x[2*i-1]=x1; scline[2*i].s=x1;
scline[2*i].e=x2;
scline[2*i].h=y2;
scline[2*i].type=-i;
x[2*i]=x2;
} x[2*n+1]=0;
x[2*n+2]=W; for(int i=1;i<=n;i++)
area[i]=getarea(i); sort(x+1,x+2*n+3);
int m=unique(x+1,x+2*n+3)-(x+1)-1; build(1,0,m); sort(scline+1,scline+2*n+1);
memset(head,0,sizeof head);
tot=1;
for(int i=1;i<=2*n;i++)
{
int l=lower_bound(x+1,x+m+1,scline[i].s)-(x+1);
int r=lower_bound(x+1,x+m+1,scline[i].e)-(x+1); if(scline[i].type>0)
{
pre[scline[i].type]=query(1,0,m,l,r);
printf("%d %d\n",scline[i].type,pre[scline[i].type]);
add(pre[scline[i].type],scline[i].type); }
update(1,0,m,l,r,scline[i].type);
} dfs(0);
sort(area,area+n+1); for(int i=0;i<=n;i++)
printf("%lld%c",area[i],i==n?'\n':' ');
}
return 0;
}
/*
2
5 5
1 1 4 4
2 2 3 3 4
10 10
1 1 5 5
2 2 3 4
6 1 9 9
7 2 8 3 4
10 10
1 1 9 6
2 2 5 5
2 7 8 9
3 6 5 7
*/
SGU 319 Kalevich Strikes Back(线段树扫描线)的更多相关文章
- SGU 319. Kalevich Strikes Back (线段树)
319. Kalevich Strikes Back Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)
题目链接:http://codeforces.com/contest/522/problem/D 题目大意: 给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...
- 【POJ-2482】Stars in your window 线段树 + 扫描线
Stars in Your Window Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11706 Accepted: ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤
3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...
- BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞
看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...
- hdu 5091(线段树+扫描线)
上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...
- POJ1151+线段树+扫描线
/* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...
随机推荐
- 用CSS3绘制图形
参考资料:http://blog.csdn.net/fense_520/article/details/37892507 本文非转载,为个人原创,转载请先联系博主,谢谢~ 准备: <!DOCTY ...
- 一个php创建webservice,并通过c#调用的真实实例
最近需要用php创建webservice供C#和JAVA来调用,通过3天的搜索和尝试,终于成功在C#下调用,JAVA的调用还没开始,为防止忘记,在这里记录下来全过程. 本文参考了许多文章,文中也采用了 ...
- php 输出带变量字符串
(一) <?php $a=50;echo "Hello World 我有"."$a"."元";?> 看此例子,变量a的输出,在p ...
- 从零开始PHP学习 - 第一天
写这个系列文章主要是为了督促自己 每天定时 定量消化一些知识! 同时也为了让需要的人 学到点啥~! 本人技术实在不高!本文中可能会有错误!希望大家发现后能提醒一下我和大家! 偷偷说下 本教程最后的目 ...
- PADS无模命令总结
1.PADS2007无模命令与快捷键 <x.y>表示坐标.<s>表示文体.<n>表示数字. 1.[C]显示平面的焊盘和热焊盘(Thermal). 2.[D]显示当前 ...
- 快速美眉(FastMM)使用手记
今天在SourceForge下到了FastMM (Fast Memory Manager),听说比官方的内存管理快多了,试了一下,果然不错.目前最新的是4.27. 就我的使用范围来说,我就是想看看我的 ...
- (12)Visual Studio 2012如何透过电子邮件部署Xamarin.Android App
原文 Visual Studio 2012如何透过电子邮件部署Xamarin.Android App Android App在部署到实机的时候不像iOS的App限制你一定要使用向Apple申请的开发者 ...
- enumerate小技巧和列表推导式
1.enumerate enumerate函数用于遍历序列中的元素以及它们的下标,这样你就可以通过index 直接定位你的数据了. 之前对list操作的时候,即想取到下表,又想取到对应值,我是这么来实 ...
- 在mac上访问自带服务器权限问题
在开发中,有时候我们需要自己的在电脑上做一些网络相关的测试功能,因此,我们必须在本地上模拟网络环境. 在模拟网络环境中,经常会遇到访问权限的问题.现在我就把自己的解决办法写出来.我用的模拟服务器站点是 ...
- 如何在MFC对话框之间自定义消息传递
在MFC项目开发中,涉及到不同模块间数据信息的传递,如用户在登录界面成功登录后向系统管理模块发送用户名和密码等信息. 首先,需明确以下两点: 谁要发送这个消息--消息发送方 谁要接受这个消息--消息接 ...