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

---恢复内容开始---

LINK

题意:同POJ1151

思路:

/** @Date    : 2017-07-19 13:24:45
  * @FileName: POJ 1389 线段树+扫描线+面积并 同1151.cpp
  * @Platform: Windows
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <stack>
#include <queue>
#include <math.h>
//#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

struct yuu
{
	int l, r;
	int flag;
	int ll, rr;
	int len;
}tt[N];

struct line{
	int y, x1, x2;
	int flag;
	line(){}
	line(int _x1, int _x2, int _y, int f){
		x1 = _x1;
		x2 = _x2;
		y = _y;
		flag = f;
	}
};
line li[N];
int cmp(line a, line b)
{
	return a.y < b.y;
}
double a[N], b[N];

void pushup(int p)
{
	if(tt[p].flag > 0)
	{
		tt[p].len = tt[p].rr - tt[p].ll;
		return ;
	}
	if(tt[p].l == tt[p].r - 1)
		tt[p].len = 0;
	else
		tt[p].len = tt[p << 1].len + tt[p << 1 | 1].len;
}
void build(int l, int r, int p)
{
	tt[p].l = l;
	tt[p].r = r;
	tt[p].len = tt[p].flag = 0;
	tt[p].ll = a[l];
	tt[p].rr = a[r];
	if(l == r - 1)
		return ;
	int mid = (l + r) >> 1;
	build(l, mid, p << 1);
	build(mid, r, p << 1 | 1);
}

void update(int x1, int x2, int flag, int p)
{
	if(x1 == tt[p].ll && x2 == tt[p].rr)
	{
		tt[p].flag += flag;
		pushup(p);
		return ;
	}
	if(x2 <= tt[p << 1].rr)
		update(x1, x2, flag, p << 1);
	else if(x1 >= tt[p << 1 | 1].ll)
		update(x1, x2, flag, p << 1 | 1);
	else
	{
		update(x1, tt[p << 1].rr, flag, p << 1);
		update(tt[p << 1 | 1].ll, x2, flag, p << 1 | 1);
	}
	pushup(p);
}

int main()
{
		int x1, x2, y1, y2;
		while(~scanf("%d%d%d%d", &x1, &y1, &x2, &y2) && ( (~x1) || (~x2) || (~y1) || (~y2)))
		{
			int cnt = 1;
			li[cnt] = line(x1, x2, y1, 1);
			a[cnt++] = x1;
			li[cnt] = line(x1, x2, y2, -1);
			a[cnt++] = x2;
			while(scanf("%d%d%d%d", &x1, &y1, &x2, &y2) && (((~x1)||(~x2)||(~y1)||(~y2))))
			{
				li[cnt] = line(x1, x2, y1, 1);
				a[cnt++] = x1;
				li[cnt] = line(x1, x2, y2, -1);
				a[cnt++] = x2;
			}
			sort(li + 1, li + cnt, cmp);
			sort(a + 1, a + cnt);
			build(1, cnt - 1, 1);
			update(li[1].x1, li[1].x2, li[1].flag, 1);
			int ans = 0;
			for(int i = 2; i < cnt; i++)
			{
				//cout << li[i].x1 << "#" << li[i].x2 << "#" << li[i].y << endl;
				ans += tt[1].len * (li[i].y - li[i - 1].y);
				update(li[i].x1, li[i].x2, li[i].flag, 1);
				//cout << "~";
			}
			printf("%d\n", ans);

		}
    return 0;
}

---恢复内容结束---

时间: 2024-08-12 01:02:45

POJ 1389 Area of Simple Polygons 扫描线+线段树面积并的相关文章

POJ 1389 Area of Simple Polygons(面积合并,线段树+离散化)

Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3257   Accepted: 1678 Description There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segment

POJ 1389 Area of Simple Polygons(扫描线)

Area of Simple Polygons Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3278   Accepted: 1694 Description There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segment

POJ 1389 Area of Simple Polygons

Area of Simple Polygons Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 138964-bit integer IO format: %lld      Java class name: Main There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sid

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

离散化后,[1,10]=[1,3]+[6,10]就丢了[4,5]这一段了. 因为更新[3,6]时,它只更新到[3,3],[6,6]. 要么在相差大于1的两点间加入一个值,要么就让左右端点为l,r的线段树节点表示到x[l]到x[r+1]的区间. 这样tree[l,r]=tree[l,m]+tree[m+1,r]=[x[l],x[m+1]]+[x[m+1],x[r+1]] #include<iostream> #include<algorithm> #include<cstdio

POJ训练计划1177_Picture(扫描线/线段树+离散)

解题报告 题意: 求矩形周长和. 思路: 左扫上扫,扫过了. #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> using namespace std; struct Seg { int lx,rx,ly,ry,h,v; friend bool operator < (Seg a,Seg b) { re

POJ 1151 Atlantis 扫描线+线段树

点击打开链接 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17252   Accepted: 6567 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of pa

poj 1151 Atlantis (离散化 + 扫描线 + 线段树)

题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include <cstdlib> 6 #include <algorithm> 7 #define LL __int64 8 #define lson l, mid, 2*rt

hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> #include <algorithm> #define MAXN 110 #define LL ((rt<<1)+1) #define RR ((rt<<1)+2) using namespace std; int n; struct segment{ double l

HDU3265_Posters(扫描线/线段树)

解题报告 题意: 给定的矩形里面有镂空的矩阵,求矩阵面积并. 思路: 直接把一个图形拆成4个矩形,进行面积并. 扫描线+线段树 #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #define LL __int64 using namespace std; struct Seg { int lx,rx,h,v; friend bool operator