题目大意:

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(线段树扫描线)的更多相关文章

  1. SGU 319. Kalevich Strikes Back (线段树)

    319. Kalevich Strikes Back Time limit per test: 0.5 second(s)Memory limit: 65536 kilobytes input: st ...

  2. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  3. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  4. 【POJ-2482】Stars in your window 线段树 + 扫描线

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11706   Accepted:  ...

  5. HDU 4419 Colourful Rectangle --离散化+线段树扫描线

    题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...

  6. BZOJ-3228 棋盘控制 线段树+扫描线+鬼畜毒瘤

    3228: [Sdoi2008]棋盘控制 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 23 Solved: 9 [Submit][Status][D ...

  7. BZOJ-3225 立方体覆盖 线段树+扫描线+乱搞

    看数据范围像是个暴力,而且理论复杂度似乎可行,然后被卡了两个点...然后来了个乱搞的线段树+扫描线.. 3225: [Sdoi2008]立方体覆盖 Time Limit: 2 Sec Memory L ...

  8. hdu 5091(线段树+扫描线)

    上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...

  9. POJ1151+线段树+扫描线

    /* 线段树+扫描线+离散化 求多个矩形的面积 */ #include<stdio.h> #include<string.h> #include<stdlib.h> ...

随机推荐

  1. Flink资料(7) -- 背压监控

    背压(backpressure)监控 本文翻译自Back Pressure Monitoring --------------------------------------------------- ...

  2. Myeclipse普通工程转为Maven工程

    在SVN导出的Maven项目,或以前不是用Maven管理的项目想要转换成Maven项目,但Myeclipse中右键Configure 找不到(eclipse可行)Convert to maven pr ...

  3. 提醒录入BOM更改原因

    应用 Oracle Bill Of   Materiel 层 Level Function 函数名 Funcgtion Name BOM_BOMFDBOM 表单名 Form Name BOMFDBOM ...

  4. Delphi COM编程技术三类型库(库文件中的工具栏,很全)

    在COM组件的使用和开发过程中经常需要获取有关组件的信息.而COM组件以二进制代码的形式发布,如果不借助特定的工具这些相关信息将难以被获取.通过访问类型库就可以查看COM组件的信息. 一.类型库的基础 ...

  5. String类的实现,内部采用字符数组实现

    #include <iostream> using namespace std; class String{ public: String(const char *str = NULL); ...

  6. android 屏幕适配问题【转】

      如何将一个应用程序适配在不同的手机上,虽然这不算是一个技术问题,但是对于刚刚做屏幕的开发人员来说,还真不是一件多么简单的事情. 首先:你需要在AndroidManifest.xml文件的<m ...

  7. c# 针对不同数据库进行参数化查询

    使用参数化 DbCommand 的一个缺点是需要参数的代码将仅适用于支持相同语法的提供程序.OLEDB.SqlClient 和 Oracle 提供程序全部使用不同的语法.例如,用于命名和指定参数的 S ...

  8. JavaScript之arguements对象学习

    简介:在JavaScript中,有一个特殊的对象-Arguements对象,它是当前函数的一个内置属性,它类似与Array对象(数组形式),但不是Array的一个实例.下面通过代码来论证: <s ...

  9. jquery数字验证大小比较

    $("#rewardForm").validate({            rules: {                "reward": {       ...

  10. mywebsite1

    http://www.wuweierwei.com/ 个人网站 http://www.yyyweb.com/demo/icons-filling/index.html CSS3图标填充效果 http: ...