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="">题目链接 题意:给定一些矩形海报.中间有 ...
随机推荐
- 一个有用的shell脚本
#!/bin/bash #if [ $1 -eq null ]; then # echo "please input params1!" # exit #fi #if [ $2 - ...
- jquery源码阅读(1)
每天坚持阅读一定量的的jquery代码,积少成多!加油加油! jquery-2.2.1的9161~9194行 1 if ( typeof define === "function" ...
- 常见的jquery一些效果
1.CSS渐变:background: linear-gradient(to bottom right, #999 , #eee);
- 定义django admin的站点头,标题等
admin.py from django.contrib import admin from app.models import Product # Register your models here ...
- Linux下的两个聊天命令的使用方法
一. write用法:write 用户名:敲回车后,自己和对方将会同时处于聊天的状态,但是被发起连接的人只能收到发起聊天请求的人的聊天内容,但是不能回复,如果想要回复的话必须先向对方发起连接,这样以来 ...
- win7系统,apache2.2下添加PHP5的配置详解
首先要说apache(服务器). php(开发语言). mysql(数据库) 之间的关系. Apache:为系统提供了Web服务支持,网站:http://www.apache.org/ PHP:为系统 ...
- JavaScript高级程序设计:第十三章
第十三章 一.理解事件流 事件流描述的是从页面中接收事件的顺序. 1.事件冒泡 IE的事件流叫做事件冒泡,即事件开始时由最具体的元素接收,然后逐级向上传播到较为不具体的节点.以下面的HTML页面为例: ...
- Struts2(result 流 )下载
jsp: <body> <a href="stream.action?fileName=psb.jpg">psb</a> <br> ...
- angular跨域访问的问题
CORS跨域资源共享 跨域资源共享(CORS )是一种网络浏览器的技术规范,它为Web服务器定义了一种方式,允许网页从不同的域访问其资源. Form responseHeaders = (Form) ...
- 用 js 做 URL 跳转带来的 Referer 丢失问题.
http 302 重定向是可以保持 referer 的.例:在 A 页面上提交登录表单到 B,B 返回一个重定向页面到 C,在 C 处理里面检查 Referer 可知道它的来源是 A 而不是 B. 但 ...