HDU 3265 Posters(线段树)
HDU 3265 Posters
pid=3265" target="_blank" style="">题目链接
题意:给定一些矩形海报。中间有孔。求贴海报的之后的海报覆盖面积并
思路:海报一张能够分割成4个矩形。然后就是普通的矩形面积并了,利用线段树维护就可以
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; typedef long long ll; const int N = 50005; struct Node {
int l, r, len, cover;
int size() {return r - l + 1;}
} node[N * 4]; struct Line {
int l, r, y, flag;
Line() {}
Line(int l, int r, int y, int flag) {
this->l = l; this->r = r;
this->y = y; this->flag = flag;
}
} line[N * 8]; struct Rec {
int x1, y1, x2, y2;
Rec() {}
Rec(int x1, int y1, int x2, int y2) {
this->x1 = x1; this->y1 = y1;
this->x2 = x2; this->y2 = y2;
}
} rec[N * 4]; bool cmp(Line a, Line b) {
return a.y < b.y;
} int n;
int x[4], y[4]; #define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) void pushup(int x) {
if (node[x].cover) node[x].len = node[x].size();
else if (node[x].l == node[x].r) node[x].len = 0;
else node[x].len = node[lson(x)].len + node[rson(x)].len;
} void build(int l, int r, int x = 0) {
node[x].l = l; node[x].r = r;
if (l == r) {
node[x].cover = node[x].len = 0;
return;
}
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
pushup(x);
} void add(int l, int r, int v, int x = 0) {
if (l > r) return;
if (node[x].l >= l && node[x].r <= r) {
node[x].cover += v;
pushup(x);
return;
}
int mid = (node[x].l + node[x].r) / 2;
if (l <= mid) add(l, r, v, lson(x));
if (r > mid) add(l, r, v, rson(x));
pushup(x);
} int main() {
while (~scanf("%d", &n) && n) {
build(0, 50000);
int rn = 0, ln = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++)
scanf("%d%d", &x[j], &y[j]);
rec[rn++] = Rec(x[0], y[0], x[1], y[2]);
rec[rn++] = Rec(x[0], y[2], x[2], y[3]);
rec[rn++] = Rec(x[0], y[3], x[1], y[1]);
rec[rn++] = Rec(x[3], y[2], x[1], y[3]);
}
for (int i = 0; i < rn; i++) {
line[ln++] = Line(rec[i].x1, rec[i].x2, rec[i].y1, 1);
line[ln++] = Line(rec[i].x1, rec[i].x2, rec[i].y2, -1);
}
n = ln;
sort(line, line + n, cmp);
ll ans = 0;
for (int i = 0; i < n; i++) {
if (i) ans += (ll)node[0].len * (line[i].y - line[i - 1].y);
add(line[i].l, line[i].r - 1, line[i].flag);
}
printf("%lld\n", ans);
}
return 0;
}
HDU 3265 Posters(线段树)的更多相关文章
- HDU 3265 Posters (线段树+扫描线)(面积并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩 ...
- poj_2528Mayor's posters(线段树)
poj_2528Mayor's posters(线段树) 标签: 线段树 题目连接 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K To ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- HDU 3265 Posters ——(线段树+扫描线)
第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm> ...
- hdu 1828 Picture(线段树 || 普通hash标记)
http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others) Mem ...
- (中等) HDU 3265 Posters , 扫描线。
Problem Description Ted has a new house with a huge window. In this big summer, Ted decides to decor ...
随机推荐
- sublime text3中的常用插件
1.All Autocomplete Sublime Text 默认的 Autocomplete 功能只考虑当前的文件,而 AllAutocomplete 插件会搜索所有打开的文件来寻找匹配的提示词. ...
- How to recover after deleting the symbolic link libc.so.6?
参考资料: http://stackoverflow.com/questions/12249547/how-to-recover-after-deleting-the-symbolic-link-li ...
- jstl经典用法
jstl的forEach使用和set变量实现自增: <body> <c:set var="index" value="0" /> < ...
- c - 每位数字尾部加空格
/* input:一个4位整数. output:每位整数后紧跟一个空格的字符串. */ char * insert(char *s) { int len = strlen(s); * len + ); ...
- 常用webservice网址
http://www.gpsso.com/Main/ServiceList.aspx http://developer.51cto.com/art/200908/147125.htm 这里记录了几个常 ...
- NSURLSessionDataTask
#import "ViewController.h" @interface ViewController ()<NSURLSessionDelegate,NSURLSessi ...
- jQuery操作元素
通常,我们在创建元素时,会使用以下代码: var p = document.createElement("p"); p.innerText = "this is para ...
- uva 10167 - Birthday Cake
题解:由于解太多,随机抓 A.B, 只要有符合就行了: (首先,Ax+By=0必须表示直线,即A.B不能同时为0:另外,要注意到直线不能过输入中的2N个点:检测点在直线的哪一侧,只需要简单的线性规划的 ...
- h5connect.js 腾讯云视频点播使用指南
http://video.qcloud.com/download/docs/QVOD_Player_Web_SDK_Developer_Guide.pdf 腾讯云视频点播服务 Web播放器SDK开发指 ...
- n阶行列式计算----c语言实现(完结)
花了半天时间,写了这个n阶行列式计算的程序,应该算是比较优美吧,有很多地方多次做了优化,程序占用内存不是很大,要是说小吧,也不合适,因为里边有一个递归,而且递归的深度还比较深.时间复杂度具体没有细看, ...