题目链接:http://poj.org/problem?id=3277
题目大意:矩形面积并。
题目思路:矩形切割。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
#include<stack>
#include<list>
#include<iostream>
#include<map>
#include<math.h>
using namespace std;
#define inf 0x3f3f3f3f
#define uint unsigned __int64
#define M 2000000
struct node
{
int p1,p2,h;
bool operator<(const node a)const
{
return h<a.h;
}
}a[M],now,b[50000];
int total;
int judge(node a,node b)
{
if(a.p2<=b.p1||a.p1>=b.p2)
return 0;
return 1;
}
void cut(node tmp)
{
int k1,k2;
k1=max(tmp.p1,now.p1);
k2=min(tmp.p2,now.p2);
if(tmp.p1<k1)
{
a[++total]=tmp;
a[total].p2=k1;
}
if(tmp.p2>k2)
{
a[++total]=tmp;
a[total].p1=k2;
}
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
total=0;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&b[i].p1,&b[i].p2,&b[i].h);
}
sort(b+1,b+n+1);
for(i=1;i<=n;i++)
{
now.p1=b[i].p1;now.p2=b[i].p2;
now.h=b[i].h;
for(j=total;j>=1;j--)
{
if(judge(a[j],now))
{
cut(a[j]);
a[j]=a[total--];
}
}
a[++total]=now;
}
long long sum=0;
for(i=1;i<=total;i++)
{
sum+=(a[i].p2-a[i].p1)*(long long)a[i].h;
}
printf("%lld\n",sum);
}
}