POJ Area of Simple Polygons 扫描线

这个题lba等神犇说可以不用离散化,但是我就是要用。

题干:

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 segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates. 

Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point. 

Example: Consider the following three rectangles: 

rectangle 1: < (0, 0) (4, 4) >, 

rectangle 2: < (1, 1) (5, 2) >, 

rectangle 3: < (1, 1) (2, 5) >. 

The total area of all simple polygons constructed by these rectangles is 18. 
Input

The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.
Output
For each test case, output the total area of all simple polygons in a line. 
Sample Input
0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1  
Sample Output
18
10 

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
const int mod = 998244353;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        {x = x * 10 + c - '0';if(x > mod) x %= mod;}
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
struct node
{
    int s,e,h,f;
}p[100000];
int n,m,ans;
int tree[500000],x[500000];
int c[500000];
void init()
{
    n = m = ans = 0;
    clean(tree);
    clean(p);
    clean(c);
    x[0] = -1;
}
bool cmp(node a,node b)
{
    return a.h < b.h;
}

void push_up(int o,int l,int r)
{
    // printf("@%d %d
",l,r);
    if(c[o] != 0) tree[o] = x[r+1] - x[l];
    else if(l == r) tree[o] = 0;
    else tree[o] = tree[o << 1] + tree[o << 1 | 1];
}

void update(int o,int l,int r,int x,int y,int w)
{
    if(l == x && r == y) c[o] += w;
    else
    {
        int mid = (l + r) >> 1;
        if(y <= mid) update(o << 1,l,mid,x,y,w);
        else if(x > mid) update(o << 1 | 1,mid + 1,r,x,y,w);
        else update(o << 1,l,mid,x,mid,w),update(o << 1 | 1,mid + 1,r,mid + 1,y,w);
    }
    push_up(o,l,r);
}
int main()
{
    int a,b,c,d;
    init();
    while(scanf("%d%d%d%d",&a,&b,&c,&d) != EOF)
    {
        if(a < 0)
        break;
        while(a >= 0)
        {
            p[++n].s = a;p[n].e = c;p[n].h = b;p[n].f = 1;
            x[n] = a;
            p[++n].s = a;p[n].e = c;p[n].h = d;p[n].f = -1;
            x[n] = c;
            read(a);read(b);read(c);read(d);
        }
        sort(x + 1,x + n + 1);
        m = unique(x + 1,x + n + 1) - x - 1;
        sort(p + 1,p + n + 1,cmp);
        duke(i,1,n-1)
        {
            int l = lower_bound(x,x + m,p[i].s) - x;
            int r = lower_bound(x,x + m,p[i].e) - x - 1;
            update(1,1,m,l,r,p[i].f);
            ans += tree[1] * (p[i + 1].h - p[i].h);
        }
        printf("%d
",ans);
        init();
    }
    return 0;
}
原文地址:https://www.cnblogs.com/DukeLv/p/9799249.html