POJ1389 给定n个整数点矩形,求面积并。

显然ans必然是整数。

记录若干个事件,每个矩形的左边的竖边记为开始,右边的竖边记为结束。

进行坐标离散化后用线段树维护每个竖的区间, 就可以快速积分了。

#include<stdio.h>
#include<stdlib.h>
#include<cstring>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std; const int maxn=100005;
struct Rectangle
{
int x1,y1,x2,y2;
}; struct Event
{
int x,y1,y2,add;
}; struct IntervalTreeNode
{
int count,total;
}; int n; Rectangle rect[maxn];
Event evt[maxn<<1];
IntervalTreeNode tree[(maxn+10)<<2];
int id[maxn<<1];
bool cmp(const Event &a,const Event &b)
{
if(a.x<b.x)return true;
return false;
} void up(int i,int lb,int rb)
{
tree[i].total=tree[i<<1].total+tree[(i<<1)+1].total;
if(tree[i].count)tree[i].total=id[rb]-id[lb];
} void ins(int i,int lb,int rb,int a,int b,int k)
{
if(a==lb&&b==rb){
tree[i].count+=k;
up(i,lb,rb);
return ;
}
int med=(lb+rb)>>1;
if(b<=med) ins(i<<1,lb,med,a,b,k);
else if(a>=med)ins((i<<1)+1,med,rb,a,b,k);
else{
ins(i<<1,lb,med,a,med,k);
ins((i<<1)+1,med,rb,med,b,k);
}
up(i,lb,rb);
} long long area()
{
for(int i=0;i<n;i++){
id[i]=rect[i].y1;
id[i+n]=rect[i].y2;
}
sort(id,id+2*n);
for(int i=0;i<2*n;i++){
rect[i].y1=lower_bound(id,id+2*n,rect[i].y1)-id;
rect[i].y2=lower_bound(id,id+2*n,rect[i].y2)-id;//坐标离散化
}
for(int i=0;i<n;i++){
evt[i].add=1;
evt[i+n].add=-1;
evt[i].x=rect[i].x1;
evt[i+n].x=rect[i].x2;
evt[i].y1=evt[i+n].y1=rect[i].y1;
evt[i].y2=evt[i+n].y2=rect[i].y2;
}
sort(evt,evt+n*2,cmp);
long long int ans=0;
for(int i=0;i<2*n;i++){
if(i>0&&evt[i].x>evt[i-1].x){
ans+=(long long )(evt[i].x-evt[i-1].x)*tree[1].total;
}
ins(1,0,2*n-1,evt[i].y1,evt[i].y2,evt[i].add);
}
return ans;
} int main()
{freopen("t.txt","r",stdin);
while(true)
{
n=0;
scanf("%d%d%d%d",&rect[0].x1,&rect[0].y1,&rect[0].x2,&rect[0].y2);
if(rect[0].x1==-1)return -1;
while(++n)
{
scanf("%d%d%d%d",&rect[n].x1,&rect[n].y1,&rect[n].x2,&rect[n].y2);
if(rect[n].x1==-1)break;
}
printf("%I64d\n",area());
}
return 0;
}

  

POJ1389 Area of Simple Polygons 线段树的更多相关文章

  1. POJ1389:Area of Simple Polygons——扫描线线段树题解+全套代码注释

    http://poj.org/problem?id=1389 题面描述在二维xy平面中有N,1 <= N <= 1,000个矩形.矩形的四边是水平或垂直线段.矩形由左下角和右上角的点定义. ...

  2. 【POJ 1389】Area of Simple Polygons(线段树+扫描线,矩形并面积)

    离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段 ...

  3. POJ 1389 Area of Simple Polygons 扫描线+线段树面积并

    ---恢复内容开始--- LINK 题意:同POJ1151 思路: /** @Date : 2017-07-19 13:24:45 * @FileName: POJ 1389 线段树+扫描线+面积并 ...

  4. Area of Simple Polygons

    poj1389:http://poj.org/problem?id=1389 题意:求矩形面积的并题解:扫描线加线段树 同poj1389 #include<iostream> #inclu ...

  5. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树

    E. A Simple Task 题目连接: http://www.codeforces.com/contest/558/problem/E Description This task is very ...

  6. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树+计数排序

    题目链接: http://codeforces.com/problemset/problem/558/E E. A Simple Task time limit per test5 secondsme ...

  7. CodeForces 588E A Simple Task(线段树)

    This task is very simple. Given a string S of length n and q queries each query is on the format i j ...

  8. POJ Area of Simple Polygons 扫描线

    这个题lba等神犇说可以不用离散化,但是我就是要用. 题干: Description There are N, <= N <= , rectangles -D xy-plane. The ...

  9. Codeforces Round #312 (Div. 2) E. A Simple Task 线段树 延时标记

    E. A Simple Task time limit per test5 seconds memory limit per test512 megabytes inputstandard input ...

随机推荐

  1. 杭电 2124 Repair the Wall(贪心)

    Description Long time ago , Kitty lived in a small village. The air was fresh and the scenery was ve ...

  2. 【Linux】date命令用法详解(日期格式)

    inux下date命令用法 date [OPTION]… [+FORMAT] date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] date命令参数 ...

  3. CookiesReader

    CookiesReader "use strict"; /** * * @author xgqfrms * @license MIT * @copyright xgqfrms * ...

  4. [luoguP2957] [USACO09OCT]谷仓里的回声Barn Echoes(Hash)

    传送门 团队里的hash水题,数据小的不用hash都能过.. 也就是前缀hash,后缀hash,再比较一下就行. ——代码 #include <cstdio> #include <c ...

  5. 【bzoj2152】聪聪可可 点分治

    [bzoj2152]聪聪可可 2014年9月7日3,5472 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是 ...

  6. 【spring boot 系列】spring data jpa 全面解析(实践 + 源码分析)

    前言 本文将从示例.原理.应用3个方面介绍spring data jpa. 以下分析基于spring boot 2.0 + spring 5.0.4版本源码 概述 JPA是什么? JPA (Java ...

  7. 2018/4/7 Mybatis源码结构概览

    在观看Mybatis源码的过程中,有一点疑惑,就是Mybatis的缓存设计明显有问题,首先,Mybatis缓存分为两级,先说一级,生命周期为一个sqlsession,只有在查询相同方法时才会命中缓存, ...

  8. Educational Codeforces Round 45 (Rated for Div. 2) C、D

      C. Bracket Sequences Concatenation Problem time limit per test 2 seconds memory limit per test 256 ...

  9. Codeforces Round #457 (Div. 2) B

    B. Jamie and Binary Sequence (changed after round) time limit per test 2 seconds memory limit per te ...

  10. 易碎的鸟蛋 概率DP

    1007: 易碎的鸟蛋 时间限制: 1 Sec  内存限制: 128 MB提交: 396  解决: 80[提交][状态][讨论版] 题目描述 你们知道吗?西电的跳楼塔上面有一个鸟巢.某SXBK的教授对 ...