HDU 3265 Posters ——(线段树+扫描线)
第一次做扫描线,然后使我对线段树的理解发生了动摇= =。。这个pushup写的有点神奇。代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define t_mid (l+r>>1)
#define ls (o<<1)
#define rs (o<<1|1)
#define lson ls,l,t_mid
#define rson rs,t_mid+1,r
using namespace std;
const int N = + ;
const int MAXN = + ;
typedef long long ll; struct seg
{
int x1,x2,y,d;
bool operator < (const seg & temp) const
{
// 先加边后减边,这样的话就不会出现lazy[o] < 0的情况了
return y == temp.y ? d > temp.d : y < temp.y;
}
}g[N*];
int n,tot,c[MAXN<<],lazy[MAXN<<]; void add(int x1,int x2,int y,int d)
{
tot ++;
g[tot] = {x1,x2,y,d};
}
void read()
{
tot = ;
for(int i=;i<=n;i++)
{
int x1, y1, x2, y2, x3, y3, x4, y4;
scanf("%d%d%d%d%d%d%d%d",&x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4);
if(x1 != x3)
{
add(x1,x3,y1,);
add(x1,x3,y2,-);
}
if(x4 != x2)
{
add(x4,x2,y1,);
add(x4,x2,y2,-);
}
add(x3,x4,y1,);
add(x3,x4,y3,-);
add(x3,x4,y4,);
add(x3,x4,y2,-);
}
sort(g+,g++tot);
} void up(int o) {c[o] = c[ls] + c[rs];}
/*void down(int o,int l,int r)
{
if(l == r) return ;
int len = r - l + 1;
if(lazy[o])
{
lazy[ls] = lazy[rs] = lazy[o];
if(lazy[o] > 0)
{
c[ls] = len - len / 2;
c[rs] = len / 2;
}
else
{
c[ls] = c[rs] = 0;
}
lazy[o] = 0;
}
}*/ void pushup(int o,int l,int r)
{
if(lazy[o] > )
{
//if(lazy[o] < 0) for(int i=1;i<=1000000000000LL;i++);
// 注意seg的排序规则!
c[o] = r - l + ;
}
else
{
// lazy[o] == 0
if(l == r) c[o] = ;
else up(o);
}
}
void update(int o,int l,int r,int ql,int qr,int f)
{
if(l == ql && r == qr)
{
lazy[o] += f;
pushup(o,l,r);
return ;
}
if(qr <= t_mid) update(lson,ql,qr,f);
else if(ql > t_mid) update(rson,ql,qr,f);
else
{
update(lson,ql,t_mid,f);
update(rson,t_mid+,qr,f);
}
pushup(o,l,r);
} void solve()
{
ll ans = ;
memset(c,,sizeof(c));
memset(lazy,,sizeof(lazy));
for(int i=;i<=tot;)
{
int j = i;
while(j <= tot && g[j].y == g[i].y) j++;
for(int k=i;k<j;k++) update(,,MAXN,g[k].x1+,g[k].x2,g[k].d);
if(j <= tot) ans += (ll)(c[])*(g[j].y-g[i].y);
i = j;
}
printf("%I64d\n",ans);
} int main()
{
while(scanf("%d",&n) == && n)
{
read();
solve();
}
return ;
}
HDU 3265 Posters ——(线段树+扫描线)的更多相关文章
- HDU 3265 Posters (线段树+扫描线)(面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩 ...
- HDU 1542 - Atlantis - [线段树+扫描线]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- 覆盖的面积 HDU - 1255 (线段树-扫描线)模板提
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...
- HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1542 Atlantis (线段树扫描线)
大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- HDU 1828 Picture (线段树:扫描线周长)
依然是扫描线,只不过是求所有矩形覆盖之后形成的图形的周长. 容易发现,扫描线中的某一条横边对答案的贡献. 其实就是 加上/去掉这条边之前的答案 和 加上/去掉这条边之后的答案 之差的绝对值 然后横着竖 ...
- Atlantis HDU - 1542 (线段树扫描线)
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- hdu 1542(线段树+扫描线 求矩形相交面积)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 3265 Posters(线段树)
HDU 3265 Posters pid=3265" target="_blank" style="">题目链接 题意:给定一些矩形海报.中间有 ...
随机推荐
- c/s架构
C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应用软件 ...
- bootsraps ch1
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- Git的Bug分支----临时保存现场git stash
软件开发中,bug就像家常便饭一样,有了bug就需要修复,在Git中,由于分支是如此的强大,所以每个bug通过一个新的分支来修复,在修复后,合并分支,然后将临时分支删除. 当你接到一个修复代号为119 ...
- permutation求全排列
include <iostream> #include <string> using namespace std; void swap(char &c1, char & ...
- override the hashcode and equals method in java
http://howtodoinjava.com/2012/10/09/working-with-hashcode-and-equals-methods-in-java/
- iOS打包app发给测试人员测试
说明:在项目开发过程中经常需要开发人员将项目打包成ipa包后,发给测试人员进行测试.本文贴图对打包的过程简单介绍. 一.Product ->archive (注意,不能是模拟器状态,如果当前调试 ...
- oc汉子转拼音
oc中可以不使用第三方库直接吧数组转成拼音: 代码如下: NSString *str = @"中国abc人民共和国"; CFStringRef aCFString=(__bridg ...
- C#输出日历
用C#输出日历,此功能可用于Ajax方式列出计划日程相关的内容,由于是C#控制输出,可以方便加上自己需要的业务处理逻辑. 1.控制台输出: using System; namespace 控制台日历 ...
- Thinkphp中使用Redis
先确保安装了redis扩展 添加Redis配置 'REDIS_HOST'=>'192.168.0.2', 'REDIS_PORT'=>6379, 其他配置根据自己ThinkPHP版本,找到 ...
- sqlserver 批量修改表前缀
先把第一句话放到sqlserver查询器中执行一下.然后把查询结果复制出来,进行编辑...一看你就懂了..简单的sql语句拼装 select ' exec sp_rename "' + na ...