题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265

给你n个中间被挖空了一个矩形的中空矩形,让你求他们的面积并。

其实一个中空矩形可以分成4个小的矩形,然后就是面积并,特别注意的是x1 == x3 || x2 == x4的时候,要特判一下,否则会RE。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 2e5 + ;
typedef long long LL;
struct data {
LL l , r , y , flag;
bool operator <(const data& cmp) const {
return y < cmp.y;
}
}a[MAXN << ];
struct segtree {
LL val , l , r , add;
}T[MAXN << ]; void build(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r , T[p].val = T[p].add = ;
if(r - l == ) {
return ;
}
build(p << , l , mid);
build((p << )| , mid , r);
} void pushup(int p) {
if(T[p].add) {
T[p].val = T[p].r - T[p].l;
}
else if(T[p].r - T[p].l == ){
T[p].val = ;
}
else {
T[p].val = T[p << ].val + T[(p << )|].val;
}
} void updata(int p , int l , int r , int add) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
T[p].add += add;
pushup(p);
return ;
}
if(r <= mid) {
updata(p << , l , r , add);
}
else if(l >= mid) {
updata((p << )| , l , r , add);
}
else {
updata(p << , l , mid , add);
updata((p << )| , mid , r , add);
}
pushup(p);
} int main()
{
int n;
LL x[] , y[];
while(~scanf("%d" , &n) && n) {
int f = ;
for(int i = ; i < n ; i++) {
for(int j = ; j <= ; j++) {
scanf("%lld %lld" , x + j , y + j);
}
sort(x + , x + );
sort(y + , y + );
a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = -;
f++;
a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = ;
f++;
a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = -;
f++;
a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = ;
f++; a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = -;
f++;
a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = ;
f++;
a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = -;
f++;
a[f].l = x[] , a[f].r = x[] , a[f].y = y[] , a[f].flag = ;
f++;
}
sort(a , a + f);
LL res = ;
build( , , 2e5 + );
//注意l == r的特殊情况
if(a[].l != a[].r)
updata( , a[].l , a[].r , a[].flag);
for(int i = ; i < f ; i++) {
res += (a[i].y - a[i - ].y) * T[].val;
if(a[i].r > a[i].l)
updata( , a[i].l , a[i].r , a[i].flag);
}
printf("%lld\n" , res);
}
}

HDU 3265 Posters (线段树+扫描线)(面积并)的更多相关文章

  1. 覆盖的面积 HDU - 1255 (线段树-扫描线)模板提

    给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1& ...

  2. HDU 1542 - Atlantis - [线段树+扫描线]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  3. poj1511,线段树扫描线面积

    经典题,线段树扫描线其实类似区间更新,一般的做法是想象一根扫描线从上扫到下或者从左扫到右,本题的做法是从上扫到下 只要扫到了一根水平线,就将其更新到线段树对应区间中,区间和它的子区间是独立更新的 #i ...

  4. hdu 1542 Atlantis (线段树扫描线)

    大意: 求矩形面积并. 枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上. #include <iostream> #inclu ...

  5. HDU 1542 Atlantis (线段树 + 扫描线 + 离散化)

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

  6. hdu 3265 Posters(线段树+扫描线+面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 题意:给你一张挖了洞的墙纸贴在墙上,问你总面积有多少. 挖了洞后其实就是多了几个矩形墙纸,一张墙 ...

  7. HDU 1255 覆盖的面积 (线段树扫描线+面积交)

    自己YY了一个的写法,不过时间复杂度太高了,网上的想法太6了  题意:给你一些矩阵,求出矩阵的面积并 首先按照x轴离散化线段到线段树上(因为是找连续区间,所以段建树更加好做). 然后我们可以想一下怎样 ...

  8. hdu 1542(线段树+扫描线 求矩形相交面积)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  9. HDU 1828 Picture (线段树+扫描线)(周长并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...

随机推荐

  1. 用imagemagick和tesseract-ocr破解简单验证码

    用imagemagick和tesseract-ocr破解简单验证码 Tesseract-ocr据说辨识程度是世界排名第三,可谓神器啊. 准备工作: 1.安装tesseract-ocr sudo apt ...

  2. Java [Leetcode 338]Counting Bits

    题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...

  3. Java [Leetcode 303]Range Sum Query - Immutable

    题目描述: Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inc ...

  4. 264分析两大利器:264VISA和Elecard StreamEye Tools

    学了264有将近3个月有余,好多时候都在学习老毕的书和反复看JM86的代码,最近才找到264分析两大利器:264VISA和Elecard StreamEye Tools.不由得感叹,恨不逢同时. 简单 ...

  5. Java基础——I/O续

    目录 二进制I/O类 文件导航和I/O 二进制I/O类 FileInputStream类和FileOutputStream类 *FileOutputStream(file: File) *FileOu ...

  6. 如何寻找设计灵感?写给刚入行的设计师(转自UI中国)

    如何寻找设计灵感?写给刚入行的设计师 如何寻找设计灵感? 这一次的文章,我想和大家聊聊年轻的设计师在没有那么多经验的情况下如何寻找设计师灵感.(希望这篇文章也能帮助感同身受的你) 每个设计师对设计都有 ...

  7. 深入浅出 iOS 之生命周期

    转:http://blog.csdn.net/kesalin/article/details/6691766 iOS应用程序的生命周期相比 Android 应用程序的生命周期来说,没那么简明易懂,但是 ...

  8. ios实现类似魔兽小地图功能 在

    写了一个类似魔兽小地图功能的控件. 比如你有一个可以放大缩小的scrollView.会在里面进行一些放大缩小,点击里面的按钮呀,等操作. 这个小地图控件.就会和你的大scrollView同步.并有缩略 ...

  9. HDU 5883 The Best Path

    The Best Path Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  10. HDU 5679 Substring 后缀数组判重

    题意:求母串中有多少不同的包含x字符的子串 分析:(首先奉上FZU官方题解) 上面那个题就是SPOJ694 ,其实这两个题一样,原理每次从小到大扫后缀sa数组,加上新的当前后缀的若干前缀,再减去重复的 ...