题目大意:

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. MYSQL中的普通索引,主健,唯一,全文索引区别

    MYSQL索引用来快速地寻找那些具有特定值的记录,所有MySQL索引都以B-树的形式保存.如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录.表里面的记 ...

  2. 用不动点组合子解递归(python实现)

    不动点组合子 Y = λf. (λx. f (x x)) (λx. f (x x)) θ = (λx. λy. (y(x x y))) (λx.λy.(y(x x y))) Y f = f (Y f) ...

  3. 源码学习之ASP.NET MVC Application Using Entity Framework

    源码学习的重要性,再一次让人信服. ASP.NET MVC Application Using Entity Framework Code First 做MVC已经有段时间了,但看了一些CodePle ...

  4. 什么是JSON对象

    1.什么是json? JSON全称是JavaScript Object Notation,是一种轻量级的数据交换格式.JSON 与XML具有相同的特性,是一种数据存储格式,但是JSON相比XML 更易 ...

  5. sql查询数据库表中重复记录方法

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people where peopleId in (select peopleId ...

  6. erlang supervisor说明

    Supervisor Behaviour是一个用来实现一个supervisor进程来监控其他子进程的模块 子进程可以是另一个supervisor,也可以是一个worker进程. worker进程一般使 ...

  7. Javascript 装载和执行

    http://coolshell.cn/articles/9749.html http://www.cnblogs.com/cheche/archive/2011/03/06/1971955.html

  8. java如何在函数中调用主函数的数组

    import javax.swing.JOptionPane; public class Test { /** * @zdz */ public static void main(String[] a ...

  9. flash跨域策略文件crossdomain.xml

    flash在跨域时唯一的限制策略就是crossdomain.xml文件,该文件限制了flash是否可以跨域读写数据以及允许从什么地方跨域读写数据. 位于www.a.com域中的SWF文件要访问www. ...

  10. poj1651 最优矩阵乘法动态规划解题

    题目描述: 有若干个矩阵{Ai},元素都为整数且已知矩阵大小. 如果要计算所有矩阵的乘积A1 * A2 * A3 .. Am,最少要多少次整数乘法? 输入 第一行一个整数n(n <= 100), ...