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

  1. HDU 3265 Posters (线段树+扫描线)(面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并. 其实一个中空矩形可以分成4个小的矩 ...

  2. poj_2528Mayor's posters(线段树)

    poj_2528Mayor's posters(线段树) 标签: 线段树 题目连接 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K To ...

  3. hdu 4031 attack 线段树区间更新

    Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Subm ...

  4. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  7. HDU 3265 Posters ——(线段树+扫描线)

    第一次做扫描线,然后使我对线段树的理解发生了动摇= =..这个pushup写的有点神奇.代码如下: #include <stdio.h> #include <algorithm> ...

  8. hdu 1828 Picture(线段树 || 普通hash标记)

    http://acm.hdu.edu.cn/showproblem.php?pid=1828 Picture Time Limit: 6000/2000 MS (Java/Others)    Mem ...

  9. (中等) HDU 3265 Posters , 扫描线。

    Problem Description Ted has a new house with a huge window. In this big summer, Ted decides to decor ...

随机推荐

  1. C#中几种换行符

    1.Windows 中的换行符"\r\n" 2.Unix/Linux 平台换行符是 "\n". 3.MessageBox.Show() 的换行符为 " ...

  2. textarea固定大小,不可拖动

    写前端,经常很多小东西容易忽略忘记,今天写页面碰到设定一个输入框大小,死活记不起怎么固定,故找了一下度娘,其实添加一个css属性就好了: resize: none; 随笔记一下!

  3. DataSet ,DataTable,DataRow 之间的关系与使用

    关系   DataSet 包含多个DataTable,DataTable包含多行DataRow. 使用情况:   有时候GridView等控件需要将数据源动态绑定到DataSet中:将多个DataSe ...

  4. centos上如何安装git

    安装依赖包 1.yum -y install zlib-devel openssl-devel perl cpio expat-devel gettext-devel 2.yum install au ...

  5. 关于textView的字数限制

    在一个项目中遇到texteView的字数限制,在iOS7.0上,会出现崩溃.我在这里栽了一个大跟头,废话不多说,下面直接贴代码吧. - (void)textViewDidChange:(UITextV ...

  6. html的form元素

    <input type="email"><br> <input type="date"><br> <inp ...

  7. 获取aplicationContext对象,从而获取任何注入的对象

    在Servlet中 方法一: 从ServletContext 取出 Spring容器上下文 ApplicationContext applicationContext = (ApplicationCo ...

  8. 利用csc.exe 手动编译C#程序

    1. 创建见 cs代码文件 using System; class TestApp{ static void Main() { Console.WriteLine("Test! 1,2,3& ...

  9. flask开发restful api系列(2)

    继续上一章所讲,上一章我们最后面说道,虽然这个是很小的程序,但还有好几个要优化的地方.先复制一下老的view.py代码. # coding:utf-8 from flask import Flask, ...

  10. iOS学习之根据文本内容动态计算文本框高度的步骤

    在视图加载的过程中,是先计算出frame,再根据frame加载视图的,所以在设计计算高度的方法的时候,设计成加号方法; //首先给外界提供计算cell高度的方法 + (CGFloat)heightFo ...